From a013d0b1ee2c70657f4bee5bddeb588bf23503f4 Mon Sep 17 00:00:00 2001 From: Ben Kennedy Date: Wed, 20 Nov 2024 13:10:51 +0000 Subject: [PATCH 1/2] ci: Added changed file list to PR Comment for icons bot --- .github/fetch_icons/action.yml | 6 +++--- .github/fetch_icons/index.js | 14 +++++++++++--- .github/workflows/build.yml | 14 ++++++++------ scripts/utils/checkGit.ts | 34 +++++++++++++++++++++++++++------- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/.github/fetch_icons/action.yml b/.github/fetch_icons/action.yml index 2034b540..2dc57b25 100644 --- a/.github/fetch_icons/action.yml +++ b/.github/fetch_icons/action.yml @@ -10,11 +10,11 @@ inputs: actions-runner-debug: description: "The date and time the action was run" required: false - default: false - type: boolean + default: "false" +# type: boolean outputs: files_changed: - description: "Boolean that gets set to true if any icons have been added or removed" + description: "Array of files if any icons have been added or removed. If this length > 0, the PR should be created." runs: using: "node20" main: "index.js" diff --git a/.github/fetch_icons/index.js b/.github/fetch_icons/index.js index 9258b79f..38ca3b1f 100644 --- a/.github/fetch_icons/index.js +++ b/.github/fetch_icons/index.js @@ -26,21 +26,29 @@ try { "outputs", VERBOSE_LOGS, ); - let filesChanged = false; + let filesChanged = []; if (newHash) { writeFileSync(hashPath, newHash); filesChanged = checkForFileChanges(VERBOSE_LOGS); - if (filesChanged) { + if (filesChanged.length > 0) { const packageJson = JSON.parse(readFileSync("./package.json").toString()); packageJson.lastUpdated = DATE; writeFileSync("./package.json", JSON.stringify(packageJson, null, 2)); stageAllFiles(); } } + const filesChangedOutput = Object.entries(filesChanged.reduce((acc, {type, path}) => { + const descriptiveChangeType = gitChangeTypeToString(type); + if(acc[descriptiveChangeType] == undefined) { + acc.descriptiveChangeType = []; + } + acc.descriptiveChangeType.push(` ${path}`); + return acc; + }, [])).map(([key, value]) => `${key}:\n${value.join("\n")}`).join("\n\n"); console.log("Files changed", filesChanged); - core.setOutput("files_changed", filesChanged); + core.setOutput("files_changed", filesChangedOutput); } catch (error) { core.setFailed(error.message); } diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ab6b4a64..ab90a222 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -52,10 +52,10 @@ jobs: - name: Icons changed run: echo ${{ steps.fetch_icons.outputs.files_changed }} - name: Create code connect files - if: ${{ steps.fetch_icons.outputs.files_changed == 'true'}} + if: ${{ steps.fetch_icons.outputs.files_changed != ''}} uses: ./.github/create_code_connect - name: Create PR title - if: ${{ steps.fetch_icons.outputs.files_changed == 'true'}} + if: ${{ steps.fetch_icons.outputs.files_changed != ''}} id: title run: | if [ ${{github.ref_name=='main'}} ]; then @@ -63,9 +63,11 @@ jobs: else TITLE='chore: Icon updates ${{ steps.date.outputs.date }}(${{github.ref_name}})' fi + BODY='${{ steps.fetch_icons.outputs.files_changed }}' echo "TITLE=$TITLE" >> $GITHUB_OUTPUT + echo "BODY=$BODY" >> $GITHUB_OUTPUT - name: Push - if: ${{ steps.fetch_icons.outputs.files_changed == 'true'}} + if: ${{ steps.fetch_icons.outputs.files_changed != ''}} run: | git checkout -b ${{steps.branch_name.outputs.BRANCH_NAME}} git config --global user.name "zeta-icons-bot" @@ -74,12 +76,12 @@ jobs: git commit -m "${{steps.title.outputs.TITLE}}" git push --set-upstream origin ${{steps.branch_name.outputs.BRANCH_NAME}} -f - name: Check if PR exists - if: ${{ steps.fetch_icons.outputs.files_changed == 'true'}} + if: ${{ steps.fetch_icons.outputs.files_changed != ''}} run: echo "pr_exists=$(gh pr list -H ${{steps.branch_name.outputs.BRANCH_NAME}} --json number -q length)" >> $GITHUB_ENV env: GITHUB_TOKEN: ${{ github.token }} - name: Create Pull Request - if: ${{env.pr_exists == 0 && steps.fetch_icons.outputs.files_changed == 'true'}} - run: gh pr create -B main -H ${{steps.branch_name.outputs.BRANCH_NAME}} --title "${{steps.title.outputs.TITLE}}" --body 'Created by the Zeta Icons bot' + if: ${{env.pr_exists == 0 && steps.fetch_icons.outputs.files_changed != ''}} + run: gh pr create -B main -H ${{steps.branch_name.outputs.BRANCH_NAME}} --title "${{steps.title.outputs.TITLE}}" --body '${{steps.title.outputs.BODY}}
Created by the Zeta Icons bot 🤖' env: GITHUB_TOKEN: ${{ github.token }} diff --git a/scripts/utils/checkGit.ts b/scripts/utils/checkGit.ts index a3f0f6e6..ec3242d6 100644 --- a/scripts/utils/checkGit.ts +++ b/scripts/utils/checkGit.ts @@ -1,16 +1,36 @@ import { execSync } from "child_process"; +// type GitChangeType = "A" | "C" | "D" | "M" | "R" | "T" | "U" | "X" | "B"; +enum GitChangeType { + A = "Added", + C = "Copied", + D = "Deleted", + M = "Modified", + R = "Renamed", + T = "File type changed", + U = "Unmerged", + X = "Unknown", + B = "Broken" +} +export type ChangedFilesDetails = { type: GitChangeType, path: string }; +export const gitChangeTypeToString = (changeType: keyof typeof GitChangeType): string => { + return GitChangeType[changeType]; +}; /** * Gets all files that have changed in the current branch * @param {boolean} verboseLogs - Logs more verbose outputs for testing. - * @returns string[] - List of files that have changed + * @returns { type: GitChangeType, path: string }[] - List of files that have changed with their change type. */ -const getAllChangedFiles = (verboseLogs?: boolean): string[] => { - const diffOutput = execSync(`git diff HEAD --name-only`).toString(); +export const getAllChangedFiles = (verboseLogs?: boolean): ChangedFilesDetails[] => { + const diffOutput = execSync(`git diff HEAD --name-status`).toString(); if (diffOutput != "" && verboseLogs) { console.log("Files changed:", execSync(`git diff HEAD`).toString()); } - return diffOutput.toString().split("\n").filter(Boolean); + return diffOutput.toString().split("\n").map((line) => { + const matches = /^(A|C|D|M|R|T|U|X|B)\s+(.*)$/.exec(line); + if (!matches) return null; + return { type: matches[1] as GitChangeType, path: matches[2] }; + }).filter((change) => change !== null); }; /** @@ -29,9 +49,9 @@ export const stageAllFiles = (verboseLogs?: boolean): void => { * Checks if any files have changed in the current branch. * The check is deliberately off by one to account for `outputs/code-connect.figma.ts` which is not yet regenerated, so will always be changed. * @param {boolean} verboseLogs - Logs more verbose outputs for testing. - * @returns boolean - Whether files have changed and the action should create a PR + * @returns string[] - The list of changed file paths. If files have changed the action should create a PR. */ -export const checkForFileChanges = (verboseLogs?: boolean): boolean => { +export const checkForFileChanges = (verboseLogs?: boolean): ChangedFilesDetails[] => { stageAllFiles(verboseLogs); - return getAllChangedFiles(verboseLogs).length > 1; + return getAllChangedFiles(verboseLogs).filter(({ path }) => path !== "outputs/code-connect.figma.ts"); }; From 516ca3229c940306e3df21e9165b8c6692c2b5a9 Mon Sep 17 00:00:00 2001 From: Ben Kennedy Date: Wed, 20 Nov 2024 13:51:44 +0000 Subject: [PATCH 2/2] attempt to fix weird ts error on GitHub action --- scripts/utils/checkGit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/utils/checkGit.ts b/scripts/utils/checkGit.ts index ec3242d6..1d0b792a 100644 --- a/scripts/utils/checkGit.ts +++ b/scripts/utils/checkGit.ts @@ -30,7 +30,7 @@ export const getAllChangedFiles = (verboseLogs?: boolean): ChangedFilesDetails[] const matches = /^(A|C|D|M|R|T|U|X|B)\s+(.*)$/.exec(line); if (!matches) return null; return { type: matches[1] as GitChangeType, path: matches[2] }; - }).filter((change) => change !== null); + }).filter((change) => change != null); }; /**