-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcheck-pr-template.sh
More file actions
executable file
·84 lines (70 loc) · 3.69 KB
/
check-pr-template.sh
File metadata and controls
executable file
·84 lines (70 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env bash
# SessionStart hook: inject PR workflow guidance.
# - Always: instruct the agent to leave a reply and resolve each PR review
# conversation it deals with.
# - Always: instruct the agent to search for matching open issues before
# opening a PR and link them via `Closes #N` / `Refs #N`.
# - When detectable: inject the repo's PR template (or the org's `.github`
# repo template as a fallback) so PRs are opened with the right structure.
set -uo pipefail
cd "${CLAUDE_PROJECT_DIR:-$PWD}" 2>/dev/null || exit 0
git rev-parse --git-dir >/dev/null 2>&1 || exit 0
template=""
src=""
for path in \
.github/pull_request_template.md \
.github/PULL_REQUEST_TEMPLATE.md \
.github/pull_request_template.txt \
docs/pull_request_template.md \
docs/PULL_REQUEST_TEMPLATE.md \
pull_request_template.md \
PULL_REQUEST_TEMPLATE.md
do
if [[ -f "$path" ]]; then
template=$(cat "$path")
src="repo: $path"
break
fi
done
if [[ -z "$template" ]] && command -v gh >/dev/null 2>&1; then
remote=$(git config --get remote.origin.url 2>/dev/null || echo "")
owner=$(printf '%s' "$remote" | sed -nE 's|.*github\.com[:/]([^/]+)/.*|\1|p')
if [[ -n "$owner" ]]; then
for path in \
.github/pull_request_template.md \
.github/PULL_REQUEST_TEMPLATE.md \
profile/pull_request_template.md \
pull_request_template.md \
PULL_REQUEST_TEMPLATE.md
do
content=$(gh api "/repos/${owner}/.github/contents/${path}" --jq '.content' 2>/dev/null | base64 -d 2>/dev/null || true)
if [[ -n "$content" ]]; then
template="$content"
src="org fallback: ${owner}/.github/${path}"
break
fi
done
fi
fi
review_block="### PR review / comment handling
When addressing PR review comments or review threads, treat each thread as done only after BOTH of these:
1. Reply to the thread with a short note describing what changed (and the commit SHA when useful) so the reviewer can see the resolution inline.
2. Resolve the conversation thread.
Use the GitHub GraphQL API via \`gh\` to resolve threads, e.g.:
gh api graphql -f query='mutation(\$id:ID!){resolveReviewThread(input:{threadId:\$id}){thread{isResolved}}}' -f id=\"<thread-node-id>\"
Reply to a thread via \`gh api -X POST /repos/{owner}/{repo}/pulls/{n}/comments/{id}/replies -f body='...'\`. Never silently push fixes without confirming each related thread is replied to and resolved."
issue_block="### Related-issue linking
Before opening a pull request, search the repo for existing open issues that match the work in the branch. Use \`gh issue list --state open --search '<keywords>'\` (and \`gh issue view <n>\` to confirm relevance) to find candidates derived from the branch name, commit messages, and changed files. For every issue the PR would resolve, include a \`Closes #<n>\` line in the PR body so GitHub auto-links and auto-closes it on merge. If the issue is related but the PR does not fully resolve it, use \`Refs #<n>\` instead. Skip this only when there are clearly no matching issues."
if [[ -n "$template" ]]; then
template_block="
### PR template
A PR template was detected (${src}). When opening a pull request with \`gh pr create\`, pass the template below as the \`--body\` argument — preserve the section headings exactly and fill each section based on the branch's actual changes. Do NOT fall back to the generic Summary/Test plan format. Keep any required PR footer (e.g. the PostHog Code attribution) appended after the template.
--- BEGIN PR TEMPLATE ---
${template}
--- END PR TEMPLATE ---"
else
template_block=""
fi
ctx="${review_block}
${issue_block}${template_block}"
jq -nc --arg c "$ctx" '{hookSpecificOutput: {hookEventName: "SessionStart", additionalContext: $c}}'