Skip to content

Commit 8c9bbba

Browse files
committed
feat: add updateChanges function to automate GitHub repository updates
1 parent 898ffd7 commit 8c9bbba

File tree

5 files changed

+89
-12
lines changed

5 files changed

+89
-12
lines changed

dist/index.js

Lines changed: 38 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import { cwd, chdir } from "process";
88
// import { coverage } from './scripts/coverage'
99
import minimist from "minimist";
1010
import { execSync } from "child_process";
11-
import { checkModifiedFiles } from "./scripts/post";
11+
import { checkModifiedFiles, updateChanges } from "./scripts/post";
1212

13-
export type Command = { label: string; command: string };
13+
export type Command = {
14+
label: string;
15+
command: string;
16+
commandList?: string[];
17+
};
1418

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

202+
const updateChangesStr: StepResponse | undefined = await updateChanges({
203+
label: "Update changes in GitHub repository",
204+
command: "",
205+
commandList: [
206+
'git config --global user.name "github-actions"',
207+
'git config --global user.email "github-actions@github.com"',
208+
"git add -A",
209+
'git commit -m "[automated commit] lint format and import sort"',
210+
"git push",
211+
],
212+
});
213+
198214
// runCoverage
199215
// const coverageStr: StepResponse | undefined = runCoverage
200216
// ? await coverage()
@@ -213,6 +229,8 @@ export async function run(): Promise<void> {
213229
playwrightStr,
214230
testingStr,
215231
tsDocStr,
232+
checkModifiedFilesStr,
233+
updateChangesStr,
216234
);
217235
}
218236
} catch (error) {

src/scripts/comment.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { Context } from "@actions/github/lib/context";
44
import { StepResponse } from "src/main";
55

66
const li = (str: string): string => {
7-
return `<li>${str}</li>`;
7+
return `
8+
<li>
9+
${str}
10+
</li>
11+
`;
812
};
913

1014
export const comment = async (
@@ -18,6 +22,8 @@ export const comment = async (
1822
playwrightStr: StepResponse | undefined,
1923
testingStr: StepResponse | undefined,
2024
tsDocStr: StepResponse | undefined,
25+
checkModifiedFilesStr: StepResponse | undefined,
26+
updateChangesStr: StepResponse | undefined,
2127
): Promise<StepResponse> => {
2228
try {
2329
const commentBody = `
@@ -31,6 +37,8 @@ export const comment = async (
3137
${playwrightStr !== undefined ? li(playwrightStr.output) : ""}
3238
${testingStr !== undefined ? li(testingStr.output) : ""}
3339
${tsDocStr !== undefined ? li(tsDocStr.output) : ""}
40+
${checkModifiedFilesStr !== undefined ? li(checkModifiedFilesStr.output) : ""}
41+
${updateChangesStr !== undefined ? li(updateChangesStr.output) : ""}
3442
</ul>`;
3543
// ## Coverage = ${coverageStr?.output}\n`
3644

src/scripts/post.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,29 @@ export const checkModifiedFiles = async (
99
const response = { output: "", error: false };
1010
return await buildComment(response, str, command.label);
1111
})
12-
.catch((error) => {
12+
.catch(async (error) => {
1313
setFailed(`Failed to check for modified files: ${error as string}`);
14-
return { output: error.message as string, error: true };
14+
const response = { output: "", error: true };
15+
return await buildComment(response, error.message, command.label);
1516
});
1617

1718
return result;
1819
};
20+
21+
export const updateChanges = async (
22+
command: Command,
23+
): Promise<StepResponse> => {
24+
let response: StepResponse = { output: "", error: false };
25+
26+
if (process.env.MODIFIED === "true") {
27+
for (const cmd of command.commandList as string[]) {
28+
await runBashCommand(cmd).catch(async (error) => {
29+
setFailed(`Failed to execute command "${cmd}": ${error as string}`);
30+
response.error = true;
31+
response = await buildComment(response, error.message, command.label);
32+
});
33+
}
34+
}
35+
36+
return response;
37+
};

0 commit comments

Comments
 (0)