Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

Commit 78848d3

Browse files
authored
Merge pull request #8 from EpocDotFr/auto-update
Prepare for release on Opera and Edge
2 parents 65d4f93 + 84c07bf commit 78848d3

File tree

5 files changed

+85
-82
lines changed

5 files changed

+85
-82
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ Due to a technical GitLab limitation, the extension has no reliable way to deter
5050

5151
It would be great, however the extension has no reliable way to do that due to a technical GitLab limitation.
5252

53+
- Pipeline status icons aren't being automatically updated on Merge Requests lists (it is on other pages), can you add this feature?
54+
55+
That was the initial idea for the 1.6 release, however it's not possible due to a technical GitLab limitation.
56+
5357
## Changelog
5458

5559
See [here](https://github.com/EpocDotFr/gitlab-merge-requests-lists-enhancer/releases).

js/content.js

Lines changed: 64 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,26 @@
1212
}
1313

1414
/**
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.
1616
*/
1717
createEndpointUrl(endpoint, queryStringParameters = null) {
18-
let url = new URL(this.baseUrl + endpoint);
18+
let endpointUrl = new URL(this.baseUrl + endpoint);
1919

2020
if (queryStringParameters) {
2121
queryStringParameters.forEach(function(queryStringParameter) {
22-
url.searchParams.append(queryStringParameter[0], queryStringParameter[1]);
22+
endpointUrl.searchParams.append(queryStringParameter[0], queryStringParameter[1]);
2323
});
2424
}
2525

26-
return url;
26+
return endpointUrl.toString();
2727
}
2828

2929
/**
3030
* Sends an HTTP request to the GitLab API.
3131
*/
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;
4435

4536
if (['post', 'put', 'patch'].includes(method.toLowerCase())) {
4637
if (!this.csrfToken) {
@@ -49,28 +40,46 @@
4940
return;
5041
}
5142

52-
xhr.setRequestHeader('X-CSRF-Token', this.csrfToken);
43+
headers['X-CSRF-Token'] = this.csrfToken;
5344
}
5445

5546
if (data) {
56-
xhr.setRequestHeader('Content-Type', 'application/json');
47+
headers['Content-Type'] = 'application/json';
5748

58-
data = JSON.stringify(data);
49+
body = JSON.stringify(data);
5950
}
6051

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;
6272
}
6373

6474
/**
6575
* Fetch details about the given Merge Requests IDs in the given project ID.
6676
*/
67-
getProjectMergeRequests(callback, projectId, mergeRequestIds) {
77+
getProjectMergeRequests(projectId, mergeRequestIds) {
6878
let queryStringParameters = mergeRequestIds.map(function(mergeRequestId) {
6979
return ['iids[]', mergeRequestId];
7080
});
7181

72-
this.sendRequest(
73-
callback,
82+
return this.sendRequest(
7483
'GET',
7584
'projects/' + projectId + '/merge_requests',
7685
queryStringParameters
@@ -80,16 +89,15 @@
8089
/**
8190
* Update the given Merge Request Id in the given project ID.
8291
*/
83-
updateProjectMergeRequest(callback, projectId, mergeRequestId, data) {
92+
updateProjectMergeRequest(projectId, mergeRequestId, data) {
8493
let dataToSend = {
8594
id: parseInt(projectId, 10),
8695
merge_request_iid: parseInt(mergeRequestId, 10)
8796
};
8897

8998
Object.assign(dataToSend, data);
9099

91-
this.sendRequest(
92-
callback,
100+
return this.sendRequest(
93101
'PUT',
94102
'projects/' + projectId + '/merge_requests/' + mergeRequestId,
95103
null,
@@ -126,14 +134,15 @@
126134
this.userAuthenticated = this.isUserAuthenticated();
127135
this.apiClient = new GitLabApiClient(this.baseApiUrl, this.getCsrfToken());
128136

129-
let currentMergeRequestIds = this.getCurrentMergeRequestIds();
137+
this.currentMergeRequestIds = this.getCurrentMergeRequestIds();
138+
130139
let preferencesManager = new globals.Gmrle.PreferencesManager();
131140

132141
let self = this;
133142

134143
preferencesManager.getAll(function(preferences) {
135144
self.preferences = preferences;
136-
self.fetchMergeRequestsDetailsThenUpdateUI(currentMergeRequestIds);
145+
self.fetchMergeRequestsDetailsThenUpdateUI(self.currentMergeRequestIds);
137146
});
138147
}
139148

@@ -194,34 +203,27 @@
194203
let self = this;
195204

196205
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+
}
204212

205-
if (self.preferences.enable_buttons_to_copy_source_and_target_branches_name) {
206-
self.attachClickEventToCopyBranchNameButtons();
207-
}
213+
self.updateMergeRequestsNodes(responseData);
208214

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+
}
212218

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+
}
218222

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+
});
225227
}
226228

227229
/**
@@ -525,28 +527,21 @@
525527
}
526528

527529
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-
},
544530
this.currentProjectId,
545531
mergeRequestNode.dataset.iid,
546532
{
547533
title: newTitle
548534
}
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+
});
550545
}
551546

552547
/**

js/options.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,14 @@
182182
getCurrentBrowserName() {
183183
let ua = navigator.userAgent;
184184

185-
if (ua.includes('Firefox') && !ua.includes('Seamonkey')) {
185+
if (ua.includes('Firefox/') && !ua.includes('Seamonkey/')) {
186186
return 'firefox';
187-
} else if (ua.includes('Chrome') && !ua.includes('Chromium')) {
187+
} else if (ua.includes('Chrome/') && !ua.includes('Chromium/')) {
188188
return 'chrome';
189+
} else if (ua.includes('Edg/')) {
190+
return 'edge';
191+
} else if (ua.includes('OPR/')) {
192+
return 'opera';
189193
}
190194

191195
return null;

js/preferences.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
* This class holds all the logic related to user preferences persistance.
2222
*/
2323
constructor() {
24-
if (globals.browser) { // Firefox uses `browser`, Chrome uses `chrome`
25-
this.getAll = this.getAllFirefox;
26-
this.setAll = this.setAllFirefox;
24+
if (globals.browser) { // Firefox and Edge uses `browser`, Chrome and Opera uses `chrome`
25+
this.getAll = this.getAllBrowser;
26+
this.setAll = this.setAllBrowser;
2727
} else if (globals.chrome) {
2828
this.getAll = this.getAllChrome;
2929
this.setAll = this.setAllChrome;
@@ -35,9 +35,9 @@
3535
/**
3636
* Get all the user's preferences.
3737
*
38-
* Used as `getAll` if the current browser is Firefox.
38+
* Used as `getAll` if the current browser is Firefox or Edge.
3939
*/
40-
getAllFirefox(successCallback) {
40+
getAllBrowser(successCallback) {
4141
browser.storage.local.get(this.defaults).then(successCallback, function() {
4242
alert('Error retrieving extension preferences.');
4343
});
@@ -46,9 +46,9 @@
4646
/**
4747
* Save all the user's preferences.
4848
*
49-
* Used as `setAll` if the current browser is Firefox.
49+
* Used as `setAll` if the current browser is Firefox or Edge.
5050
*/
51-
setAllFirefox(preferences, successCallback, errorCallback) {
51+
setAllBrowser(preferences, successCallback, errorCallback) {
5252
browser.storage.local.set(preferences).then(successCallback, function() {
5353
errorCallback();
5454

@@ -59,7 +59,7 @@
5959
/**
6060
* Get all the user's preferences.
6161
*
62-
* Used as `getAll` if the current browser is Chrome.
62+
* Used as `getAll` if the current browser is Chrome or Opera.
6363
*/
6464
getAllChrome(successCallback) {
6565
chrome.storage.local.get(this.defaults, function(preferences) {
@@ -76,7 +76,7 @@
7676
/**
7777
* Save all the user's preferences.
7878
*
79-
* Used as `setAll` if the current browser is Chrome.
79+
* Used as `setAll` if the current browser is Chrome or Opera.
8080
*/
8181
setAllChrome(preferences, successCallback, errorCallback) {
8282
chrome.storage.local.set(preferences, function() {

scripts/manage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def create_manifest_file(target):
1616
}
1717
}
1818
elif target == 'chrome':
19-
data['minimum_chrome_version'] = '66'
2019
data['options_ui']['chrome_style'] = True
20+
data['minimum_chrome_version'] = '66'
2121

2222
with open('manifest.json', 'w', encoding='utf-8') as f:
2323
json.dump(data, f, indent=2)
@@ -53,7 +53,7 @@ def build(target):
5353
def run():
5454
arg_parser = argparse.ArgumentParser()
5555
arg_parser.add_argument('action', choices=['build', 'switch'])
56-
arg_parser.add_argument('target', choices=['firefox', 'chrome'])
56+
arg_parser.add_argument('target', choices=['firefox', 'chrome', 'opera', 'edge'])
5757

5858
args = arg_parser.parse_args()
5959

0 commit comments

Comments
 (0)