name: README contributors # Keeps the contributors' avatars in README.md in step with GitHub's contributors list. When the # list changes, it opens or updates a pull request from the readme-contributors branch; it never # pushes to main. The logic and its tests are in .github/scripts/readme-contributors.mjs. on: schedule: - cron: "41 6 * * 1" workflow_dispatch: permissions: {} concurrency: group: readme-contributors cancel-in-progress: false jobs: check: name: Compare the contributors runs-on: ubuntu-latest timeout-minutes: 5 permissions: contents: read pull-requests: write outputs: changed: ${{ steps.compare.outputs.result }} steps: - uses: actions/checkout@v7 with: ref: ${{ github.event.repository.default_branch }} sparse-checkout: .github/scripts persist-credentials: false - id: compare uses: actions/github-script@v9 with: result-encoding: string script: | const { updateReadme } = await import(`${process.env.GITHUB_WORKSPACE}/.github/scripts/readme-contributors.mjs`); return String(await updateReadme({ github, context, core })); - name: Close a pull request the README no longer needs if: steps.compare.outputs.result == 'false' env: GH_TOKEN: ${{ github.token }} BASE: ${{ github.event.repository.default_branch }} BRANCH: readme-contributors run: | # Only pull requests this workflow opened carry the marker; --head also matches forks. gh pr list --repo "$GITHUB_REPOSITORY" --head "$BRANCH" --base "$BASE" --state open \ --json number,headRefOid,isCrossRepository,body --jq '.[] | select((.isCrossRepository | not) and (.body // "" | contains(""))) | "\(.number) \(.headRefOid)"' \ > "$RUNNER_TEMP/prs" # The pull-request job tells these closures from a maintainer's by the marker. note=$'\n' note+="The README already shows GitHub's contributors list, so this pull request is no longer needed." while read -r -u 3 number head; do # Commits someone else wrote or committed, as in the pull-request job. others=$(gh api "repos/$GITHUB_REPOSITORY/compare/$BASE...$head" --jq '[.commits[] | select([.commit.author.email, .commit.committer.email] | any(. != "41898282+github-actions[bot]@users.noreply.github.com"))] | length') if [[ "$others" != 0 ]]; then echo "::warning::#$number has commits this workflow didn't make; leaving it open." continue fi # Narrows the window for a push made since the check; gh pr close can't require a head. current=$(gh pr view "$number" --repo "$GITHUB_REPOSITORY" --json headRefOid --jq .headRefOid) if [[ "$current" != "$head" ]]; then echo "::warning::#$number changed during the check; leaving it open." continue fi gh pr close "$number" --repo "$GITHUB_REPOSITORY" \ --comment "$note" done 3< "$RUNNER_TEMP/prs" pull-request: name: Open the pull request needs: check if: >- needs.check.outputs.changed == 'true' && vars.RELEASE_APP_CLIENT_ID != '' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch) # The release app's key lives in this environment. A pull request opened with GITHUB_TOKEN # starts no workflows, so its required checks would never run. environment: release runs-on: ubuntu-latest timeout-minutes: 5 permissions: contents: read steps: - name: Create release app token id: app-token uses: actions/create-github-app-token@v3 with: client-id: ${{ vars.RELEASE_APP_CLIENT_ID }} private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }} permission-contents: write permission-pull-requests: write - uses: actions/checkout@v7 with: ref: ${{ github.event.repository.default_branch }} sparse-checkout: .github/scripts persist-credentials: false - uses: actions/github-script@v9 with: script: | const { updateReadme } = await import(`${process.env.GITHUB_WORKSPACE}/.github/scripts/readme-contributors.mjs`); await updateReadme({ github, context, core }); - name: Push the branch and open its pull request env: GH_TOKEN: ${{ steps.app-token.outputs.token }} BASE: ${{ github.event.repository.default_branch }} BRANCH: readme-contributors TITLE: "docs: update the contributors' avatars in the README" run: | if git diff --quiet -- README.md; then echo "$BASE caught up since the comparison." exit 0 fi # Counts this repository's open pull requests from the branch, and those this workflow opened, # which carry the marker; --head also matches forks. read -r ours all < <(gh pr list --repo "$GITHUB_REPOSITORY" --head "$BRANCH" --state open \ --json isCrossRepository,baseRefName,body --jq '[.[] | select(.isCrossRepository | not)] | "\(map(select(.baseRefName == env.BASE and (.body // "" | contains("")))) | length) \(length)"') if (( all > ours )); then echo "::error::A pull request this workflow didn't open uses the $BRANCH branch; leaving it alone." exit 1 fi remote="" files="" if git fetch --depth 1 origin "refs/heads/$BRANCH" 2>/dev/null; then remote=$(git rev-parse FETCH_HEAD) # The files the branch changes from the base, then how many of its commits someone else # wrote or committed: an amended commit keeps its author but not its committer. compare=$(gh api "repos/$GITHUB_REPOSITORY/compare/$BASE...$remote" --jq '([.files[].filename] | join(" ")), ([.commits[] | select([.commit.author.email, .commit.committer.email] | any(. != "41898282+github-actions[bot]@users.noreply.github.com"))] | length)') files=$(sed -n 1p <<< "$compare") others=$(sed -n 2p <<< "$compare") if [[ "$others" != 0 ]]; then echo "::error::$BRANCH has commits this workflow didn't make; leaving it alone." exit 1 fi fi # A branch whose whole change from the base is this README keeps its commit and its checks. # Anything else is rebuilt, and the lease keeps a push made meanwhile. kept="" if [[ -n "$remote" && "$files" == README.md ]] && git diff --quiet FETCH_HEAD -- README.md; then echo "$BRANCH already has this list." kept=1 else git add README.md git -c user.name='github-actions[bot]' \ -c user.email='41898282+github-actions[bot]@users.noreply.github.com' commit -m "$TITLE" git push --force-with-lease="refs/heads/$BRANCH:$remote" \ "https://x-access-token:$GH_TOKEN@github.com/$GITHUB_REPOSITORY.git" "HEAD:refs/heads/$BRANCH" fi # A maintainer who closed the pull request for this very commit said no to it; a new list # makes a new commit and asks again. The check job's own closures carry a marker. vetoed=0 if [[ -n "$kept" ]]; then # An assignment, so a failed lookup stops the job instead of reading as a veto. vetoed=$(REMOTE="$remote" gh pr list --repo "$GITHUB_REPOSITORY" --head "$BRANCH" \ --base "$BASE" --state closed --json headRefOid,isCrossRepository,body,comments \ --jq 'map(select((.isCrossRepository | not) and .headRefOid == env.REMOTE and (.body // "" | contains("")) and (any(.comments[]; .body | contains("")) | not))) | length') fi if [[ "$vetoed" != 0 ]]; then echo "The pull request for this list was closed; not opening it again." elif (( ours == 0 )); then gh pr create --repo "$GITHUB_REPOSITORY" --base "$BASE" --head "$BRANCH" --title "$TITLE" --body-file - <<'EOF' ## Problem GitHub lists contributors that the avatars in the README don't show yet, or no longer lists some it shows. ## Changes Rewrites the avatars between the `contributors` markers in README.md from GitHub's contributors list: most contributions first, without bots, with second accounts counted under their first (`.github/scripts/readme-contributors.mjs`). ## Testing Opened by the README contributors workflow (`.github/workflows/readme-contributors.yml`), whose logic `node --test .github/scripts/*.test.mjs` covers. No agent EOF fi