|
| 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 | +})(); |
0 commit comments