Skip to content

Commit

Permalink
fix(alert-cli): change diff by replacing github-api by a local di…
Browse files Browse the repository at this point in the history
…ff (#972)
  • Loading branch information
maxgfr authored Jul 11, 2023
1 parent 2507ea9 commit ab1c0cd
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 130 deletions.
3 changes: 3 additions & 0 deletions targets/alert-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
"@socialgouv/cdtn-slugify": "4.52.1",
"@socialgouv/cdtn-sources": "4.52.1",
"@socialgouv/dila-api-client": "1.2.4",
"diff": "^5.1.0",
"memoizee": "0.4.15",
"p-map": "4",
"semver": "7.3.5",
"simple-git": "^3.19.1",
"unist-util-parents": "1.0.3",
"unist-util-select": "4.0.1"
},
Expand All @@ -27,6 +29,7 @@
"@socialgouv/fiches-travail-data-types": "4.191.0",
"@socialgouv/kali-data-types": "2.127.0",
"@socialgouv/legi-data-types": "2.73.1",
"@types/diff": "^5.0.3",
"@types/jest": "27.4.0",
"@types/memoizee": "0.4.6",
"@types/node": "16.11.11",
Expand Down
100 changes: 0 additions & 100 deletions targets/alert-cli/src/APIs/__tests__/api.diff.test.ts

This file was deleted.

31 changes: 2 additions & 29 deletions targets/alert-cli/src/APIs/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Commit, GitTagData } from "../types";
import { Diff } from "../diff/type";
import { getDiff } from "../diff/get-diff";

export type Tags = GitTagData[];

Expand All @@ -17,10 +18,6 @@ export interface GithubDiffFile {
status: "added" | "modified" | "removed";
}

interface GithubResponse {
files?: GithubDiffFile[];
}

interface CommitGithubResponse {
commit: {
author: {
Expand Down Expand Up @@ -72,38 +69,14 @@ export class GithubApi {
}

async diff(project: string, from: GitTagData, to: GitTagData): Promise<Diff> {
let page = 1;
let allDiffs: GithubDiffFile[] = [];
let diffs: GithubDiffFile[] = [];
do {
diffs = await this._diff(project, from.ref, to.ref, page);
allDiffs = allDiffs.concat(diffs);
page = page + 1;
} while (diffs.length > 0);
const allDiffs: GithubDiffFile[] = await getDiff(project, from.ref, to.ref);
return {
from,
to,
files: allDiffs,
};
}

private async _diff(
project: string,
from: string,
to: string,
page = 1,
limit = 100
): Promise<GithubDiffFile[]> {
const url = `https://api.github.com/repos/${project}/compare/${from}...${to}?per_page=${limit}&page=${page}`;
const diffs = await this.fetchJson<GithubResponse>(url);
return (
diffs.files?.map((diff) => ({
filename: diff.filename,
status: diff.status,
})) ?? []
);
}

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);
Expand Down
11 changes: 11 additions & 0 deletions targets/alert-cli/src/diff/__tests__/get-diff.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { getDiff } from "../get-diff";

describe("getDiff", () => {
it("should return an empty array if the project does not exist", async () => {
const project = "does-not-exist-blabla";
const fromTag = "v1.0.0";
const toTag = "v2.0.0";
const diff = await getDiff(project, fromTag, toTag);
expect(diff).toEqual([]);
});
});
46 changes: 46 additions & 0 deletions targets/alert-cli/src/diff/get-diff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { GithubDiffFile } from "../APIs/api";
import { simpleGit } from "simple-git";
import { parsePatch } from "diff";
import fs from "fs";

export async function getDiff(
project: string,
fromTag: string,
toTag: string
): Promise<GithubDiffFile[]> {
const repoPath = `/tmp/${project}`;
try {
if (fs.existsSync(repoPath)) {
// remove the repo if it already exists
fs.rmdirSync(repoPath, { recursive: true });
}
await simpleGit().clone(`https://github.com/${project}`);
const diffString = await simpleGit(repoPath).diff([
`${fromTag}...${toTag}`,
]);
const diffDetail = parsePatch(diffString);
const result: GithubDiffFile[] = [];
diffDetail.forEach((file) => {
if (file.newFileName && !file.oldFileName) {
result.push({
filename: file.newFileName,
status: "added",
});
} else if (file.newFileName && file.oldFileName) {
result.push({
filename: file.newFileName,
status: "modified",
});
} else if (!file.newFileName && file.oldFileName) {
result.push({
filename: file.oldFileName,
status: "removed",
});
}
});
return result;
} catch (error) {
console.error(error);
return [];
}
}
28 changes: 27 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2135,6 +2135,18 @@
methods "^1.1.2"
path-to-regexp "^6.1.0"

"@kwsites/file-exists@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@kwsites/file-exists/-/file-exists-1.1.1.tgz#ad1efcac13e1987d8dbaf235ef3be5b0d96faa99"
integrity sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==
dependencies:
debug "^4.1.1"

"@kwsites/promise-deferred@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@kwsites/promise-deferred/-/promise-deferred-1.1.1.tgz#8ace5259254426ccef57f3175bc64ed7095ed919"
integrity sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==

"@lerna/[email protected]":
version "5.6.2"
resolved "https://registry.yarnpkg.com/@lerna/add/-/add-5.6.2.tgz#d0e25fd4900b6f8a9548f940cc016ce8a3e2d2ba"
Expand Down Expand Up @@ -4661,6 +4673,11 @@
dependencies:
"@types/node" "*"

"@types/diff@^5.0.3":
version "5.0.3"
resolved "https://registry.yarnpkg.com/@types/diff/-/diff-5.0.3.tgz#1f89e49ff83b5d200d78964fb896c68498ce1828"
integrity sha512-amrLbRqTU9bXMCc6uX0sWpxsQzRIo9z6MJPkH1pkez/qOxuqSZVuryJAWoBRq94CeG8JxY+VK4Le9HtjQR5T9A==

"@types/express-serve-static-core@^4.17.33":
version "4.17.35"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.35.tgz#c95dd4424f0d32e525d23812aa8ab8e4d3906c4f"
Expand Down Expand Up @@ -8115,7 +8132,7 @@ diff@^4.0.1:
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==

diff@^5.0.0:
diff@^5.0.0, diff@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-5.1.0.tgz#bc52d298c5ea8df9194800224445ed43ffc87e40"
integrity sha512-D+mk+qE8VC/PAUrlAU34N+VfXev0ghe5ywmpqrawphmVZc1bEfn56uo9qpyGp1p4xpzOHkSW4ztBd6L7Xx4ACw==
Expand Down Expand Up @@ -16374,6 +16391,15 @@ signal-exit@^4.0.1:
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-4.0.2.tgz#ff55bb1d9ff2114c13b400688fa544ac63c36967"
integrity sha512-MY2/qGx4enyjprQnFaZsHib3Yadh3IXyV2C321GY0pjGfVBu4un0uDJkwgdxqO+Rdx8JMT8IfJIRwbYVz3Ob3Q==

simple-git@^3.19.1:
version "3.19.1"
resolved "https://registry.yarnpkg.com/simple-git/-/simple-git-3.19.1.tgz#ff9c021961a3d876a1b115b1893bed9a28855d30"
integrity sha512-Ck+rcjVaE1HotraRAS8u/+xgTvToTuoMkT9/l9lvuP5jftwnYUp6DwuJzsKErHgfyRk8IB8pqGHWEbM3tLgV1w==
dependencies:
"@kwsites/file-exists" "^1.1.1"
"@kwsites/promise-deferred" "^1.1.1"
debug "^4.3.4"

simple-oauth2@^4.2.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/simple-oauth2/-/simple-oauth2-4.3.0.tgz#8b8c28a7f7145a77a2a03086b1adb1e9ba5abc98"
Expand Down

0 comments on commit ab1c0cd

Please sign in to comment.