|
| 1 | +import * as core from "@actions/core"; |
| 2 | +import { exec, getExecOutput } from "@actions/exec"; |
| 3 | +import * as github from "@actions/github"; |
| 4 | +import { commitChangesFromRepo } from "@changesets/ghcommit/git"; |
| 5 | +import { Octokit } from "./octokit"; |
| 6 | + |
| 7 | +const push = async (branch: string, { force }: { force?: boolean } = {}) => { |
| 8 | + await exec( |
| 9 | + "git", |
| 10 | + ["push", "origin", `HEAD:${branch}`, force && "--force"].filter<string>( |
| 11 | + Boolean as any |
| 12 | + ) |
| 13 | + ); |
| 14 | +}; |
| 15 | + |
| 16 | +const switchToMaybeExistingBranch = async (branch: string) => { |
| 17 | + let { stderr } = await getExecOutput("git", ["checkout", branch], { |
| 18 | + ignoreReturnCode: true, |
| 19 | + }); |
| 20 | + let isCreatingBranch = !stderr |
| 21 | + .toString() |
| 22 | + .includes(`Switched to a new branch '${branch}'`); |
| 23 | + if (isCreatingBranch) { |
| 24 | + await exec("git", ["checkout", "-b", branch]); |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +const reset = async ( |
| 29 | + pathSpec: string, |
| 30 | + mode: "hard" | "soft" | "mixed" = "hard" |
| 31 | +) => { |
| 32 | + await exec("git", ["reset", `--${mode}`, pathSpec]); |
| 33 | +}; |
| 34 | + |
| 35 | +const commitAll = async (message: string) => { |
| 36 | + await exec("git", ["add", "."]); |
| 37 | + await exec("git", ["commit", "-m", message]); |
| 38 | +}; |
| 39 | + |
| 40 | +const checkIfClean = async (): Promise<boolean> => { |
| 41 | + const { stdout } = await getExecOutput("git", ["status", "--porcelain"]); |
| 42 | + return !stdout.length; |
| 43 | +}; |
| 44 | + |
| 45 | +export class Git { |
| 46 | + octokit; |
| 47 | + constructor(octokit?: Octokit) { |
| 48 | + this.octokit = octokit; |
| 49 | + } |
| 50 | + |
| 51 | + async setupUser() { |
| 52 | + if (this.octokit) { |
| 53 | + return; |
| 54 | + } |
| 55 | + await exec("git", ["config", "user.name", `"github-actions[bot]"`]); |
| 56 | + await exec("git", [ |
| 57 | + "config", |
| 58 | + "user.email", |
| 59 | + `"41898282+github-actions[bot]@users.noreply.github.com"`, |
| 60 | + ]); |
| 61 | + } |
| 62 | + |
| 63 | + async pushTag(tag: string) { |
| 64 | + if (this.octokit) { |
| 65 | + return this.octokit.rest.git |
| 66 | + .createRef({ |
| 67 | + ...github.context.repo, |
| 68 | + ref: `refs/tags/${tag}`, |
| 69 | + sha: github.context.sha, |
| 70 | + }) |
| 71 | + .catch((err) => { |
| 72 | + // Assuming tag was manually pushed in custom publish script |
| 73 | + core.warning(`Failed to create tag ${tag}: ${err.message}`); |
| 74 | + }); |
| 75 | + } |
| 76 | + await exec("git", ["push", "origin", tag]); |
| 77 | + } |
| 78 | + |
| 79 | + async prepareBranch(branch: string) { |
| 80 | + if (this.octokit) { |
| 81 | + // Preparing a new local branch is not necessary when using the API |
| 82 | + return; |
| 83 | + } |
| 84 | + await switchToMaybeExistingBranch(branch); |
| 85 | + await reset(github.context.sha); |
| 86 | + } |
| 87 | + |
| 88 | + async pushChanges({ branch, message }: { branch: string; message: string }) { |
| 89 | + if (this.octokit) { |
| 90 | + return commitChangesFromRepo({ |
| 91 | + octokit: this.octokit, |
| 92 | + ...github.context.repo, |
| 93 | + branch, |
| 94 | + message, |
| 95 | + base: { |
| 96 | + commit: github.context.sha, |
| 97 | + }, |
| 98 | + force: true, |
| 99 | + }); |
| 100 | + } |
| 101 | + if (!(await checkIfClean())) { |
| 102 | + await commitAll(message); |
| 103 | + } |
| 104 | + await push(branch, { force: true }); |
| 105 | + } |
| 106 | +} |
0 commit comments