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

Change authentication to permanent token of user #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 9 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.idea
node_modules
last/*
src/bot-config.json
/.idea/
/.vscode/
/node_modules/
/last/
/logs/
/src/bot-config.json
/INSTRUCTION.md
logs/
_bot-config.json
app2.json
/bot-config.json
/app2.json
/package-lock.json
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const debug = require('debug')('youtrack:app');
const YouTrackBot = require('./youtrackbot');
const config = require('./bot-config-sample.json');
const config = require('./bot-config.json');

debug.log = console.log.bind(console);

Expand Down
10 changes: 5 additions & 5 deletions src/app.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "youtrack-bot",
"script": "/home/ubuntu/youtrack_telegram_bot/src/app.js",
"script": "./src/app.js",
"node_args": [
"--harmony"
],
Expand All @@ -10,9 +10,9 @@
"DEBUG_COLORS": 0
},
"log_date_format": "YYYY-MM-DD HH:mm:ss Z",
"out_file": "/home/ubuntu/youtrack_telegram_bot/logs/youtrack_bot.log",
"error_file": "/home/ubuntu/youtrack_telegram_bot/logs/youtrack_bot_error.log",
"out_file": "./logs/youtrack_bot.log",
"error_file": "./logs/youtrack_bot_error.log",
"exec_mode": "cluster",
"instances": 1,
"cron_restart": "*/5 * * * *"
}
"cron_restart": "*/1 * * * *"
}
19 changes: 7 additions & 12 deletions src/bot-config-sample.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
{
"youtrack": {
"authType": "oauth2",
"baseUrl": "https://my-company.myjetbrains.com/youtrack",
"oauth2": {
"url": "https://my-company.myjetbrains.com/hub/api/rest/oauth2/token",
"clientServiceId": "1fac2ed9-2140-43d9-af3e-3a4a2550ba34",
"clientServiceSecret": "41f1dd66-9ece-4408-94d3-ab683a0a684e",
"scope": "1fac2ed9-2140-43d9-af3e-3a4a2550ba34"
}
"authType": "Bearer",
"baseUrl": "https://example.myjetbrains.com/youtrack",
"token": "perm:0123456789dvdg==.SG9tZS15b3V0cmFjay0123456789bS1ib3Q=.l01234567899Jlj2YRJFSYMgfBaK2w"
},
"telegram": {
"defaultToken": "183108291:BAFK8LNaQc0MP1GlAe9SxCd-cjc1hz-zCdw",
"defaultToken": "012345678:AAFRIKAKfAR3dQNMgQAwmye7r7-0123456789",
"max": 500,
"projects": [
{
"token": "183108291:BAFK8LNaQc0MP1GlAe9SxCd-cjc1hz-zCdw",
"projectName": "MY_PROJECT_NAME",
"chatId": "-1002095315097"
"token": "012345678:AAFKfAR3dQNMgQAwmye7r7-0123456789",
"projectName": "PROJECT",
"chatId": "-0123456789"
}
]
}
Expand Down
52 changes: 11 additions & 41 deletions src/youtrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,9 @@ class Youtrack {
constructor(config) {
this.config = config;
this.baseUrl = this.config.youtrack.baseUrl;
this.accessToken = null;
this.tokenType = null;
}

/**
* Gets access token by Client Service ID and Client Service Secret values.
* @returns {Promise.<void>}
*/
async getAccessToken() {
// generate Base64(CLIENT_SERVICE_ID:CLIENT_SERVICE_SECRET) Authorization value
let authValue = (new Buffer(this.config.youtrack.oauth2.clientServiceId + ':' + this.config.youtrack.oauth2.clientServiceSecret)).toString('base64');

let params = {
url: this.config.youtrack.oauth2.url,
headers: {
Accept: 'application/json',
Authorization: 'Basic ' + authValue
},
form: {grant_type: 'client_credentials', scope: this.config.youtrack.oauth2.scope}
};

let response = await request.post(params);
debug('getAccessToken() response=%O', response);

response = JSON.parse(response);

this.tokenType = response.token_type;
this.accessToken = response.access_token;

return response;
}

async issuesChanges(projectName, options = {updatedAfter: moment().subtract(1, 'days').format('x'), max: 10}) {
async issuesChanges(projectName, options = { updatedAfter: moment().subtract(1, 'days').format('x'), max: 10 }) {
let issues = await this.issuesByProject(projectName, options);
let issuesWithChanges = [];
for (let issue of issues) {
Expand All @@ -59,7 +29,7 @@ class Youtrack {
return issuesWithChanges;
}

async issuesByProject(projectName, options = {updatedAfter: moment().subtract(1, 'days').format('x'), max: 10}) {
async issuesByProject(projectName, options = { updatedAfter: moment().subtract(1, 'days').format('x'), max: 10 }) {
let queryParams = {
updatedAfter: options.updatedAfter,
max: options.max,
Expand All @@ -68,7 +38,7 @@ class Youtrack {

queryParams = qs.stringify(queryParams);
let url = `${this.baseUrl}/rest/issue/byproject/${projectName}?${queryParams}`;
let params = {url: url, headers: this._getHeaders()};
let params = { url: url, headers: this._getHeaders() };
let response = await request.get(params);
response = JSON.parse(response);

Expand All @@ -80,7 +50,7 @@ class Youtrack {
debug('issuesByProject() Ignoring invalid issue: ', issue);
continue;
}
let _issue = Object.assign({id: issue.id}, this._normalizeFields(issue.field));
let _issue = Object.assign({ id: issue.id }, this._normalizeFields(issue.field));
issueList.push(_issue);
}

Expand All @@ -91,8 +61,8 @@ class Youtrack {

async issueChanges(issueId, updatedAfter) {
let url = `${this.baseUrl}/rest/issue/${issueId}/changes`;
let params = {url: url, headers: this._getHeaders()};
let issue = {changes: []};
let params = { url: url, headers: this._getHeaders() };
let issue = { changes: [] };

let response = null;

Expand All @@ -119,7 +89,7 @@ class Youtrack {

if (response.change) {
for (let change of response.change) {
let _change = {changedFields: []};
let _change = { changedFields: [] };
let res = this._normalizeFields(change.field);
issue.changes.push(res);
}
Expand Down Expand Up @@ -165,7 +135,7 @@ class Youtrack {
}
}

object[field.name] = {oldValue: oldVal, newValue: newVal};
object[field.name] = { oldValue: oldVal, newValue: newVal };
}
else {
object[field.name] = field.value;
Expand Down Expand Up @@ -194,11 +164,11 @@ class Youtrack {
}

_getHeaders() {
let headers = {Accept: 'application/json'};
let headers = { Accept: 'application/json' };

// set access token if exists
if (this.accessToken)
headers.Authorization = this.tokenType + ' ' + this.accessToken;
if (this.config.youtrack.token)
headers.Authorization = this.config.youtrack.authType + ' ' + this.config.youtrack.token;

return headers;
}
Expand Down
1 change: 0 additions & 1 deletion src/youtrackbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class YoutrackBot {

this.yt = new Youtrack(this.config);

let token = await this.yt.getAccessToken();
let last = await this._getUpdatedAfter();

this.issues = await this.yt.issuesChanges(this.projectName, {updatedAfter: last.ts, max: max});
Expand Down
1 change: 1 addition & 0 deletions start.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pm2 start ./src/app.json
1 change: 1 addition & 0 deletions stop.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pm2 delete ./src/app.json