Skip to content

Commit

Permalink
feat: add updateChanges function to automate GitHub repository updates
Browse files Browse the repository at this point in the history
  • Loading branch information
DE7924 committed Dec 20, 2024
1 parent 898ffd7 commit 8c9bbba
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 12 deletions.
44 changes: 38 additions & 6 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import { cwd, chdir } from "process";
// import { coverage } from './scripts/coverage'
import minimist from "minimist";
import { execSync } from "child_process";
import { checkModifiedFiles } from "./scripts/post";
import { checkModifiedFiles, updateChanges } from "./scripts/post";

export type Command = { label: string; command: string };
export type Command = {
label: string;
command: string;
commandList?: string[];
};

export type StepResponse = { output: string; error: boolean };
export const failedEmoji = "❌";
Expand Down Expand Up @@ -195,6 +199,18 @@ export async function run(): Promise<void> {
'echo "modified=$(if [ -n "$(git status --porcelain)" ]; then echo "true"; else echo "false"; fi)" >> $GITHUB_ENV',
});

const updateChangesStr: StepResponse | undefined = await updateChanges({
label: "Update changes in GitHub repository",
command: "",
commandList: [
'git config --global user.name "github-actions"',
'git config --global user.email "[email protected]"',
"git add -A",
'git commit -m "[automated commit] lint format and import sort"',
"git push",
],
});

// runCoverage
// const coverageStr: StepResponse | undefined = runCoverage
// ? await coverage()
Expand All @@ -213,6 +229,8 @@ export async function run(): Promise<void> {
playwrightStr,
testingStr,
tsDocStr,
checkModifiedFilesStr,
updateChangesStr,
);
}
} catch (error) {
Expand Down
10 changes: 9 additions & 1 deletion src/scripts/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { Context } from "@actions/github/lib/context";
import { StepResponse } from "src/main";

const li = (str: string): string => {
return `<li>${str}</li>`;
return `
<li>
${str}
</li>
`;
};

export const comment = async (
Expand All @@ -18,6 +22,8 @@ export const comment = async (
playwrightStr: StepResponse | undefined,
testingStr: StepResponse | undefined,
tsDocStr: StepResponse | undefined,
checkModifiedFilesStr: StepResponse | undefined,
updateChangesStr: StepResponse | undefined,
): Promise<StepResponse> => {
try {
const commentBody = `
Expand All @@ -31,6 +37,8 @@ export const comment = async (
${playwrightStr !== undefined ? li(playwrightStr.output) : ""}
${testingStr !== undefined ? li(testingStr.output) : ""}
${tsDocStr !== undefined ? li(tsDocStr.output) : ""}
${checkModifiedFilesStr !== undefined ? li(checkModifiedFilesStr.output) : ""}
${updateChangesStr !== undefined ? li(updateChangesStr.output) : ""}
</ul>`;
// ## Coverage = ${coverageStr?.output}\n`

Expand Down
23 changes: 21 additions & 2 deletions src/scripts/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,29 @@ export const checkModifiedFiles = async (
const response = { output: "", error: false };
return await buildComment(response, str, command.label);
})
.catch((error) => {
.catch(async (error) => {
setFailed(`Failed to check for modified files: ${error as string}`);
return { output: error.message as string, error: true };
const response = { output: "", error: true };
return await buildComment(response, error.message, command.label);
});

return result;
};

export const updateChanges = async (
command: Command,
): Promise<StepResponse> => {
let response: StepResponse = { output: "", error: false };

if (process.env.MODIFIED === "true") {
for (const cmd of command.commandList as string[]) {
await runBashCommand(cmd).catch(async (error) => {
setFailed(`Failed to execute command "${cmd}": ${error as string}`);
response.error = true;
response = await buildComment(response, error.message, command.label);
});
}
}

return response;
};

0 comments on commit 8c9bbba

Please sign in to comment.