|
12 | 12 | } |
13 | 13 |
|
14 | 14 | /** |
15 | | - * Creates an `URL` object representing the full URL to the given GitLab API endpoint. |
| 15 | + * Returns the full URL to the given GitLab API endpoint. |
16 | 16 | */ |
17 | 17 | createEndpointUrl(endpoint, queryStringParameters = null) { |
18 | | - let url = new URL(this.baseUrl + endpoint); |
| 18 | + let endpointUrl = new URL(this.baseUrl + endpoint); |
19 | 19 |
|
20 | 20 | if (queryStringParameters) { |
21 | 21 | queryStringParameters.forEach(function(queryStringParameter) { |
22 | | - url.searchParams.append(queryStringParameter[0], queryStringParameter[1]); |
| 22 | + endpointUrl.searchParams.append(queryStringParameter[0], queryStringParameter[1]); |
23 | 23 | }); |
24 | 24 | } |
25 | 25 |
|
26 | | - return url; |
| 26 | + return endpointUrl.toString(); |
27 | 27 | } |
28 | 28 |
|
29 | 29 | /** |
30 | 30 | * Sends an HTTP request to the GitLab API. |
31 | 31 | */ |
32 | | - sendRequest(callback, method, endpoint, queryStringParameters = null, data = null) { |
33 | | - let xhr = new XMLHttpRequest(); |
34 | | - |
35 | | - xhr.responseType = 'json'; |
36 | | - |
37 | | - xhr.onload = callback; |
38 | | - |
39 | | - xhr.onerror = function() { |
40 | | - alert('Error while communicating with GitLab.'); |
41 | | - }; |
42 | | - |
43 | | - xhr.open(method, this.createEndpointUrl(endpoint, queryStringParameters)); |
| 32 | + sendRequest(method, endpoint, queryStringParameters = null, data = null) { |
| 33 | + let headers = {}; |
| 34 | + let body = null; |
44 | 35 |
|
45 | 36 | if (['post', 'put', 'patch'].includes(method.toLowerCase())) { |
46 | 37 | if (!this.csrfToken) { |
|
49 | 40 | return; |
50 | 41 | } |
51 | 42 |
|
52 | | - xhr.setRequestHeader('X-CSRF-Token', this.csrfToken); |
| 43 | + headers['X-CSRF-Token'] = this.csrfToken; |
53 | 44 | } |
54 | 45 |
|
55 | 46 | if (data) { |
56 | | - xhr.setRequestHeader('Content-Type', 'application/json'); |
| 47 | + headers['Content-Type'] = 'application/json'; |
57 | 48 |
|
58 | | - data = JSON.stringify(data); |
| 49 | + body = JSON.stringify(data); |
59 | 50 | } |
60 | 51 |
|
61 | | - xhr.send(data); |
| 52 | + let fetchPromise = fetch(this.createEndpointUrl(endpoint, queryStringParameters), { |
| 53 | + method: method, |
| 54 | + headers: headers, |
| 55 | + body: body, |
| 56 | + credentials: 'same-origin' |
| 57 | + }).then(function(response) { |
| 58 | + if (response.ok) { |
| 59 | + return response.json(); |
| 60 | + } else { |
| 61 | + return Promise.reject(response); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + fetchPromise.catch(function(err) { |
| 66 | + console.error('Got error from GitLab:', err); |
| 67 | + |
| 68 | + alert('Got error from GitLab, check console for more information.'); |
| 69 | + }); |
| 70 | + |
| 71 | + return fetchPromise; |
62 | 72 | } |
63 | 73 |
|
64 | 74 | /** |
65 | 75 | * Fetch details about the given Merge Requests IDs in the given project ID. |
66 | 76 | */ |
67 | | - getProjectMergeRequests(callback, projectId, mergeRequestIds) { |
| 77 | + getProjectMergeRequests(projectId, mergeRequestIds) { |
68 | 78 | let queryStringParameters = mergeRequestIds.map(function(mergeRequestId) { |
69 | 79 | return ['iids[]', mergeRequestId]; |
70 | 80 | }); |
71 | 81 |
|
72 | | - this.sendRequest( |
73 | | - callback, |
| 82 | + return this.sendRequest( |
74 | 83 | 'GET', |
75 | 84 | 'projects/' + projectId + '/merge_requests', |
76 | 85 | queryStringParameters |
|
80 | 89 | /** |
81 | 90 | * Update the given Merge Request Id in the given project ID. |
82 | 91 | */ |
83 | | - updateProjectMergeRequest(callback, projectId, mergeRequestId, data) { |
| 92 | + updateProjectMergeRequest(projectId, mergeRequestId, data) { |
84 | 93 | let dataToSend = { |
85 | 94 | id: parseInt(projectId, 10), |
86 | 95 | merge_request_iid: parseInt(mergeRequestId, 10) |
87 | 96 | }; |
88 | 97 |
|
89 | 98 | Object.assign(dataToSend, data); |
90 | 99 |
|
91 | | - this.sendRequest( |
92 | | - callback, |
| 100 | + return this.sendRequest( |
93 | 101 | 'PUT', |
94 | 102 | 'projects/' + projectId + '/merge_requests/' + mergeRequestId, |
95 | 103 | null, |
|
126 | 134 | this.userAuthenticated = this.isUserAuthenticated(); |
127 | 135 | this.apiClient = new GitLabApiClient(this.baseApiUrl, this.getCsrfToken()); |
128 | 136 |
|
129 | | - let currentMergeRequestIds = this.getCurrentMergeRequestIds(); |
| 137 | + this.currentMergeRequestIds = this.getCurrentMergeRequestIds(); |
| 138 | + |
130 | 139 | let preferencesManager = new globals.Gmrle.PreferencesManager(); |
131 | 140 |
|
132 | 141 | let self = this; |
133 | 142 |
|
134 | 143 | preferencesManager.getAll(function(preferences) { |
135 | 144 | self.preferences = preferences; |
136 | | - self.fetchMergeRequestsDetailsThenUpdateUI(currentMergeRequestIds); |
| 145 | + self.fetchMergeRequestsDetailsThenUpdateUI(self.currentMergeRequestIds); |
137 | 146 | }); |
138 | 147 | } |
139 | 148 |
|
|
194 | 203 | let self = this; |
195 | 204 |
|
196 | 205 | this.apiClient.getProjectMergeRequests( |
197 | | - function() { |
198 | | - if (this.status == 200) { |
199 | | - if (self.preferences.display_source_and_target_branches) { |
200 | | - self.removeExistingTargetBranchNodes(); |
201 | | - } |
202 | | - |
203 | | - self.updateMergeRequestsNodes(this.response); |
| 206 | + this.currentProjectId, |
| 207 | + mergeRequestIds |
| 208 | + ).then(function(responseData) { |
| 209 | + if (self.preferences.display_source_and_target_branches) { |
| 210 | + self.removeExistingTargetBranchNodes(); |
| 211 | + } |
204 | 212 |
|
205 | | - if (self.preferences.enable_buttons_to_copy_source_and_target_branches_name) { |
206 | | - self.attachClickEventToCopyBranchNameButtons(); |
207 | | - } |
| 213 | + self.updateMergeRequestsNodes(responseData); |
208 | 214 |
|
209 | | - if (self.preferences.enable_button_to_copy_mr_info) { |
210 | | - self.attachClickEventToCopyMergeRequestInfoButtons(); |
211 | | - } |
| 215 | + if (self.preferences.enable_buttons_to_copy_source_and_target_branches_name) { |
| 216 | + self.attachClickEventToCopyBranchNameButtons(); |
| 217 | + } |
212 | 218 |
|
213 | | - if (self.userAuthenticated && self.preferences.enable_button_to_toggle_wip_status) { |
214 | | - self.attachClickEventToToggleWipStatusButtons(); |
215 | | - } |
216 | | - } else { |
217 | | - console.error('Got error from GitLab:', this.status, this.response); |
| 219 | + if (self.preferences.enable_button_to_copy_mr_info) { |
| 220 | + self.attachClickEventToCopyMergeRequestInfoButtons(); |
| 221 | + } |
218 | 222 |
|
219 | | - alert('Got error from GitLab, check console for more information.'); |
220 | | - } |
221 | | - }, |
222 | | - this.currentProjectId, |
223 | | - mergeRequestIds |
224 | | - ); |
| 223 | + if (self.userAuthenticated && self.preferences.enable_button_to_toggle_wip_status) { |
| 224 | + self.attachClickEventToToggleWipStatusButtons(); |
| 225 | + } |
| 226 | + }); |
225 | 227 | } |
226 | 228 |
|
227 | 229 | /** |
|
525 | 527 | } |
526 | 528 |
|
527 | 529 | this.apiClient.updateProjectMergeRequest( |
528 | | - function() { |
529 | | - if (this.status == 200) { |
530 | | - mergeRequestNode.dataset.isWip = this.response.work_in_progress; |
531 | | - mergeRequestNode.dataset.title = this.response.title; |
532 | | - |
533 | | - mergeRequestNode.querySelector('.merge-request-title-text a').textContent = this.response.title; |
534 | | - } else { |
535 | | - console.error('Got error from GitLab:', this.status, this.response); |
536 | | - |
537 | | - alert('Got error from GitLab, check console for more information.'); |
538 | | - } |
539 | | - |
540 | | - toggleButton.disabled = false; |
541 | | - toggleButton.firstChild.classList.add('fa-wrench'); |
542 | | - toggleButton.firstChild.classList.remove('fa-spinner', 'fa-spin'); |
543 | | - }, |
544 | 530 | this.currentProjectId, |
545 | 531 | mergeRequestNode.dataset.iid, |
546 | 532 | { |
547 | 533 | title: newTitle |
548 | 534 | } |
549 | | - ); |
| 535 | + ).then(function(responseData) { |
| 536 | + mergeRequestNode.dataset.isWip = responseData.work_in_progress; |
| 537 | + mergeRequestNode.dataset.title = responseData.title; |
| 538 | + |
| 539 | + mergeRequestNode.querySelector('.merge-request-title-text a').textContent = responseData.title; |
| 540 | + }).finally(function() { |
| 541 | + toggleButton.disabled = false; |
| 542 | + toggleButton.firstChild.classList.add('fa-wrench'); |
| 543 | + toggleButton.firstChild.classList.remove('fa-spinner', 'fa-spin'); |
| 544 | + }); |
550 | 545 | } |
551 | 546 |
|
552 | 547 | /** |
|
0 commit comments