Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New GitHub workflow to auto-update list of contributors #6

Merged
merged 7 commits into from
Nov 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .github/workflows/auto-update-list-contributors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# GitHub Actions documentation:
# https://docs.github.com/en/actions

name: Auto-update list of contributors

on:
workflow_call:

# https://github.com/akhilmhdh/contributors-readme-action#optional-parameters
inputs:
use_username:
description: If people should be listed with their username instead of their full name.
default: true
required: false
type: boolean

jobs:
update_contributors:
runs-on: ubuntu-latest
timeout-minutes: 20
steps:

# https://github.com/akhilmhdh/contributors-readme-action
- name: Update the list of contributors in the README file
uses: akhilmhdh/[email protected]
id: update_contributors
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
commit_message: 'chore: auto-update contributors list (GH Action)'
committer_username: 'akhilmhdh/contributors-readme-action'
pr_title_on_protected: Auto-update list of contributors
use_username: ${{ inputs.use_username }}

# The previous step might have created a PR to make the update.
# If that's the case, we want to close any previous similar PR, to avoid duplication.
- name: 'Close previous pull request (if it exists)'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
request="$( \
gh api search/issues \
--method GET \
--raw-field q='\
repo:${{ github.repository }} \
type:pr \
state:open \
sort:updated-asc \
author:app/github-actions \
"Auto-update list of contributors" in:title\
' \
)"

count_pr=$( jq '.total_count' <<< $request )

# We are supposed to have either 1 or 2 PRs:
# - One PR: 1 created by the previous step + no existing PR
# - Two PRs: 1 created by the previous step + 1 existing PR
if [ $count_pr -eq 2 ]; then

# PRs are sorted by least recently updated date => the first is the oldest.
id_pr_oldest=$( jq '.items[0].number' <<< $request )

# Comment the oldest PR with a link to the new PR.
gh pr comment $id_pr_oldest \
--repo ${{ github.repository }} \
--body "Replaced by #${{ steps.update_contributors.outputs.pr_id }} ."

# And finally, close the PR.
gh pr close $id_pr_oldest \
--repo ${{ github.repository }} \
--delete-branch

fi