Skip to content

Commit

Permalink
Finish automatic updating action
Browse files Browse the repository at this point in the history
  • Loading branch information
Ackuq committed Jun 3, 2024
1 parent 28ee2a3 commit b585456
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
4 changes: 4 additions & 0 deletions actions/update-standpoints/src/constants.mjs
Original file line number Diff line number Diff line change
@@ -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]";
Expand Down
31 changes: 19 additions & 12 deletions actions/update-standpoints/src/github-helper.mjs
Original file line number Diff line number Diff line change
@@ -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];
}
24 changes: 18 additions & 6 deletions actions/update-standpoints/src/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
COMMIT_MESSAGE,
GIT_EMAIL,
GIT_USERNAME,
MAIN_BRANCH,
PR_BASE,
TEMP_BRANCH_NAME,
} from "./constants.mjs";
import {
Expand All @@ -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();
Expand All @@ -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...`);
Expand All @@ -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}`);
Expand All @@ -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();

0 comments on commit b585456

Please sign in to comment.