Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 17/1 #167

Merged
merged 7 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "katalon-agent",
"version": "v2.0.0",
"version": "v2.0.1",
"description": "",
"main": "cli.js",
"scripts": {
Expand Down
36 changes: 35 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,36 @@ 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);
// Minus more 2 minutes to ensure regenerate presigned url
dateExpires.setSeconds(dateExpires.getSeconds(), -(2 * 60 * 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
18 changes: 18 additions & 0 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ module.exports = {
}, command);
},

overrideCommand(command, ...options) {
return options.reduce((cmd, option) => {
const { flag, value } = option;
if (value) {
if (cmd.includes(flag)) {
const arStr = cmd.split(' ');
const index = arStr.findIndex((t) => t.startsWith(flag));
if (index >= 0) {
arStr[index] = arStr[index].replace(/=.*/, `="${value}"`);
return arStr.join(' ');
}
}
return `${cmd} ${flag}="${value}"`;
}
return cmd;
}, command);
},

getVersion() {
return packageJson.version;
},
Expand Down
10 changes: 6 additions & 4 deletions src/service/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
const uploadPath = body.path;

if (jobInfo.uploadPath) {
jobInfo.oldUploadPath = jobInfo.uploadPath;

Check warning on line 50 in src/service/agent.js

View workflow job for this annotation

GitHub Actions / build

Assignment to property of function parameter 'jobInfo'

Check warning on line 50 in src/service/agent.js

View workflow job for this annotation

GitHub Actions / build

Assignment to property of function parameter 'jobInfo'
}

jobInfo.uploadUrl = uploadUrl;

Check warning on line 53 in src/service/agent.js

View workflow job for this annotation

GitHub Actions / build

Assignment to property of function parameter 'jobInfo'

Check warning on line 53 in src/service/agent.js

View workflow job for this annotation

GitHub Actions / build

Assignment to property of function parameter 'jobInfo'
jobInfo.uploadPath = uploadPath;

Check warning on line 54 in src/service/agent.js

View workflow job for this annotation

GitHub Actions / build

Assignment to property of function parameter 'jobInfo'

Check warning on line 54 in src/service/agent.js

View workflow job for this annotation

GitHub Actions / build

Assignment to property of function parameter 'jobInfo'

await api.uploadFile(uploadUrl, filePath);

Expand Down Expand Up @@ -156,6 +156,8 @@
jLogger.add(
new S3FileTransport(
{
jobInfo,
apiKey,
filePath: logFilePath,
signedUrl: jobInfo.uploadUrl,
logger,
Expand Down Expand Up @@ -301,12 +303,12 @@

let ksArgs;
if (config.isOnPremise) {
ksArgs = utils.updateCommand(parameter.command, {
ksArgs = utils.overrideCommand(parameter.command, {
flag: '-apiKeyOnPremise',
value: apiKey,
});
} else {
ksArgs = utils.updateCommand(
ksArgs = utils.overrideCommand(
parameter.command,
{ flag: '-apiKey', value: apiKey },
);
Expand Down Expand Up @@ -394,12 +396,12 @@

let ksArgs;
if (config.isOnPremise) {
ksArgs = utils.updateCommand(parameter.command, {
ksArgs = utils.overrideCommand(parameter.command, {
flag: '-apiKeyOnPremise',
value: apiKey,
});
} else {
ksArgs = utils.updateCommand(
ksArgs = utils.overrideCommand(
parameter.command,
{ flag: '-apiKey', value: apiKey },
);
Expand Down
Loading