Skip to content

Commit

Permalink
Merge pull request #74 from leonhartX/support-gitlab
Browse files Browse the repository at this point in the history
Fix several issue for gitlab
  • Loading branch information
leonhartX authored Apr 9, 2018
2 parents 6f1f1e9 + c0b889d commit 877268b
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 142 deletions.
132 changes: 59 additions & 73 deletions options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ $(() => {
$('.logout-container').show();
let user = item.user, domain, userLink, tokenLink;



if(item.scm === 'bitbucket') {
domain = '@Bitbucket.org';
userLink = `https://bitbucket.org/${user}`;
Expand Down Expand Up @@ -81,14 +79,14 @@ function getGithubParam() {
const scm = 'github';
const username = $('#username').val();
const password = $('#password').val();
const personalToken = $('#accesstoken').val();
const token = $('#accesstoken').val();
const baseUrl = `https://api.github.com`;
const otp = $('#otp').val();
return {
scm,
username,
password,
personalToken,
token,
baseUrl,
otp
};
Expand Down Expand Up @@ -128,21 +126,19 @@ function getGitLabParam() {
const scm = 'gitlab';
const username = $('#gitlab-email').val();
const password = $('#gitlab-password').val();
const personalToken = $('#gitlab-accesstoken').val();
const tokenType = (personalToken && personalToken.length > 0) ? 'personalToken' : 'oAuth';
const token = $('#gitlab-accesstoken').val();
const tokenType = (token && token.length > 0) ? 'personalToken' : 'oAuth';
const baseUrl = ($('#gitlab-url').val() || 'https://gitlab.com') + '/api/v4';
//const baseUrl = 'https://gitlab.com/api/v4';
return {
scm,
username,
password,
tokenType,
personalToken,
token,
baseUrl
}
}


function addCred(param) {
if (param.username === '') {
return;
Expand All @@ -152,21 +148,24 @@ function addCred(param) {
}

if (param.scm === 'bitbucket') return loginBitbucket(param);
if (param.scm === 'gitlab' && param.tokenType ==='oAuth') return loginGitLabOauth(param);
if (param.scm === 'gitlab' && param.tokenType ==='personalToken') return loginGitLabToken(param);
if (param.password !== '' && param.scm === 'github') return loginGithub(param);

addStar(param.token)
.then(() => {
chrome.storage.sync.set({scm: param.scm, user: param.username, token: param.token, baseUrl: param.baseUrl}, () => {
location.reload();
});
chrome.storage.local.get('tab', (item) => {
if(item.tab) {
chrome.tabs.reload(item.tab);
}
});
})
if (param.scm === 'gitlab') {
if (param.token !== '') return loginGitLabToken(param);
return loginGitLabOauth(param);
}
if (param.scm === 'github') {
if (param.password !== '') return loginGithub(param);
addStar(param.token)
.then(() => {
chrome.storage.sync.set({scm: param.scm, user: param.username, token: param.token, baseUrl: param.baseUrl}, () => {
location.reload();
});
chrome.storage.local.get('tab', (item) => {
if(item.tab) {
chrome.tabs.reload(item.tab);
}
});
})
}
}

function loginGithub(param) {
Expand Down Expand Up @@ -284,60 +283,47 @@ function loginGitLabOauth(param) {
password: password
}
})
.done(response => {
return $.getJSON(
`${baseUrl}/user`,
{ access_token: response.access_token }
)
.done(user => {
chrome.storage.sync.set({scm: param.scm, user: user.username, token: {type : 'oAuth', token :response.access_token}, baseUrl: baseUrl}, () => {
location.reload();
});
chrome.storage.local.get('tab', (item) => {
if(item.tab) {
chrome.tabs.reload(item.tab);
}
});
});
})
.fail(err => {
if (err.status == 401 &&
err.getResponseHeader('X-GitLab-OTP') !== null &&
$('.login-item-otp').filter(':visible').length == 0) {
$('.login-item').animate({height: 'toggle', opacity: 'toggle'}, 'slow');
} else {
$('.error').show();
}
})
.done(response => {
return $.getJSON(
`${baseUrl}/user`,
{ access_token: response.access_token }
)
.done(user => {
chrome.storage.sync.set({scm: param.scm, user: user.username, token: {type : 'oAuth', token :response.access_token}, baseUrl: baseUrl}, () => {
location.reload();
});
chrome.storage.local.get('tab', (item) => {
if(item.tab) {
chrome.tabs.reload(item.tab);
}
});
});
})
.fail(err => {
$('.error').show();
})
}

function loginGitLabToken(param) {
const personalToken = param.personalToken;
const personalToken = param.token;
const baseUrl = param.baseUrl;
const headers = {};
$.getJSON(
`${baseUrl}/user`,
{ private_token: personalToken }
)
.done(user => {
chrome.storage.sync.set({scm: param.scm, user: user.username, token: {type : 'personalToken', token :personalToken}, baseUrl: baseUrl}, () => {
location.reload();
});
chrome.storage.local.get('tab', (item) => {
if(item.tab) {
chrome.tabs.reload(item.tab);
}
});
})
.fail(err => {
if (err.status == 401 &&
err.getResponseHeader('X-GitLab-OTP') !== null &&
$('.login-item-otp').filter(':visible').length == 0) {
$('.login-item').animate({height: 'toggle', opacity: 'toggle'}, 'slow');
} else {
$('.error').show();
$.getJSON(
`${baseUrl}/user`,
{ private_token: personalToken }
)
.done(user => {
chrome.storage.sync.set({scm: param.scm, user: user.username, token: {type : 'personalToken', token :personalToken}, baseUrl: baseUrl}, () => {
location.reload();
});
chrome.storage.local.get('tab', (item) => {
if(item.tab) {
chrome.tabs.reload(item.tab);
}
})
});
})
.fail(err => {
$('.error').show();
})
}

function logout() {
Expand Down
1 change: 0 additions & 1 deletion src/gas-hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ $(() => {
.then(updateGist)
.then(initPageEvent)
.catch((err) => {
debugger;
switch (err.message) {
case 'need login' :
initLoginContent();
Expand Down
Loading

0 comments on commit 877268b

Please sign in to comment.