Skip to content

Commit 31be37d

Browse files
committed
Refactor params; switch to axios
1 parent 457615a commit 31be37d

File tree

4 files changed

+182
-1048
lines changed

4 files changed

+182
-1048
lines changed

bin/cli.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,32 @@ const dateFormat = require('dateformat');
77

88
program
99
.version('1.2.0')
10-
.option('-a, --accessKey [ASSERTTHAT_ACCESS_KEY]', 'Access key')
11-
.option('-s, --secretKey [ASSERTTHAT_SECRET_KEY]', 'Secret key')
12-
.option('-t, --token [ASSERTTHAT_API_TOKEN]', 'Jira API token (Server and DC only)')
13-
.option('-u, --jiraServerUrl [URL]', 'Jira server URL e.g https://mycompanyjira.com')
14-
.option('-f, --features', 'Download features')
15-
.option('-r, --report', 'Upload report')
16-
.option('-i, --projectId <ID>', 'Jira project id')
17-
.option('-j, --jsonReportFolder [FOLDER PATH]', 'Cucumber json files folder')
18-
.option('-m, --mode <mode>', 'Features to download', /^(automated|manual|both)$/i, 'automated')
19-
.option('-l, --tags <tag-expression>', 'Cucumber tag expression for filtering scenarios', '')
20-
.option('-n, --runName [NAME]', 'Test run name')
21-
.option('-d, --metadata [FILE PATH]', 'Metadata json file path')
22-
.option('-o, --outputFolder [FOLDER PATH]', 'Jira project id')
23-
.option('-q, --jql [JQL]', 'JQL filter for features download and report upload')
24-
.option('-p, --jsonReportIncludePattern [PATTERN]', 'Pattern for json file names')
25-
.option('-x, --proxyURI [URI]', 'Proxy URI')
26-
.option('-b, --numbered [true|false]', 'Append number to feature name on download');
10+
.option('--accessKey [ASSERTTHAT_ACCESS_KEY]', 'Access key')
11+
.option('--secretKey [ASSERTTHAT_SECRET_KEY]', 'Secret key')
12+
.option('--token [ASSERTTHAT_API_TOKEN]', 'Jira API token (Server and DC only)')
13+
.option('--jiraServerUrl [URL]', 'Jira server URL e.g https://mycompanyjira.com')
14+
.option('--features', 'Download features')
15+
.option('--report', 'Upload report')
16+
.option('--projectId <ID>', 'Jira project id')
17+
.option('--jsonReportFolder [FOLDER PATH]', 'Cucumber json files folder')
18+
.option('--mode <mode>', 'Features to download', /^(automated|manual|both)$/i, 'automated')
19+
.option('--tags <tag-expression>', 'Cucumber tag expression for filtering scenarios', '')
20+
.option('--runName [NAME]', 'Test run name')
21+
.option('--metadata [FILE PATH]', 'Metadata json file path')
22+
.option('--outputFolder [FOLDER PATH]', 'Jira project id')
23+
.option('--jql [JQL]', 'JQL filter for features download and report upload')
24+
.option('--jsonReportIncludePattern [PATTERN]', 'Pattern for json file names')
25+
.option('--proxyHost [Host]', 'Proxy Host')
26+
.option('--proxyPort [Port]', 'Proxy Port')
27+
.option('--numbered [true|false]', 'Append number to feature name on download');
2728

2829
program.on('--help', function(){
2930
console.log('')
3031
console.log('Examples:');
31-
console.log(' $ assertthat-bdd -f -i 10001 -a "access_key" -s "secret_key"');
32-
console.log(' $ assertthat-bdd -f -i 10001 -t JIRA_API_TOKEN');
33-
console.log(' $ assertthat-bdd -r -i 10001 -a "access_key" -s "secret_key"');
34-
console.log(' $ assertthat-bdd -r -i 10001 -t JIRA_API_TOKEN');
32+
console.log(' $ assertthat-bdd --features --projectId 10001 --accessKey "access_key" --secretKey "secret_key"');
33+
console.log(' $ assertthat-bdd --features --projectId 10001 --token JIRA_API_TOKEN');
34+
console.log(' $ assertthat-bdd --report --projectId 10001 --accessKey "access_key" --secretKey "secret_key"');
35+
console.log(' $ assertthat-bdd --report --projectId 10001 --token JIRA_API_TOKEN');
3536
console.log(' $ assertthat-bdd -h');
3637
});
3738

@@ -52,8 +53,8 @@ const settings = {
5253
jql: program.jql || '',
5354
tags: program.tags || '',
5455
jsonReportIncludePattern: program.jsonReportIncludePattern || '.+.json',
55-
proxyURI: program.proxyURI || process.env.http_proxy,
56-
56+
proxyHost: program.proxyHost,
57+
proxyPort: program.proxyPort,
5758
};
5859

5960
function make_red(txt) {
@@ -64,12 +65,12 @@ if(program.features){
6465
if(!settings.projectId || (!settings.token && (!settings.accessKey || !settings.secretKey))){
6566
if(!program.projectId){
6667
console.log('');
67-
console.log(make_red('projectId (-i) option is required'));
68+
console.log(make_red('--projectId option is required'));
6869
console.log('');
6970
}
7071
if(!settings.token && (!settings.accessKey || !settings.secretKey)){
7172
console.log('');
72-
console.log(make_red('accessKey (-a) with secretKey (-s) or Jira API token (-t) option is required'));
73+
console.log(make_red('--accessKey with --secretKey or Jira API --token option is required'));
7374
console.log('');
7475
}
7576
program.outputHelp(make_red);

lib/assertthat-bdd.js

Lines changed: 108 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,137 @@
11
#!/usr/bin/env node
2-
'use strict'
2+
'use strict';
33

44
const admZip = require('adm-zip');
5-
const request = require('superagent');
6-
require('superagent-proxy')(request);
5+
const axios = require('axios');
76
const fs = require('fs');
8-
const path = require('path')
7+
const path = require('path');
98

10-
const downloadFeatures = function(settings, callback) {
11-
let featuresUrl = "https://bdd.assertthat.app/rest/api/1/project/" + settings.projectId + "/features"
12-
if(settings.jiraServerUrl){
13-
featuresUrl = settings.jiraServerUrl+"/rest/assertthat/latest/project/"+settings.projectId+"/client/features"
9+
const downloadFeatures = function (settings, callback) {
10+
let featuresUrl = "https://bdd.assertthat.app/rest/api/1/project/" + settings.projectId + "/features";
11+
if (settings.jiraServerUrl) {
12+
featuresUrl = settings.jiraServerUrl + "/rest/assertthat/latest/project/" + settings.projectId + "/client/features";
1413
}
15-
if (!fs.existsSync(settings.outputFolder)){
14+
if (!fs.existsSync(settings.outputFolder)) {
1615
fs.mkdirSync(settings.outputFolder);
1716
}
18-
const req = request.get(featuresUrl)
19-
if(settings.proxyURI){
20-
req.proxy(settings.proxyURI)
21-
}
22-
if(settings.jql){
23-
req.query({ jql: settings.jql })
24-
}
2517

26-
if(settings.mode){
27-
req.query({ mode: settings.mode })
28-
}
29-
if(settings.tags){
30-
req.query({ tags: settings.tags })
31-
}
32-
33-
req.query({ numbered: settings.numbered })
34-
35-
if(settings.token){
36-
req.set({'Authorization': 'Bearer ' + settings.token})
37-
}else{
38-
req.auth(settings.accessKey, settings.secretKey);
39-
}
40-
41-
req.on('error', function(error) {
42-
console.log(error);
43-
})
44-
.on('response', function(response) {
18+
axios.get(featuresUrl, {
19+
params: {
20+
jql: settings.jql,
21+
mode: settings.mode,
22+
tags: settings.tags,
23+
numbered: settings.numbered
24+
},
25+
proxy: settings.proxyHost ? {
26+
protocol: 'http',
27+
host: settings.proxyHost,
28+
port: settings.proxyPort
29+
} : undefined,
30+
headers: settings.token ? {
31+
'Authorization': 'Bearer ' + settings.token
32+
} : undefined,
33+
auth: settings.token ? undefined : {
34+
username: settings.accessKey,
35+
password: settings.secretKey
36+
},
37+
responseType: 'stream'
38+
})
39+
.then(response => {
4540
if (response.status !== 200) {
46-
console.log("Failed to download feature files: " + response.error);
47-
req.abort();
41+
console.log("Failed to download feature files: " + response.statusText);
42+
return;
4843
}
49-
})
50-
.pipe(fs.createWriteStream(settings.outputFolder+'/features.zip'))
51-
.on('finish', function() {
52-
const files=fs.readdirSync(settings.outputFolder);
53-
for(let i=0;i<files.length;i++){
54-
let filename=path.join(settings.outputFolder,files[i]);
55-
if (filename.indexOf('.feature')>=0) {
56-
fs.unlinkSync(filename);
44+
const filePath = path.join(settings.outputFolder, 'features.zip');
45+
const writer = fs.createWriteStream(filePath);
46+
response.data.pipe(writer);
47+
writer.on('finish', () => {
48+
writer.close();
49+
const files = fs.readdirSync(settings.outputFolder);
50+
for (let i = 0; i < files.length; i++) {
51+
let filename = path.join(settings.outputFolder, files[i]);
52+
if (filename.indexOf('.feature') >= 0) {
53+
fs.unlinkSync(filename);
54+
}
5755
}
58-
}
59-
const zip = new admZip(settings.outputFolder+'/features.zip');
60-
zip.extractAllTo( settings.outputFolder, true);
61-
fs.unlinkSync(settings.outputFolder+'/features.zip');
62-
if(callback) callback();
63-
});
56+
const zip = new admZip(filePath);
57+
zip.extractAllTo(settings.outputFolder, true);
58+
fs.unlinkSync(filePath);
59+
if (callback) callback();
60+
});
61+
})
62+
.catch(error => {
63+
console.log(error);
64+
});
6465
};
6566

66-
const uploadReports = async function(settings, callback) {
67+
const uploadReports = async function (settings, callback) {
6768
const files = fs.readdirSync(settings.jsonReportFolder);
68-
let jsonReportIncludePattern = ".+.json"
69-
if(settings.jsonReportIncludePattern){
70-
jsonReportIncludePattern = settings.jsonReportIncludePattern;
69+
let jsonReportIncludePattern = ".+.json";
70+
if (settings.jsonReportIncludePattern) {
71+
jsonReportIncludePattern = settings.jsonReportIncludePattern;
7172
}
7273
console.log('Using ' + jsonReportIncludePattern + ' when searching for cucumber.json files');
7374
let runId = -1;
74-
for(let i=0;i<files.length;i++){
75-
let filename=path.join(settings.jsonReportFolder,files[i]);
76-
if (filename.indexOf('.json')>=0 && new RegExp(jsonReportIncludePattern, 'gi').test(files[i])) {
77-
console.log('Sending file ' + filename + ' with runId ' + runId);
78-
const res = sendReport(settings,filename,runId);
79-
await res.then(res => { runId=JSON.parse(res.text).runId});
75+
for (let i = 0; i < files.length; i++) {
76+
let filename = path.join(settings.jsonReportFolder, files[i]);
77+
if (filename.indexOf('.json') >= 0 && new RegExp(jsonReportIncludePattern, 'gi').test(files[i])) {
78+
console.log('Sending file ' + filename + ' with runId ' + runId);
79+
try {
80+
const res = await sendReport(settings, filename, runId);
81+
runId = res.data.runId;
82+
} catch (error) {
83+
console.log(error);
84+
}
8085
}
8186
}
82-
83-
if(callback) callback();
87+
88+
if (callback) callback();
8489
};
8590

86-
async function sendReport(settings, filename, runId){
87-
let reportUrl = "https://bdd.assertthat.app/rest/api/1/project/" + settings.projectId + "/report"
88-
if(settings.jiraServerUrl){
89-
reportUrl = settings.jiraServerUrl+"/rest/assertthat/latest/project/"+settings.projectId+"/client/report"
90-
}
91-
const req = request.post(reportUrl)
92-
93-
if(settings.proxyURI){
94-
req.proxy(settings.proxyURI)
91+
const FormData = require('form-data');
92+
93+
async function sendReport(settings, filename, runId) {
94+
let reportUrl = "https://bdd.assertthat.app/rest/api/1/project/" + settings.projectId + "/report";
95+
if (settings.jiraServerUrl) {
96+
reportUrl = settings.jiraServerUrl + "/rest/assertthat/latest/project/" + settings.projectId + "/client/report";
9597
}
98+
9699
let metadata = '';
97-
if (fs.existsSync(settings.metadata)){
98-
metadata = fs.readFileSync(settings.metadata, 'utf8');
100+
if (fs.existsSync(settings.metadata)) {
101+
metadata = fs.readFileSync(settings.metadata, 'utf8');
99102
}
100-
if(settings.token){
101-
req.set({'Authorization': 'Bearer ' + settings.token})
102-
}else{
103-
req.auth(settings.accessKey, settings.secretKey);
103+
104+
const formData = new FormData();
105+
formData.append('file', fs.createReadStream(filename));
106+
formData.append('projectId', settings.projectId);
107+
formData.append('runName', settings.runName);
108+
formData.append('metadata', metadata);
109+
formData.append('runId', runId);
110+
formData.append('jql', settings.jql);
111+
112+
try {
113+
const response = await axios.post(reportUrl, formData, {
114+
headers: {
115+
...formData.getHeaders(),
116+
...settings.token ? { 'Authorization': 'Bearer ' + settings.token } : {}
117+
},
118+
proxy: settings.proxyHost ? {
119+
protocol: 'http',
120+
host: settings.proxyHost,
121+
port: settings.proxyPort
122+
} : undefined,
123+
auth: settings.token ? undefined : {
124+
username: settings.accessKey,
125+
password: settings.secretKey
126+
}
127+
});
128+
return response;
129+
} catch (error) {
130+
console.log(error);
131+
throw error;
104132
}
105-
const response = req
106-
.set('Content-Type', 'application/octet-stream')
107-
.field('projectId', settings.projectId)
108-
.field('runName', settings.runName)
109-
.field('metadata', metadata)
110-
.field('runId', runId)
111-
.field('jql', settings.jql)
112-
.attach('file', filename)
113-
return response;
114-
};
133+
}
134+
115135

116136
module.exports = {
117137
downloadFeatures: downloadFeatures,

0 commit comments

Comments
 (0)