From 77ef934a4afb137dcf7f0ff8a845405b8391ca4a Mon Sep 17 00:00:00 2001 From: Kikemotos12 <12patofa9@gmail.com> Date: Sat, 31 Aug 2024 03:22:25 -0600 Subject: [PATCH] Actualizar auto-close-pr.properties.json --- .../auto-close-pr.properties.json | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/workflow-templates/auto-close-pr.properties.json b/workflow-templates/auto-close-pr.properties.json index 8b9e20d..1ffa620 100644 --- a/workflow-templates/auto-close-pr.properties.json +++ b/workflow-templates/auto-close-pr.properties.json @@ -1,4 +1,63 @@ { "name": "Auto close PRs", "description": "Automatically close pull requests from non-staff." -} \ No newline at end of file +}import { Octokit } from "octokit"; + +const octokit = new Octokit({ }); + +async function getPaginatedData(url) { + const nextPattern = /(?<=<)([\S]*)(?=>; rel="Next")/i; + let pagesRemaining = true; + let data = []; + + while (pagesRemaining) { + const response = await octokit.request(`GET ${url}`, { + per_page: 100, + headers: { + "X-GitHub-Api-Version": + "2022-11-28", + }, + }); + + const parsedData = parseData(response.data) + data = [...data, ...parsedData]; + + const linkHeader = response.headers.link; + + pagesRemaining = linkHeader && linkHeader.includes(`rel=\"next\"`); + + if (pagesRemaining) { + url = linkHeader.match(nextPattern)[0]; + } + } + + return data; +} + +function parseData(data) { + // If the data is an array, return that + if (Array.isArray(data)) { + return data + } + + // Some endpoints respond with 204 No Content instead of empty array + // when there is no data. In that case, return an empty array. + if (!data) { + return [] + } + + // Otherwise, the array of items that we want is in an object + // Delete keys that don't include the array of items + delete data.incomplete_results; + delete data.repository_selection; + delete data.total_count; + // Pull out the array of items + const namespaceKey = Object.keys(data)[0]; + data = data[namespaceKey]; + + return data; +} + +const data = await getPaginatedData("/repos/octocat/Spoon-Knife/issues"); + +console.log(data);