Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Added changed file list to PR Comment for icons bot #62

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }}
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) => {
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");
};
Loading