-
Notifications
You must be signed in to change notification settings - Fork 517
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Actualizar auto-close-pr.properties.json
- Loading branch information
1 parent
23f49ce
commit 77ef934
Showing
1 changed file
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,63 @@ | ||
{ | ||
"name": "Auto close PRs", | ||
"description": "Automatically close pull requests from non-staff." | ||
} | ||
}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); |