aboutsummaryrefslogtreecommitdiff
path: root/hooks/gh-pr-create-confirm.py
blob: b983352db736adb256a9ca6ff933ad285a802b54 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python3
"""PreToolUse hook for Bash: gate `gh pr create` behind a confirmation modal.

Parses title, body, base, head, reviewers, labels, draft flag from the
`gh pr create` command and renders a modal so the user sees exactly what
will be opened.

Wire in ~/.claude/settings.json alongside git-commit-confirm.py:

    {
      "hooks": {
        "PreToolUse": [
          {
            "matcher": "Bash",
            "hooks": [
              {
                "type": "command",
                "command": "~/.claude/hooks/gh-pr-create-confirm.py"
              }
            ]
          }
        ]
      }
    }
"""

import json
import re
import sys


MAX_BODY_LINES = 20


def main() -> int:
    try:
        payload = json.loads(sys.stdin.read())
    except (json.JSONDecodeError, ValueError):
        return 0

    if payload.get("tool_name") != "Bash":
        return 0

    cmd = payload.get("tool_input", {}).get("command", "")
    if not re.search(r"(?:^|[\s;&|()])gh\s+pr\s+create\b", cmd):
        return 0

    fields = parse_pr_create(cmd)
    reason = format_pr_confirmation(fields)

    output = {
        "hookSpecificOutput": {
            "hookEventName": "PreToolUse",
            "permissionDecision": "ask",
            "permissionDecisionReason": reason,
        }
    }
    print(json.dumps(output))
    return 0


def parse_pr_create(cmd: str) -> dict:
    fields: dict = {
        "title": None,
        "body": None,
        "base": None,
        "head": None,
        "reviewers": [],
        "labels": [],
        "assignees": [],
        "milestone": None,
        "draft": False,
    }

    # Title — quoted string after --title / -t
    t = re.search(r"--title\s+([\"'])(.*?)\1", cmd, re.DOTALL)
    if not t:
        t = re.search(r"\s-t\s+([\"'])(.*?)\1", cmd, re.DOTALL)
    if t:
        fields["title"] = t.group(2)

    # Body — HEREDOC inside $() first, then plain quoted string, then --body-file
    body_heredoc = re.search(
        r"--body\s+\"\$\(cat\s*<<-?\s*['\"]?(\w+)['\"]?\s*\n(.*?)\n\s*\1\s*\)\"",
        cmd,
        re.DOTALL,
    )
    if body_heredoc:
        fields["body"] = body_heredoc.group(2).strip()
    else:
        b = re.search(r"--body\s+([\"'])(.*?)\1", cmd, re.DOTALL)
        if b:
            fields["body"] = b.group(2).strip()
        else:
            bf = re.search(r"--body-file\s+(\S+)", cmd)
            if bf:
                fields["body"] = f"(body read from file: {bf.group(1)})"

    # Base / head
    base = re.search(r"--base\s+(\S+)", cmd)
    if not base:
        base = re.search(r"\s-B\s+(\S+)", cmd)
    if base:
        fields["base"] = base.group(1)

    head = re.search(r"--head\s+(\S+)", cmd)
    if not head:
        head = re.search(r"\s-H\s+(\S+)", cmd)
    if head:
        fields["head"] = head.group(1)

    # Multi-valued flags (comma-separated or repeated)
    for name, key in (
        ("reviewer", "reviewers"),
        ("label", "labels"),
        ("assignee", "assignees"),
    ):
        pattern = rf"--{name}[=\s]([\"']?)([^\s\"']+)\1"
        for match in re.finditer(pattern, cmd):
            fields[key].extend(match.group(2).split(","))

    # Milestone
    m = re.search(r"--milestone[=\s]([\"'])?([^\s\"']+)\1?", cmd)
    if m:
        fields["milestone"] = m.group(2)

    # Draft flag
    if re.search(r"--draft\b", cmd):
        fields["draft"] = True

    return fields


def format_pr_confirmation(fields: dict) -> str:
    lines = ["Create pull request?", ""]

    if fields["draft"]:
        lines.append("[DRAFT]")
        lines.append("")

    lines.append(f"Title: {fields['title'] or '(not parsed)'}")

    base = fields["base"] or "(default — usually main)"
    head = fields["head"] or "(current branch)"
    lines.append(f"Base ← Head: {base} ← {head}")

    if fields["reviewers"]:
        lines.append(f"Reviewers: {', '.join(fields['reviewers'])}")
    if fields["assignees"]:
        lines.append(f"Assignees: {', '.join(fields['assignees'])}")
    if fields["labels"]:
        lines.append(f"Labels: {', '.join(fields['labels'])}")
    if fields["milestone"]:
        lines.append(f"Milestone: {fields['milestone']}")

    lines.append("")
    if fields["body"]:
        lines.append("Body:")
        body_lines = fields["body"].splitlines()
        for line in body_lines[:MAX_BODY_LINES]:
            lines.append(f"  {line}")
        if len(body_lines) > MAX_BODY_LINES:
            lines.append(f"  ... ({len(body_lines) - MAX_BODY_LINES} more lines)")
    else:
        lines.append("Body: (not parsed)")

    lines.append("")
    lines.append("Confirm target branch, title, body, and reviewers before proceeding.")
    return "\n".join(lines)


if __name__ == "__main__":
    sys.exit(main())