handle invalid sha

This commit is contained in:
tdurieux
2021-04-22 08:49:26 +02:00
parent 7c88392aba
commit aaf7e49bb4
+15 -6
View File
@@ -76,8 +76,13 @@ function tree2tree(tree, partialTree, parentPath) {
async function getTruncatedTree(repoConfig, truncatedTree, sha, parentPath) { async function getTruncatedTree(repoConfig, truncatedTree, sha, parentPath) {
const repo = gh(repoConfig.fullName); const repo = gh(repoConfig.fullName);
if (!sha) { if (!sha || !/^[a-f0-9]+$/.test(sha)) {
sha = repoConfig.commit ? repoConfig.commit : "HEAD"; if (repoConfig.commit && /^[a-f0-9]+$/.test(repoConfig.commit)) {
sha = repoConfig.commit;
} else {
sha = "HEAD";
}
repoConfig.commit = sha;
} }
const octokit = new Octokit({ const octokit = new Octokit({
@@ -119,13 +124,14 @@ async function getTruncatedTree(repoConfig, truncatedTree, sha, parentPath) {
module.exports.getTree = async (repoConfig, sha, truncatedTree, parentPath) => { module.exports.getTree = async (repoConfig, sha, truncatedTree, parentPath) => {
const repo = gh(repoConfig.fullName); const repo = gh(repoConfig.fullName);
if (!sha) { if (!sha || !/^[a-f0-9]+$/.test(sha)) {
if (repoConfig.commit && !/^[a-f0-9]+$/.test(repoConfig.commit)) { if (repoConfig.commit && /^[a-f0-9]+$/.test(repoConfig.commit)) {
sha = repoConfig.commit sha = repoConfig.commit;
} else { } else {
sha = "HEAD" sha = "HEAD";
} }
} }
if (!parentPath) parentPath = ""; if (!parentPath) parentPath = "";
const octokit = new Octokit({ const octokit = new Octokit({
@@ -137,6 +143,8 @@ module.exports.getTree = async (repoConfig, sha, truncatedTree, parentPath) => {
tree_sha: sha, tree_sha: sha,
recursive: true, recursive: true,
}); });
sha = ghRes.data.sha;
repoConfig.commit = sha;
const tree = tree2tree(ghRes.data.tree, truncatedTree, parentPath); const tree = tree2tree(ghRes.data.tree, truncatedTree, parentPath);
if (ghRes.data.truncated) { if (ghRes.data.truncated) {
@@ -172,6 +180,7 @@ module.exports.getFileList = async (options) => {
{ repoId: repoConfig.repoId }, { repoId: repoConfig.repoId },
{ {
$set: { $set: {
commit: repoConfig.commit,
originalFiles: tree.child, originalFiles: tree.child,
files, files,
}, },