Skip to content

Commit dd55f44

Browse files
authored
use latest upload-artifact action, apply PR fixes onto PR head (#14)
* remove node_modules * update to latest upload-artifact, apply fixes on PR head
1 parent ea32e3a commit dd55f44

File tree

278 files changed

+2570
-19316
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

278 files changed

+2570
-19316
lines changed

.github/workflows/autofix.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: autofix.ci # needed to securely identify the workflow
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [ "main" ]
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
autofix:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- uses: actions/setup-node@v4
17+
with:
18+
node-version: 20
19+
- run: npm ci
20+
- run: npm run build
21+
22+
- uses: autofix-ci/action@d3e591514b99d0fca6779455ff8338516663f7cc

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See https://github.com/autofix-ci/action/releases.

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ branding:
55
color: 'green'
66
runs:
77
using: 'node20'
8-
main: 'index.js'
8+
main: 'index.dist.js'
99
inputs:
1010
fail-fast:
1111
description: 'Cancel all other workflows associated with a commit when fixing it.'

index.dist.js

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

index.js

Lines changed: 0 additions & 121 deletions
This file was deleted.

index.ts

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import {DefaultArtifactClient} from '@actions/artifact'
2+
import {getInput, isDebug, setFailed, setOutput} from "@actions/core";
3+
import {exec, getExecOutput} from "@actions/exec";
4+
import {HttpClient} from "@actions/http-client";
5+
import {readFile, rm, writeFile} from "fs/promises";
6+
7+
8+
async function main() {
9+
setOutput('autofix_started', false);
10+
11+
const event = JSON.parse(
12+
await readFile(process.env.GITHUB_EVENT_PATH, {encoding: 'utf8'})
13+
);
14+
if (isDebug()) {
15+
console.log(event);
16+
}
17+
18+
if (process.env.GITHUB_WORKFLOW !== "autofix.ci") {
19+
throw `For security reasons, the workflow in which the autofix.ci action is used must be named "autofix.ci".`;
20+
}
21+
22+
await exec("git", ["reset"]);
23+
24+
await exec("git", ["-c", "core.fileMode=false", "add", "--all"]);
25+
26+
// Git consistently uses unix-style paths, so we do not need to worry about path conversions.
27+
let {stdout} = await getExecOutput("git", ["diff", "--name-only", "--staged", "--no-renames"])
28+
if (stdout === "") {
29+
console.log("Nothing to do! ✨");
30+
return;
31+
}
32+
let changes = stdout.trim().split("\n");
33+
// UX: Already check here if we have forbidden files.
34+
// This is truly enforced on the server, but this way we can give a better error message.
35+
if (changes.some((path) => path.includes(".github"))) {
36+
throw "The autofix.ci action is not allowed to modify the .github directory.";
37+
}
38+
console.log(`Need to update ${changes.length} files.`);
39+
40+
// For pull requests, we need to rebase the changes onto the PR head,
41+
// see https://github.com/autofix-ci/action/issues/12.
42+
if (event.pull_request) {
43+
// Create a commit
44+
await exec("git", ["config", "user.name", "autofix.ci"]);
45+
await exec("git", ["config", "user.email", "noreply@autofix.ci"]);
46+
await exec("git", [
47+
"commit",
48+
"-m", "autofix",
49+
]);
50+
let commit_hash = (
51+
await getExecOutput("git", ["rev-parse", "HEAD"])
52+
).stdout.trim();
53+
if (isDebug()) {
54+
await exec("git", ["show", commit_hash]);
55+
}
56+
// Fetch and check out PR head
57+
await exec("git", ["fetch", "--depth=1", "origin", `+refs/pull/${event.pull_request.number}/head`]);
58+
await exec("git", ["checkout", "--force", "FETCH_HEAD"]);
59+
if (isDebug()) {
60+
await exec("git", ["status"]);
61+
}
62+
// Reapply fixes.
63+
await exec("git", ["cherry-pick", "--no-commit", commit_hash]);
64+
}
65+
66+
const fileChanges = {
67+
additions: [],
68+
deletions: []
69+
};
70+
await Promise.all(changes.map((async (filename) => {
71+
let buf: Buffer;
72+
try {
73+
buf = await readFile(filename);
74+
} catch (e) {
75+
fileChanges.deletions.push({path: filename})
76+
return;
77+
}
78+
fileChanges.additions.push({
79+
path: filename,
80+
contents: buf.toString("base64")
81+
});
82+
})));
83+
if (isDebug()) {
84+
console.log(fileChanges);
85+
}
86+
87+
const client = new DefaultArtifactClient();
88+
89+
const filename = "autofix.json";
90+
try {
91+
await writeFile(filename, JSON.stringify({
92+
version: 1,
93+
changes: fileChanges,
94+
failFast: getInput("fail-fast") === "true",
95+
comment: getInput("comment") || undefined,
96+
commitMessage: getInput("commit-message") || undefined,
97+
}));
98+
await client.uploadArtifact("autofix.ci", [filename], ".", {
99+
retentionDays: 1
100+
});
101+
} finally {
102+
await rm(filename, {maxRetries: 3});
103+
}
104+
105+
let url = (
106+
"https://api.autofix.ci/fix" +
107+
"?owner=" + encodeURIComponent(event.repository.owner.login) +
108+
"&repo=" + encodeURIComponent(event.repository.name)
109+
)
110+
if (event.pull_request) {
111+
url += "&pull=" + encodeURIComponent(event.pull_request.number);
112+
} else {
113+
url += "&branch=" + encodeURIComponent(event.ref.replace(/^refs\/heads\//, ""));
114+
}
115+
116+
const http = new HttpClient("autofix-action/v2");
117+
const resp = await http.post(url, null);
118+
const body = await resp.readBody();
119+
if (resp.message.statusCode === 200) {
120+
setFailed("✅ Autofix task started.");
121+
setOutput('autofix_started', true);
122+
} else {
123+
console.log(resp.message.statusCode, body);
124+
setFailed(body);
125+
}
126+
// show the user what needs to be changed.
127+
await exec("git", ["diff", "--staged"]);
128+
}
129+
130+
(async function run() {
131+
try {
132+
await main();
133+
} catch (error) {
134+
setFailed(String(error).replace(/^Error: /, ""));
135+
}
136+
})();

node_modules/.bin/rimraf

Lines changed: 0 additions & 1 deletion
This file was deleted.

node_modules/.bin/uuid

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)