Skip to content

Commit 9227801

Browse files
committed
workflows: close incomplete PRs
- Close PRs that appear to retain most of the template text so maintainers do not need to triage unfilled submissions manually. - Reopen workflow-closed PRs once the template is properly filled in, while avoiding manually closed PRs. - Keep the matching logic in style-checked Ruby and the workflow logic in a shell helper.
1 parent dd39104 commit 9227801

3 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
# Checks whether a pull request body preserves enough of the current pull request template.
5+
6+
CHECKBOX_MARKER = /\A- \[[ xX]\] /
7+
NORMALISED_CHECKBOX_MARKER = "- [ ] "
8+
REQUIRED_TEMPLATE_PERCENTAGE = 51
9+
PERCENTAGE_SCALE = 100
10+
11+
pr_body_path = ARGV.fetch(0)
12+
template_path = ARGV.fetch(1)
13+
14+
pr_lines, template_lines = [pr_body_path, template_path].map do |path|
15+
File.foreach(path, chomp: true).filter_map do |line|
16+
line = line.strip.sub(CHECKBOX_MARKER, NORMALISED_CHECKBOX_MARKER)
17+
line unless line.empty?
18+
end
19+
end
20+
21+
template_lines.uniq!
22+
matching_template_lines = template_lines.count { |line| pr_lines.include?(line) }
23+
scaled_matching_percentage = matching_template_lines * PERCENTAGE_SCALE
24+
scaled_required_percentage = template_lines.count * REQUIRED_TEMPLATE_PERCENTAGE
25+
26+
exit scaled_matching_percentage >= scaled_required_percentage
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env bash
2+
3+
# Closes incomplete pull requests and reopens workflow-closed pull requests once the template is filled in.
4+
5+
set -xeuo pipefail
6+
7+
PR_BODY_FILE="$(mktemp)"
8+
trap 'rm -f "${PR_BODY_FILE}"' EXIT
9+
printf "%s" "${PR_BODY:-}" >"${PR_BODY_FILE}"
10+
11+
if brew ruby .github/workflows/incomplete-prs.rb "${PR_BODY_FILE}" .github/PULL_REQUEST_TEMPLATE.md
12+
then
13+
complete_template=true
14+
else
15+
complete_template=false
16+
fi
17+
18+
if [[ "${PR_STATE:?}" == "closed" ]]
19+
then
20+
if [[ "${complete_template}" == "true" ]] &&
21+
gh api --paginate "repos/${GITHUB_REPOSITORY:?}/issues/${PR_NUMBER:?}/comments" \
22+
--jq '.[] | select(.user.login == "github-actions[bot]" and (.body | contains("<!-- incomplete-pr-template -->"))) | .id' |
23+
grep -q .
24+
then
25+
gh api --method PATCH "repos/${GITHUB_REPOSITORY:?}/pulls/${PR_NUMBER:?}" \
26+
-f state=open
27+
fi
28+
29+
exit 0
30+
fi
31+
32+
if [[ "${complete_template}" == "true" ]]
33+
then
34+
exit 0
35+
fi
36+
37+
gh api --method POST "repos/${GITHUB_REPOSITORY:?}/issues/${PR_NUMBER:?}/comments" \
38+
--raw-field body="$(
39+
cat <<COMMENT
40+
<!-- incomplete-pr-template -->
41+
Thanks for your pull request. This has been closed because it appears to use an incomplete or outdated pull request template.
42+
43+
This pull request may be reopened on request if a human properly fills in the current [pull request template](${PR_TEMPLATE_URL:?}).
44+
COMMENT
45+
)"
46+
47+
gh api --method PATCH "repos/${GITHUB_REPOSITORY:?}/pulls/${PR_NUMBER:?}" \
48+
-f state=closed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Close incomplete pull requests
2+
3+
on:
4+
pull_request:
5+
types:
6+
- opened
7+
- edited
8+
- reopened
9+
10+
permissions: {}
11+
12+
defaults:
13+
run:
14+
shell: bash -xeuo pipefail {0}
15+
16+
concurrency:
17+
group: "incomplete-pr-${{ github.event.pull_request.number }}"
18+
cancel-in-progress: true
19+
20+
jobs:
21+
manage:
22+
if: github.repository == 'Homebrew/brew'
23+
runs-on: ubuntu-latest
24+
permissions:
25+
issues: write
26+
pull-requests: write
27+
env:
28+
GH_TOKEN: ${{ github.token }}
29+
PR_BODY: ${{ github.event.pull_request.body }}
30+
PR_NUMBER: ${{ github.event.pull_request.number }}
31+
PR_STATE: ${{ github.event.pull_request.state }}
32+
PR_TEMPLATE_URL: ${{ github.server_url }}/${{ github.repository }}/blob/HEAD/.github/PULL_REQUEST_TEMPLATE.md
33+
steps:
34+
- name: Set up Homebrew
35+
id: set-up-homebrew
36+
uses: Homebrew/actions/setup-homebrew@main
37+
with:
38+
core: false
39+
cask: false
40+
41+
- name: Manage incomplete pull request
42+
working-directory: ${{ steps.set-up-homebrew.outputs.repository-path }}
43+
run: bash .github/workflows/incomplete-prs.sh

0 commit comments

Comments
 (0)