-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update to latest upload-artifact, apply fixes on PR head
- Loading branch information
Showing
9 changed files
with
846 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: autofix.ci # needed to securely identify the workflow | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: [ "main" ] | ||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
autofix: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 20 | ||
- run: npm ci | ||
- run: npm run build | ||
|
||
- uses: autofix-ci/action@d3e591514b99d0fca6779455ff8338516663f7cc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
See https://github.com/autofix-ci/action/releases. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import {DefaultArtifactClient} from '@actions/artifact' | ||
import {getInput, isDebug, setFailed, setOutput} from "@actions/core"; | ||
import {exec, getExecOutput} from "@actions/exec"; | ||
import {HttpClient} from "@actions/http-client"; | ||
import {readFile, rm, writeFile} from "fs/promises"; | ||
|
||
|
||
async function main() { | ||
setOutput('autofix_started', false); | ||
|
||
const event = JSON.parse( | ||
await readFile(process.env.GITHUB_EVENT_PATH, {encoding: 'utf8'}) | ||
); | ||
if (isDebug()) { | ||
console.log(event); | ||
} | ||
|
||
if (process.env.GITHUB_WORKFLOW !== "autofix.ci") { | ||
throw `For security reasons, the workflow in which the autofix.ci action is used must be named "autofix.ci".`; | ||
} | ||
|
||
await exec("git", ["reset"]); | ||
|
||
await exec("git", ["-c", "core.fileMode=false", "add", "--all"]); | ||
|
||
// Git consistently uses unix-style paths, so we do not need to worry about path conversions. | ||
let {stdout} = await getExecOutput("git", ["diff", "--name-only", "--staged", "--no-renames"]) | ||
if (stdout === "") { | ||
console.log("Nothing to do! ✨"); | ||
return; | ||
} | ||
let changes = stdout.trim().split("\n"); | ||
// UX: Already check here if we have forbidden files. | ||
// This is truly enforced on the server, but this way we can give a better error message. | ||
if (changes.some((path) => path.includes(".github"))) { | ||
throw "The autofix.ci action is not allowed to modify the .github directory."; | ||
} | ||
console.log(`Need to update ${changes.length} files.`); | ||
|
||
// For pull requests, we need to rebase the changes onto the PR head, | ||
// see https://github.com/autofix-ci/action/issues/12. | ||
if (event.pull_request) { | ||
// Create a commit | ||
await exec("git", ["config", "user.name", "autofix.ci"]); | ||
await exec("git", ["config", "user.email", "[email protected]"]); | ||
await exec("git", [ | ||
"commit", | ||
"-m", "autofix", | ||
]); | ||
let commit_hash = ( | ||
await getExecOutput("git", ["rev-parse", "HEAD"]) | ||
).stdout.trim(); | ||
// Fetch and check out PR head | ||
await exec("git", ["fetch", "--depth=1", "origin", `+refs/pull/${event.pull_request.number}/head`]); | ||
await exec("git", ["checkout", "--force", "FETCH_HEAD"]); | ||
// Reapply fixes. | ||
await exec("git", ["cherry-pick", "--no-edit", commit_hash]); | ||
} | ||
|
||
const fileChanges = { | ||
additions: [], | ||
deletions: [] | ||
}; | ||
await Promise.all(changes.map((async (filename) => { | ||
let buf: Buffer; | ||
try { | ||
buf = await readFile(filename); | ||
} catch (e) { | ||
fileChanges.deletions.push({path: filename}) | ||
return; | ||
} | ||
fileChanges.additions.push({ | ||
path: filename, | ||
contents: buf.toString("base64") | ||
}); | ||
}))); | ||
if (isDebug()) { | ||
console.log(fileChanges); | ||
} | ||
|
||
const client = new DefaultArtifactClient(); | ||
|
||
const filename = "autofix.json"; | ||
try { | ||
await writeFile(filename, JSON.stringify({ | ||
version: 1, | ||
changes: fileChanges, | ||
failFast: getInput("fail-fast") === "true", | ||
comment: getInput("comment") || undefined, | ||
commitMessage: getInput("commit-message") || undefined, | ||
})); | ||
await client.uploadArtifact("autofix.ci", [filename], ".", { | ||
retentionDays: 1 | ||
}); | ||
} finally { | ||
await rm(filename, {maxRetries: 3}); | ||
} | ||
|
||
let url = ( | ||
"https://api.autofix.ci/fix" + | ||
"?owner=" + encodeURIComponent(event.repository.owner.login) + | ||
"&repo=" + encodeURIComponent(event.repository.name) | ||
) | ||
if (event.pull_request) { | ||
url += "&pull=" + encodeURIComponent(event.pull_request.number); | ||
} else { | ||
url += "&branch=" + encodeURIComponent(event.ref.replace(/^refs\/heads\//, "")); | ||
} | ||
|
||
const http = new HttpClient("autofix-action/v2"); | ||
const resp = await http.post(url, null); | ||
const body = await resp.readBody(); | ||
if (resp.message.statusCode === 200) { | ||
setFailed("✅ Autofix task started."); | ||
setOutput('autofix_started', true); | ||
} else { | ||
console.log(resp.message.statusCode, body); | ||
setFailed(body); | ||
} | ||
// show the user what needs to be changed. | ||
await exec("git", ["diff", "--staged"]); | ||
} | ||
|
||
(async function run() { | ||
try { | ||
await main(); | ||
} catch (error) { | ||
setFailed(String(error).replace(/^Error: /, "")); | ||
} | ||
})(); |
Oops, something went wrong.