-
Notifications
You must be signed in to change notification settings - Fork 18
chore: squash-merge commit body from ## Description only #825
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #!/usr/bin/env bash | ||
| # Extract the ## Description section from a GitHub PR body. | ||
| # | ||
| # Usage: | ||
| # scripts/pr-description.sh <pr-number-or-url> [--repo OWNER/REPO] | ||
| # | ||
| # Prints the Description section (until the next ## heading), with HTML | ||
| # comments stripped. Intended as the squash-merge commit message body. | ||
| set -euo pipefail | ||
|
|
||
| usage() { | ||
| echo "Usage: $0 <pr-number-or-url> [--repo OWNER/REPO]" >&2 | ||
| exit 2 | ||
| } | ||
|
|
||
| if [[ $# -lt 1 ]]; then | ||
| usage | ||
| fi | ||
|
|
||
| pr="$1" | ||
| shift | ||
| repo_args=() | ||
| while [[ $# -gt 0 ]]; do | ||
| case "$1" in | ||
| --repo) | ||
| [[ $# -ge 2 ]] || usage | ||
| repo_args=(--repo "$2") | ||
| shift 2 | ||
| ;; | ||
| *) | ||
| usage | ||
| ;; | ||
| esac | ||
| done | ||
|
|
||
| # Accept bare numbers or full PR URLs. | ||
| if [[ "$pr" =~ ^https?://github.com/([^/]+/[^/]+)/pull/([0-9]+) ]]; then | ||
| repo_args=(--repo "${BASH_REMATCH[1]}") | ||
| pr="${BASH_REMATCH[2]}" | ||
| fi | ||
|
|
||
| body="$(gh pr view "$pr" "${repo_args[@]}" --json body -q .body)" | ||
|
|
||
| python3 - "$body" <<'PY' | ||
| import re, sys | ||
|
|
||
| body = sys.argv[1] | ||
| # Strip HTML comments. | ||
| body = re.sub(r"<!--.*?-->", "", body, flags=re.S) | ||
|
|
||
| # Find ## Description (allow optional trailing whitespace / BOM noise). | ||
| m = re.search(r"(?im)^##\s+Description\s*\n", body) | ||
| if not m: | ||
| sys.stderr.write("error: no '## Description' section found in PR body\n") | ||
| sys.exit(1) | ||
|
|
||
| rest = body[m.end():] | ||
| # Next markdown H2 ends the section. | ||
| nxt = re.search(r"(?m)^##\s+\S", rest) | ||
| section = rest[: nxt.start()] if nxt else rest | ||
| section = section.strip() + "\n" | ||
| if not section.strip(): | ||
| sys.stderr.write("error: '## Description' section is empty\n") | ||
| sys.exit(1) | ||
| sys.stdout.write(section) | ||
| PY | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #!/usr/bin/env bash | ||
| # Squash-merge a PR using PR title as subject and ## Description as body. | ||
| # | ||
| # Usage: | ||
| # scripts/pr-squash-merge.sh <pr-number-or-url> [--repo OWNER/REPO] [extra gh pr merge args...] | ||
| # | ||
| # Examples: | ||
| # scripts/pr-squash-merge.sh 824 | ||
| # scripts/pr-squash-merge.sh 824 --repo Galxe/gravity-sdk --admin | ||
| set -euo pipefail | ||
|
|
||
| usage() { | ||
| echo "Usage: $0 <pr-number-or-url> [--repo OWNER/REPO] [extra gh pr merge args...]" >&2 | ||
| exit 2 | ||
| } | ||
|
|
||
| if [[ $# -lt 1 ]]; then | ||
| usage | ||
| fi | ||
|
|
||
| pr="$1" | ||
| shift | ||
|
|
||
| repo_args=() | ||
| extra=() | ||
| while [[ $# -gt 0 ]]; do | ||
| case "$1" in | ||
| --repo) | ||
| [[ $# -ge 2 ]] || usage | ||
| repo_args=(--repo "$2") | ||
| shift 2 | ||
| ;; | ||
| -h|--help) | ||
| usage | ||
| ;; | ||
| *) | ||
| extra+=("$1") | ||
| shift | ||
| ;; | ||
| esac | ||
| done | ||
|
|
||
| if [[ "$pr" =~ ^https?://github.com/([^/]+/[^/]+)/pull/([0-9]+) ]]; then | ||
| repo_args=(--repo "${BASH_REMATCH[1]}") | ||
| pr="${BASH_REMATCH[2]}" | ||
| fi | ||
|
|
||
| root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | ||
| desc="$("$root/scripts/pr-description.sh" "$pr" "${repo_args[@]}")" | ||
| title="$(gh pr view "$pr" "${repo_args[@]}" --json title -q .title)" | ||
|
|
||
| echo "Squash-merging #$pr" | ||
| echo "---- subject ----" | ||
| echo "$title" | ||
| echo "---- body (## Description) ----" | ||
| printf '%s' "$desc" | ||
| echo "-----------------" | ||
|
|
||
| gh pr merge "$pr" "${repo_args[@]}" --squash --subject "$title" --body "$desc" "${extra[@]}" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When this helper is run with the advertised AGENTS.md reference: AGENTS.md:L5-L12 Useful? React with 👍 / 👎. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a Description containing a fenced Markdown example with a line such as
## Example, this regex treats that line as the next section heading even though Markdown renders it as code. The extractor consequently truncates the commit body—potentially in the middle of an open fence—and drops all subsequent Description text. A minimal body of## Description,before, an opening fence,## Example, a closing fence,after, and then the real next H2 reproduces output containing onlybeforeand the opening fence; heading detection needs to account for fenced blocks.AGENTS.md reference: AGENTS.md:L6-L12
Useful? React with 👍 / 👎.