From c0b889de942411e3a53c4f3be8d2b855a8d0621a Mon Sep 17 00:00:00 2001 From: Ke Xu Date: Mon, 9 Apr 2018 17:28:38 +0900 Subject: [PATCH] support gitlab --- options/options.js | 132 ++++++++++++++++++++------------------------ src/gas-hub.js | 1 - src/scm/gitlab.js | 133 +++++++++++++++++++++++---------------------- src/util.js | 4 +- 4 files changed, 128 insertions(+), 142 deletions(-) diff --git a/options/options.js b/options/options.js index 8717839..f5976a8 100644 --- a/options/options.js +++ b/options/options.js @@ -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}`; @@ -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 }; @@ -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; @@ -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) { @@ -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() { diff --git a/src/gas-hub.js b/src/gas-hub.js index 9345f03..b4f4eb1 100644 --- a/src/gas-hub.js +++ b/src/gas-hub.js @@ -19,7 +19,6 @@ $(() => { .then(updateGist) .then(initPageEvent) .catch((err) => { - debugger; switch (err.message) { case 'need login' : initLoginContent(); diff --git a/src/scm/gitlab.js b/src/scm/gitlab.js index 4f38abe..a549490 100644 --- a/src/scm/gitlab.js +++ b/src/scm/gitlab.js @@ -31,12 +31,9 @@ class Gitlab { return false; } - commitFiles(repo, branch, parent, newFiles, changedFiles, deleteFiles, comment) { return new Promise((resolve, reject) => { - let data = {branch: branch, - commit_message: comment, - actions: []}; + const data = {branch: branch, commit_message: comment, actions: []}; if (newFiles && newFiles.length > 0) { data.actions = data.actions.concat(newFiles.map((file) => { return { @@ -70,15 +67,15 @@ class Gitlab { traditional: true, data: JSON.stringify(data) }) - .then(resolve) - .fail(reject); + .then(resolve) + .fail(reject); }); } push(code) { const changed = $('.diff-file:checked').toArray().map(elem => elem.value); const changedFiles = changed.filter(f => code.gas[f]).map(f => { - return {name: f.replace(/\.gs$/, context.config.filetype), content: code.gas[f]} + return {name: f, content: code.gas[f]} }); const deleteFiles = changed.filter(f => !code.gas[f]); const newFileNames = changed.filter(f => !code.scm[f]); @@ -116,25 +113,25 @@ class Gitlab { return $.getJSON( `${this.baseUrl}/projects/${context.repo.id}/repository/tree?ref=${context.branch}&recursive=true&${this.tokenParam}`, {} ) - .then(resolve) - .fail(reject) + .then(resolve) + .fail(reject) }) - .then(response => { - const re = new RegExp(`(\\${context.config.filetype}|\\.html${context.config.manifestEnabled ? '|^appsscript.json' : ''})$`); - const promises = response.filter((tree) => { - return tree.type === 'blob' && re.test(tree.path); + .then(response => { + const re = new RegExp(`(\\${context.config.filetype}|\\.html${context.config.manifestEnabled ? '|^appsscript.json' : ''})$`); + const promises = response.filter((tree) => { + return tree.type === 'blob' && re.test(tree.path); + }) + .map(tree => { + return new Promise((resolve, reject) => { + $.getJSON(`${this.baseUrl}/projects/${context.repo.id}/repository/files/${encodeURIComponent(tree.path)}?ref=${context.branch}&${this.tokenParam}`, {}) + .then((content) => { + resolve({file: tree.path, content: decodeURIComponent(escape(atob(content.content)))}); }) - .map(tree => { - return new Promise((resolve, reject) => { - $.getJSON(`${this.baseUrl}/projects/${context.repo.id}/repository/files/${encodeURIComponent(tree.path)}?ref=${context.branch}&${this.tokenParam}`, {}) - .then((content) => { - resolve({file: tree.path, content: decodeURIComponent(escape(atob(content.content)))}); - }) - .fail(reject) - }); - }); - return Promise.all(promises); + .fail(reject) }); + }); + return Promise.all(promises); + }); } getNamespaces() { @@ -147,13 +144,14 @@ class Gitlab { this.followPaginate, 'gitlab' ) - .then(groups => { - this.namespaces = [this.user].concat(groups.map(group => group.name)); - return this.namespaces; - }) - .catch((err) => { - showAlert('Failed to get user info.', LEVEL_ERROR); - }); + .then(groups => { + this.namespaces = [this.user].concat(groups.map(group => group.name)); + this.namesToIds.groups = groups.reduce((obj, item) => (obj[item.name] = item.id, obj), {}); + return this.namespaces; + }) + .catch((err) => { + showAlert('Failed to get user info.', LEVEL_ERROR); + }); } getRepos() { // Named Projects in gitlab @@ -161,21 +159,21 @@ class Gitlab { { tokenParam: this.tokenParam, items: [], - url: `${this.baseUrl}/users/${this.user}/projects?per_page=25` + url: `${this.baseUrl}/projects?per_page=25&membership=true` }), this.followPaginate, 'gitlab' ) - .then(response => { - this.namesToIds.repos = response.reduce((obj, item) => (obj[item.name] = item.id, obj), {}); - const repos = Object.keys(this.namesToIds.repos); - //if current bind still existed, use it - const repo = context.bindRepo[context.id]; - if (repo && $.inArray(repo.fullName, repos) >= 0) { - context.repo = repo; - } - return repos; - }); + .then(response => { + this.namesToIds.repos = response.reduce((obj, item) => (obj[item.path_with_namespace] = item.id, obj), {}); + const repos = Object.keys(this.namesToIds.repos); + //if current bind still existed, use it + const repo = context.bindRepo[context.id]; + if (repo && $.inArray(repo.fullName, repos) >= 0) { + context.repo = repo; + } + return repos; + }); } createRepo() { @@ -184,10 +182,13 @@ class Gitlab { const desc = $('#new-repo-desc').val(); const visibility = ($('#new-repo-type').val() !== 'public') ? 'private' : 'public'; const payload = { - name: name, + path: name, description: desc, visibility: visibility }; + if (this.namesToIds.groups[owner]) { + payload.namespace_id = this.namesToIds.groups[owner]; + } if (!name || name === '') return; return new Promise((resolve, reject) => { return $.ajax({ @@ -199,31 +200,31 @@ class Gitlab { contentType: 'application/json', data: JSON.stringify(payload) }) - .then(resolve) - .fail(reject); + .then(resolve) + .fail(reject); }) - .then(response => { - const repo = { - fullName: response.name, - id: response.id - }; - context.repo = repo; - Object.assign(context.bindRepo, {[context.id]: repo}); - if (context.bindBranch[context.id]) { - delete context.bindBranch[context.id]; - } - chrome.storage.sync.set({bindRepo: context.bindRepo}); - return response.name; - }) - .then(repo => { - return this.commitFiles(repo, 'master', null, [{name: "README.md", content: "initialed by gas-github"}], null,null, 'initial commit') - .then(() => { - return repo; - }); + .then(response => { + const repo = { + fullName: response.path_with_namespace, + id: response.id + }; + context.repo = repo; + Object.assign(context.bindRepo, {[context.id]: repo}); + if (context.bindBranch[context.id]) { + delete context.bindBranch[context.id]; + } + chrome.storage.sync.set({bindRepo: context.bindRepo}); + return response.path_with_namespace; + }) + .then(repo => { + return this.commitFiles(repo, 'master', null, [{name: "README.md", content: "initialed by gas-github"}], null,null, 'initial commit') + .then(() => { + return repo; + }); }) - .catch((err) => { - throw new Error('Failed to create new repository.'); - }); + .catch((err) => { + throw new Error('Failed to create new repository.'); + }); } createBranch() { @@ -259,7 +260,7 @@ class Gitlab { followPaginate(data) { return new Promise((resolve, reject) => { - $.getJSON(`${data.url}&${data.tokenParm}`) + $.getJSON(`${data.url}&${data.tokenParam}`) .then((response, status, xhr) => { data.items = data.items.concat(response); const link = xhr.getResponseHeader('Link'); diff --git a/src/util.js b/src/util.js index 99d6f82..76b6758 100644 --- a/src/util.js +++ b/src/util.js @@ -13,8 +13,8 @@ function createSCM(item) { return new Github(item.baseUrl, item.user, item.token); case 'bitbucket': return new Bitbucket(item.baseUrl, item.user, item.token); - case 'gitlab': - return new Gitlab(item.baseUrl, item.user, item.token); + case 'gitlab': + return new Gitlab(item.baseUrl, item.user, item.token); default: return new Github(item.baseUrl, item.user, item.token); }