From e1eb7ec350f000c0f8a35644ce01d88446bef3b8 Mon Sep 17 00:00:00 2001 From: "xiaoqi.cxq" Date: Thu, 14 Jul 2022 20:48:42 +0800 Subject: [PATCH] =?UTF-8?q?gitee=E6=8E=A5=E5=8F=A3=E9=99=90=E5=88=B6?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E8=A7=A3=E5=86=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/gitee.js | 4 +- src/services/providers/helpers/giteeHelper.js | 52 ++++++++++++------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/server/gitee.js b/server/gitee.js index 4a196ecd..8f5c6fdb 100644 --- a/server/gitee.js +++ b/server/gitee.js @@ -3,13 +3,15 @@ const request = require('request'); const conf = require('./conf'); function giteeToken(clientId, code, oauth2RedirectUri) { + const clientIndex = conf.values.giteeClientId.split(',').indexOf(clientId); + const clientSecret = conf.values.giteeClientSecret.split(',')[clientIndex]; return new Promise((resolve, reject) => { request({ method: 'POST', url: 'https://gitee.com/oauth/token', form: { client_id: clientId, - client_secret: conf.values.giteeClientSecret, + client_secret: clientSecret, code, grant_type: 'authorization_code', redirect_uri: oauth2RedirectUri, diff --git a/src/services/providers/helpers/giteeHelper.js b/src/services/providers/helpers/giteeHelper.js index 0700ed0a..8b173dd0 100644 --- a/src/services/providers/helpers/giteeHelper.js +++ b/src/services/providers/helpers/giteeHelper.js @@ -69,7 +69,8 @@ export default { * https://developer.gitee.com/apps/building-oauth-apps/authorization-options-for-oauth-apps/ */ async startOauth2(lastToken, silent = false, isMain) { - const clientId = store.getters['data/serverConf'].giteeClientId; + const giteeClientIds = store.getters['data/serverConf'].giteeClientId.split(','); + const clientId = giteeClientIds[Math.floor((giteeClientIds.length * Math.random()))]; let tokenBody; if (!silent) { // Get an OAuth2 code @@ -105,13 +106,21 @@ export default { } const accessToken = tokenBody.access_token; // Call the user info endpoint - const user = (await networkSvc.request({ - method: 'GET', - url: 'https://gitee.com/api/v5/user', - params: { - access_token: accessToken, - }, - })).body; + let user = null; + try { + user = (await networkSvc.request({ + method: 'GET', + url: 'https://gitee.com/api/v5/user', + params: { + access_token: accessToken, + }, + })).body; + } catch (err) { + if (err.status === 401) { + this.startOauth2(); + } + throw err; + } if (user.avatar_url && user.avatar_url.endsWith('.png') && !user.avatar_url.endsWith('no_portrait.png')) { user.avatar_url = `${user.avatar_url}!avatar60`; } @@ -194,17 +203,24 @@ export default { repo, branch, }) { - const refreshedToken = await this.refreshToken(token); - const { commit } = await repoRequest(refreshedToken, owner, repo, { - url: `commits/${encodeURIComponent(branch)}`, - }); - const { tree, truncated } = await repoRequest(refreshedToken, owner, repo, { - url: `git/trees/${encodeURIComponent(commit.tree.sha)}?recursive=1`, - }); - if (truncated) { - throw new Error('Git tree too big. Please remove some files in the repository.'); + try { + const refreshedToken = await this.refreshToken(token); + const { commit } = await repoRequest(refreshedToken, owner, repo, { + url: `commits/${encodeURIComponent(branch)}`, + }); + const { tree, truncated } = await repoRequest(refreshedToken, owner, repo, { + url: `git/trees/${encodeURIComponent(commit.tree.sha)}?recursive=1`, + }); + if (truncated) { + throw new Error('Git tree too big. Please remove some files in the repository.'); + } + return tree; + } catch (err) { + if (err.status === 401) { + this.startOauth2(); + } + throw err; } - return tree; }, async checkAndCreateRepo(token) {