-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get GitHub issues from dependency repo
- Loading branch information
Showing
5 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const { request } = require('@octokit/request'); | ||
|
||
// Check whether a specific time has reached the GitHub time limit. | ||
// @param {Date} datetime - a Date object representing a moment in the past | ||
// @returns {Boolean} | ||
const hasGitHubExpired = (dateTime) => new Date() - dateTime > 60 * 60 * 1000; | ||
|
||
const gitHubInformationRequestor = {}; | ||
|
||
async function constructGitHubRequest(gitHubUrl) { | ||
const labels = ['hacktoberfest', 'good first issue', 'help wanted']; | ||
|
||
const [owner, repo] = new URL(gitHubUrl).pathname.substring(1).split('/'); | ||
|
||
const requestPromises = labels.map((label) => { | ||
return request('GET /repos/{owner}/{repo}/issues{?assignee,state,labels}', { | ||
owner, | ||
repo, | ||
assignee: 'none', | ||
state: 'open', | ||
labels: label, | ||
}); | ||
}); | ||
|
||
// A single request might look like a response in | ||
// https://docs.github.com/en/rest/reference/issues#list-repository-issues | ||
const responses = await Promise.all(requestPromises); | ||
|
||
return responses | ||
.filter((response) => response.status === 200) | ||
.flatMap((response) => | ||
response.data.map((issue) => { | ||
return { | ||
htmlUrl: issue.html_url, | ||
title: issue.title, | ||
body: issue.body, | ||
createdAt: issue.created_at, | ||
}; | ||
}) | ||
) | ||
.filter((issue, index, array) => array.findIndex((i) => i.htmlUrl === issue.htmlUrl) === index); | ||
} | ||
|
||
function requestGitHubInfo(packageName, gitHubUrl) { | ||
const cachedRequest = gitHubInformationRequestor[packageName]; | ||
|
||
if (cachedRequest && !hasGitHubExpired(cachedRequest.expireAt)) { | ||
return cachedRequest.request; | ||
} | ||
|
||
const newRequest = constructGitHubRequest(gitHubUrl); | ||
|
||
const now = new Date(); | ||
now.setHours(now.getHours() + 1); | ||
|
||
gitHubInformationRequestor[packageName] = { | ||
expireAt: now, | ||
request: newRequest, | ||
}; | ||
|
||
return newRequest; | ||
} | ||
|
||
module.exports = { | ||
requestGitHubInfo, | ||
}; |
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