diff --git a/index.js b/index.js index 4863ff1..f8b4932 100644 --- a/index.js +++ b/index.js @@ -7,40 +7,22 @@ async function getAndPostPullRequests() { // Retrieve inputs and token const baseUrl = core.getInput("base_url") || "https://git.horstenkamp.eu"; - core.info(`Base URL: ${baseUrl}`); - const owner = core.getInput("owner") || process.env.GITHUB_REPOSITORY_OWNER; - core.info(`Repository Owner: ${owner}`); - const repo = core.getInput("repo") || process.env.GITHUB_REPOSITORY.split("/")[1]; - core.info(`Repository Name: ${repo}`); - const base_branch = core.getInput("base_branch") || "main"; - const assigneesInput = core.getInput("assignees"); - const reviewersInput = core.getInput("reviewers"); - const labelsInput = core.getInput("labels"); - const githubToken = core.getInput("github_token"); - const title = core.getInput("title"); - const body = core.getInput("body"); + const body = core.getInput("body") || ""; + const assignee = core.getInput("assignee"); + const assigneesInput = core.getInput("assignees"); + const labelsInput = core.getInput("labels"); + const dueDate = core.getInput("due_date"); + const milestone = core.getInput("milestone"); + const githubToken = core.getInput("github_token"); + let branch = core.getInput("branch"); - const branch = core.getInput("branch"); - if (!branch) { - const ref = process.env.GITHUB_REF; - if (ref && ref.startsWith("refs/heads/")) { - branch = ref.replace("refs/heads/", ""); - } - } - - core.info(`The branch ${branch} should be merged into ${base_branch} .`); - if (!body) { - core.warning("Warning: PR body is empty."); - } - - // core.info(`Post Data: ${JSON.stringify(postData)}`); - - core.info("GitHub token retrieved."); + // Construct the URL for the requests + const url = `${baseUrl}/api/v1/repos/${owner}/${repo}/pulls`; // Headers with Authorization const headers = { @@ -48,47 +30,45 @@ async function getAndPostPullRequests() { "Content-Type": "application/json", }; - // Construct the URL for the requests - const url = `${baseUrl}/api/v1/repos/${owner}/${repo}/pulls`; - core.info(`Constructed URL: ${url}`); + // Logic for current branch + if (!branch) { + const ref = process.env.GITHUB_REF; + if (ref && ref.startsWith("refs/heads/")) { + branch = ref.replace("refs/heads/", ""); + } + } - // Perform the GET request - core.info("Performing GET request..."); - const getResponse = await fetch(`${url}?state=open`, { headers }); + core.info(`Preparing to merge branch ${branch} into ${base_branch}`); + + // Prepare POST data + const postData = { + title: title, + body: body, + head: branch, + base: base_branch, + assignee: assignee, + assignees: assigneesInput ? assigneesInput.split(",") : [], + labels: labelsInput ? labelsInput.split(",").map(Number) : [], + due_date: dueDate, + milestone: milestone ? parseInt(milestone, 10) : null, + }; + + // Perform the GET request to check if a similar PR exists + const getResponse = await fetch(`${url}?state=open&head=${branch}`, { + headers, + }); if (!getResponse.ok) { throw new Error(`HTTP error! status: ${getResponse.status}`); } const pulls = await getResponse.json(); - core.info(`GET request completed. Number of pulls: ${pulls.length}`); - // core.info(`GET repsonse ${JSON.stringify(pulls, 2)}`); - - // Logic to decide if a POST request is needed 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 - // Prepare POST data - const postData = { - title: title, - body: body, - head: branch, - base: base_branch, - }; - - // if (assigneesInput) postData.assignees = assigneesInput.split(","); - // if (reviewersInput) postData.reviewers = reviewersInput.split(","); - // if (labelsInput) postData.labels = labelsInput.split(","); - - core.info(url); - core.info(JSON.stringify(postData)); // Perform the POST request const postResponse = await fetch(url, { method: "POST", @@ -104,11 +84,9 @@ async function getAndPostPullRequests() { core.info("POST request completed successfully."); return postResult; } else { - core.info( - `There is already an PR for open that merges ${branch} into ${base_branch}.`, - ); + core.info(`A PR already exists to merge ${branch} into ${base_branch}.`); return { - message: `There is already an PR for open that merges ${branch} into ${base_branch}.`, + message: `A PR already exists to merge ${branch} into ${base_branch}.`, }; } } catch (error) {