From 1a139366a7ebb564f43de0e05a0f3c9beedb969a Mon Sep 17 00:00:00 2001 From: Philipp Horstenkamp Date: Sun, 19 Nov 2023 22:10:16 +0100 Subject: [PATCH] Reworked the index.js --- index.js | 66 ++++++++++++++++---------------------------------------- 1 file changed, 19 insertions(+), 47 deletions(-) diff --git a/index.js b/index.js index b62e267..7048754 100644 --- a/index.js +++ b/index.js @@ -25,19 +25,10 @@ async function getAndPostPullRequests() { // Headers with Authorization const headers = { - Authorization: `token ${token}`, "Content-Type": "application/json", }; core.info(`token ${token}`); // Logic for current branch - if (!branch) { - const ref = process.env.GITHUB_REF; - if (ref && ref.startsWith("refs/heads/")) { - branch = ref.replace("refs/heads/", ""); - } - } - - core.info(`Preparing to merge branch ${branch} into ${base_branch}`); // Prepare POST data const postData = { @@ -55,48 +46,29 @@ async function getAndPostPullRequests() { headers, }); - if (!getResponse.ok) { + // Perform the POST request + + core.info(`URL for POST request: ${url}`); + core.info(`POST data being sent: ${JSON.stringify(postData, null, 2)}`); + const postResponse = await fetch(`${url}?access_token=${token}`, { + method: "POST", + headers, + body: JSON.stringify(postData), + }); + + const postResponseText = await postResponse.text(); + core.info(`POST request status: ${postResponse.status}`); + core.info(`POST response body: ${postResponseText}`); + + if (!postResponse.ok) { throw new Error( - `HTTP error! status on checking the PR request status: ${getResponse.status}`, + `HTTP error! status on creating a new PR: ${postResponse.status}`, ); } - const pulls = await getResponse.json(); - const targetPRFound = _.any( - pulls, - (pr) => pr.head.ref === branch && pr.base.ref === base_branch, - ); - - if (!targetPRFound) { - // Perform the POST request - - core.info(`URL for POST request: ${url}`); - core.info(`POST data being sent: ${JSON.stringify(postData, null, 2)}`); - const postResponse = await fetch(url, { - method: "POST", - headers, - body: JSON.stringify(postData), - }); - - const postResponseText = await postResponse.text(); - core.info(`POST request status: ${postResponse.status}`); - core.info(`POST response body: ${postResponseText}`); - - if (!postResponse.ok) { - throw new Error( - `HTTP error! status on creating a new PR: ${postResponse.status}`, - ); - } - - const postResult = await postResponse.json(); - core.info("POST request completed successfully."); - return postResult; - } else { - core.info(`A PR already exists to merge ${branch} into ${base_branch}.`); - return { - message: `A PR already exists to merge ${branch} into ${base_branch}.`, - }; - } + const postResult = await postResponse.json(); + core.info("POST request completed successfully."); + return postResult; } catch (error) { core.setFailed(`Action failed with error: ${error}`); }