Skip to content

Commit 877268b

Browse files
authored
Merge pull request #74 from leonhartX/support-gitlab
Fix several issue for gitlab
2 parents 6f1f1e9 + c0b889d commit 877268b

4 files changed

Lines changed: 128 additions & 142 deletions

File tree

options/options.js

Lines changed: 59 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ $(() => {
2828
$('.logout-container').show();
2929
let user = item.user, domain, userLink, tokenLink;
3030

31-
32-
3331
if(item.scm === 'bitbucket') {
3432
domain = '@Bitbucket.org';
3533
userLink = `https://bitbucket.org/${user}`;
@@ -81,14 +79,14 @@ function getGithubParam() {
8179
const scm = 'github';
8280
const username = $('#username').val();
8381
const password = $('#password').val();
84-
const personalToken = $('#accesstoken').val();
82+
const token = $('#accesstoken').val();
8583
const baseUrl = `https://api.github.com`;
8684
const otp = $('#otp').val();
8785
return {
8886
scm,
8987
username,
9088
password,
91-
personalToken,
89+
token,
9290
baseUrl,
9391
otp
9492
};
@@ -128,21 +126,19 @@ function getGitLabParam() {
128126
const scm = 'gitlab';
129127
const username = $('#gitlab-email').val();
130128
const password = $('#gitlab-password').val();
131-
const personalToken = $('#gitlab-accesstoken').val();
132-
const tokenType = (personalToken && personalToken.length > 0) ? 'personalToken' : 'oAuth';
129+
const token = $('#gitlab-accesstoken').val();
130+
const tokenType = (token && token.length > 0) ? 'personalToken' : 'oAuth';
133131
const baseUrl = ($('#gitlab-url').val() || 'https://gitlab.com') + '/api/v4';
134-
//const baseUrl = 'https://gitlab.com/api/v4';
135132
return {
136133
scm,
137134
username,
138135
password,
139136
tokenType,
140-
personalToken,
137+
token,
141138
baseUrl
142139
}
143140
}
144141

145-
146142
function addCred(param) {
147143
if (param.username === '') {
148144
return;
@@ -152,21 +148,24 @@ function addCred(param) {
152148
}
153149

154150
if (param.scm === 'bitbucket') return loginBitbucket(param);
155-
if (param.scm === 'gitlab' && param.tokenType ==='oAuth') return loginGitLabOauth(param);
156-
if (param.scm === 'gitlab' && param.tokenType ==='personalToken') return loginGitLabToken(param);
157-
if (param.password !== '' && param.scm === 'github') return loginGithub(param);
158-
159-
addStar(param.token)
160-
.then(() => {
161-
chrome.storage.sync.set({scm: param.scm, user: param.username, token: param.token, baseUrl: param.baseUrl}, () => {
162-
location.reload();
163-
});
164-
chrome.storage.local.get('tab', (item) => {
165-
if(item.tab) {
166-
chrome.tabs.reload(item.tab);
167-
}
168-
});
169-
})
151+
if (param.scm === 'gitlab') {
152+
if (param.token !== '') return loginGitLabToken(param);
153+
return loginGitLabOauth(param);
154+
}
155+
if (param.scm === 'github') {
156+
if (param.password !== '') return loginGithub(param);
157+
addStar(param.token)
158+
.then(() => {
159+
chrome.storage.sync.set({scm: param.scm, user: param.username, token: param.token, baseUrl: param.baseUrl}, () => {
160+
location.reload();
161+
});
162+
chrome.storage.local.get('tab', (item) => {
163+
if(item.tab) {
164+
chrome.tabs.reload(item.tab);
165+
}
166+
});
167+
})
168+
}
170169
}
171170

172171
function loginGithub(param) {
@@ -284,60 +283,47 @@ function loginGitLabOauth(param) {
284283
password: password
285284
}
286285
})
287-
.done(response => {
288-
return $.getJSON(
289-
`${baseUrl}/user`,
290-
{ access_token: response.access_token }
291-
)
292-
.done(user => {
293-
chrome.storage.sync.set({scm: param.scm, user: user.username, token: {type : 'oAuth', token :response.access_token}, baseUrl: baseUrl}, () => {
294-
location.reload();
295-
});
296-
chrome.storage.local.get('tab', (item) => {
297-
if(item.tab) {
298-
chrome.tabs.reload(item.tab);
299-
}
300-
});
301-
});
302-
})
303-
.fail(err => {
304-
if (err.status == 401 &&
305-
err.getResponseHeader('X-GitLab-OTP') !== null &&
306-
$('.login-item-otp').filter(':visible').length == 0) {
307-
$('.login-item').animate({height: 'toggle', opacity: 'toggle'}, 'slow');
308-
} else {
309-
$('.error').show();
310-
}
311-
})
286+
.done(response => {
287+
return $.getJSON(
288+
`${baseUrl}/user`,
289+
{ access_token: response.access_token }
290+
)
291+
.done(user => {
292+
chrome.storage.sync.set({scm: param.scm, user: user.username, token: {type : 'oAuth', token :response.access_token}, baseUrl: baseUrl}, () => {
293+
location.reload();
294+
});
295+
chrome.storage.local.get('tab', (item) => {
296+
if(item.tab) {
297+
chrome.tabs.reload(item.tab);
298+
}
299+
});
300+
});
301+
})
302+
.fail(err => {
303+
$('.error').show();
304+
})
312305
}
313306

314307
function loginGitLabToken(param) {
315-
const personalToken = param.personalToken;
308+
const personalToken = param.token;
316309
const baseUrl = param.baseUrl;
317-
const headers = {};
318-
$.getJSON(
319-
`${baseUrl}/user`,
320-
{ private_token: personalToken }
321-
)
322-
.done(user => {
323-
chrome.storage.sync.set({scm: param.scm, user: user.username, token: {type : 'personalToken', token :personalToken}, baseUrl: baseUrl}, () => {
324-
location.reload();
325-
});
326-
chrome.storage.local.get('tab', (item) => {
327-
if(item.tab) {
328-
chrome.tabs.reload(item.tab);
329-
}
330-
});
331-
})
332-
.fail(err => {
333-
if (err.status == 401 &&
334-
err.getResponseHeader('X-GitLab-OTP') !== null &&
335-
$('.login-item-otp').filter(':visible').length == 0) {
336-
$('.login-item').animate({height: 'toggle', opacity: 'toggle'}, 'slow');
337-
} else {
338-
$('.error').show();
310+
$.getJSON(
311+
`${baseUrl}/user`,
312+
{ private_token: personalToken }
313+
)
314+
.done(user => {
315+
chrome.storage.sync.set({scm: param.scm, user: user.username, token: {type : 'personalToken', token :personalToken}, baseUrl: baseUrl}, () => {
316+
location.reload();
317+
});
318+
chrome.storage.local.get('tab', (item) => {
319+
if(item.tab) {
320+
chrome.tabs.reload(item.tab);
339321
}
340-
})
322+
});
323+
})
324+
.fail(err => {
325+
$('.error').show();
326+
})
341327
}
342328

343329
function logout() {

src/gas-hub.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ $(() => {
1919
.then(updateGist)
2020
.then(initPageEvent)
2121
.catch((err) => {
22-
debugger;
2322
switch (err.message) {
2423
case 'need login' :
2524
initLoginContent();

0 commit comments

Comments
 (0)