Skip to content

Commit

Permalink
Merge pull request #163 from katalon-studio/TES-417-refresh-presigned…
Browse files Browse the repository at this point in the history
…-url

[TES-417] The session log could not be pushed to TestOps
  • Loading branch information
quidl authored Jan 8, 2024
2 parents 11e223e + 4e5da49 commit 8575adb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/config/transports.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const _ = require('lodash');
const path = require('path');
const moment = require('moment');
const TransportStream = require('winston-transport');

const { generateUuid } = require('../helper/agent');
const api = require('../core/api');

class S3FileTransport extends TransportStream {
constructor(options = {}, afterLog) {
super(options);
this.jobInfo = options.jobInfo;
this.apiKey = options.apiKey;
this.filePath = options.filePath;
this.signedUrl = options.signedUrl;
this.parentLogger = options.logger;
Expand All @@ -17,7 +22,34 @@ class S3FileTransport extends TransportStream {
this.uploadToS3Throttled = _.throttle(this.uploadToS3, this.wait, { trailing: false });
}

uploadToS3() {
async uploadToS3() {
const parsedUrl = new URL(this.signedUrl);
const params = new URLSearchParams(parsedUrl.search);

const amzDate = params.get('X-Amz-Date');
const amzExpires = params.get('X-Amz-Expires');

const dateFormart = 'YYYYMMDDTHHmmss[Z]';
const dateExpires = moment.utc(amzDate, dateFormart).toDate();
dateExpires.setSeconds(dateExpires.getSeconds(), amzExpires * 1000);

const now = new Date();
if (dateExpires < now) {
const requestGetUploadInfo = async () => {
const response = await api.getUploadInfo(this.jobInfo.projectId, this.apiKey);
if (!response || !response.body) {
return null;
}
const { body } = response;
const { uploadUrl } = body;
this.signedUrl = uploadUrl;
const batch = generateUuid();
const fileName = path.basename(this.filePath);
return api.saveJobLog(this.jobInfo, batch, fileName, this.apiKey);
};
await requestGetUploadInfo();
}

return api
.uploadFile(this.signedUrl, this.filePath)
.then(() => this.afterLog && this.afterLog())
Expand Down
2 changes: 2 additions & 0 deletions src/service/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ async function executeJob(jobInfo, keepFiles) {
jLogger.add(
new S3FileTransport(
{
jobInfo,
apiKey,
filePath: logFilePath,
signedUrl: jobInfo.uploadUrl,
logger,
Expand Down

0 comments on commit 8575adb

Please sign in to comment.