| name: labels | |
| on: | |
| pull_request_target: | |
| types: [ opened, edited, synchronize, reopened, converted_to_draft, ready_for_review, closed ] | |
| issues: | |
| # Only the blocked job runs on these; the other jobs that run on | |
| # "reopened" check that a PR was reopened. | |
| types: [ closed, reopened ] | |
| schedule: | |
| - cron: '41 */3 * * *' | |
| workflow_dispatch: | |
| inputs: | |
| job: | |
| description: Job to run over every open PR | |
| type: choice | |
| default: all | |
| options: [ all, template, area, blocked, reviewed, conflict, inactive, size ] | |
| permissions: | |
| pull-requests: write | |
| jobs: | |
| open: | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event.action == 'opened' }} | |
| steps: | |
| - env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| ISSUE: ${{ github.event.pull_request.html_url }} | |
| LABEL: "${{ github.event.pull_request.draft && 'Status: Work in Progress' || 'Status: Code Review Needed' }}" | |
| run: | | |
| gh pr edit "$ISSUE" --add-label "$LABEL" | |
| template: | |
| runs-on: ubuntu-latest | |
| if: ${{ (github.event_name == 'workflow_dispatch' && (inputs.job == 'all' || inputs.job == 'template')) || github.event.action == 'opened' || (github.event.action == 'edited' && github.event.pull_request.state == 'open') }} | |
| concurrency: | |
| group: template-${{ github.event.pull_request.number || 'all' }} | |
| # An edit right after opening waits for the run that labels it. | |
| cancel-in-progress: false | |
| steps: | |
| - shell: bash | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| GH_REPO: ${{ github.repository }} | |
| PR: ${{ github.event.pull_request.number }} | |
| ACTION: ${{ github.event.action }} | |
| run: | | |
| # The labels of the boxes ticked in the "Types of Changes" and | |
| # "Priority" sections of the PR template, and the Type and active | |
| # Component labels of the issues the PR closes, which take the | |
| # place of the ticked boxes of their kind since a maintainer chose | |
| # them. A PR gets them when it is opened, and later only those of | |
| # a kind it has no label of yet, so a label set by hand is never | |
| # overridden. A priority needs exactly one box ticked. A manual | |
| # run labels every open PR. | |
| boxes='Bug fix|Type: Defect|ededed| | |
| New feature|Type: Feature|ededed| | |
| Performance enhancement|Type: Performance|ededed| | |
| Code cleanup|Type: Code Cleanup|ededed| | |
| Quality assurance|Type: QA|ededed| | |
| Breaking change|Type: Breaking Change|ededed| | |
| Library ABI change|Type: Library API Change|ededed| | |
| Documentation|Type: Documentation|ededed| | |
| Critical|Priority: Critical|b60205|Now: data loss or corruption, security, or the build or CI broken for every PR | |
| High|Priority: High|d93f0b|Next release: panic or hang in normal use, regression, failing tests, planned work | |
| Medium|Priority: Medium|fbca04|A later release: fixes for uncommon setups, features, performance, test and CI work | |
| Low|Priority: Low|c2e0c6|Whenever convenient: messages, documentation, cleanups, debug-only issues' | |
| # "Breaking change" and "Library ABI change" say what a change does | |
| # rather than what it is, which the type of an issue cannot tell: | |
| # each is a kind of its own, so other types do not replace it. | |
| effects='^Type: (Breaking Change|Library API Change)$' | |
| # The lines read that are labels of kind $1: those with that | |
| # prefix, or the label itself for an effect. | |
| of_kind() { | |
| sed -E -n "/$effects/{/^$1\$/p;d;}; /^$1: /p" | |
| } | |
| labels=$(gh label list --limit 1000 --json name --jq '.[].name') | |
| while IFS='|' read -r box name color desc; do | |
| if ! grep -qixF "$name" <<< "$labels"; then | |
| gh label create "$name" --color "$color" --description "$desc" | |
| fi | |
| done <<< "$boxes" | |
| fields=number,body,labels,closingIssuesReferences | |
| if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ]; then | |
| prs=$(gh pr list --state open --limit 1000 --json "$fields") | |
| else | |
| # Read now, not from the event, so that a label added since by | |
| # the run for an earlier event counts. | |
| prs=$(gh pr view "$PR" --json "$fields" --jq '[.]') | |
| fi | |
| jq -c '.[]' <<< "$prs" | while read -r p; do | |
| pr=$(jq -r .number <<< "$p") | |
| have= | |
| if [ "$ACTION" != opened ]; then | |
| have=$(jq -r '.labels[].name' <<< "$p") | |
| fi | |
| ticked=$(jq -r '.body // ""' <<< "$p" | tr -d '\r' | | |
| sed -n -E 's/^- \[[xX]\] ([^(]*[^ (]) \(.*/\1/p') | |
| picked=$(while IFS='|' read -r box name _; do | |
| if grep -qxF "$box" <<< "$ticked"; then | |
| echo "$name" | |
| fi | |
| done <<< "$boxes") | |
| # At most 10 issues are read; one that cannot be read is skipped. | |
| issues=$(jq -r --arg repo "$GH_REPO" '[.closingIssuesReferences[] | | |
| select(.repository.owner.login + "/" + .repository.name == | |
| $repo) | .number] | unique | .[:10][]' <<< "$p") | |
| linked=$(for i in $issues; do | |
| gh api "repos/$GH_REPO/issues/$i" --jq '.labels[] | | |
| select(.archived_at == null) | .name | | |
| select(startswith("Type: ") or startswith("Component: "))' \ | |
| < /dev/null || true | |
| done | sort -u) | |
| add=() | |
| for kind in Type Component Priority \ | |
| "Type: Breaking Change" "Type: Library API Change"; do | |
| new=$(of_kind "$kind" <<< "$linked") | |
| if [ -z "$new" ]; then | |
| new=$(of_kind "$kind" <<< "$picked") | |
| fi | |
| if [ -z "$new" ] || [ -n "$(of_kind "$kind" <<< "$have")" ]; then | |
| continue | |
| fi | |
| if [ "$kind" = Priority ] && [ "$(wc -l <<< "$new")" -ne 1 ]; then | |
| continue | |
| fi | |
| while read -r l; do | |
| add+=(-f "labels[]=$l") | |
| done <<< "$new" | |
| done | |
| if [ ${#add[@]} -ne 0 ]; then | |
| gh api --silent -X POST "repos/$GH_REPO/issues/$pr/labels" \ | |
| "${add[@]}" < /dev/null | |
| # GitHub allows 80 label changes a minute; leave room for | |
| # the other jobs doing the same. | |
| if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ]; then | |
| sleep 3 | |
| fi | |
| fi | |
| done | |
| push: | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event.action == 'synchronize' || (github.event.action == 'reopened' && github.event_name == 'pull_request_target') }} | |
| steps: | |
| - env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| ISSUE: ${{ github.event.pull_request.html_url }} | |
| run: | | |
| gh pr edit $ISSUE --remove-label "Status: Accepted,Status: Inactive,Status: Revision Needed,Status: Stale" | |
| # The author has pushed after changes were requested: back to review. | |
| - if: "${{ !github.event.pull_request.draft && contains(github.event.pull_request.labels.*.name, 'Status: Revision Needed') }}" | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| ISSUE: ${{ github.event.pull_request.html_url }} | |
| run: | | |
| gh pr edit "$ISSUE" --add-label "Status: Code Review Needed" | |
| area: | |
| runs-on: ubuntu-latest | |
| if: ${{ (github.event_name == 'workflow_dispatch' && (inputs.job == 'all' || inputs.job == 'area')) || github.event.action == 'opened' || github.event.action == 'synchronize' || (github.event.action == 'reopened' && github.event_name == 'pull_request_target') }} | |
| concurrency: | |
| group: area-${{ github.event.pull_request.number || 'all' }} | |
| cancel-in-progress: true | |
| steps: | |
| - shell: bash | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| GH_REPO: ${{ github.repository }} | |
| run: | | |
| # A PR that changes nothing but CI, tests, documentation or the | |
| # build and packaging gets the label of each of these it changes. | |
| # Labels are only added, so labels set by hand stay. A manual run | |
| # labels every open PR. A PR whose files cannot be listed, or that | |
| # cannot be labelled, waits for its next push or a manual run. | |
| labels=$(gh label list --limit 1000 --json name --jq '.[].name') | |
| for l in "Component: CI" "Component: Test Suite" \ | |
| "Type: Documentation" "Type: Building"; do | |
| if ! grep -qixF "$l" <<< "$labels"; then | |
| gh label create "$l" --color ededed | |
| fi | |
| done | |
| if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ]; then | |
| prs=$(gh pr list --state open --limit 1000 \ | |
| --json number,baseRefName,labels) | |
| else | |
| prs=$(jq '[.pull_request | .baseRefName = .base.ref]' \ | |
| "$GITHUB_EVENT_PATH") | |
| fi | |
| # git lists the files a PR changes since the merge base of its | |
| # head and its base branch, as GitHub does, but without the diff | |
| # service that GitHub's list needs, which fails under load. One | |
| # fetch reads the commits and trees of every PR, and no PR is | |
| # checked out or run. | |
| export GIT_DIR=$RUNNER_TEMP/prs.git | |
| git init -q --bare | |
| git remote add origin "$GITHUB_SERVER_URL/$GH_REPO" | |
| jq -r '([.[].baseRefName] | unique[] | | |
| "+refs/heads/\(.):refs/base/\(.)"), | |
| (.[].number | "+refs/pull/\(.)/head:refs/pr/\(.)")' <<< "$prs" | | |
| git fetch -q --no-tags --filter=blob:none --stdin origin | |
| jq -r '.[] | [.number, .baseRefName, | |
| ([.labels[].name] | join(","))] | @tsv' <<< "$prs" | | |
| while IFS=$'\t' read -r pr base have; do | |
| if ! kinds=$(git diff --name-only \ | |
| "refs/base/$base...refs/pr/$pr" < /dev/null | | |
| awk '/^\.github\// { print "Component: CI"; next } | |
| /^tests\// { print "Component: Test Suite"; next } | |
| /^man\// || /\.md$/ { print "Type: Documentation"; next } | |
| /^(config|contrib\/debian|rpm)\// || /(^|\/)Makefile\.am$/ || | |
| /^(META|autogen\.sh|configure\.ac|copy-builtin)$/ || | |
| /^(zfs\.release\.in|module\/(Kbuild\.in|Makefile\.(bsd|in)))$/ || | |
| /^scripts\/(dkms\.mkconf|dkms\.postbuild|kmodtool|make_gitrev\.sh)$/ { | |
| print "Type: Building"; next } | |
| { print "code" }' | sort -u); then | |
| echo "::warning::Could not list the files of PR #$pr" | |
| continue | |
| fi | |
| if [ -z "$kinds" ] || grep -qx code <<< "$kinds"; then | |
| continue | |
| fi | |
| while read -r l; do | |
| if ! tr ',' '\n' <<< "$have" | grep -qxF "$l"; then | |
| gh api --silent -X POST "repos/$GH_REPO/issues/$pr/labels" \ | |
| -f "labels[]=$l" < /dev/null || | |
| echo "::warning::Could not add \"$l\" to PR #$pr" | |
| # GitHub allows 80 label changes a minute; leave room for | |
| # another job doing the same. | |
| if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ]; then | |
| sleep 2 | |
| fi | |
| fi | |
| done <<< "$kinds" | |
| done | |
| blocked: | |
| runs-on: ubuntu-latest | |
| if: ${{ (github.event_name == 'workflow_dispatch' && (inputs.job == 'all' || inputs.job == 'blocked')) || github.event.action == 'opened' || (github.event.action == 'edited' && github.event.pull_request.state == 'open') || github.event.action == 'synchronize' || github.event.action == 'reopened' || github.event.action == 'closed' }} | |
| concurrency: | |
| # An edit or a push right after a reopening must not cancel the | |
| # update of the PRs that depend on it. | |
| group: blocked-${{ (github.event.action == 'closed' || github.event.action == 'reopened') && 'dependents-' || '' }}${{ github.event.pull_request.number || github.event.issue.number || 'all' }} | |
| cancel-in-progress: true | |
| steps: | |
| - shell: bash | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| GH_REPO: ${{ github.repository }} | |
| NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }} | |
| ACTION: ${{ github.event.action }} | |
| run: | | |
| # A PR with a line of its description that starts with "Depends | |
| # on" and one or more PRs or issues of this repository, as "#N", | |
| # "owner/repo#N" or their URL, is blocked while any of them is | |
| # open. Only such PRs get "Status: Blocked" or lose it, so a label | |
| # set by hand on any other PR stays, as does the label of a PR | |
| # whose line is deleted. Only the first 10 dependencies are read, | |
| # to bound the work of every event, and a PR that names more is | |
| # told so once, and a PR that names itself is not blocked by it. | |
| # When a PR or an issue is closed or reopened, the open PRs that | |
| # depend on it, and a reopened PR itself, are updated; a manual run | |
| # updates them all. | |
| label="Status: Blocked" | |
| marker='<!-- labels-yml:depends-cap -->' | |
| note="$marker | |
| This PR names more than 10 PRs or issues on its \"Depends on\" lines. | |
| Only the first 10 are checked for the \"Status: Blocked\" label." | |
| labels=$(gh label list --limit 1000 --json name --jq '.[].name') | |
| if ! grep -qixF "$label" <<< "$labels"; then | |
| gh label create "$label" --color e60000 \ | |
| --description "Depends on another pending change" | |
| fi | |
| sweep= | |
| if [ "$ACTION" = closed ] || [ "$ACTION" = reopened ]; then | |
| sweep=true | |
| fi | |
| if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ] || | |
| [ -n "$sweep" ]; then | |
| prs=$(gh pr list --state open --limit 1000 --json number,body,labels) | |
| else | |
| prs=$(gh pr view "$NUMBER" --json number,body,labels --jq '[.]') | |
| fi | |
| jq -c '.[]' <<< "$prs" | while read -r p; do | |
| pr=$(jq -r .number <<< "$p") | |
| refs=$(jq -r --arg repo "$GH_REPO" '.number as $self | | |
| "(\($repo)#|#|https://github\\.com/\($repo)/(pull|issues)/)[0-9]+" | |
| as $ref | "(\\s*,\\s*|\\s+)(and\\s+)?" as $sep | | |
| [(.body // "") | gsub("\r"; "") | split("\n")[] | | |
| capture("^depends on( pr| pull request)?:? " + | |
| "(?<refs>\($ref)(\($sep)\($ref))*)"; "i").refs | | |
| scan("(?:#|/(?:pull|issues)/)([0-9]+)")[0] | tonumber | | |
| select(. != $self)] | | |
| reduce .[] as $n ([]; if index([$n]) then . else . + [$n] end) | | |
| .[]' <<< "$p") | |
| deps=$(head -n 10 <<< "$refs") | |
| if [ -z "$deps" ] || { [ -n "$sweep" ] && [ "$pr" != "$NUMBER" ] && | |
| ! grep -qxF "$NUMBER" <<< "$deps"; }; then | |
| continue | |
| fi | |
| if [ "$(wc -l <<< "$refs")" -gt 10 ] && | |
| comments=$(gh api --paginate "repos/$GH_REPO/issues/$pr/comments?per_page=100" \ | |
| --jq '.[] | select(.user.login == "github-actions[bot]") | .body' \ | |
| < /dev/null) && | |
| ! grep -qF "$marker" <<< "$comments"; then | |
| gh api --silent -X POST "repos/$GH_REPO/issues/$pr/comments" \ | |
| -f body="$note" < /dev/null | |
| fi | |
| states=$(for n in $deps; do | |
| gh api "repos/$GH_REPO/issues/$n" --jq .state < /dev/null || | |
| echo unknown | |
| done) | |
| # A dependency that cannot be read leaves the PR as it is. | |
| if grep -qx unknown <<< "$states"; then | |
| continue | |
| fi | |
| blocked=$(grep -qx open <<< "$states" && echo true || echo false) | |
| has=$(jq --arg l "$label" 'any(.labels[]; .name == $l)' <<< "$p") | |
| if [ "$blocked" = "$has" ]; then | |
| continue | |
| elif [ "$blocked" = true ]; then | |
| gh api --silent -X POST "repos/$GH_REPO/issues/$pr/labels" \ | |
| -f "labels[]=$label" < /dev/null | |
| else | |
| # Another run may have removed it already. | |
| gh api --silent -X DELETE \ | |
| "repos/$GH_REPO/issues/$pr/labels/Status:%20Blocked" \ | |
| < /dev/null || true | |
| fi | |
| # GitHub allows 80 label changes a minute; leave room for the | |
| # other jobs doing the same. | |
| if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ] || | |
| [ -n "$sweep" ]; then | |
| sleep 3 | |
| fi | |
| done | |
| draft: | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event.action == 'converted_to_draft' }} | |
| steps: | |
| - env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| ISSUE: ${{ github.event.pull_request.html_url }} | |
| run: | | |
| gh pr edit $ISSUE --remove-label "Status: Accepted,Status: Code Review Needed,Status: Inactive,Status: Revision Needed,Status: Stale" --add-label "Status: Work in Progress" | |
| rfr: | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event.action == 'ready_for_review' }} | |
| steps: | |
| - env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| ISSUE: ${{ github.event.pull_request.html_url }} | |
| run: | | |
| gh pr edit $ISSUE --remove-label "Status: Accepted,Status: Inactive,Status: Revision Needed,Status: Stale,Status: Work in Progress" --add-label "Status: Code Review Needed" | |
| reviewed: | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && (inputs.job == 'all' || inputs.job == 'reviewed')) }} | |
| permissions: | |
| # actions: read finds when the last periodic run started. | |
| actions: read | |
| pull-requests: write | |
| concurrency: | |
| group: reviewed | |
| # A periodic run waits for a manual one rather than cancelling it. | |
| cancel-in-progress: false | |
| steps: | |
| - shell: bash | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| GH_REPO: ${{ github.repository }} | |
| run: | | |
| # An open PR gets "Status: Reviewed" once someone other than its | |
| # author, and not a bot, has submitted a review of it, whatever its | |
| # verdict, so that PRs reviewed by anyone stand out, and | |
| # "Status: Maintainer Reviewed" when one of them can push to the | |
| # repository, so that PRs a maintainer has reviewed do too. It gets | |
| # "Status: Revision Needed", unless it is a draft, when the latest | |
| # verdict of someone with write access asks for changes to its | |
| # head commit, which no push has replaced yet; a push removes it. | |
| # A review event cannot label a PR from a fork, hence a periodic | |
| # run. It reads only the reviews submitted since 10 minutes | |
| # before the last successful periodic run started, so that a | |
| # failed or skipped run misses none and a label removed by hand | |
| # comes back only with a new review (or once more, for a review | |
| # submitted just before a run); a manual run reads all of them. | |
| labels=$(gh label list --limit 1000 --json name --jq '.[].name') | |
| for l in "Status: Reviewed|0e8a16|Reviewed by someone other than its author" \ | |
| "Status: Revision Needed|e60000|Changes are required for the PR to be accepted" \ | |
| "Status: Maintainer Reviewed|0a5c2b|Reviewed by someone with push access other than its author"; do | |
| IFS='|' read -r name color desc <<< "$l" | |
| if ! grep -qixF "$name" <<< "$labels"; then | |
| gh label create "$name" --color "$color" --description "$desc" | |
| fi | |
| done | |
| since= | |
| if [ "$GITHUB_EVENT_NAME" = schedule ]; then | |
| since=$(gh api -X GET \ | |
| "repos/$GH_REPO/actions/workflows/labels.yml/runs" \ | |
| -f event=schedule -f status=success -f per_page=1 \ | |
| --jq '.workflow_runs[0].run_started_at // empty | | |
| fromdate - 600 | todate' < /dev/null) || since= | |
| # The last 4 hours when there is no such run or it cannot be read. | |
| if [ -z "$since" ]; then | |
| since=$(date -u -d '-4 hours' +%Y-%m-%dT%H:%M:%SZ) | |
| fi | |
| fi | |
| # The last 100 reviews of every open PR, and the latest verdict of | |
| # each reviewer with write access, 50 PRs a request; the names that | |
| # start with $ are GraphQL variables. | |
| # shellcheck disable=SC2016 | |
| gh api graphql --paginate -f owner="${GH_REPO%/*}" \ | |
| -f repo="${GH_REPO#*/}" -f query=' | |
| query($owner: String!, $repo: String!, $endCursor: String) { | |
| repository(owner: $owner, name: $repo) { | |
| pullRequests(states: OPEN, first: 50, after: $endCursor) { | |
| pageInfo { hasNextPage endCursor } | |
| nodes { | |
| number | |
| author { login } | |
| headRefOid | |
| isDraft | |
| labels(first: 100) { nodes { name } } | |
| reviews(last: 100) { | |
| nodes { | |
| author { __typename login } | |
| state | |
| submittedAt | |
| authorCanPushToRepository | |
| } | |
| } | |
| latestOpinionatedReviews(first: 100, writersOnly: true) { | |
| nodes { state submittedAt commit { oid } } | |
| } | |
| } | |
| } | |
| } | |
| }' --jq '.data.repository.pullRequests.nodes[]' | | |
| jq -r --arg since "$since" '.number as $n | .author.login as $a | | |
| .headRefOid as $head | [.labels.nodes[].name] as $have | | |
| (select(any(.reviews.nodes[]; .state != "PENDING" and | |
| .submittedAt > $since and .author.__typename != "Bot" and | |
| .author.login != $a)) | "Status: Reviewed"), | |
| (select(any(.reviews.nodes[]; .state != "PENDING" and | |
| .submittedAt > $since and .author.__typename != "Bot" and | |
| .author.login != $a and .authorCanPushToRepository)) | | |
| "Status: Maintainer Reviewed"), | |
| (select((.isDraft | not) and | |
| any(.latestOpinionatedReviews.nodes[]; | |
| .state == "CHANGES_REQUESTED" and .commit.oid == $head and | |
| .submittedAt > $since)) | "Status: Revision Needed") | | |
| select(. as $l | all($have[]; . != $l)) | "\($n)\t\(.)"' | | |
| while IFS=$'\t' read -r pr l; do | |
| gh api --silent -X POST "repos/$GH_REPO/issues/$pr/labels" \ | |
| -f "labels[]=$l" < /dev/null | |
| # GitHub allows 80 label changes a minute; leave room for the | |
| # other jobs doing the same. | |
| sleep 3 | |
| done | |
| conflict: | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && (inputs.job == 'all' || inputs.job == 'conflict')) || github.event.action == 'opened' || github.event.action == 'synchronize' || (github.event.action == 'reopened' && github.event_name == 'pull_request_target') }} | |
| concurrency: | |
| group: conflict-${{ github.event.pull_request.number || 'all' }} | |
| # A push cancels the check of the one before; a periodic run waits | |
| # for a manual one rather than cancelling it. | |
| cancel-in-progress: ${{ github.event_name == 'pull_request_target' }} | |
| steps: | |
| - shell: bash | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| GH_REPO: ${{ github.repository }} | |
| PR: ${{ github.event.pull_request.number }} | |
| run: | | |
| # "Status: Merge Conflict" while GitHub reports that a PR | |
| # conflicts with its base, removed once it can be merged again; | |
| # only this job sets it. GitHub works this out in the background: | |
| # a PR it reports as UNKNOWN is left as it is, and when a PR is | |
| # opened, pushed to or reopened it is asked again for up to a | |
| # minute. The periodic and manual runs check every open PR, as a | |
| # change to the base can make them conflict. A PR that cannot be | |
| # labelled waits for the next run. | |
| label="Status: Merge Conflict" | |
| labels=$(gh label list --limit 1000 --json name --jq '.[].name') | |
| if ! grep -qixF "$label" <<< "$labels"; then | |
| gh label create "$label" --color d93f0b \ | |
| --description "Conflicts with its base branch" | |
| fi | |
| # Prints the open PRs that the GraphQL query $1 reads, $2 a | |
| # request. A request that gets a server error or a body that is | |
| # not JSON, as GitHub sends under load, is made again for the | |
| # same PRs, half as many down to 10; any other failure, or one | |
| # at 10, fails. | |
| open_prs() { | |
| local first=$2 cursor page status | |
| while :; do | |
| # -i prints the status line and headers before the body. | |
| page=$(gh api -i graphql -F first="$first" \ | |
| ${cursor:+-f "endCursor=$cursor"} -f owner="${GH_REPO%/*}" \ | |
| -f repo="${GH_REPO#*/}" -f query="$1" < /dev/null) || true | |
| status=$(sed -n 's/^HTTP[^ ]* //p; q' <<< "$page") | |
| page=$(sed '1,/^\r$/d' <<< "$page") | |
| if [ "${status%% *}" = 200 ] && | |
| jq -e '.data and .errors == null' > /dev/null 2>&1 \ | |
| <<< "$page"; then | |
| jq -c '.data.repository.pullRequests.nodes[]' <<< "$page" | |
| cursor=$(jq -r '.data.repository.pullRequests.pageInfo | | |
| select(.hasNextPage) | .endCursor' <<< "$page") | |
| if [ -z "$cursor" ]; then | |
| return 0 | |
| fi | |
| elif [ "$first" -gt 10 ] && { [[ $status = 5* ]] || | |
| ! jq -e . > /dev/null 2>&1 <<< "$page"; }; then | |
| first=$((first / 2 > 10 ? first / 2 : 10)) | |
| echo "::warning::Listing PRs failed (${status:-no answer})," \ | |
| "retrying $first at a time" >&2 | |
| else | |
| echo "::error::Listing PRs failed (${status:-no answer})," \ | |
| "$first at a time" >&2 | |
| return 1 | |
| fi | |
| done | |
| } | |
| if [ -n "$PR" ]; then | |
| for _ in 1 2 3 4 5 6; do | |
| prs=$(gh pr view "$PR" --json number,mergeable,labels --jq '[.]') | |
| if [ "$(jq -r '.[].mergeable' <<< "$prs")" != UNKNOWN ]; then | |
| break | |
| fi | |
| sleep 10 | |
| done | |
| else | |
| # The names that start with $ are GraphQL variables. | |
| # shellcheck disable=SC2016 | |
| prs=$(open_prs ' | |
| query($owner: String!, $repo: String!, $first: Int!, | |
| $endCursor: String) { | |
| repository(owner: $owner, name: $repo) { | |
| pullRequests(states: OPEN, first: $first, | |
| after: $endCursor) { | |
| pageInfo { hasNextPage endCursor } | |
| nodes { | |
| number | |
| mergeable | |
| labels(first: 100) { nodes { name } } | |
| } | |
| } | |
| } | |
| }' 100 | jq -s 'map(.labels = .labels.nodes)') | |
| fi | |
| jq -r --arg l "$label" '.[] | | |
| [.number, .mergeable, any(.labels[]; .name == $l)] | @tsv' \ | |
| <<< "$prs" | | |
| while IFS=$'\t' read -r pr state has; do | |
| if [ "$state" = CONFLICTING ] && [ "$has" = false ]; then | |
| gh api --silent -X POST "repos/$GH_REPO/issues/$pr/labels" \ | |
| -f "labels[]=$label" < /dev/null || | |
| echo "::warning::Could not add \"$label\" to PR #$pr" | |
| elif [ "$state" = MERGEABLE ] && [ "$has" = true ]; then | |
| # Another run may have removed it already. | |
| if ! error=$(gh api --silent -X DELETE \ | |
| "repos/$GH_REPO/issues/$pr/labels/Status:%20Merge%20Conflict" \ | |
| 2>&1 < /dev/null) && ! grep -q 'HTTP 404' <<< "$error"; then | |
| echo "::warning::Could not remove \"$label\" from PR #$pr" | |
| fi | |
| else | |
| continue | |
| fi | |
| # GitHub allows 80 label changes a minute; leave room for the | |
| # other jobs doing the same. | |
| if [ -z "$PR" ]; then | |
| sleep 3 | |
| fi | |
| done | |
| inactive: | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && (inputs.job == 'all' || inputs.job == 'inactive')) }} | |
| concurrency: | |
| group: inactive | |
| # A periodic run waits for a manual one rather than cancelling it. | |
| cancel-in-progress: false | |
| steps: | |
| - shell: bash | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| GH_REPO: ${{ github.repository }} | |
| run: | | |
| # "Status: Inactive" on an open PR, draft or not, with no activity | |
| # by anyone but a bot for 90 days: no commit or force-push, comment, | |
| # review or reopening, and no change between draft and ready for | |
| # review, so that the events that remove the label count too. The | |
| # PR's updatedAt would not do, as label changes move it. A commit | |
| # counts from its commit date, the date of a push being unknown. | |
| # The label is removed once there is activity again, but only from | |
| # a PR it was added to by a bot, so one added by hand stays; nor is | |
| # it added again after someone removed it by hand, until there is | |
| # new activity. A PR that cannot be labelled waits for the next | |
| # run. The last 50 of these events and label changes are read for | |
| # every open PR, 50 PRs a request, or fewer when GitHub is under | |
| # load; the names that start with $ are GraphQL variables. | |
| label="Status: Inactive" | |
| labels=$(gh label list --limit 1000 --json name --jq '.[].name') | |
| if ! grep -qixF "$label" <<< "$labels"; then | |
| gh label create "$label" --color cfd3d7 \ | |
| --description "Not being actively updated" | |
| fi | |
| # Prints the open PRs that the GraphQL query $1 reads, $2 a | |
| # request. A request that gets a server error or a body that is | |
| # not JSON, as GitHub sends under load, is made again for the | |
| # same PRs, half as many down to 10; any other failure, or one | |
| # at 10, fails. | |
| open_prs() { | |
| local first=$2 cursor page status | |
| while :; do | |
| # -i prints the status line and headers before the body. | |
| page=$(gh api -i graphql -F first="$first" \ | |
| ${cursor:+-f "endCursor=$cursor"} -f owner="${GH_REPO%/*}" \ | |
| -f repo="${GH_REPO#*/}" -f query="$1" < /dev/null) || true | |
| status=$(sed -n 's/^HTTP[^ ]* //p; q' <<< "$page") | |
| page=$(sed '1,/^\r$/d' <<< "$page") | |
| if [ "${status%% *}" = 200 ] && | |
| jq -e '.data and .errors == null' > /dev/null 2>&1 \ | |
| <<< "$page"; then | |
| jq -c '.data.repository.pullRequests.nodes[]' <<< "$page" | |
| cursor=$(jq -r '.data.repository.pullRequests.pageInfo | | |
| select(.hasNextPage) | .endCursor' <<< "$page") | |
| if [ -z "$cursor" ]; then | |
| return 0 | |
| fi | |
| elif [ "$first" -gt 10 ] && { [[ $status = 5* ]] || | |
| ! jq -e . > /dev/null 2>&1 <<< "$page"; }; then | |
| first=$((first / 2 > 10 ? first / 2 : 10)) | |
| echo "::warning::Listing PRs failed (${status:-no answer})," \ | |
| "retrying $first at a time" >&2 | |
| else | |
| echo "::error::Listing PRs failed (${status:-no answer})," \ | |
| "$first at a time" >&2 | |
| return 1 | |
| fi | |
| done | |
| } | |
| cutoff=$(date -u -d '-90 days' +%Y-%m-%dT%H:%M:%SZ) | |
| # shellcheck disable=SC2016 | |
| open_prs ' | |
| query($owner: String!, $repo: String!, $first: Int!, | |
| $endCursor: String) { | |
| repository(owner: $owner, name: $repo) { | |
| pullRequests(states: OPEN, first: $first, after: $endCursor) { | |
| pageInfo { hasNextPage endCursor } | |
| nodes { | |
| number | |
| createdAt | |
| labels(first: 100) { nodes { name } } | |
| timelineItems(last: 50, itemTypes: [PULL_REQUEST_COMMIT, | |
| HEAD_REF_FORCE_PUSHED_EVENT, ISSUE_COMMENT, | |
| PULL_REQUEST_REVIEW, REOPENED_EVENT, | |
| READY_FOR_REVIEW_EVENT, CONVERT_TO_DRAFT_EVENT, | |
| LABELED_EVENT, UNLABELED_EVENT]) { | |
| nodes { | |
| __typename | |
| ... on PullRequestCommit { commit { committedDate } } | |
| ... on HeadRefForcePushedEvent { | |
| createdAt actor { __typename } | |
| } | |
| ... on IssueComment { createdAt author { __typename } } | |
| ... on PullRequestReview { | |
| submittedAt author { __typename } | |
| } | |
| ... on ReopenedEvent { createdAt actor { __typename } } | |
| ... on ReadyForReviewEvent { | |
| createdAt actor { __typename } | |
| } | |
| ... on ConvertToDraftEvent { | |
| createdAt actor { __typename } | |
| } | |
| ... on LabeledEvent { | |
| createdAt actor { __typename } label { name } | |
| } | |
| ... on UnlabeledEvent { | |
| createdAt actor { __typename } label { name } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| }' 50 | | |
| jq -r --arg l "$label" --arg cutoff "$cutoff" '.number as $n | | |
| [.timelineItems.nodes[] | {type: .__typename, | |
| at: (.createdAt // .submittedAt // .commit.committedDate), | |
| bot: ((.author // .actor).__typename == "Bot"), | |
| label: .label.name} | select(.at != null)] as $t | | |
| ([.createdAt] + [$t[] | select(.label == null and (.bot | not)) | | |
| .at] | max) as $last | | |
| ([$t[] | select(.label == $l)] | last) as $mark | | |
| if any(.labels.nodes[]; .name == $l) then | |
| select($mark.type == "LabeledEvent" and $mark.bot and | |
| $last > $mark.at) | "\($n)\tDELETE" | |
| else | |
| select($last < $cutoff and | |
| all($t[]; .label != $l or .bot or .at < $last)) | | |
| "\($n)\tPOST" | |
| end' | | |
| while IFS=$'\t' read -r pr method; do | |
| if [ "$method" = POST ]; then | |
| gh api --silent -X POST "repos/$GH_REPO/issues/$pr/labels" \ | |
| -f "labels[]=$label" < /dev/null || | |
| echo "::warning::Could not add \"$label\" to PR #$pr" | |
| else | |
| # Another run may have removed it already. | |
| if ! error=$(gh api --silent -X DELETE \ | |
| "repos/$GH_REPO/issues/$pr/labels/Status:%20Inactive" \ | |
| 2>&1 < /dev/null) && ! grep -q 'HTTP 404' <<< "$error"; then | |
| echo "::warning::Could not remove \"$label\" from PR #$pr" | |
| fi | |
| fi | |
| # GitHub allows 80 label changes a minute; leave room for the | |
| # other jobs doing the same. | |
| sleep 3 | |
| done | |
| size: | |
| runs-on: ubuntu-latest | |
| if: ${{ (github.event_name == 'workflow_dispatch' && (inputs.job == 'all' || inputs.job == 'size')) || github.event.action == 'opened' || github.event.action == 'synchronize' || (github.event.action == 'reopened' && github.event_name == 'pull_request_target') }} | |
| concurrency: | |
| group: size-${{ github.event.pull_request.number || 'all' }} | |
| cancel-in-progress: true | |
| steps: | |
| - shell: bash | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| GH_REPO: ${{ github.repository }} | |
| run: | | |
| # OpenZFS PR size classes, from the lines added plus removed as | |
| # the PR page shows them. A manual run labels every open PR, | |
| # and can be repeated. | |
| labels=$(gh label list --limit 1000 --json name --jq '.[].name') | |
| for l in "Size: XS|d4e9f7|Fewer than 10 lines changed" \ | |
| "Size: S|c5def5|10 to 29 lines changed" \ | |
| "Size: M|9cc8f0|30 to 99 lines changed" \ | |
| "Size: L|5a9be0|100 to 499 lines changed" \ | |
| "Size: XL|1d76db|500 to 999 lines changed" \ | |
| "Size: XXL|0b4ea2|1000 or more lines changed"; do | |
| IFS='|' read -r name color desc <<< "$l" | |
| if ! grep -qxF "$name" <<< "$labels"; then | |
| gh label create "$name" --color "$color" --description "$desc" | |
| fi | |
| done | |
| if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ]; then | |
| prs=$(gh pr list --state open --limit 1000 \ | |
| --json number,additions,deletions,labels) | |
| else | |
| prs=$(jq '[.pull_request]' "$GITHUB_EVENT_PATH") | |
| fi | |
| jq -r '.[] | [.number, .additions + .deletions, | |
| ([.labels[].name | select(startswith("Size: "))] | join(","))] | |
| | @tsv' <<< "$prs" | | |
| while IFS=$'\t' read -r pr lines old; do | |
| if [ "$lines" -lt 10 ]; then | |
| new="Size: XS" | |
| elif [ "$lines" -lt 30 ]; then | |
| new="Size: S" | |
| elif [ "$lines" -lt 100 ]; then | |
| new="Size: M" | |
| elif [ "$lines" -lt 500 ]; then | |
| new="Size: L" | |
| elif [ "$lines" -lt 1000 ]; then | |
| new="Size: XL" | |
| else | |
| new="Size: XXL" | |
| fi | |
| [ "$old" = "$new" ] && continue | |
| gh api --silent -X POST "repos/$GH_REPO/issues/$pr/labels" \ | |
| -f "labels[]=$new" < /dev/null | |
| tr ',' '\n' <<< "$old" | while read -r l; do | |
| [ -z "$l" ] || [ "$l" = "$new" ] || gh api --silent -X DELETE \ | |
| "repos/$GH_REPO/issues/$pr/labels/$(jq -rn --arg l "$l" '$l | @uri')" | |
| done | |
| # GitHub allows 80 label changes a minute; leave room for | |
| # another job doing the same. | |
| if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ]; then | |
| sleep 2 | |
| fi | |
| done |