Skip to content

Commit

Permalink
fix: on new file, patch return old file with value /dev/null (#983)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-maillot authored Jul 31, 2023
1 parent d20d0a0 commit e47f62f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
17 changes: 5 additions & 12 deletions targets/alert-cli/src/APIs/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class GithubApi {

async raw(project: string, path: string, tag: GitTagData): Promise<string> {
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;
}

Expand All @@ -103,19 +103,12 @@ export class GithubApi {
return data as T;
}

private async fetchText(url: string, ignoreError?: boolean): Promise<string> {
private async fetchText(url: string): Promise<string> {
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();
}
Expand Down
21 changes: 11 additions & 10 deletions targets/alert-cli/src/diff/get-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,32 @@ 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,
});
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",
});
}
});
Expand Down

0 comments on commit e47f62f

Please sign in to comment.