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

This commit is contained in:
Philipp Horstenkamp 2023-11-19 04:11:59 +01:00
parent ff32b18cc4
commit 69168ed3f5
Signed by: Philipp
GPG Key ID: DD53EAC36AFB61B4

View File

@ -1,40 +1,73 @@
const core = require("@actions/core");
const github = require("@actions/github");
const { Octokit } = require("@octokit/rest");
async function run() {
async function getAndPostPullRequests() {
try {
const branch = core.getInput("branch");
const token = core.getInput("github_token");
const octokit = new Octokit({ auth: token });
const context = github.context;
core.info("Starting the action...");
// Check for existing PRs
const { data: pullRequests } = await octokit.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: `${context.repo.owner}:${branch}`,
});
// Retrieve inputs and token
const baseUrl = core.getInput("base_url") || "https://api.github.com";
core.info(`Base URL: ${baseUrl}`);
if (pullRequests.length > 0) {
console.log("A pull request already exists for this branch.");
return;
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 postData = JSON.parse(core.getInput("post_data"));
core.info(`Post Data: ${JSON.stringify(postData)}`);
const githubToken = core.getInput("github_token");
core.info("GitHub token retrieved.");
// Headers with Authorization
const headers = {
Authorization: `token ${githubToken}`,
"Content-Type": "application/json",
};
// Construct the URL for the requests
const url = `${baseUrl}/repos/${owner}/${repo}/pulls`;
core.info(`Constructed URL: ${url}`);
// Perform the GET request
core.info("Performing GET request...");
const getResponse = await fetch(url, { headers });
if (!getResponse.ok) {
throw new Error(`HTTP error! status: ${getResponse.status}`);
}
// Creating a new PR
const newPr = await octokit.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "Your PR Title", // Customize your PR title
head: branch,
base: "main", // Change this to your default branch if it's not 'main'
body: "Your PR description", // Customize your PR description
const pulls = await getResponse.json();
core.info(`GET request completed. Number of pulls: ${pulls.length}`);
// Logic to decide if a POST request is needed
if (pulls.length === 0) {
core.info("No open pulls found, proceeding with POST request...");
// Perform the POST request
const postResponse = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify(postData),
});
console.log(`Created a new pull request: ${newPr.data.html_url}`);
if (!postResponse.ok) {
throw new Error(`HTTP error! status: ${postResponse.status}`);
}
const postResult = await postResponse.json();
core.info("POST request completed successfully.");
return postResult;
} else {
core.info("No POST request made as conditions were not met.");
return { message: "No POST request made as conditions were not met." };
}
} catch (error) {
core.setFailed(`Action failed with error ${error}`);
core.setFailed(`Action failed with error: ${error}`);
}
}
run();
// Call the function
getAndPostPullRequests();