Reworked the index.js
Some checks failed
Auto Maintenance Cycle / pre-commit Autoupdate (push) Failing after 35s
Some checks failed
Auto Maintenance Cycle / pre-commit Autoupdate (push) Failing after 35s
This commit is contained in:
parent
ff32b18cc4
commit
69168ed3f5
89
index.js
89
index.js
@ -1,40 +1,73 @@
|
|||||||
const core = require("@actions/core");
|
const core = require("@actions/core");
|
||||||
const github = require("@actions/github");
|
|
||||||
const { Octokit } = require("@octokit/rest");
|
|
||||||
|
|
||||||
async function run() {
|
async function getAndPostPullRequests() {
|
||||||
try {
|
try {
|
||||||
const branch = core.getInput("branch");
|
core.info("Starting the action...");
|
||||||
const token = core.getInput("github_token");
|
|
||||||
const octokit = new Octokit({ auth: token });
|
|
||||||
const context = github.context;
|
|
||||||
|
|
||||||
// Check for existing PRs
|
// Retrieve inputs and token
|
||||||
const { data: pullRequests } = await octokit.rest.pulls.list({
|
const baseUrl = core.getInput("base_url") || "https://api.github.com";
|
||||||
owner: context.repo.owner,
|
core.info(`Base URL: ${baseUrl}`);
|
||||||
repo: context.repo.repo,
|
|
||||||
head: `${context.repo.owner}:${branch}`,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (pullRequests.length > 0) {
|
const owner = core.getInput("owner") || process.env.GITHUB_REPOSITORY_OWNER;
|
||||||
console.log("A pull request already exists for this branch.");
|
core.info(`Repository Owner: ${owner}`);
|
||||||
return;
|
|
||||||
|
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 pulls = await getResponse.json();
|
||||||
const newPr = await octokit.rest.pulls.create({
|
core.info(`GET request completed. Number of pulls: ${pulls.length}`);
|
||||||
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
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(`Created a new pull request: ${newPr.data.html_url}`);
|
// 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),
|
||||||
|
});
|
||||||
|
|
||||||
|
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) {
|
} catch (error) {
|
||||||
core.setFailed(`Action failed with error ${error}`);
|
core.setFailed(`Action failed with error: ${error}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
run();
|
// Call the function
|
||||||
|
getAndPostPullRequests();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user