From 89ac8c9b06bc9a3043b71fc42206d02b3d4a8191 Mon Sep 17 00:00:00 2001 From: MrChocolatine <47531779+MrChocolatine@users.noreply.github.com> Date: Thu, 4 Nov 2021 18:19:59 +0000 Subject: [PATCH 1/7] add wf to auto-update contributors list in README --- .../auto-update-contributors-readme.yml | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 .github/workflows/auto-update-contributors-readme.yml diff --git a/.github/workflows/auto-update-contributors-readme.yml b/.github/workflows/auto-update-contributors-readme.yml new file mode 100644 index 0000000..ccbf12a --- /dev/null +++ b/.github/workflows/auto-update-contributors-readme.yml @@ -0,0 +1,87 @@ +# GitHub Actions documentation: +# https://docs.github.com/en/actions + +name: Auto-update list of contributors + +on: + workflow_call: + inputs: + is_protected: + description: Indicate if the branch to be updated is protected. + default: true + required: false + type: boolean + +jobs: + update_contributors_list: + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + + - name: Check if a pull request already exists + if: ${{ inputs.is_protected == true }} + id: find_existing_pr + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + ID_PR="$( \ + gh api search/issues \ + --method GET \ + --raw-field q='repo:${{ github.repository }} type:pr state:open "Auto-update list of contributors" in:title' \ + --jq '.items.[0].number' \ + )" + + echo "::set-output name=id_pr_to_close::$ID_PR" + + - name: Close the existing pull request + if: ${{ steps.find_existing_pr.outputs.id_pr_to_close }} + id: close_existing_pr + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh pr close ${{ steps.find_existing_pr.outputs.id_pr_to_close }} \ + --repo ${{ github.repository }} \ + --delete-branch + + # https://github.com/akhilmhdh/contributors-readme-action + - name: Update the list of contributors in the README file + uses: akhilmhdh/contributors-readme-action@v2.3 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + commit_message: 'chore: auto-update contributors list (GH Action)' + committer_username: 'akhilmhdh/contributors-readme-action' + is_protected: ${{ inputs.is_protected }} + use_username: true + + # The Action `akhilmhdh/contributors-readme-action` creates PRs with a fixed title: + # https://github.com/akhilmhdh/contributors-readme-action/blob/v2.3/src/index.js#L169 + - name: Rename the new PR with a better title + if: ${{ inputs.is_protected == true }} + id: rename_new_pr + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + sleep 10 # Some delay is needed to make sure GitHub had time to create the new PR + + ID_PR="$( \ + gh api search/issues \ + --method GET \ + --raw-field q='repo:${{ github.repository }} type:pr state:open "contributors readme action update" in:title' \ + --jq '.items.[0].number' \ + )" + + gh pr edit $ID_PR \ + --repo ${{ github.repository }} \ + --title "Auto-update list of contributors" + + echo "::set-output name=id_new_pr::$ID_PR" + + - name: Comment the previous PR with reference to the new PR + if: ${{ steps.close_existing_pr.outcome == 'success' }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh pr comment ${{ steps.find_existing_pr.outputs.id_pr_to_close }} \ + --repo ${{ github.repository }} \ + --body "This PR has been replaced by #${{ steps.rename_new_pr.outputs.id_new_pr }} ." From 2feab9d466f740a72ba7c9a85782dab2a1c88020 Mon Sep 17 00:00:00 2001 From: MrChocolatine <47531779+MrChocolatine@users.noreply.github.com> Date: Fri, 5 Nov 2021 15:37:36 +0000 Subject: [PATCH 2/7] auto-detect if default branch is protected --- .../auto-update-contributors-readme.yml | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/.github/workflows/auto-update-contributors-readme.yml b/.github/workflows/auto-update-contributors-readme.yml index ccbf12a..cf3c8a5 100644 --- a/.github/workflows/auto-update-contributors-readme.yml +++ b/.github/workflows/auto-update-contributors-readme.yml @@ -5,12 +5,6 @@ name: Auto-update list of contributors on: workflow_call: - inputs: - is_protected: - description: Indicate if the branch to be updated is protected. - default: true - required: false - type: boolean jobs: update_contributors_list: @@ -18,8 +12,26 @@ jobs: timeout-minutes: 20 steps: + - name: Check if the default branch is protected + id: default_branch + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + defaultBranch="$( \ + gh api /repos/${{ github.repository }} \ + --method GET \ + --jq '.default_branch' + )" + + isProtected="$( \ + gh api /repos/${{ github.repository }}/branches/$defaultBranch \ + --jq '.protected' + )" + + echo "::set-output name=is_protected::$isProtected" + - name: Check if a pull request already exists - if: ${{ inputs.is_protected == true }} + if: ${{ steps.default_branch.outputs.is_protected == 'true' }} id: find_existing_pr env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -51,13 +63,13 @@ jobs: with: commit_message: 'chore: auto-update contributors list (GH Action)' committer_username: 'akhilmhdh/contributors-readme-action' - is_protected: ${{ inputs.is_protected }} + is_protected: ${{ steps.default_branch.outputs.is_protected == 'true' }} use_username: true # The Action `akhilmhdh/contributors-readme-action` creates PRs with a fixed title: # https://github.com/akhilmhdh/contributors-readme-action/blob/v2.3/src/index.js#L169 - name: Rename the new PR with a better title - if: ${{ inputs.is_protected == true }} + if: ${{ steps.default_branch.outputs.is_protected == 'true' }} id: rename_new_pr env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From d30bd5c5211a51b802728bff536f084905cad8dc Mon Sep 17 00:00:00 2001 From: MrChocolatine <47531779+MrChocolatine@users.noreply.github.com> Date: Mon, 8 Nov 2021 16:41:21 +0000 Subject: [PATCH 3/7] over simplify logic Thanks to the new version `akhilmhdh/contributors-readme-action@v2.3.2`. --- .../auto-update-contributors-readme.yml | 94 ++++++------------- 1 file changed, 27 insertions(+), 67 deletions(-) diff --git a/.github/workflows/auto-update-contributors-readme.yml b/.github/workflows/auto-update-contributors-readme.yml index cf3c8a5..9950feb 100644 --- a/.github/workflows/auto-update-contributors-readme.yml +++ b/.github/workflows/auto-update-contributors-readme.yml @@ -12,88 +12,48 @@ jobs: timeout-minutes: 20 steps: - - name: Check if the default branch is protected - id: default_branch - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - defaultBranch="$( \ - gh api /repos/${{ github.repository }} \ - --method GET \ - --jq '.default_branch' - )" - - isProtected="$( \ - gh api /repos/${{ github.repository }}/branches/$defaultBranch \ - --jq '.protected' - )" - - echo "::set-output name=is_protected::$isProtected" - - - name: Check if a pull request already exists - if: ${{ steps.default_branch.outputs.is_protected == 'true' }} - id: find_existing_pr - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - ID_PR="$( \ - gh api search/issues \ - --method GET \ - --raw-field q='repo:${{ github.repository }} type:pr state:open "Auto-update list of contributors" in:title' \ - --jq '.items.[0].number' \ - )" - - echo "::set-output name=id_pr_to_close::$ID_PR" - - - name: Close the existing pull request - if: ${{ steps.find_existing_pr.outputs.id_pr_to_close }} - id: close_existing_pr - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - gh pr close ${{ steps.find_existing_pr.outputs.id_pr_to_close }} \ - --repo ${{ github.repository }} \ - --delete-branch - # https://github.com/akhilmhdh/contributors-readme-action - name: Update the list of contributors in the README file - uses: akhilmhdh/contributors-readme-action@v2.3 + uses: akhilmhdh/contributors-readme-action@v2.3.2 + 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' - is_protected: ${{ steps.default_branch.outputs.is_protected == 'true' }} + pr_title_on_protected: Auto-update list of contributors use_username: true - # The Action `akhilmhdh/contributors-readme-action` creates PRs with a fixed title: - # https://github.com/akhilmhdh/contributors-readme-action/blob/v2.3/src/index.js#L169 - - name: Rename the new PR with a better title - if: ${{ steps.default_branch.outputs.is_protected == 'true' }} - id: rename_new_pr + # 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: | - sleep 10 # Some delay is needed to make sure GitHub had time to create the new PR - - ID_PR="$( \ + request="$( \ gh api search/issues \ --method GET \ - --raw-field q='repo:${{ github.repository }} type:pr state:open "contributors readme action update" in:title' \ - --jq '.items.[0].number' \ + --raw-field q='repo:${{ github.repository }} type:pr state:open sort:updated-asc "Auto-update list of contributors" in:title' \ )" - gh pr edit $ID_PR \ - --repo ${{ github.repository }} \ - --title "Auto-update list of contributors" + count_pr=$( jq '.total_count' <<< $request ) - echo "::set-output name=id_new_pr::$ID_PR" + # 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 - - name: Comment the previous PR with reference to the new PR - if: ${{ steps.close_existing_pr.outcome == 'success' }} - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - gh pr comment ${{ steps.find_existing_pr.outputs.id_pr_to_close }} \ - --repo ${{ github.repository }} \ - --body "This PR has been replaced by #${{ steps.rename_new_pr.outputs.id_new_pr }} ." + # 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 From fb0911604538f8d076de677df864daa63ad617d6 Mon Sep 17 00:00:00 2001 From: MrChocolatine <47531779+MrChocolatine@users.noreply.github.com> Date: Mon, 8 Nov 2021 20:06:38 +0000 Subject: [PATCH 4/7] accept input `use_username` --- .github/workflows/auto-update-contributors-readme.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/auto-update-contributors-readme.yml b/.github/workflows/auto-update-contributors-readme.yml index 9950feb..e897125 100644 --- a/.github/workflows/auto-update-contributors-readme.yml +++ b/.github/workflows/auto-update-contributors-readme.yml @@ -6,6 +6,14 @@ 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_list: runs-on: ubuntu-latest @@ -22,7 +30,7 @@ jobs: 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: true + 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. From 2ef95ff0e3b97bc3b2f06c9064db59f2324b991a Mon Sep 17 00:00:00 2001 From: MrChocolatine <47531779+MrChocolatine@users.noreply.github.com> Date: Tue, 9 Nov 2021 09:00:35 +0000 Subject: [PATCH 5/7] filter PR on author to make query more precise --- .github/workflows/auto-update-contributors-readme.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/auto-update-contributors-readme.yml b/.github/workflows/auto-update-contributors-readme.yml index e897125..26c3380 100644 --- a/.github/workflows/auto-update-contributors-readme.yml +++ b/.github/workflows/auto-update-contributors-readme.yml @@ -41,7 +41,7 @@ jobs: request="$( \ gh api search/issues \ --method GET \ - --raw-field q='repo:${{ github.repository }} type:pr state:open sort:updated-asc "Auto-update list of contributors" in:title' \ + --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 ) From 999b062e734ef61c84be9e1421c95cfd707a8216 Mon Sep 17 00:00:00 2001 From: MrChocolatine <47531779+MrChocolatine@users.noreply.github.com> Date: Tue, 9 Nov 2021 09:23:22 +0000 Subject: [PATCH 6/7] split query over multiple lines --- .github/workflows/auto-update-contributors-readme.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/auto-update-contributors-readme.yml b/.github/workflows/auto-update-contributors-readme.yml index 26c3380..ab9af72 100644 --- a/.github/workflows/auto-update-contributors-readme.yml +++ b/.github/workflows/auto-update-contributors-readme.yml @@ -41,7 +41,14 @@ jobs: 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' \ + --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 ) From 365bbe22e1d4fc512732809eb1f5a7fcc306dfc8 Mon Sep 17 00:00:00 2001 From: MrChocolatine <47531779+MrChocolatine@users.noreply.github.com> Date: Tue, 9 Nov 2021 10:25:40 +0000 Subject: [PATCH 7/7] rename workflow file Mentioning `readme` in the filename does not really make since we can make the GitHub Action updates another file, see: https://github.com/akhilmhdh/contributors-readme-action/blob/v2.3.2/.github/workflows/main.yml#L16-L17 --- ...ontributors-readme.yml => auto-update-list-contributors.yml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename .github/workflows/{auto-update-contributors-readme.yml => auto-update-list-contributors.yml} (98%) diff --git a/.github/workflows/auto-update-contributors-readme.yml b/.github/workflows/auto-update-list-contributors.yml similarity index 98% rename from .github/workflows/auto-update-contributors-readme.yml rename to .github/workflows/auto-update-list-contributors.yml index ab9af72..9d1833b 100644 --- a/.github/workflows/auto-update-contributors-readme.yml +++ b/.github/workflows/auto-update-list-contributors.yml @@ -15,7 +15,7 @@ on: type: boolean jobs: - update_contributors_list: + update_contributors: runs-on: ubuntu-latest timeout-minutes: 20 steps: