@@ -538,11 +538,13 @@ jobs:
538538 filterOutClosed : true
539539
540540 # Combine the output from the previous action with the metadata supplied by GitHub itself.
541+ # Note that if we fall back to github.event.pull_request data, the list of labels is generated when this workflow is triggered
542+ # and not updated afterwards!
541543 - id : PR
542544 shell : bash
543545 run : |
544- echo "number=${{ steps.PR_from_push.outputs.number || github.event.pull_request.number }}" >> $GITHUB_OUTPUT
545- echo "pr_labels=${{ steps.PR_from_push.outputs.pr_labels || join(github.event.pull_request.labels.*.name, ',') }}" >> $GITHUB_OUTPUT
546+ echo "number=${{ steps.PR_from_push.outputs.number || github.event.pull_request.number }}" | tee -a " $GITHUB_OUTPUT"
547+ echo "pr_labels=${{ steps.PR_from_push.outputs.pr_labels || join(github.event.pull_request.labels.*.name, ',') }}" | tee -a " $GITHUB_OUTPUT"
546548
547549 - if : contains(steps.PR.outputs.pr_labels, 'bench-after-CI')
548550 name : If `bench-after-CI` is present, add a `!bench` comment.
@@ -563,6 +565,74 @@ jobs:
563565 --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}'
564566
565567 - if : contains(steps.PR.outputs.pr_labels, 'auto-merge-after-CI')
568+ name : Get PR label timeline data
569+ # 'auto-merge-after-CI' must be within the last 100 labels added (could be increased to 250 if needed)
570+ # query from https://stackoverflow.com/a/67939355
571+ # unfortunately we cannot query only for 'auto-merge-after-CI' events
572+ # so we have to process this with jq in the next step
573+ id : get-timeline
574+ uses : octokit/graphql-action@8ad880e4d437783ea2ab17010324de1075228110 # v2.3.2
575+ with :
576+ query : |
577+ query($owner: String!, $name: String!, $number: Int!) {
578+ repository(owner: $owner, name: $name) {
579+ pullRequest(number: $number) {
580+ timelineItems(itemTypes: LABELED_EVENT, last: 100) {
581+ nodes {
582+ ... on LabeledEvent {
583+ createdAt
584+ actor { login }
585+ label { name }
586+ }
587+ }
588+ }
589+ }
590+ }
591+ }
592+ owner : leanprover-community
593+ name : mathlib4
594+ number : ${{ steps.PR.outputs.number }}
595+ env :
596+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
597+
598+ - if : contains(steps.PR.outputs.pr_labels, 'auto-merge-after-CI')
599+ name : Extract label actor username
600+ id : get-label-actor
601+ run : |
602+ # Parse the GraphQL response and filter for the specific label
603+ echo '${{ steps.get-timeline.outputs.data }}'
604+ USERNAME=$(echo '${{ steps.get-timeline.outputs.data }}' | jq -r '
605+ .repository.pullRequest.timelineItems.nodes
606+ | map(select(.label.name == "auto-merge-after-CI"))
607+ | sort_by(.createdAt)
608+ | last
609+ | .actor.login // empty
610+ ')
611+
612+ # Validate username format (GitHub usernames are alphanumeric + hyphens, max 39 chars)
613+ printf 'USERNAME: %s\n' "$USERNAME"
614+ if [[ -z "$USERNAME" ]]; then
615+ echo "Error: No actor found for the specified label"
616+ exit 1
617+ elif ! [[ "$USERNAME" =~ ^[a-zA-Z0-9-]{1,39}$ ]]; then
618+ echo "Error: Invalid username format: $USERNAME"
619+ exit 1
620+ fi
621+
622+ echo "username=$USERNAME" >> "$GITHUB_OUTPUT"
623+ echo "Found label actor: $USERNAME"
624+
625+ - if : contains(steps.PR.outputs.pr_labels, 'auto-merge-after-CI')
626+ name : check team membership
627+ uses : tspascoal/get-user-teams-membership@57e9f42acd78f4d0f496b3be4368fc5f62696662 # v3.0.0
628+ id : actorTeams
629+ with :
630+ organization : leanprover-community # optional. Default value ${{ github.repository_owner }}
631+ # Organization to get membership from.
632+ username : ${{ steps.get-label-actor.outputs.username }}
633+ GITHUB_TOKEN : ${{ secrets.MATHLIB_REVIEWERS_TEAM_KEY }} # (Requires scope: `read:org`)
634+
635+ - if : ${{ contains(steps.PR.outputs.pr_labels, 'auto-merge-after-CI') && (contains(steps.actorTeams.outputs.teams, 'mathlib-maintainers') || contains(steps.actorTeams.outputs.teams, 'bot-users')) }}
566636 name : If `auto-merge-after-CI` is present, add a `bors merge` comment.
567637 uses : GrantBirki/comment@608e41b19bc973020ec0e189ebfdae935d7fe0cc # v2.1.1
568638 with :
0 commit comments