Reowrked the code.
Some checks failed
Auto Maintenance Cycle / pre-commit Autoupdate (push) Failing after 32s

This commit is contained in:
Philipp Horstenkamp 2023-11-19 15:20:52 +01:00
parent a0a223d89d
commit 0f1b437509
Signed by: Philipp
GPG Key ID: DD53EAC36AFB61B4

100
index.js
View File

@ -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}`);
// Perform the GET request
core.info("Performing GET request...");
const getResponse = await fetch(`${url}?state=open`, { headers });
if (!getResponse.ok) {
throw new Error(`HTTP error! status: ${getResponse.status}`);
// Logic for current branch
if (!branch) {
const ref = process.env.GITHUB_REF;
if (ref && ref.startsWith("refs/heads/")) {
branch = ref.replace("refs/heads/", "");
}
}
const pulls = await getResponse.json();
core.info(`GET request completed. Number of pulls: ${pulls.length}`);
// core.info(`GET repsonse ${JSON.stringify(pulls, 2)}`);
core.info(`Preparing to merge branch ${branch} into ${base_branch}`);
// 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,
assignee: assignee,
assignees: assigneesInput ? assigneesInput.split(",") : [],
labels: labelsInput ? labelsInput.split(",").map(Number) : [],
due_date: dueDate,
milestone: milestone ? parseInt(milestone, 10) : null,
};
// if (assigneesInput) postData.assignees = assigneesInput.split(",");
// if (reviewersInput) postData.reviewers = reviewersInput.split(",");
// if (labelsInput) postData.labels = labelsInput.split(",");
// Perform the GET request to check if a similar PR exists
const getResponse = await fetch(`${url}?state=open&head=${branch}`, {
headers,
});
core.info(url);
core.info(JSON.stringify(postData));
if (!getResponse.ok) {
throw new Error(`HTTP error! status: ${getResponse.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
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) {