Unverified Commit c7766c63 authored by Wolfgang Walther's avatar Wolfgang Walther
Browse files

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.
parent 7c9035e2
Loading
Loading
Loading
Loading
+14 −9
Original line number Diff line number Diff line
// 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.
let committers
const members = {}

async function runChecklist({ github, context, pull_request, maintainers }) {
  const pull_number = pull_request.number

  if (!committers) {
  function getTeamMembers(team_slug) {
    if (context.eventName === 'pull_request') {
      // 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
      // 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.
      committers = new Set()
    } else {
      committers = github
      return new Set()
    }

    if (!members[team_slug]) {
      members[team_slug] = github
        .paginate(github.rest.teams.listMembersInOrg, {
          org: context.repo.owner,
          team_slug: 'nixpkgs-committers',
          team_slug,
          per_page: 100,
        })
        .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, {
    ...context.repo,
@@ -50,7 +55,7 @@ async function runChecklist({ github, context, pull_request, maintainers }) {
    ),
    'PR authored by r-ryantm or committer.':
      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,
  }