Reowrked the code.
Some checks failed
Auto Maintenance Cycle / pre-commit Autoupdate (push) Failing after 32s
Some checks failed
Auto Maintenance Cycle / pre-commit Autoupdate (push) Failing after 32s
This commit is contained in:
parent
a0a223d89d
commit
0f1b437509
100
index.js
100
index.js
@ -7,40 +7,22 @@ async function getAndPostPullRequests() {
|
|||||||
|
|
||||||
// Retrieve inputs and token
|
// Retrieve inputs and token
|
||||||
const baseUrl = core.getInput("base_url") || "https://git.horstenkamp.eu";
|
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;
|
const owner = core.getInput("owner") || process.env.GITHUB_REPOSITORY_OWNER;
|
||||||
core.info(`Repository Owner: ${owner}`);
|
|
||||||
|
|
||||||
const repo =
|
const repo =
|
||||||
core.getInput("repo") || process.env.GITHUB_REPOSITORY.split("/")[1];
|
core.getInput("repo") || process.env.GITHUB_REPOSITORY.split("/")[1];
|
||||||
core.info(`Repository Name: ${repo}`);
|
|
||||||
|
|
||||||
const base_branch = core.getInput("base_branch") || "main";
|
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 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");
|
// Construct the URL for the requests
|
||||||
if (!branch) {
|
const url = `${baseUrl}/api/v1/repos/${owner}/${repo}/pulls`;
|
||||||
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.");
|
|
||||||
|
|
||||||
// Headers with Authorization
|
// Headers with Authorization
|
||||||
const headers = {
|
const headers = {
|
||||||
@ -48,47 +30,45 @@ async function getAndPostPullRequests() {
|
|||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
};
|
};
|
||||||
|
|
||||||
// Construct the URL for the requests
|
// Logic for current branch
|
||||||
const url = `${baseUrl}/api/v1/repos/${owner}/${repo}/pulls`;
|
if (!branch) {
|
||||||
core.info(`Constructed URL: ${url}`);
|
const ref = process.env.GITHUB_REF;
|
||||||
|
if (ref && ref.startsWith("refs/heads/")) {
|
||||||
// Perform the GET request
|
branch = ref.replace("refs/heads/", "");
|
||||||
core.info("Performing GET request...");
|
}
|
||||||
const getResponse = await fetch(`${url}?state=open`, { headers });
|
|
||||||
|
|
||||||
if (!getResponse.ok) {
|
|
||||||
throw new Error(`HTTP error! status: ${getResponse.status}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const pulls = await getResponse.json();
|
core.info(`Preparing to merge branch ${branch} into ${base_branch}`);
|
||||||
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
|
// Prepare POST data
|
||||||
const postData = {
|
const postData = {
|
||||||
title: title,
|
title: title,
|
||||||
body: body,
|
body: body,
|
||||||
head: branch,
|
head: branch,
|
||||||
base: base_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(",");
|
// Perform the GET request to check if a similar PR exists
|
||||||
// if (reviewersInput) postData.reviewers = reviewersInput.split(",");
|
const getResponse = await fetch(`${url}?state=open&head=${branch}`, {
|
||||||
// if (labelsInput) postData.labels = labelsInput.split(",");
|
headers,
|
||||||
|
});
|
||||||
|
|
||||||
core.info(url);
|
if (!getResponse.ok) {
|
||||||
core.info(JSON.stringify(postData));
|
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
|
// Perform the POST request
|
||||||
const postResponse = await fetch(url, {
|
const postResponse = await fetch(url, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@ -104,11 +84,9 @@ async function getAndPostPullRequests() {
|
|||||||
core.info("POST request completed successfully.");
|
core.info("POST request completed successfully.");
|
||||||
return postResult;
|
return postResult;
|
||||||
} else {
|
} else {
|
||||||
core.info(
|
core.info(`A PR already exists to merge ${branch} into ${base_branch}.`);
|
||||||
`There is already an PR for open that merges ${branch} into ${base_branch}.`,
|
|
||||||
);
|
|
||||||
return {
|
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) {
|
} catch (error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user