Some checks failed
Auto Maintenance Cycle / pre-commit Autoupdate (push) Has been cancelled
114 lines
3.8 KiB
JavaScript
114 lines
3.8 KiB
JavaScript
const core = require("@actions/core");
|
|
const _ = require("underscore");
|
|
|
|
function Sleep(milliseconds) {
|
|
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
}
|
|
|
|
async function getAndPostPullRequests() {
|
|
try {
|
|
core.info("Starting the action...");
|
|
|
|
// Retrieve inputs and token
|
|
const baseUrl = core.getInput("base_url") || "https://git.horstenkamp.eu";
|
|
const owner = core.getInput("owner") || process.env.GITHUB_REPOSITORY_OWNER;
|
|
const repo =
|
|
core.getInput("repo") || process.env.GITHUB_REPOSITORY.split("/")[1];
|
|
const base_branch = core.getInput("base_branch") || "main";
|
|
const title = core.getInput("title");
|
|
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");
|
|
|
|
// Construct the URL for the requests
|
|
const url = `${baseUrl}/api/v1/repos/${owner}/${repo}/pulls`;
|
|
|
|
// Headers with Authorization
|
|
const headers = {
|
|
Authorization: `token ${githubToken}`,
|
|
"Content-Type": "application/json",
|
|
};
|
|
core.info(`token ${githubToken}`);
|
|
// 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 = {
|
|
title: title,
|
|
body: body,
|
|
head: branch,
|
|
base: base_branch,
|
|
assignee: assignee || undefined, // Changed to be omitted if empty
|
|
assignees: assigneesInput ? assigneesInput.split(",") : undefined, // Changed to be omitted if empty
|
|
labels: labelsInput ? labelsInput.split(",").map(Number) : undefined, // Changed to be omitted if empty
|
|
due_date: dueDate || undefined, // Changed to be omitted if empty
|
|
milestone: milestone ? parseInt(milestone, 10) : undefined, // Changed to be omitted if empty
|
|
};
|
|
// 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 on checking the PR request 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
|
|
|
|
core.info(`URL for POST request: ${url}`);
|
|
core.info(`POST data being sent: ${JSON.stringify(postData, null, 2)}`);
|
|
await Sleep(50000);
|
|
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}.`,
|
|
};
|
|
}
|
|
} catch (error) {
|
|
core.setFailed(`Action failed with error: ${error}`);
|
|
}
|
|
}
|
|
|
|
// Call the function
|
|
getAndPostPullRequests();
|