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

This commit is contained in:
Philipp Horstenkamp 2023-11-19 22:10:16 +01:00
parent 3baa2f745b
commit 1a139366a7
Signed by: Philipp
GPG Key ID: DD53EAC36AFB61B4

View File

@ -25,19 +25,10 @@ async function getAndPostPullRequests() {
// Headers with Authorization // Headers with Authorization
const headers = { const headers = {
Authorization: `token ${token}`,
"Content-Type": "application/json", "Content-Type": "application/json",
}; };
core.info(`token ${token}`); core.info(`token ${token}`);
// Logic for current branch // 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 // Prepare POST data
const postData = { const postData = {
@ -55,48 +46,29 @@ async function getAndPostPullRequests() {
headers, 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( 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 postResult = await postResponse.json();
const targetPRFound = _.any( core.info("POST request completed successfully.");
pulls, return postResult;
(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}.`,
};
}
} catch (error) { } catch (error) {
core.setFailed(`Action failed with error: ${error}`); core.setFailed(`Action failed with error: ${error}`);
} }