From fc539899467509f5ae0a37c6fc430b28ac133c36 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Fri, 19 Apr 2024 10:47:00 -0400 Subject: [PATCH 01/11] Create pr-check.yml For users with many open PRs, creates a warning message in the PR description when opening a new PR, encouraging users to complete existing PRs before opening new ones. --- .github/workflows/pr-check.yml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 .github/workflows/pr-check.yml diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml new file mode 100644 index 000000000..951fd02c1 --- /dev/null +++ b/.github/workflows/pr-check.yml @@ -0,0 +1,26 @@ +name: PR Check + +on: + pull_request: + types: + - opened + +jobs: + check-pr: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Get PR count + id: pr-count + run: | + PR_COUNT=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ + "https://api.github.com/repos/naturalcrit/homebrewery/pulls? state=open&head=${{ github.actor }}:${{ github.head_ref }}" | jq '. | length') + echo "::set-output name=pr_count::$PR_COUNT" + + - name: Update PR description + if: ${{ steps.pr_count.outputs.pr_count }} -ge 1 + run: | + gh pr edit ${{ github.event.number }} --body "You already have ${{ steps.pr-count.outputs.pr_count }} PRs open. Consider completing some of your existing PRs before opening new ones." From 228041913ee02ef1af795d206e373bfdf7261b7d Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Fri, 19 Apr 2024 11:15:27 -0400 Subject: [PATCH 02/11] Create limit-pull-requests.yml --- .github/workflows/limit-pull-requests.yml | 103 ++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 .github/workflows/limit-pull-requests.yml diff --git a/.github/workflows/limit-pull-requests.yml b/.github/workflows/limit-pull-requests.yml new file mode 100644 index 000000000..413fa0624 --- /dev/null +++ b/.github/workflows/limit-pull-requests.yml @@ -0,0 +1,103 @@ +name: Limit pull requests +description: > + Limit the number of open pull requests to the repository created by a user +author: ZhongRuoyu (from Homebrew repository) +branding: + icon: alert-triangle + color: yellow + +inputs: + token: + description: GitHub token + required: false + default: ${{ github.token }} + except-users: + description: The users exempted from the limit, one per line + required: false + # https://docs.github.com/en/graphql/reference/enums#commentauthorassociation + except-author-associations: + description: The author associations exempted from the limit, one per line + required: false + comment-limit: + description: > + Post the comment when the user's number of open pull requests exceeds this + number and `comment` is not empty + required: true + default: "10" + comment: + description: The comment to post when the limit is reached + required: false + close-limit: + description: > + Close the pull request when the user's number of open pull requests + exceeds this number and `close` is set to `true` + required: true + default: "50" + close: + description: Whether to close the pull request when the limit is reached + required: true + default: "false" + +runs: + using: composite + steps: + - name: Check the number of pull requests + id: count-pull-requests + run: | + # If the user is exempted, assume they have no pull requests. + if grep -Fiqx '${{ github.actor }}' <<<"$EXCEPT_USERS"; then + echo "::notice::@${{ github.actor }} is exempted from the limit." + echo "count=0" >>"$GITHUB_OUTPUT" + exit 0 + fi + if grep -Fiqx '${{ github.event.pull_request.author_association }}' <<<"$EXCEPT_AUTHOR_ASSOCIATIONS"; then + echo "::notice::@{{ github.actor }} is a ${{ github.event.pull_request.author_association }} exempted from the limit." + echo "count=0" >>"$GITHUB_OUTPUT" + exit 0 + fi + + count="$( + gh api \ + --method GET \ + --header 'Accept: application/vnd.github+json' \ + --header 'X-GitHub-Api-Version: 2022-11-28' \ + --field state=open \ + --paginate \ + '/repos/{owner}/{repo}/pulls' | + jq \ + --raw-output \ + --arg USER '${{ github.actor }}' \ + 'map(select(.user.login == $USER)) | length' + )" + echo "::notice::@${{ github.actor }} has $count open pull request(s)." + echo "count=$count" >>"$GITHUB_OUTPUT" + env: + GH_REPO: ${{ github.repository }} + GH_TOKEN: ${{ inputs.token }} + EXCEPT_USERS: ${{ inputs.except-users }} + EXCEPT_AUTHOR_ASSOCIATIONS: ${{ inputs.except-author-associations }} + shell: bash + + - name: Comment on pull request + if: > + fromJSON(steps.count-pull-requests.outputs.count) > fromJSON(inputs.comment-limit) && + inputs.comment != '' + run: | + gh pr comment '${{ github.event.pull_request.number }}' \ + --body="${COMMENT_BODY}" + env: + GH_REPO: ${{ github.repository }} + GH_TOKEN: ${{ inputs.token }} + COMMENT_BODY: ${{ inputs.comment }} + shell: bash + + - name: Close pull request + if: > + fromJSON(steps.count-pull-requests.outputs.count) > fromJSON(inputs.close-limit) && + inputs.close == 'true' + run: | + gh pr close '${{ github.event.pull_request.number }}' + env: + GH_REPO: ${{ github.repository }} + GH_TOKEN: ${{ inputs.token }} + shell: bash From f23959bb057df011aa3595f0e2579017cf7eac15 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Fri, 19 Apr 2024 11:24:18 -0400 Subject: [PATCH 03/11] Update pr-check.yml --- .github/workflows/pr-check.yml | 43 +++++++++++++++++----------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 951fd02c1..077eb0c4d 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -1,26 +1,25 @@ name: PR Check - -on: - pull_request: - types: - - opened - +on: pull_request_target +env: + GH_REPO: ${{ github.repository }} + GH_NO_UPDATE_NOTIFIER: 1 + GH_PROMPT_DISABLED: 1 +permissions: + contents: read + issues: write + pull-requests: write + statuses: write jobs: - check-pr: + limit-pull-requests: + if: always() && github.repository_owner == 'Homebrew' runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v2 - - - name: Get PR count - id: pr-count - run: | - PR_COUNT=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ - "https://api.github.com/repos/naturalcrit/homebrewery/pulls? state=open&head=${{ github.actor }}:${{ github.head_ref }}" | jq '. | length') - echo "::set-output name=pr_count::$PR_COUNT" - - - name: Update PR description - if: ${{ steps.pr_count.outputs.pr_count }} -ge 1 - run: | - gh pr edit ${{ github.event.number }} --body "You already have ${{ steps.pr-count.outputs.pr_count }} PRs open. Consider completing some of your existing PRs before opening new ones." + - uses: Homebrew/actions/limit-pull-requests@master + with: + except-users: | + dependabot + comment-limit: 1 + comment: | + Hi, thanks for your contribution to the Homebrewery! You already have >=3 open pull requests. Consider completing some of your existing PRs before opening new ones. Thanks! + close-limit: 5 + close: false From 8b6be1cab8d2ae4fd775c68f69037d2448b6522d Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Fri, 19 Apr 2024 11:33:55 -0400 Subject: [PATCH 04/11] Create limit-pull-requests.yml --- .../limit-pull-requests.yml | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 .github/actions/limit-pull-requests/limit-pull-requests.yml diff --git a/.github/actions/limit-pull-requests/limit-pull-requests.yml b/.github/actions/limit-pull-requests/limit-pull-requests.yml new file mode 100644 index 000000000..413fa0624 --- /dev/null +++ b/.github/actions/limit-pull-requests/limit-pull-requests.yml @@ -0,0 +1,103 @@ +name: Limit pull requests +description: > + Limit the number of open pull requests to the repository created by a user +author: ZhongRuoyu (from Homebrew repository) +branding: + icon: alert-triangle + color: yellow + +inputs: + token: + description: GitHub token + required: false + default: ${{ github.token }} + except-users: + description: The users exempted from the limit, one per line + required: false + # https://docs.github.com/en/graphql/reference/enums#commentauthorassociation + except-author-associations: + description: The author associations exempted from the limit, one per line + required: false + comment-limit: + description: > + Post the comment when the user's number of open pull requests exceeds this + number and `comment` is not empty + required: true + default: "10" + comment: + description: The comment to post when the limit is reached + required: false + close-limit: + description: > + Close the pull request when the user's number of open pull requests + exceeds this number and `close` is set to `true` + required: true + default: "50" + close: + description: Whether to close the pull request when the limit is reached + required: true + default: "false" + +runs: + using: composite + steps: + - name: Check the number of pull requests + id: count-pull-requests + run: | + # If the user is exempted, assume they have no pull requests. + if grep -Fiqx '${{ github.actor }}' <<<"$EXCEPT_USERS"; then + echo "::notice::@${{ github.actor }} is exempted from the limit." + echo "count=0" >>"$GITHUB_OUTPUT" + exit 0 + fi + if grep -Fiqx '${{ github.event.pull_request.author_association }}' <<<"$EXCEPT_AUTHOR_ASSOCIATIONS"; then + echo "::notice::@{{ github.actor }} is a ${{ github.event.pull_request.author_association }} exempted from the limit." + echo "count=0" >>"$GITHUB_OUTPUT" + exit 0 + fi + + count="$( + gh api \ + --method GET \ + --header 'Accept: application/vnd.github+json' \ + --header 'X-GitHub-Api-Version: 2022-11-28' \ + --field state=open \ + --paginate \ + '/repos/{owner}/{repo}/pulls' | + jq \ + --raw-output \ + --arg USER '${{ github.actor }}' \ + 'map(select(.user.login == $USER)) | length' + )" + echo "::notice::@${{ github.actor }} has $count open pull request(s)." + echo "count=$count" >>"$GITHUB_OUTPUT" + env: + GH_REPO: ${{ github.repository }} + GH_TOKEN: ${{ inputs.token }} + EXCEPT_USERS: ${{ inputs.except-users }} + EXCEPT_AUTHOR_ASSOCIATIONS: ${{ inputs.except-author-associations }} + shell: bash + + - name: Comment on pull request + if: > + fromJSON(steps.count-pull-requests.outputs.count) > fromJSON(inputs.comment-limit) && + inputs.comment != '' + run: | + gh pr comment '${{ github.event.pull_request.number }}' \ + --body="${COMMENT_BODY}" + env: + GH_REPO: ${{ github.repository }} + GH_TOKEN: ${{ inputs.token }} + COMMENT_BODY: ${{ inputs.comment }} + shell: bash + + - name: Close pull request + if: > + fromJSON(steps.count-pull-requests.outputs.count) > fromJSON(inputs.close-limit) && + inputs.close == 'true' + run: | + gh pr close '${{ github.event.pull_request.number }}' + env: + GH_REPO: ${{ github.repository }} + GH_TOKEN: ${{ inputs.token }} + shell: bash From 6e69696b4a6eaf94641ef44510da693cbecda9f9 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Fri, 19 Apr 2024 11:34:06 -0400 Subject: [PATCH 05/11] Update pr-check.yml --- .github/workflows/pr-check.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 077eb0c4d..7784ce98e 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -14,7 +14,7 @@ jobs: if: always() && github.repository_owner == 'Homebrew' runs-on: ubuntu-latest steps: - - uses: Homebrew/actions/limit-pull-requests@master + - uses: ./.github/actions/limit-pull-requests with: except-users: | dependabot From 9f4545606610a65d09070ed63c110c4f341bfdb1 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Fri, 19 Apr 2024 11:34:26 -0400 Subject: [PATCH 06/11] Delete .github/workflows/limit-pull-requests.yml --- .github/workflows/limit-pull-requests.yml | 103 ---------------------- 1 file changed, 103 deletions(-) delete mode 100644 .github/workflows/limit-pull-requests.yml diff --git a/.github/workflows/limit-pull-requests.yml b/.github/workflows/limit-pull-requests.yml deleted file mode 100644 index 413fa0624..000000000 --- a/.github/workflows/limit-pull-requests.yml +++ /dev/null @@ -1,103 +0,0 @@ -name: Limit pull requests -description: > - Limit the number of open pull requests to the repository created by a user -author: ZhongRuoyu (from Homebrew repository) -branding: - icon: alert-triangle - color: yellow - -inputs: - token: - description: GitHub token - required: false - default: ${{ github.token }} - except-users: - description: The users exempted from the limit, one per line - required: false - # https://docs.github.com/en/graphql/reference/enums#commentauthorassociation - except-author-associations: - description: The author associations exempted from the limit, one per line - required: false - comment-limit: - description: > - Post the comment when the user's number of open pull requests exceeds this - number and `comment` is not empty - required: true - default: "10" - comment: - description: The comment to post when the limit is reached - required: false - close-limit: - description: > - Close the pull request when the user's number of open pull requests - exceeds this number and `close` is set to `true` - required: true - default: "50" - close: - description: Whether to close the pull request when the limit is reached - required: true - default: "false" - -runs: - using: composite - steps: - - name: Check the number of pull requests - id: count-pull-requests - run: | - # If the user is exempted, assume they have no pull requests. - if grep -Fiqx '${{ github.actor }}' <<<"$EXCEPT_USERS"; then - echo "::notice::@${{ github.actor }} is exempted from the limit." - echo "count=0" >>"$GITHUB_OUTPUT" - exit 0 - fi - if grep -Fiqx '${{ github.event.pull_request.author_association }}' <<<"$EXCEPT_AUTHOR_ASSOCIATIONS"; then - echo "::notice::@{{ github.actor }} is a ${{ github.event.pull_request.author_association }} exempted from the limit." - echo "count=0" >>"$GITHUB_OUTPUT" - exit 0 - fi - - count="$( - gh api \ - --method GET \ - --header 'Accept: application/vnd.github+json' \ - --header 'X-GitHub-Api-Version: 2022-11-28' \ - --field state=open \ - --paginate \ - '/repos/{owner}/{repo}/pulls' | - jq \ - --raw-output \ - --arg USER '${{ github.actor }}' \ - 'map(select(.user.login == $USER)) | length' - )" - echo "::notice::@${{ github.actor }} has $count open pull request(s)." - echo "count=$count" >>"$GITHUB_OUTPUT" - env: - GH_REPO: ${{ github.repository }} - GH_TOKEN: ${{ inputs.token }} - EXCEPT_USERS: ${{ inputs.except-users }} - EXCEPT_AUTHOR_ASSOCIATIONS: ${{ inputs.except-author-associations }} - shell: bash - - - name: Comment on pull request - if: > - fromJSON(steps.count-pull-requests.outputs.count) > fromJSON(inputs.comment-limit) && - inputs.comment != '' - run: | - gh pr comment '${{ github.event.pull_request.number }}' \ - --body="${COMMENT_BODY}" - env: - GH_REPO: ${{ github.repository }} - GH_TOKEN: ${{ inputs.token }} - COMMENT_BODY: ${{ inputs.comment }} - shell: bash - - - name: Close pull request - if: > - fromJSON(steps.count-pull-requests.outputs.count) > fromJSON(inputs.close-limit) && - inputs.close == 'true' - run: | - gh pr close '${{ github.event.pull_request.number }}' - env: - GH_REPO: ${{ github.repository }} - GH_TOKEN: ${{ inputs.token }} - shell: bash From 08e273bfd6183b8043b18e35989dac7a91c20743 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Fri, 19 Apr 2024 11:37:23 -0400 Subject: [PATCH 07/11] Update pr-check.yml --- .github/workflows/pr-check.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 7784ce98e..7c986de7e 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -1,5 +1,8 @@ name: PR Check -on: pull_request_target +on: + pull_request: + types: + - opened env: GH_REPO: ${{ github.repository }} GH_NO_UPDATE_NOTIFIER: 1 From 19961c7ec55e7dcbea69fa82b2b6bb5db4418f9a Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Fri, 19 Apr 2024 11:48:30 -0400 Subject: [PATCH 08/11] Update pr-check.yml --- .github/workflows/pr-check.yml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 7c986de7e..c2c3a5a04 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -1,8 +1,5 @@ name: PR Check -on: - pull_request: - types: - - opened +on: pull_request_target env: GH_REPO: ${{ github.repository }} GH_NO_UPDATE_NOTIFIER: 1 @@ -14,7 +11,7 @@ permissions: statuses: write jobs: limit-pull-requests: - if: always() && github.repository_owner == 'Homebrew' + if: always() && github.repository_owner == 'naturalcrit' runs-on: ubuntu-latest steps: - uses: ./.github/actions/limit-pull-requests From 1e5e3d5f4197f1409e1f068184e94802ba55f753 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Fri, 19 Apr 2024 11:50:01 -0400 Subject: [PATCH 09/11] Rename limit-pull-requests.yml to action.yml --- .../limit-pull-requests/{limit-pull-requests.yml => action.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/actions/limit-pull-requests/{limit-pull-requests.yml => action.yml} (100%) diff --git a/.github/actions/limit-pull-requests/limit-pull-requests.yml b/.github/actions/limit-pull-requests/action.yml similarity index 100% rename from .github/actions/limit-pull-requests/limit-pull-requests.yml rename to .github/actions/limit-pull-requests/action.yml From 80bcf92fa3e070a99778794b0b617c72de197a32 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Fri, 19 Apr 2024 11:57:44 -0400 Subject: [PATCH 10/11] Update pr-check.yml --- .github/workflows/pr-check.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index c2c3a5a04..ac1eb3190 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -14,7 +14,11 @@ jobs: if: always() && github.repository_owner == 'naturalcrit' runs-on: ubuntu-latest steps: - - uses: ./.github/actions/limit-pull-requests + - name: Checkout + uses: actions/checkout@v2 + + - name : Run limit-pull-requests action + uses: ./.github/actions/limit-pull-requests with: except-users: | dependabot From 98d032913b0c236acabaf3a98ca2bea2fc087f76 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Fri, 19 Apr 2024 12:00:11 -0400 Subject: [PATCH 11/11] Update pr-check.yml --- .github/workflows/pr-check.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index ac1eb3190..e5adb2561 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -22,7 +22,7 @@ jobs: with: except-users: | dependabot - comment-limit: 1 + comment-limit: 3 comment: | Hi, thanks for your contribution to the Homebrewery! You already have >=3 open pull requests. Consider completing some of your existing PRs before opening new ones. Thanks! close-limit: 5