|
| 1 | +name: Classify PR |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +concurrency: |
| 8 | + group: classify-pr-${{ github.event.pull_request.number }} |
| 9 | + cancel-in-progress: true |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: read |
| 13 | + issues: write |
| 14 | + models: read |
| 15 | + pull-requests: write |
| 16 | + |
| 17 | +jobs: |
| 18 | + classify: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + steps: |
| 21 | + - uses: actions/checkout@v4 |
| 22 | + |
| 23 | + - name: Build prompt |
| 24 | + env: |
| 25 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 26 | + run: | |
| 27 | + PR=${{ github.event.pull_request.number }} |
| 28 | + gh pr diff "$PR" > /tmp/pr.diff |
| 29 | + gh pr view "$PR" --json title --jq .title > /tmp/pr-title.txt |
| 30 | + gh pr view "$PR" --json body --jq '.body // ""' > /tmp/pr-body.txt |
| 31 | +
|
| 32 | + { |
| 33 | + printf 'PR #%s: %s\n' "$PR" "$(cat /tmp/pr-title.txt)" |
| 34 | + echo "" |
| 35 | + cat /tmp/pr-body.txt |
| 36 | + echo "" |
| 37 | + echo "Diff (truncated):" |
| 38 | + head -c 100000 /tmp/pr.diff |
| 39 | + } > /tmp/user-message.txt |
| 40 | +
|
| 41 | + python3 -c " |
| 42 | + with open('.github/prompts/classify-pr.prompt.yml') as f: |
| 43 | + lines = f.readlines() |
| 44 | + with open('/tmp/user-message.txt') as f: |
| 45 | + user_msg = f.read() |
| 46 | +
|
| 47 | + insert_at = len(lines) |
| 48 | + for i, line in enumerate(lines): |
| 49 | + if i == 0: |
| 50 | + continue |
| 51 | + if line.strip() and not line[0].isspace(): |
| 52 | + insert_at = i |
| 53 | + break |
| 54 | +
|
| 55 | + entry = [' - role: user\n', ' content: |\n'] |
| 56 | + for ln in user_msg.splitlines(): |
| 57 | + entry.append(' ' + ln + '\n') |
| 58 | +
|
| 59 | + lines[insert_at:insert_at] = entry |
| 60 | + with open('/tmp/prompt.yml', 'w') as f: |
| 61 | + f.writelines(lines) |
| 62 | +
|
| 63 | + try: |
| 64 | + import yaml |
| 65 | + doc = yaml.safe_load(open('/tmp/prompt.yml')) |
| 66 | + assert doc['messages'][-1]['role'] == 'user', 'prompt splice failed' |
| 67 | + except ImportError: |
| 68 | + pass |
| 69 | + " |
| 70 | +
|
| 71 | + - name: Classify |
| 72 | + id: classify |
| 73 | + uses: actions/ai-inference@v2 |
| 74 | + with: |
| 75 | + prompt-file: /tmp/prompt.yml |
| 76 | + |
| 77 | + - name: Apply label |
| 78 | + env: |
| 79 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 80 | + run: | |
| 81 | + LABEL=$(echo "${{ steps.classify.outputs.response }}" | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]') |
| 82 | + case "$LABEL" in |
| 83 | + bug|enhancement|documentation) ;; |
| 84 | + *) echo "Unexpected: $LABEL — skipping"; exit 0 ;; |
| 85 | + esac |
| 86 | + PR=${{ github.event.pull_request.number }} |
| 87 | + CURRENT=$(gh pr view "$PR" --json labels --jq '.labels[].name') |
| 88 | + for L in bug enhancement documentation; do |
| 89 | + if [ "$L" != "$LABEL" ] && echo "$CURRENT" | grep -qx "$L"; then |
| 90 | + gh pr edit "$PR" --remove-label "$L" 2>/dev/null || true |
| 91 | + fi |
| 92 | + done |
| 93 | + if ! echo "$CURRENT" | grep -qx "$LABEL"; then |
| 94 | + gh pr edit "$PR" --add-label "$LABEL" 2>/dev/null || true |
| 95 | + fi |
| 96 | +
|
| 97 | + breaking: |
| 98 | + runs-on: ubuntu-latest |
| 99 | + steps: |
| 100 | + - uses: actions/checkout@v4 |
| 101 | + |
| 102 | + - name: Build prompt |
| 103 | + id: cmd-diff |
| 104 | + env: |
| 105 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 106 | + run: | |
| 107 | + PR=${{ github.event.pull_request.number }} |
| 108 | +
|
| 109 | + PATTERNS=( |
| 110 | + "internal/cmd/*.go" |
| 111 | + ) |
| 112 | + gh pr diff "$PR" > /tmp/full.diff |
| 113 | +
|
| 114 | + python3 -c " |
| 115 | + import sys, re, fnmatch |
| 116 | + diff = open('/tmp/full.diff').read() |
| 117 | + patterns = sys.argv[1:] |
| 118 | + sections = re.split(r'(?=^diff --git)', diff, flags=re.MULTILINE) |
| 119 | + for s in sections: |
| 120 | + m = re.match(r'diff --git a/(\S+)', s) |
| 121 | + if m: |
| 122 | + path = m.group(1) |
| 123 | + if any(fnmatch.fnmatch(path, p) for p in patterns): |
| 124 | + sys.stdout.write(s) |
| 125 | + " "${PATTERNS[@]}" > /tmp/cmd.diff |
| 126 | +
|
| 127 | + if [ ! -s /tmp/cmd.diff ]; then |
| 128 | + echo "skip=true" >> $GITHUB_OUTPUT |
| 129 | + else |
| 130 | + TITLE=$(gh pr view "$PR" --json title --jq .title) |
| 131 | +
|
| 132 | + { |
| 133 | + printf 'PR #%s: %s\n' "$PR" "$TITLE" |
| 134 | + echo "" |
| 135 | + echo "Diff of CLI command surface files:" |
| 136 | + head -c 100000 /tmp/cmd.diff |
| 137 | + } > /tmp/user-message.txt |
| 138 | +
|
| 139 | + python3 -c " |
| 140 | + with open('.github/prompts/detect-breaking.prompt.yml') as f: |
| 141 | + lines = f.readlines() |
| 142 | + with open('/tmp/user-message.txt') as f: |
| 143 | + user_msg = f.read() |
| 144 | +
|
| 145 | + insert_at = len(lines) |
| 146 | + for i, line in enumerate(lines): |
| 147 | + if i == 0: |
| 148 | + continue |
| 149 | + if line.strip() and not line[0].isspace(): |
| 150 | + insert_at = i |
| 151 | + break |
| 152 | +
|
| 153 | + entry = [' - role: user\n', ' content: |\n'] |
| 154 | + for ln in user_msg.splitlines(): |
| 155 | + entry.append(' ' + ln + '\n') |
| 156 | +
|
| 157 | + lines[insert_at:insert_at] = entry |
| 158 | + with open('/tmp/prompt.yml', 'w') as f: |
| 159 | + f.writelines(lines) |
| 160 | +
|
| 161 | + try: |
| 162 | + import yaml |
| 163 | + doc = yaml.safe_load(open('/tmp/prompt.yml')) |
| 164 | + assert doc['messages'][-1]['role'] == 'user', 'prompt splice failed' |
| 165 | + except ImportError: |
| 166 | + pass |
| 167 | + " |
| 168 | + echo "skip=false" >> $GITHUB_OUTPUT |
| 169 | + fi |
| 170 | +
|
| 171 | + - name: Detect breaking changes |
| 172 | + if: steps.cmd-diff.outputs.skip != 'true' |
| 173 | + id: detect |
| 174 | + uses: actions/ai-inference@v2 |
| 175 | + with: |
| 176 | + prompt-file: /tmp/prompt.yml |
| 177 | + |
| 178 | + - name: Apply breaking label |
| 179 | + if: steps.cmd-diff.outputs.skip != 'true' |
| 180 | + env: |
| 181 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 182 | + run: | |
| 183 | + RESPONSE_FILE="${{ steps.detect.outputs.response-file }}" |
| 184 | + if [ -z "$RESPONSE_FILE" ] || [ ! -f "$RESPONSE_FILE" ]; then |
| 185 | + echo "::warning::Model response file is missing; skipping breaking label." |
| 186 | + exit 0 |
| 187 | + fi |
| 188 | + if ! jq empty "$RESPONSE_FILE" 2>/dev/null; then |
| 189 | + echo "::warning::Model response is not valid JSON; skipping breaking label." |
| 190 | + echo "## Breaking change detection failed" >> "$GITHUB_STEP_SUMMARY" |
| 191 | + echo "Model returned invalid JSON. Breaking label was **not** applied." >> "$GITHUB_STEP_SUMMARY" |
| 192 | + if [ -s "$RESPONSE_FILE" ]; then |
| 193 | + echo '```' >> "$GITHUB_STEP_SUMMARY" |
| 194 | + cat "$RESPONSE_FILE" >> "$GITHUB_STEP_SUMMARY" |
| 195 | + echo '```' >> "$GITHUB_STEP_SUMMARY" |
| 196 | + fi |
| 197 | + exit 0 |
| 198 | + fi |
| 199 | + BREAKING=$(jq -r '.breaking' "$RESPONSE_FILE") |
| 200 | + PR=${{ github.event.pull_request.number }} |
| 201 | +
|
| 202 | + if [ "$BREAKING" = "true" ]; then |
| 203 | + ITEMS=$(jq -r '.items[]' "$RESPONSE_FILE" | sed 's/^/- /') |
| 204 | + gh label create breaking --color "B60205" 2>/dev/null || true |
| 205 | + gh pr edit "$PR" --add-label "breaking" |
| 206 | +
|
| 207 | + { |
| 208 | + echo "⚠️ **Potential breaking changes detected:**" |
| 209 | + echo "" |
| 210 | + echo "$ITEMS" |
| 211 | + echo "" |
| 212 | + echo "_Review carefully before merging. Consider a major version bump._" |
| 213 | + } > /tmp/breaking-comment.md |
| 214 | +
|
| 215 | + EXISTING=$(gh pr view "$PR" --json comments --jq '.comments[] | select(.body | startswith("⚠️ **Potential breaking")) | .id' | head -1) |
| 216 | + if [ -n "$EXISTING" ]; then |
| 217 | + gh api graphql -f query="mutation { updateIssueComment(input: {id: \"$EXISTING\", body: $(jq -Rs . /tmp/breaking-comment.md)}) { issueComment { id } } }" |
| 218 | + else |
| 219 | + gh pr comment "$PR" --body-file /tmp/breaking-comment.md |
| 220 | + fi |
| 221 | + else |
| 222 | + gh pr edit "$PR" --remove-label "breaking" 2>/dev/null || true |
| 223 | + fi |
0 commit comments