From b5854566bd2cb95de28938f4733a3f2dbc11c6e9 Mon Sep 17 00:00:00 2001 From: Axel Pettersson Date: Mon, 3 Jun 2024 11:48:07 +0200 Subject: [PATCH] Finish automatic updating action --- actions/update-standpoints/src/constants.mjs | 4 +++ .../update-standpoints/src/github-helper.mjs | 31 ++++++++++++------- actions/update-standpoints/src/main.mjs | 24 ++++++++++---- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/actions/update-standpoints/src/constants.mjs b/actions/update-standpoints/src/constants.mjs index d1a92e172..6d7c87e11 100644 --- a/actions/update-standpoints/src/constants.mjs +++ b/actions/update-standpoints/src/constants.mjs @@ -1,7 +1,11 @@ // @ts-check export const COMMIT_MESSAGE = "chore(party-data): Update standpoints"; +export const PR_TITLE = COMMIT_MESSAGE; +export const PR_BODY = + "This PR was automatically generated by the update-standpoints action."; export const BRANCH_NAME = "action/update-standpoints"; +export const PR_BASE = "main"; export const TEMP_BRANCH_NAME = `temp-${BRANCH_NAME}`; export const GIT_USERNAME = "github-actions[bot]"; diff --git a/actions/update-standpoints/src/github-helper.mjs b/actions/update-standpoints/src/github-helper.mjs index 91c99cac6..31b9130e2 100644 --- a/actions/update-standpoints/src/github-helper.mjs +++ b/actions/update-standpoints/src/github-helper.mjs @@ -1,26 +1,33 @@ import core from "@actions/core"; import github from "@actions/github"; +import { BRANCH_NAME, PR_BASE, PR_BODY, PR_TITLE } from "./constants.mjs"; + 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") { +export async function createPullRequest() { const { data } = await client.rest.pulls.create({ owner: context.repo.owner, repo: context.repo.repo, - title: title, - body, - head, - base, + title: PR_TITLE, + body: PR_BODY, + head: BRANCH_NAME, + base: PR_BASE, }); return data; } + +export async function findOpenPullRequest() { + const { data } = await client.rest.pulls.list({ + owner: context.repo.owner, + repo: context.repo.repo, + base: PR_BASE, + head: `${context.repo.owner}:${BRANCH_NAME}`, + state: "open", + }); + + return data[0]; +} diff --git a/actions/update-standpoints/src/main.mjs b/actions/update-standpoints/src/main.mjs index 336944ce4..64878389d 100644 --- a/actions/update-standpoints/src/main.mjs +++ b/actions/update-standpoints/src/main.mjs @@ -6,7 +6,7 @@ import { COMMIT_MESSAGE, GIT_EMAIL, GIT_USERNAME, - MAIN_BRANCH, + PR_BASE, TEMP_BRANCH_NAME, } from "./constants.mjs"; import { @@ -19,6 +19,7 @@ import { push, setGitIdentity, } from "./git-helper.mjs"; +import { createPullRequest, findOpenPullRequest } from "./github-helper.mjs"; core.startGroup("Checking if changes exist"); const hasDiff = await checkHasDiff(); @@ -33,7 +34,7 @@ core.startGroup("Setting git identity"); await setGitIdentity(GIT_USERNAME, GIT_EMAIL); core.endGroup(); -core.startGroup("Create branch and commit changes"); +core.startGroup("Creating / updating branch and commit changes"); const branchExists = await checkIfBranchExists(BRANCH_NAME); if (branchExists) { core.info(`Branch ${BRANCH_NAME} already exists, rebasing...`); @@ -48,8 +49,7 @@ if (branchExists) { await commit(COMMIT_MESSAGE); core.endGroup(); -core.startGroup("Push changes"); -// Push changes +core.startGroup("Pushing changes"); if (branchExists) { core.info(`Force pushing changes to ${BRANCH_NAME}...`); await forcePush(`${TEMP_BRANCH_NAME}:${BRANCH_NAME}`); @@ -59,5 +59,17 @@ if (branchExists) { } core.endGroup(); -// Create pull request -// TODO +core.startGroup("Creating pull request"); +core.info("Checking if pull request already exists..."); +const existingPR = await findOpenPullRequest(); +if (existingPR) { + core.info(`Pull request #${existingPR.number} already exists`); + core.info(`URL: ${existingPR.html_url}`); + process.exit(0); +} +core.info("Pull request does not exist, creating..."); +const data = await createPullRequest(); +core.info( + `Pull request created for ${BRANCH_NAME} to ${PR_BASE}. URL: ${data.html_url}`, +); +core.endGroup();