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

Commit 6015665

Browse files
committed
Refactoring
1 parent 8086cfa commit 6015665

File tree

1 file changed

+85
-66
lines changed

1 file changed

+85
-66
lines changed

js/content.js

Lines changed: 85 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,63 @@
11
(function(globals) {
22
'use strict';
33

4-
globals.gmrle = {
5-
currentProjectId: null,
6-
baseProjectUrl: null,
7-
baseUrl: null,
8-
mergeRequestsDetailsApiUrl: null,
4+
class GitLabApiClient {
5+
constructor(baseUrl) {
6+
this.baseUrl = baseUrl;
7+
}
98

9+
/**
10+
* Create an `URL` object representing the full URL to the given GitLab API endpoint.
11+
*/
12+
createEndpointUrl(endpoint, queryStringParameters = null) {
13+
let url = new URL(this.baseUrl + endpoint);
14+
15+
if (queryStringParameters) {
16+
queryStringParameters.forEach(function(queryStringParameter) {
17+
url.searchParams.append(queryStringParameter[0], queryStringParameter[1]);
18+
});
19+
}
20+
21+
return url;
22+
}
23+
24+
sendRequest(method, endpoint, callback, queryStringParameters = null) {
25+
let xhr = new XMLHttpRequest();
26+
27+
xhr.responseType = 'json';
28+
29+
xhr.onload = callback;
30+
31+
xhr.onerror = function() {
32+
console.error('Error while communicating with GitLab');
33+
};
34+
35+
xhr.open(method, this.createEndpointUrl(endpoint, queryStringParameters));
36+
xhr.send();
37+
}
38+
39+
/**
40+
* Fetch details about the given Merge Requests IDs.
41+
*/
42+
getMergeRequests(projectId, mergeRequestIds, callback) {
43+
let queryStringParameters = mergeRequestIds.map(function(mergeRequestId) {
44+
return ['iids[]', mergeRequestId];
45+
});
46+
47+
this.sendRequest(
48+
'GET',
49+
'projects/' + projectId + '/merge_requests',
50+
callback,
51+
queryStringParameters
52+
);
53+
}
54+
}
55+
56+
class ContentScript {
1057
/**
1158
* Initialize the content script of the extension which is executed in the context of the page.
1259
*/
13-
init() {
60+
constructor() {
1461
this.currentProjectId = this.getCurrentProjectId();
1562

1663
if (!this.currentProjectId) {
@@ -30,15 +77,23 @@
3077
}
3178

3279
this.baseUrl = location.protocol + '//' + location.host;
33-
this.mergeRequestsDetailsApiUrl = this.baseUrl + '/api/v4/projects/' + this.currentProjectId + '/merge_requests';
80+
this.baseApiUrl = this.baseUrl + '/api/v4/';
81+
this.apiClient = new GitLabApiClient(this.baseApiUrl);
3482

3583
console.debug('Current project ID:', this.currentProjectId);
3684
console.debug('Base project URL:', this.baseProjectUrl);
3785
console.debug('GitLab base URL:', this.baseUrl);
38-
console.debug('API URL:', this.mergeRequestsDetailsApiUrl);
86+
console.debug('GitLab API base URL:', this.baseApiUrl);
87+
88+
this.removeExistingTargetBranchNodes();
89+
90+
let currentMergeRequestIds = this.getCurrentMergeRequestIdsAndSetUuidDataAttributes();
91+
92+
console.debug('Current merge requests IDs:', currentMergeRequestIds);
93+
94+
this.fetchMergeRequestsDetails(currentMergeRequestIds);
95+
}
3996

40-
this.updateUI();
41-
},
4297
/**
4398
* Finds and returns the GitLab project ID whe're looking merge requests at.
4499
*/
@@ -50,31 +105,17 @@
50105
}
51106

52107
return body.dataset.projectId;
53-
},
108+
}
109+
54110
/**
55111
* Finds and returns the URI to the project whe're looking merge requests at.
56112
*/
57113
getBaseProjectUrl() {
58114
let link = document.querySelector('.nav-sidebar .context-header a');
59115

60116
return link ? link.getAttribute('href') : null;
61-
},
62-
/**
63-
* Initialize an UI update process:
64-
* - Removes all branches that may have been already displayed by GitLab
65-
* - Get all Merge Requests IDs that are currently displayed
66-
* - Fetch their details using the GitLab API
67-
* - Actually update the UI by altering the DOM
68-
*/
69-
updateUI() {
70-
this.removeExistingTargetBranchNodes();
71-
72-
let currentMergeRequestIds = this.getCurrentMergeRequestIdsAndSetUuidDataAttributes();
73-
74-
console.debug('Current merge requests IDs:', currentMergeRequestIds);
117+
}
75118

76-
this.fetchMergeRequestsDetails(currentMergeRequestIds);
77-
},
78119
/**
79120
* Gets all Merge Requests IDs that are currently displayed AND sets the `iid` data attribute (public Merge
80121
* Request identifier) on all DOM nodes representing a Merge Requests (it's used later in the process).
@@ -89,59 +130,37 @@
89130

90131
return iid;
91132
});
92-
},
133+
}
134+
93135
/**
94136
* Performs an HTTP GET request to the GitLab API to retrieve details about Merge Requests that are
95137
* currently displayed. If successful, it actually updates the UI by altering the DOM.
96138
*/
97139
fetchMergeRequestsDetails(mergeRequestIds) {
98140
let self = this;
99-
let xhr = new XMLHttpRequest();
100-
101-
xhr.responseType = 'json';
102141

103-
xhr.onload = function() {
104-
if (this.status == 200) {
105-
if (this.response == '') {
106-
console.error('Got empty response from GitLab');
107-
108-
return;
142+
this.apiClient.getMergeRequests(
143+
this.currentProjectId,
144+
mergeRequestIds,
145+
function() {
146+
if (this.status == 200) {
147+
self.updateMergeRequestsNodes(this.response);
148+
} else {
149+
console.error('Got error from GitLab:', this.status, this.response);
109150
}
110-
111-
self.updateMergeRequestsNodes(this.response);
112-
} else {
113-
console.error('Got error from GitLab:', this.status, this.response);
114151
}
115-
};
116-
117-
xhr.onerror = function() {
118-
console.error('Error while communicating with GitLab');
119-
};
120-
121-
xhr.open('GET', this.createMergeRequestsDetailsApiUrl(mergeRequestIds));
122-
xhr.send();
123-
},
124-
/**
125-
* Create an `URL` object representing the URL to the GitLab API endpoint that returns details about
126-
* Merge Requests that are currently displayed (and only these ones).
127-
*/
128-
createMergeRequestsDetailsApiUrl(mergeRequestIds) {
129-
let url = new URL(this.mergeRequestsDetailsApiUrl);
130-
131-
mergeRequestIds.forEach(function(mergeRequestId) {
132-
url.searchParams.append('iids[]', mergeRequestId);
133-
});
152+
);
153+
}
134154

135-
return url;
136-
},
137155
/**
138156
* Removes all branches that may have been already displayed by GitLab.
139157
*/
140158
removeExistingTargetBranchNodes() {
141159
document.querySelectorAll('.mr-list .merge-request .project-ref-path').forEach(function(el) {
142160
el.parentNode.removeChild(el);
143161
});
144-
},
162+
}
163+
145164
/**
146165
* Actually updates the UI by altering the DOM by adding our stuff.
147166
*/
@@ -165,7 +184,7 @@
165184
.appendChild(branchesInfoNode);
166185
});
167186
}
168-
};
187+
}
169188

170-
globals.gmrle.init();
171-
}(this));
189+
let cs = new ContentScript();
190+
}(this));

0 commit comments

Comments
 (0)