This repository was archived by the owner on Apr 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYouTrack.js
More file actions
executable file
·176 lines (167 loc) · 6.53 KB
/
YouTrack.js
File metadata and controls
executable file
·176 lines (167 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const axios = require("axios");
const Cache = require('rgcache').Cache;
const StringSimilarity = require('string-similarity');
module.exports = class YouTrack {
cache = new Cache({
ttl: 300, loadStrategy: "one", thisArg: this, loader: async (key, payload) => {
let tokenInfo = await this.database.getToken(key);
if (!tokenInfo) return null;
//if token expired or will expire in 5 minutes, refresh it
if (tokenInfo.expires_at < Date.now() + 300000) {
let params = new (require('url').URLSearchParams)();
params.set("grant_type", "refresh_token");
params.set("refresh_token", tokenInfo.refresh_token);
let resp = await axios.post("https://yt.kioskapi.ru/hub/api/rest/oauth2/token", params.toString(), {
auth: {
username: "ece4c096-1a40-4021-9a9c-84cbab5e4755",
password: process.env.YOUTRACK_SECRET
},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
await this.database.setToken(key, resp.data.access_token, tokenInfo.refresh_token);
return resp.data.access_token;
}
return tokenInfo.access_token;
}
});
constructor(baseUrl, database) {
this.baseUrl = baseUrl;
this.database = database;
}
/**
*
* @param userId number
* @return {Promise<{
* "leader": {
* "login": string,
* "name": string,
* "id": string,
* "$type": string
* },
* "shortName": string,
* "name": string,
* "id": string,
* "$type": string,
* }[]>}
*/
async getProjects(userId) {
let res = await axios.get(this.baseUrl + `/admin/projects?fields=id,name,shortName,createdBy(login,name,id),leader(login,name,id),key`, {
headers: {
authorization: `Bearer ${await this.cache.get(userId)}`
}
})
return res.data;
}
/**
*
* @param userId
* @param project
* @return {Promise<{
* "leader": {
* "login": string,
* "name": string,
* "id": string,
* "$type": string
* },
* "shortName": string,
* "name": string,
* "id": string,
* "$type": string,
* }>}
*/
async searchProject(userId, project) {
let projects = await this.getProjects(userId);
let id = null;
if (/^[A-z]{1,4}$/.test(project)) { //short name
let match = StringSimilarity.findBestMatch(project, projects.map(p => p.shortName)).bestMatch.target;
id = projects.find(p => p.shortName === match);
} else { //name
let match = StringSimilarity.findBestMatch(project, projects.map(p => p.name)).bestMatch.target;
id = projects.find(p => p.name === match);
}
return id;
}
async createIssue(userId, project, summary, description) {
let res = await axios.post(this.baseUrl + `/issues?fields=idReadable`, {
summary,
description,
project: {
id: project
}
},
{
headers: {
authorization: `Bearer ${await this.cache.get(userId)}`
}
});
return res.data.idReadable;
}
/**
*
* @param userId
* @param query
* @return {Promise<{error: null|string, results: {idReadable:string,summary:string,description:string,reporter:string,project:string,assignee:string,priority:string,state:string,dueDate:string|number}[]}>}
*/
async searchIssues(userId, query) {
let res = await axios.get(this.baseUrl + `/issues?query=${encodeURIComponent(query)}&fields=id,summary,description,reporter(login),project(id,name,shortName),idReadable,customFields(name,value(name))&customFields=assignee&customFields=priority&customFields=state&customFields=due%20date`,
{
headers: {
authorization: `Bearer ${await this.cache.get(userId)}`
},
validateStatus: (status) => status < 300 || status >= 400 && status < 500
});
if (res.data.error) {
return {
results: [],
error: res.data.error_children[0].error
};
} else {
return {
results: res.data.map(issue => {
return {
idReadable: issue.idReadable,
summary: issue.summary,
description: issue.description,
reporter: issue.reporter.login,
project: issue.project.name,
assignee: issue.customFields.find(f => f.name === "Assignee")?.value?.map(u => u.name).join(", ") || "Нет",
priority: issue.customFields.find(f => f.name === "Priority")?.value?.name,
state: issue.customFields.find(f => f.name === "State")?.value?.name,
dueDate: issue.customFields.find(f => f.name === "Due Date")?.value || "Нет",
}
}),
error: null
}
}
}
async isUserAuthorized(userId) {
let token = await this.cache.get(userId);
if(token === null) {
await this.cache.delete(userId);
return false;
}
return true;
}
async createComment(userId, issueId, text) {
let res = await axios.post(this.baseUrl + `/issues/${issueId}/comments?fields=`+encodeURIComponent(`id,author(login,name,id),deleted,text,updated,visibility(permittedGroups(name,id),permittedUsers(id,name,login))`), {
text
},
{
headers: {
authorization: `Bearer ${await this.cache.get(userId)}`
}
});
return res.data.id;
}
async createAnonymousComment(issueId, author, text) {
let res = await axios.post(this.baseUrl + `/issues/${issueId}/comments?fields=`+encodeURIComponent(`id,author(login,name,id),deleted,text,updated,visibility(permittedGroups(name,id),permittedUsers(id,name,login))`), {
text: author + ":\n" + text
},
{
headers: {
authorization: `Bearer ${process.env.ANON_TOKEN}`
}
});
return res.data.id;
}
}