Skip to content

Commit 849d859

Browse files
committed
Add support for Jira API tokens on server and DC
1 parent e84269f commit 849d859

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ import * as assertThat from '@assertthat/assertthat-bdd'
5757

5858
* For downloading feature files:
5959

60+
Using Basic auth
61+
6062
```js
6163
const assertThat = require('assertthat-bdd');
6264

@@ -70,11 +72,26 @@ assertThat.downloadFeatures({
7072
});
7173
```
7274

75+
Using Jira API token (more info can be found here [Using personal access tokens](https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html))
76+
77+
```js
78+
const assertThat = require('assertthat-bdd');
79+
80+
assertThat.downloadFeatures({
81+
"projectId": PROJECT_ID,
82+
"token": "ASSERTTHAT_API_TOKEN",
83+
"jiraServerUrl": "Jira server URL." //Omit if using Jira Cloud
84+
}, function() {
85+
// some optional callback code
86+
});
87+
```
88+
7389
Available parameters:
7490

7591
```
7692
-a, --accessKey [ASSERTTHAT_ACCESS_KEY] Access key
7793
-s, --secretKey [ASSERTTHAT_SECRET_KEY] Secret key
94+
-t, --token [ASSERTTHAT_API_TOKEN] Jira API token (Server and DC only)
7895
-u, --jiraServerUrl [URL] Jira server URL e.g https://mycompanyjira.com
7996
-i, --projectId <ID> Jira project id
8097
-m, --mode <mode> Features to download (default: "automated")
@@ -87,6 +104,8 @@ Available parameters:
87104

88105
* For uploading reports:
89106

107+
Using Basic auth
108+
90109
```js
91110
const assertThat = require('assertthat-bdd');
92111

@@ -100,11 +119,26 @@ assertThat.uploadReports({
100119
});
101120
```
102121

122+
Using Jira API token (more info can be found here [Using personal access tokens](https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html))
123+
124+
```js
125+
const assertThat = require('assertthat-bdd');
126+
127+
assertThat.uploadReports({
128+
"projectId": PROJECT_ID,
129+
"token": "ASSERTTHAT_API_TOKEN",
130+
"jiraServerUrl": "Jira server URL." //Omit if using Jira Cloud
131+
}, function() {
132+
// some optional callback code
133+
});
134+
```
135+
103136
Available parameters:
104137

105138
```
106139
-a, --accessKey [ASSERTTHAT_ACCESS_KEY] Access key
107140
-s, --secretKey [ASSERTTHAT_SECRET_KEY] Secret key
141+
-t, --token [ASSERTTHAT_API_TOKEN] Jira API token (Server and DC only)
108142
-i, --projectId <ID> Jira project id
109143
-j, --jsonReportFolder [FOLDER PATH] Cucumber json files folder
110144
-n, --runName [NAME] Test run name

bin/cli.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ program
99
.version('1.2.0')
1010
.option('-a, --accessKey [ASSERTTHAT_ACCESS_KEY]', 'Access key')
1111
.option('-s, --secretKey [ASSERTTHAT_SECRET_KEY]', 'Secret key')
12+
.option('-t, --token [ASSERTTHAT_API_TOKEN]', 'Jira API token (Server and DC only)')
1213
.option('-u, --jiraServerUrl [URL]', 'Jira server URL e.g https://mycompanyjira.com')
1314
.option('-f, --features', 'Download features')
1415
.option('-r, --report', 'Upload report')
@@ -20,15 +21,17 @@ program
2021
.option('-d, --metadata [FILE PATH]', 'Metadata json file path')
2122
.option('-o, --outputFolder [FOLDER PATH]', 'Jira project id')
2223
.option('-q, --jql [JQL]', 'JQL filter for features download and report upload')
23-
.option('-t, --jsonReportIncludePattern [PATTERN]', 'Pattern for json file names')
24+
.option('-p, --jsonReportIncludePattern [PATTERN]', 'Pattern for json file names')
2425
.option('-x, --proxyURI [URI]', 'Proxy URI')
2526
.option('-b, --numbered [true|false]', 'Append number to feature name on download');
2627

2728
program.on('--help', function(){
2829
console.log('')
2930
console.log('Examples:');
3031
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');
3133
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');
3235
console.log(' $ assertthat-bdd -h');
3336
});
3437

@@ -38,6 +41,7 @@ const settings = {
3841
projectId: program.projectId || process.env.ASSERTTHAT_PROJECT_ID,
3942
accessKey: program.accessKey || process.env.ASSERTTHAT_ACCESS_KEY,
4043
secretKey: program.secretKey || process.env.ASSERTTHAT_SECRET_KEY,
44+
token: program.token || process.env.ASSERTTHAT_API_TOKEN,
4145
jiraServerUrl: program.jiraServerUrl,
4246
jsonReportFolder: program.jsonReportFolder || './reports/',
4347
mode: program.mode,
@@ -57,20 +61,15 @@ function make_red(txt) {
5761
}
5862

5963
if(program.features){
60-
if(!settings.projectId || !settings.accessKey || !settings.secretKey){
64+
if(!settings.projectId || (!settings.token && (!settings.accessKey || !settings.secretKey))){
6165
if(!program.projectId){
6266
console.log('');
6367
console.log(make_red('projectId (-i) option is required'));
6468
console.log('');
6569
}
66-
if(!program.accessKey){
70+
if(!settings.token && (!settings.accessKey || !settings.secretKey)){
6771
console.log('');
68-
console.log(make_red('accessKey (-a) option is required'));
69-
console.log('');
70-
}
71-
if(!program.secretKey){
72-
console.log('');
73-
console.log(make_red('secretKey (-s) option is required'));
72+
console.log(make_red('accessKey (-a) with secretKey (-s) or Jira API token (-t) option is required'));
7473
console.log('');
7574
}
7675
program.outputHelp(make_red);
@@ -80,20 +79,15 @@ if(program.features){
8079
}
8180

8281
if(program.report){
83-
if(!settings.projectId || !settings.accessKey || !settings.secretKey){
82+
if(!settings.projectId || (!settings.token && (!settings.accessKey || !settings.secretKey))){
8483
if(!program.projectId){
8584
console.log('');
8685
console.log(make_red('projectId (-i) option is required'));
8786
console.log('');
8887
}
89-
if(!program.accessKey){
90-
console.log('');
91-
console.log(make_red('accessKey (-a) option is required'));
92-
console.log('');
93-
}
94-
if(!program.secretKey){
88+
if(!settings.token && (!settings.accessKey || !settings.secretKey)){
9589
console.log('');
96-
console.log(make_red('secretKey (-s) option is required'));
90+
console.log(make_red('accessKey (-a) with secretKey (-s) or Jira API token (-t) option is required'));
9791
console.log('');
9892
}
9993
program.outputHelp(make_red);

lib/assertthat-bdd.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@ const downloadFeatures = function(settings, callback) {
3232

3333
req.query({ numbered: settings.numbered })
3434

35-
req.auth(settings.accessKey, settings.secretKey)
36-
.on('error', function(error) {
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) {
3742
console.log(error);
3843
})
3944
.on('response', function(response) {
4045
if (response.status !== 200) {
41-
console.log("Failed to download feature files: " + response.text);
46+
console.log("Failed to download feature files: " + response.error);
4247
req.abort();
4348
}
4449
})
@@ -87,8 +92,12 @@ async function sendReport(settings, filename, runId){
8792
if (fs.existsSync(settings.metadata)){
8893
metadata = fs.readFileSync(settings.metadata, 'utf8');
8994
}
95+
if(settings.token){
96+
req.set({'Authorization': 'Bearer ' + settings.token})
97+
}else{
98+
req.auth(settings.accessKey, settings.secretKey);
99+
}
90100
const response = req
91-
.auth(settings.accessKey, settings.secretKey)
92101
.set('Content-Type', 'application/octet-stream')
93102
.field('projectId', settings.projectId)
94103
.field('runName', settings.runName)

typings/index.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
declare module '@assertthat/assertthat-bdd' {
22
export type Parameters = {
3-
accessKey: string;
4-
secretKey: string;
3+
accessKey?: string;
4+
secretKey?: string;
5+
token?: string;
56
jiraServerUrl?: string;
67
projectId: string;
78
mode?: string;

0 commit comments

Comments
 (0)