From 827bf3f34c9a84f6fdc4836e3dff6770cd58876c Mon Sep 17 00:00:00 2001 From: "Oleg@Home" Date: Wed, 18 Oct 2017 20:10:50 +0300 Subject: [PATCH 1/3] Added start-stop scripts for Windows --- src/app.json | 10 +++++----- start.bat | 1 + stop.bat | 1 + 3 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 start.bat create mode 100644 stop.bat diff --git a/src/app.json b/src/app.json index 4f1cf50..0772732 100644 --- a/src/app.json +++ b/src/app.json @@ -1,6 +1,6 @@ { "name": "youtrack-bot", - "script": "/home/ubuntu/youtrack_telegram_bot/src/app.js", + "script": "./src/app.js", "node_args": [ "--harmony" ], @@ -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 * * * *" +} \ No newline at end of file diff --git a/start.bat b/start.bat new file mode 100644 index 0000000..3fe42ee --- /dev/null +++ b/start.bat @@ -0,0 +1 @@ +pm2 start ./src/app.json diff --git a/stop.bat b/stop.bat new file mode 100644 index 0000000..f543d49 --- /dev/null +++ b/stop.bat @@ -0,0 +1 @@ +pm2 delete ./src/app.json From 21c5e0170e3e23f377ba8c354dc6482a798aefa3 Mon Sep 17 00:00:00 2001 From: "Oleg@Home" Date: Wed, 18 Oct 2017 20:14:38 +0300 Subject: [PATCH 2/3] Changed ingnores --- .gitignore | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index d5ba8ef..59444bf 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file +/bot-config.json +/app2.json +/package-lock.json From 9b058202d923fdb9bd5139b9ef6b7a46c6015cd5 Mon Sep 17 00:00:00 2001 From: "Oleg@Home" Date: Wed, 18 Oct 2017 20:15:36 +0300 Subject: [PATCH 3/3] New config and changed authorization --- src/app.js | 2 +- src/bot-config-sample.json | 19 +++++--------- src/youtrack.js | 52 ++++++++------------------------------ src/youtrackbot.js | 1 - 4 files changed, 19 insertions(+), 55 deletions(-) diff --git a/src/app.js b/src/app.js index e6fdb96..2e85894 100644 --- a/src/app.js +++ b/src/app.js @@ -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); diff --git a/src/bot-config-sample.json b/src/bot-config-sample.json index 512e598..fdaab3c 100644 --- a/src/bot-config-sample.json +++ b/src/bot-config-sample.json @@ -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" } ] } diff --git a/src/youtrack.js b/src/youtrack.js index 183a913..c97b7ac 100644 --- a/src/youtrack.js +++ b/src/youtrack.js @@ -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.} - */ - 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) { @@ -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, @@ -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); @@ -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); } @@ -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; @@ -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); } @@ -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; @@ -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; } diff --git a/src/youtrackbot.js b/src/youtrackbot.js index 58c6dcc..3b25ecc 100644 --- a/src/youtrackbot.js +++ b/src/youtrackbot.js @@ -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});