ci/github-script/merge: improve caching of team members

This removes the need to `await` committers further down in the function
and allows re-using the cache for other teams later.
This commit is contained in:
Wolfgang Walther 2025-11-02 10:11:42 +01:00
parent 7c9035e29d
commit c7766c637f
No known key found for this signature in database
GPG key ID: B39893FA5F65CAE1

View file

@ -1,27 +1,32 @@
// Caching the list of committers saves API requests when running the bot on the schedule and // Caching the list of team members saves API requests when running the bot on the schedule and
// processing many PRs at once. // processing many PRs at once.
let committers const members = {}
async function runChecklist({ github, context, pull_request, maintainers }) { async function runChecklist({ github, context, pull_request, maintainers }) {
const pull_number = pull_request.number const pull_number = pull_request.number
if (!committers) { function getTeamMembers(team_slug) {
if (context.eventName === 'pull_request') { if (context.eventName === 'pull_request') {
// We have no chance of getting a token in the pull_request context with the right // We have no chance of getting a token in the pull_request context with the right
// permissions to access the members endpoint below. Thus, we're pretending to have // permissions to access the members endpoint below. Thus, we're pretending to have
// no committers. This is OK; because this is only for the Test workflow, not for // no members. This is OK; because this is only for the Test workflow, not for
// real use. // real use.
committers = new Set() return new Set()
} else { }
committers = github
if (!members[team_slug]) {
members[team_slug] = github
.paginate(github.rest.teams.listMembersInOrg, { .paginate(github.rest.teams.listMembersInOrg, {
org: context.repo.owner, org: context.repo.owner,
team_slug: 'nixpkgs-committers', team_slug,
per_page: 100, per_page: 100,
}) })
.then((members) => new Set(members.map(({ id }) => id))) .then((members) => new Set(members.map(({ id }) => id)))
} }
return members[team_slug]
} }
committers = await getTeamMembers('nixpkgs-committers')
const files = await github.paginate(github.rest.pulls.listFiles, { const files = await github.paginate(github.rest.pulls.listFiles, {
...context.repo, ...context.repo,
@ -50,7 +55,7 @@ async function runChecklist({ github, context, pull_request, maintainers }) {
), ),
'PR authored by r-ryantm or committer.': 'PR authored by r-ryantm or committer.':
pull_request.user.login === 'r-ryantm' || pull_request.user.login === 'r-ryantm' ||
(await committers).has(pull_request.user.id), committers.has(pull_request.user.id),
'PR has maintainers eligible for merge.': eligible.size > 0, 'PR has maintainers eligible for merge.': eligible.size > 0,
} }