From a68c8c3af6ba3f8ce21935b222e51534e8929ab2 Mon Sep 17 00:00:00 2001 From: Axel Pettersson Date: Sun, 2 Jun 2024 23:54:54 +0200 Subject: [PATCH] chore: Update standpoints action with git helpers and check for differences before committing --- .github/workflows/update-standpoints.yml | 2 ++ actions/update-standpoints/action.yaml | 4 +++ actions/update-standpoints/src/git-helper.mjs | 2 +- .../update-standpoints/src/github-helper.mjs | 26 +++++++++++++++++++ actions/update-standpoints/src/main.mjs | 19 +++++++++++++- 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 actions/update-standpoints/src/github-helper.mjs diff --git a/.github/workflows/update-standpoints.yml b/.github/workflows/update-standpoints.yml index c06fd3ee7..1b25b45d7 100644 --- a/.github/workflows/update-standpoints.yml +++ b/.github/workflows/update-standpoints.yml @@ -35,3 +35,5 @@ jobs: - name: Run action to potentially create PR uses: ./actions/update-standpoints + with: + github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/actions/update-standpoints/action.yaml b/actions/update-standpoints/action.yaml index 5a8e1db1f..25435827b 100644 --- a/actions/update-standpoints/action.yaml +++ b/actions/update-standpoints/action.yaml @@ -1,5 +1,9 @@ name: Update Standpoints description: Script that updates the party standpoints +inputs: + github-token: + description: GitHub token + required: true runs: using: node20 main: dist/index.mjs diff --git a/actions/update-standpoints/src/git-helper.mjs b/actions/update-standpoints/src/git-helper.mjs index c43f20307..279c2c6a4 100644 --- a/actions/update-standpoints/src/git-helper.mjs +++ b/actions/update-standpoints/src/git-helper.mjs @@ -29,7 +29,7 @@ export async function push() { await exec(command); } -export async function hasDiff() { +export async function checkHasDiff() { const command = ["diff", "--quiet"]; const output = await exec(command); // Exit code 1 means there are differences diff --git a/actions/update-standpoints/src/github-helper.mjs b/actions/update-standpoints/src/github-helper.mjs new file mode 100644 index 000000000..91c99cac6 --- /dev/null +++ b/actions/update-standpoints/src/github-helper.mjs @@ -0,0 +1,26 @@ +import core from "@actions/core"; +import github from "@actions/github"; + +const { context } = github; + +const token = core.getInput("github-token"); +const client = github.getOctokit(token); + +/** + * + * @param {string} title + * @param {string} body + * @param {string} head + * @param {string} base + */ +export async function createPullRequest(title, body, head, base = "main") { + const { data } = await client.rest.pulls.create({ + owner: context.repo.owner, + repo: context.repo.repo, + title: title, + body, + head, + base, + }); + return data; +} diff --git a/actions/update-standpoints/src/main.mjs b/actions/update-standpoints/src/main.mjs index d94a5cbfc..8f9cf39e8 100644 --- a/actions/update-standpoints/src/main.mjs +++ b/actions/update-standpoints/src/main.mjs @@ -1,3 +1,20 @@ // @ts-check +import github from "@actions/github"; -console.log("Hello from update-standpoints!"); +import { add, checkHasDiff, checkout, commit, push } from "./git-helper.mjs"; + +const hasDiff = await checkHasDiff(); + +if (!hasDiff) { + console.log("No changes detected, exiting..."); + process.exit(0); +} + +const runId = github.context.runId; +const branchName = `update-standpoints-${runId}`; + +// Checkout and push changes +await checkout(branchName, "main"); +await add(); +await commit("Update standpoints"); +await push();