From e47f62f70481195fb969a0e5645ea3804df6c9ff Mon Sep 17 00:00:00 2001 From: Martial Maillot Date: Mon, 31 Jul 2023 16:12:44 +0200 Subject: [PATCH] fix: on new file, patch return old file with value /dev/null (#983) --- targets/alert-cli/src/APIs/api.ts | 17 +++++------------ targets/alert-cli/src/diff/get-diff.ts | 21 +++++++++++---------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/targets/alert-cli/src/APIs/api.ts b/targets/alert-cli/src/APIs/api.ts index 0106a9133..7c7c0dd55 100644 --- a/targets/alert-cli/src/APIs/api.ts +++ b/targets/alert-cli/src/APIs/api.ts @@ -79,7 +79,7 @@ export class GithubApi { async raw(project: string, path: string, tag: GitTagData): Promise { const url = `https://raw.githubusercontent.com/${project}/${tag.ref}/${path}`; - const data = await this.fetchText(url, true); + const data = await this.fetchText(url); return data; } @@ -103,19 +103,12 @@ export class GithubApi { return data as T; } - private async fetchText(url: string, ignoreError?: boolean): Promise { + private async fetchText(url: string): Promise { const response = await this.fetchGithub(url); if (!response.ok) { - if (ignoreError) { - console.error( - `Failed to fetch ${url} - ${response.status} : ${response.statusText}` - ); - return ""; - } else { - throw new Error( - `Failed to fetch ${url} - ${response.status} : ${response.statusText}` - ); - } + throw new Error( + `Failed to fetch ${url} - ${response.status} : ${response.statusText}` + ); } return response.text(); } diff --git a/targets/alert-cli/src/diff/get-diff.ts b/targets/alert-cli/src/diff/get-diff.ts index bc7e1baaa..e0a22cc2a 100644 --- a/targets/alert-cli/src/diff/get-diff.ts +++ b/targets/alert-cli/src/diff/get-diff.ts @@ -12,7 +12,7 @@ export async function getDiff( try { if (fs.existsSync(repoPath)) { // remove the repo if it already exists - fs.rmdirSync(repoPath, { recursive: true }); + fs.rmSync(repoPath, { recursive: true }); } await simpleGit().clone(`https://github.com/${project}`, repoPath, { "--depth": 50, @@ -20,23 +20,24 @@ export async function getDiff( const diffString = await simpleGit(repoPath).diff([ `${fromTag}...${toTag}`, ]); + const diffDetail = parsePatch(diffString); const result: GithubDiffFile[] = []; - diffDetail.forEach((file) => { - if (file.newFileName && !file.oldFileName) { + diffDetail.forEach(({ oldFileName, newFileName }) => { + if (oldFileName === "/dev/null" && newFileName) { result.push({ - filename: formatFileName(file.newFileName), + filename: formatFileName(newFileName), status: "added", }); - } else if (file.newFileName && file.oldFileName) { + } else if (newFileName === "/dev/null" && oldFileName) { result.push({ - filename: formatFileName(file.newFileName), - status: "modified", + filename: formatFileName(oldFileName), + status: "removed", }); - } else if (!file.newFileName && file.oldFileName) { + } else if (newFileName && oldFileName) { result.push({ - filename: formatFileName(file.oldFileName), - status: "removed", + filename: formatFileName(newFileName), + status: "modified", }); } });