From 3467a6ee0911b4fc668b1f47c3e10b835192bf4b Mon Sep 17 00:00:00 2001 From: "xiaoqi.cxq" Date: Wed, 20 Dec 2023 11:21:01 +0800 Subject: [PATCH] =?UTF-8?q?github=E4=BD=9C=E4=B8=BA=E4=B8=BB=E7=A9=BA?= =?UTF-8?q?=E9=97=B4=E6=97=B6=E7=9A=84bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../providers/helpers/githubHelper.js | 64 +++++++++++++------ 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/src/services/providers/helpers/githubHelper.js b/src/services/providers/helpers/githubHelper.js index 65e0f7e2..f186870b 100644 --- a/src/services/providers/helpers/githubHelper.js +++ b/src/services/providers/helpers/githubHelper.js @@ -169,7 +169,7 @@ export default { if (err.status === 404) { await request(token, { method: 'POST', - url: 'https://api.github.com/repos/mafgwo/stackeditplus-appdata-template/generate', + url: 'https://api.github.com/repos/mafgwo/stackedit-appdata-template/generate', body: { owner: token.name, name: appDataRepo, @@ -193,11 +193,29 @@ export default { repo, sha, path, + tryTimes, }) { - return repoRequest(token, owner, repo, { - url: 'commits', - params: { sha, path }, - }); + let tryCount = tryTimes || 1; + try { + return repoRequest(token, owner, repo, { + url: 'commits', + params: { sha, path }, + }); + } catch (err) { + // 主文档 并且 409 则重试3次 + if (tryCount <= 3 && err.status === 409 && repo === appDataRepo) { + tryCount += 1; + return this.getCommits({ + token, + owner, + repo, + sha, + path, + tryTimes: tryCount, + }); + } + throw err; + } }, /** @@ -270,22 +288,30 @@ export default { path, isImg, }) { - const { sha, content, encoding } = await repoRequest(token, owner, repo, { - url: `contents/${encodeURIComponent(path)}`, - params: { ref: branch }, - }); - let tempContent = content; - // 如果是图片且 encoding 为 none 则 需要获取 blob - if (isImg && encoding === 'none') { - const blobInfo = await repoRequest(token, owner, repo, { - url: `git/blobs/${sha}`, + try { + const { sha, content, encoding } = await repoRequest(token, owner, repo, { + url: `contents/${encodeURIComponent(path)}`, + params: { ref: branch }, }); - tempContent = blobInfo.content; + let tempContent = content; + // 如果是图片且 encoding 为 none 则 需要获取 blob + if (isImg && encoding === 'none') { + const blobInfo = await repoRequest(token, owner, repo, { + url: `git/blobs/${sha}`, + }); + tempContent = blobInfo.content; + } + return { + sha, + data: !isImg ? utils.decodeBase64(tempContent) : tempContent, + }; + } catch (err) { + // not .stackedit-data throw err + if (err.status === 404 && path.indexOf('.stackedit-data') >= 0) { + return {}; + } + throw err; } - return { - sha, - data: !isImg ? utils.decodeBase64(tempContent) : tempContent, - }; }, /** * 获取仓库信息