forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgitlab-client.js
More file actions
executable file
·198 lines (164 loc) · 5.71 KB
/
Copy pathgitlab-client.js
File metadata and controls
executable file
·198 lines (164 loc) · 5.71 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import crypto from 'crypto';
import { throttledQueue } from 'throttled-queue';
import parallel from './parallel.js';
/**
* GitLab REST/GraphQL client: owns the request throttle, single/parallel
* paginated GET, POST and GraphQL. Root of the API-model hierarchy.
*/
class GitlabClient {
static throttle;
static init(config) {
if(GitlabClient.throttle == undefined){
GitlabClient.throttle = throttledQueue(config.data.throttleMaxRequestsPerInterval, config.data.throttleInterval);
}
}
/**
* construct
* @param config
*/
constructor(config) {
GitlabClient.init(config);
this.config = config;
this.url = config.get('url').endsWith('/') ? config.get('url') : `${config.get('url')}/`;
this.token = config.get('token');
this._perPage = this.config ? this.config.get('_perPage') : 100;
this._parallel = this.config ? this.config.get('_parallel') : 4;
}
/**
* query the given path
* @param path
* @param data
* @returns {Promise}
*/
post(path, data) {
return GitlabClient.throttle(async () => {
const response = await fetch(`${this.url}${path}`, {
method: 'POST',
headers: {
'PRIVATE-TOKEN': this.token,
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
await GitlabClient.assertOk(response, 'POST', path);
const isJson = (response.headers.get('content-type') ?? '').startsWith('application/json');
const body = isJson ? await response.json() : undefined;
return { body, headers: response.headers };
});
}
/**
* throw a descriptive error if the response is not OK
* @param response
* @param method
* @param path
*/
static async assertOk(response, method, path) {
if (response.ok) return;
let body = '';
try {
body = (await response.text()).slice(0, 512);
} catch { /* body is optional error context */ }
throw new Error(`${method} ${path} failed: ${response.status} ${response.statusText}${body ? ` — ${body}` : ''}`);
}
/**
* throw if the response is not JSON
* @param response
* @param method
* @param path
*/
static assertJson(response, method, path) {
const contentType = response.headers.get('content-type') ?? '';
if (!contentType.startsWith('application/json'))
throw new Error(`${method} ${path} returned content-type "${contentType}", expected application/json`);
}
/**
* query the given path
* @param data
* @returns {Promise}
*/
graphQL(data) {
// remove v4/ from url, add graphql
const path = this.url.substr(0, this.url.length-3) + 'graphql';
return GitlabClient.throttle(async () => {
const response = await fetch(`${path}`, {
method: 'POST',
headers: {
'Authorization': 'Bearer '+this.token,
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
await GitlabClient.assertOk(response, 'POST', path);
GitlabClient.assertJson(response, 'POST', path);
return { body: await response.json(), headers: response.headers };
});
}
/**
* query the given path
* @param path
* @param page
* @param perPage
* @returns {Promise}
*/
get(path, page = 1, perPage = this._perPage) {
path += (path.includes('?') ? '&' : '?') + `page=${page}&per_page=${perPage}`;
return GitlabClient.throttle(async () => {
const response = await fetch(`${this.url}${path}`, {
headers: {
'PRIVATE-TOKEN': this.token
}
});
await GitlabClient.assertOk(response, 'GET', path);
GitlabClient.assertJson(response, 'GET', path);
return { body: await response.json(), headers: response.headers };
});
}
/**
* query the given path and paginate automatically and in parallel
* through all available pages
* @param path
* @param perPage
* @param runners
* @returns {Promise}
*/
async all(path, perPage = this._perPage, runners = this._parallel) {
const response = await this.get(path, 1, perPage);
const collect = [...response.body];
const pages = parseInt(response.headers.get('x-total-pages'));
if (pages === 1) return collect;
const tasks = GitlabClient.createGetTasks(path, pages, 2, perPage);
await this.getParallel(tasks, collect, runners);
return collect;
}
/**
* make multiple get requests by the given tasks and apply the
* data to the given set
* @param tasks
* @param collect
* @param runners
* @returns {Promise}
*/
getParallel(tasks, collect = [], runners = this._parallel) {
return parallel(tasks, async task => {
const response = await this.get(task.path, task.page, task.perPage);
response.body.forEach(item => collect.push(item));
}, this.config, runners);
}
/**
* create a task list to get all pages from
* the given path
* @param path
* @param to
* @param from
* @param perPage
* @returns {Array}
*/
static createGetTasks(path, to, from = 2, perPage = this._perPage) {
let tasks = [];
for (let i = from; i <= to; i++) {
tasks.push({path, perPage, page: i})
}
return tasks;
}
}
export default GitlabClient;