From 9e76cefde386323a288b4dcc32e5ad05706f9f07 Mon Sep 17 00:00:00 2001 From: Philipp Horstenkamp Date: Sun, 19 Nov 2023 13:01:36 +0100 Subject: [PATCH] Added an a check that validates if an similar PR already exists. --- .gitea/workflows/maintenance.yaml | 6 +++--- action.yml | 7 +++++-- index.js | 22 ++++++++++++++++++---- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/.gitea/workflows/maintenance.yaml b/.gitea/workflows/maintenance.yaml index 1c7557b..8cc5c9e 100644 --- a/.gitea/workflows/maintenance.yaml +++ b/.gitea/workflows/maintenance.yaml @@ -30,8 +30,8 @@ jobs: uses: ./ with: github_token: ${{ secrets.GITHUB_TOKEN }} - branch: my-branch - pr_title: My PR Title - pr_body: My PR Body + branch: update/pre-commit-hooks + title: My PR Title + body: My PR Body assignees: Philipp reviewers: Philipp diff --git a/action.yml b/action.yml index 515cb7f..b820669 100644 --- a/action.yml +++ b/action.yml @@ -7,10 +7,13 @@ inputs: branch: description: Branch for which to create the PR required: true - pr_title: + base_branch: + description: Branch in which should be mergbed. Defaults to main. + required: false + title: description: Title of the pull request required: true - pr_body: + body: description: Body of the pull request required: true assignees: diff --git a/index.js b/index.js index 0f0146c..7b04db9 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ const core = require("@actions/core"); +const _ = require("underscore"); async function getAndPostPullRequests() { try { @@ -18,6 +19,9 @@ async function getAndPostPullRequests() { const branch = core.getInput("branch"); core.info(`The branch ${branch} should be merged.`); + const base_branch = core.getInput("base_branch"); + core.info(`The branch ${base_branch} should be merged into.`); + // core.info(`Post Data: ${JSON.stringify(postData)}`); const githubToken = core.getInput("github_token"); @@ -47,8 +51,14 @@ async function getAndPostPullRequests() { core.info(`GET repsonse ${JSON.stringify(pulls, 2)}`); // Logic to decide if a POST request is needed - if (pulls.length === 0) { - core.info("No open pulls found, proceeding with POST request..."); + const targetPRFound = _.any( + pulls, + (pr) => pr.head.ref === branch && pr.base.ref === base_branch, + ); + if (targetPRFound) { + core.info( + `No open pulls found, to merge ${branch} into ${base_branch}. Trying to create a new PR.`, + ); return { message: "Abort for thesting" }; // Perform the POST request const postResponse = await fetch(url, { @@ -65,8 +75,12 @@ async function getAndPostPullRequests() { core.info("POST request completed successfully."); return postResult; } else { - core.info("No POST request made as conditions were not met."); - return { message: "No POST request made as conditions were not met." }; + core.info( + `There is already an PR for open that merges ${branch} into ${base_branch}.`, + ); + return { + message: `There is already an PR for open that merges ${branch} into ${base_branch}.`, + }; } } catch (error) { core.setFailed(`Action failed with error: ${error}`);