This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from CCristi/master
Add [readonly] jira integration
- Loading branch information
Showing
9 changed files
with
287 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function invariant(condition, message, ...args) { | ||
if (!condition) { | ||
let idx = 0; | ||
|
||
throw new Error(message.replace(/%s/g, () => args[idx++])); // eslint-disable-line no-plusplus | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import url from 'url'; | ||
import AbstractProvider from './abstract-provider'; | ||
import Logger from '../logger'; | ||
import JiraClient from './rest/client/jira-client'; | ||
import BasicAuthProvider from './rest/auth/basic-auth-provider'; | ||
|
||
export default class JiraProvider extends AbstractProvider { | ||
setupClient() { | ||
Logger.debug(`Connect to Jira on ${this.options.host}`); | ||
|
||
JiraClient.updateOptions({ | ||
authProvider: new BasicAuthProvider( | ||
this.options.username, | ||
this.options.password, | ||
), | ||
baseURL: this.options.host, | ||
}); | ||
} | ||
|
||
generateId(issue) { // eslint-disable-line | ||
return `JIRA-${issue.id}`; | ||
} | ||
|
||
generateLink(issue) { // eslint-disable-line | ||
return url.resolve(this.options.host, `/browse/${issue.key}`); | ||
} | ||
|
||
generateName(issue) { // eslint-disable-line | ||
return `[${issue.key}] ${issue.fields.summary}`; | ||
} | ||
|
||
async synchronize() { | ||
this.setupClient(); | ||
|
||
const response = await JiraClient.search({ | ||
jql: 'assignee=currentuser() AND status!=closed', | ||
}); | ||
|
||
const itemsToLookup = response.data.issues.map(issue => ({ | ||
projectId: issue.key, | ||
name: this.generateName(issue), | ||
issueId: this.generateId(issue), | ||
link: this.generateLink(issue), | ||
})); | ||
|
||
itemsToLookup.forEach((item) => { | ||
const { issueId } = item; | ||
|
||
const existingItem = this.db.findOne({ issueId }); | ||
|
||
if (!existingItem) { | ||
Logger.info(`Add new Jira issue: ${item.name} #${issueId}`); | ||
|
||
this.db.save(item); | ||
} | ||
}); | ||
} | ||
|
||
async report(minLogTime = 0) { // eslint-disable-line | ||
Logger.info(`JiraProvider::report(${minLogTime})`); | ||
|
||
return Promise.resolve(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default class AnonProvider { | ||
sign(requestConfig) { // eslint-disable-line | ||
return requestConfig; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export default class BasicAuthProvider { | ||
constructor(username, password) { | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
sign(requestConfig) { | ||
const signedRequestConfig = Object.assign({}, requestConfig); | ||
|
||
signedRequestConfig.headers = Object.assign(signedRequestConfig.headers || {}, { | ||
Authorization: `Basic ${new Buffer(`${this.username}:${this.password}`).toString('base64')}`, | ||
}); | ||
|
||
return signedRequestConfig; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import RestClient from './rest-client'; | ||
|
||
/** | ||
* @callback ApiMethod | ||
* @param {Object=} payload | ||
* @returns {Promise<{status: Number, headers: Object, data: Object}>} | ||
*/ | ||
|
||
/** | ||
* @typedef {RestClient} JiraRestClient | ||
* | ||
* @property {ApiMethod} getIssue | ||
* @property {ApiMethod} search | ||
* | ||
* @type {JiraRestClient} | ||
*/ | ||
const JiraClient = new RestClient({ | ||
getIssue: { | ||
method: 'GET', | ||
path: 'rest/api/2/issue/{issueId}', | ||
}, | ||
search: { | ||
method: 'GET', | ||
path: '/rest/api/2/search', | ||
}, | ||
}); | ||
|
||
export default JiraClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import axios from 'axios'; | ||
import invariant from '../../../invariant'; | ||
import AnonProvider from '../auth/anon-provider'; | ||
|
||
export default class RestClient { | ||
constructor(methodsMap = {}, options = {}) { | ||
this.options = options; | ||
this.options.authProvider = Object.assign(RestClient.defaults, options); | ||
|
||
this.createApiMethods(methodsMap); | ||
} | ||
|
||
// @TODO: add options schema | ||
updateOptions(newOptions) { | ||
this.options = Object.assign(this.options, newOptions); | ||
} | ||
|
||
createApiMethods(methodsMap) { | ||
Object.keys(methodsMap).forEach((methodName) => { | ||
const methodDefinition = methodsMap[methodName]; | ||
|
||
this[methodName] = this.createApiMethod(methodDefinition); | ||
}); | ||
} | ||
|
||
createApiMethod(methodDefinition) { | ||
return (payload = {}, overrideOptions = {}) => { | ||
const options = this.options; | ||
const requestConfig = Object.assign({ | ||
baseURL: options.baseURL, | ||
method: methodDefinition.method, | ||
adapter: methodDefinition.adapter, | ||
responseType: 'json', | ||
url: this.buildUrl(methodDefinition.path, payload), | ||
[this.getParametersSendType(methodDefinition)]: payload, | ||
}, overrideOptions); | ||
|
||
invariant(options.baseURL, 'You should configure API baseURL before making any api calls'); | ||
invariant(options.authProvider, 'Missing authentication provider'); | ||
|
||
const signedRequestConfig = this.options.authProvider.sign(requestConfig); | ||
|
||
return axios.request(signedRequestConfig); | ||
}; | ||
} | ||
|
||
buildUrl(urlTemplate, params = {}) { // eslint-disable-line | ||
return urlTemplate.replace(/{\s*([^/]+)\s*}/g, (match, paramName) => { | ||
invariant( | ||
paramName in params, | ||
'Missing "%s" parameter. Cannot build "%s" url template', | ||
paramName, | ||
urlTemplate, | ||
); | ||
|
||
const paramValue = params[paramName]; | ||
delete params[paramName]; // eslint-disable-line no-param-reassign | ||
|
||
return paramValue.toString(); | ||
}); | ||
} | ||
|
||
getParametersSendType(endpointDefinition) { // eslint-disable-line | ||
return ['GET', 'HEAD'].includes(endpointDefinition.method.toUpperCase()) ? 'params' : 'data'; | ||
} | ||
|
||
static get defaults() { | ||
return { | ||
authProvider: new AnonProvider(), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters