Skip to content

Commit

Permalink
ci: Added changed file list to PR Comment for icons bot
Browse files Browse the repository at this point in the history
  • Loading branch information
benken committed Nov 20, 2024
1 parent ec17713 commit ed9b6a3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 30 deletions.
6 changes: 3 additions & 3 deletions .github/fetch_icons/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
14 changes: 11 additions & 3 deletions .github/fetch_icons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
14 changes: 8 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,22 @@ 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
TITLE='chore: Icon updates ${{ steps.date.outputs.date }}'
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"
Expand All @@ -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}}<br /><sub>Created by the Zeta Icons bot 🤖</sub>'
env:
GITHUB_TOKEN: ${{ github.token }}
20 changes: 11 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@types/chai": "^4.3.16",
"@types/md5": "^2.3.5",
"@types/mocha": "^10.0.6",
"@types/node": "^20.12.7",
"@types/node": "^20.17.6",
"@types/react": "^18.3.11",
"@types/sinon": "^17.0.3",
"chai": "^5.1.0",
Expand Down Expand Up @@ -75,4 +75,4 @@
"url": "https://github.com/zebratechnologies/zeta-icons/issues"
},
"homepage": "https://github.com/zebratechnologies/zeta-icons#readme"
}
}
34 changes: 27 additions & 7 deletions scripts/utils/checkGit.ts
Original file line number Diff line number Diff line change
@@ -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) => {

Check failure on line 29 in scripts/utils/checkGit.ts

View workflow job for this annotation

GitHub Actions / Compile JS Modules

Type '({ type: GitChangeType; path: string; } | null)[]' is not assignable to type '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);
};

/**
Expand All @@ -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");
};

0 comments on commit ed9b6a3

Please sign in to comment.