Skip to content

Commit 24dac0c

Browse files
committed
✂️ Refactored & Added file and services
1 parent 43f1256 commit 24dac0c

8 files changed

+139
-105
lines changed

.eslintrc.js

-8
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,4 @@ module.exports = {
44
commonjs: true,
55
es2021: true,
66
},
7-
// extends: [
8-
// 'airbnb-base',
9-
// ],
10-
// parserOptions: {
11-
// ecmaVersion: 12,
12-
// },
13-
// rules: {
14-
// },
157
};

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"assets": "./node_modules/figlet/fonts/Standard.flf"
4646
},
4747
"dependencies": {
48+
"axios": "^0.24.0",
4849
"chalk": "^2.4.2",
4950
"commander": "^8.2.0",
5051
"configstore": "^4.0.0",
@@ -54,7 +55,7 @@
5455
"inquirer": "^6.3.1",
5556
"inquirer-autocomplete-prompt": "^1.4.0",
5657
"jira-connector": "^3.1.0",
57-
"jira.js": "^2.5.1",
58+
"jira.js": "^2.8.0",
5859
"log-update": "^3.2.0",
5960
"minimist": "^1.2.0",
6061
"open": "^8.2.1",

src/api/board.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
const fuzzy = require('fuzzy');
66
const inquirer = require('inquirer');
77
const autocompletePrompt = require('inquirer-autocomplete-prompt');
8-
const authenticate = require('../authentication');
8+
const authService = require('../services/AuthServices');
99
inquirer.registerPrompt('autocomplete', autocompletePrompt);
1010
const store = require('../store');
1111
const log = require('../utility/console');
@@ -16,7 +16,7 @@ let names = [];
1616
module.exports = {
1717
getBoards() {
1818
spinner.start();
19-
authenticate.jiraConnector().board.getAllBoards({}, (err, response) => {
19+
authService.jiraConnector().board.getAllBoards({}, (err, response) => {
2020
if (response) {
2121
spinner.stop();
2222
names = response.values.map(el => el.name);

src/authentication.js

-91
This file was deleted.

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const program = require('commander');
44
const input = require('./store');
55

66
const { loadBoardCommands } = require('./commands/boards');
7+
const { loadTasksCommands } = require('./commands/tasks')
78

89
program.version('1.0.0').description('CLI Tool for accessing JIRA');
910
program
@@ -22,4 +23,6 @@ program
2223

2324
// Add other sub commands here
2425
loadBoardCommands();
26+
loadTasksCommands();
27+
2528
program.parse(process.argv);

src/services/AuthServices.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Authentication Module
3+
* -------------------------------------------------------------
4+
* return the jiraAuth BASE object and then we can call the apis
5+
* on the base of the main object.
6+
*/
7+
const Configstore = require('configstore');
8+
const configStore = new Configstore('jiraconfig');
9+
const { Version2Client } = require('jira.js');
10+
const jiraConnector = require('jira-connector');
11+
const emailAddress = configStore.get('emailAddress');
12+
const apiToken = configStore.get('apiToken');
13+
const hostname = configStore.get('hostname');
14+
15+
module.exports = {
16+
/**
17+
* Config Object contains the username, hostname and API token
18+
* @param {*} config
19+
*/
20+
authenticate(config, callback) {
21+
const HOST_NAME = config.host_name;
22+
let client = new Version2Client({
23+
host: `https://${config.host_name}`,
24+
authentication: {
25+
basic: {
26+
email: config.user_name,
27+
apiToken: config.api_token
28+
}
29+
}
30+
});
31+
32+
client.myself
33+
.getCurrentUser()
34+
.then(user => {
35+
if (user) {
36+
return callback({
37+
user,
38+
url: `https://${config.host_name}`,
39+
hostname: HOST_NAME,
40+
apiToken: config.api_token
41+
});
42+
}
43+
})
44+
.catch(err => {
45+
return callback({ error: 'UnAuthorized' });
46+
});
47+
},
48+
49+
/**
50+
* Returns the recently authenticated user JIRA-Connector Object
51+
* JIRA.js
52+
*/
53+
jira() {
54+
const emailAddress = configStore.get('emailAddress');
55+
const apiToken = configStore.get('apiToken');
56+
const hostUrl = configStore.get('url');
57+
if (!apiToken) {
58+
console.log('Please login using your API Token');
59+
} else {
60+
const JIRA_CONFIG = new Version2Client({
61+
host: hostUrl,
62+
authentication: {
63+
basic: {
64+
email: emailAddress,
65+
apiToken: apiToken
66+
}
67+
}
68+
});
69+
return JIRA_CONFIG;
70+
}
71+
},
72+
73+
/**
74+
* Returns the recently authenticated jira Object
75+
* JIRA -CONNECTOR
76+
*/
77+
jiraConnector() {
78+
if (!apiToken) {
79+
console.log('Please login using your API Token');
80+
} else {
81+
const jira = new jiraConnector({
82+
host: hostname,
83+
basic_auth: {
84+
email: emailAddress,
85+
api_token: apiToken
86+
}
87+
});
88+
return jira;
89+
}
90+
}
91+
};
92+

src/services/AxiosService.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const Configstore = require('configstore');
2+
const configStore = new Configstore('jiraconfig');
3+
const axios = require('axios');
4+
const emailAddress = configStore.get('emailAddress');
5+
const apiToken = configStore.get('apiToken');
6+
const hostname = configStore.get('hostname');
7+
8+
// encode email and token for basic auth as per JIRA authentications
9+
const encodeEmailToken = Buffer.from(`${emailAddress}:${apiToken}`).toString('base64')
10+
11+
const instance = axios.create({
12+
baseURL: `https://${hostname}`,
13+
headers: {
14+
'Authorization': `Basic ${encodeEmailToken}`,
15+
'Cache-Control': 'no-cache',
16+
},
17+
})
18+
19+
20+
instance.interceptors.request.use(function (config) {
21+
return config;
22+
}, function (error) {
23+
return Promise.reject(error);
24+
});
25+
26+
axios.interceptors.response.use(function (response) {
27+
// Any status code that lie within the range of 2xx cause this function to trigger
28+
// Do something with response data
29+
return response;
30+
}, function (error) {
31+
// Any status codes that falls outside the range of 2xx cause this function to trigger
32+
// Do something with response error
33+
return Promise.reject(error);
34+
});
35+
36+
module.exports = instance

src/store.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
*/
66

77
const Configstore = require('configstore');
8+
const configStore = new Configstore('jiraconfig');
89
// apis
9-
const jira = require('./authentication');
10+
const authService = require('./services/AuthServices');
1011
const print = require('./utility/console');
1112
const question = require('./api/questions');
1213
const util = require('./utility/utils');
1314

14-
const configStore = new Configstore('jiraconfig');
15+
1516
const spinner = util.spinner('Authenticating...');
1617
/**
1718
* Verify the user and save the object
@@ -32,7 +33,7 @@ module.exports = {
3233

3334
verifyAndSave: config => {
3435
// authenticate and save the inputs in config store
35-
jira.authenticate(config, data => {
36+
authService.authenticate(config, data => {
3637
if (data) {
3738
spinner.stop();
3839
}

0 commit comments

Comments
 (0)