Is it possible to check every x
seconds and determine if a new pull request was created on a github repository, and perform a function on it (I am using the JavaScript library @octokit/rest
)?
So far I have
const { Octokit } = require('@octokit/rest');
const octokit = new Octokit({
auth: "REDACTED"
});
var data = (async () => {
var response = await octokit.request('/repos/foo/bar/pulls?state=all');
return response;
})();
with data as the response of the github api. How do I determine if a new pr was created, the easiest way I figure out is ofc to actually compare the length of the variable data
, but so far I couldn't actually figure out how to do so.
By default, pulls
sorts by created
, so we can check to see if the 0th index has a number
property greater than the old number
. (the number
is like #1, #4, etc. that shows up on the website). During the time that we're waiting for a new PR, multiple PR's could be made, so we need to filter out data
so that we can have multiple PR's above the previous value.
This was based off of the documentation:
and testing the routes with curl
const { Octokit } = require('@octokit/rest');
const octokit = new Octokit({
auth: "REDACTED"
});
const getData = () => octokit.request('/repos/foo/bar/pulls?state=all');
const getLatestPRNumber = (data) => data.length === 0 ? 0 : data[0]["number"];
const getPRsAboveNumber = (data, prNumber) => data.filter(({number}) => number > prNumber)
(async () => {
const data = await getData();
let latestPR = getLatestPRNumber(data);
setInterval(async () => {
const data = await getData();
const firstPRNumber = getLatestPRNumber(data);
if (firstPRNumber > latestPR) {
const newPRs = getPRsAboveNumber(data, latestPR);
console.log(newPRs);
latestPR = firstPRNumber;
}
}, 1000 * 60); // 1000ms * 60 = 60s
})();
If too many PR's are made, then some PR's wouldn't be accessible since the GitHub API using pagination
Per the documentation:
Name | Type | In | Desc |
---|---|---|---|
per_page | integer | query | Results per page (max 100). Default: 30 |
page | integer | query | Page number of the results to fetch. Default: 1 |