-
Notifications
You must be signed in to change notification settings - Fork 829
117 lines (96 loc) · 4.1 KB
/
Copy pathdetect-api-changes.yml
File metadata and controls
117 lines (96 loc) · 4.1 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
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
---
name: Detect API Changes
on:
# pull_request_target is used so the workflow can update labels and comments
# without checking out or executing code from the PR branch.
# zizmor: ignore[dangerous-triggers] -- this workflow only reads PR metadata
# via the GitHub API and never checks out or executes code from the PR branch.
pull_request_target:
types:
- opened
- synchronize
- reopened
- ready_for_review
permissions: {}
jobs:
detect-api-changes:
if: github.repository == 'prometheus/client_java'
runs-on: ubuntu-24.04
permissions:
issues: write
pull-requests: write
steps:
- name: Check for API changes and update PR
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
run: |
MARKER="<!-- api-change-detector -->"
BREAKING_LABEL="breaking-api-change"
api_files=$(gh api "repos/${REPO}/pulls/${PR_NUMBER}/files" --paginate \
--jq '.[] | select(.filename | startswith("docs/apidiffs/current_vs_latest/")) | .filename')
comment_id=$(gh api "repos/${REPO}/issues/${PR_NUMBER}/comments" --paginate \
--jq ".[] | select(.body | startswith(\"${MARKER}\")) | .id" | head -1)
if [[ -z "$api_files" ]]; then
echo "No API diff files changed."
gh pr edit "$PR_NUMBER" --repo "$REPO" --remove-label "api-change" 2>/dev/null || true
gh pr edit "$PR_NUMBER" --repo "$REPO" --remove-label "$BREAKING_LABEL" 2>/dev/null || true
if [[ -n "$comment_id" ]]; then
gh api --method DELETE "repos/${REPO}/issues/comments/${comment_id}"
fi
exit 0
fi
echo "API diff files changed:"
echo "$api_files"
gh pr edit "$PR_NUMBER" --repo "$REPO" --add-label "api-change"
modules=$(echo "$api_files" \
| sed 's|docs/apidiffs/current_vs_latest/||' \
| sed 's|\.txt$||' \
| sort \
| sed 's/^/- /')
HEAD_SHA=$(gh api "repos/${REPO}/pulls/${PR_NUMBER}" --jq .head.sha)
breaking_files=()
while IFS= read -r file; do
[[ -z "$file" ]] && continue
content=$(gh api \
-H "Accept: application/vnd.github.raw" \
"repos/${REPO}/contents/${file}?ref=${HEAD_SHA}")
if grep -Eq '^[[:space:]]*(\*\*\*!|---!|\+\+\+!)' <<<"$content"; then
breaking_files+=("$file")
fi
done <<<"$api_files"
if [[ ${#breaking_files[@]} -gt 0 ]]; then
gh pr edit "$PR_NUMBER" --repo "$REPO" --add-label "$BREAKING_LABEL"
breaking_modules=$(printf '%s\n' "${breaking_files[@]}" \
| sed 's|docs/apidiffs/current_vs_latest/||' \
| sed 's|\.txt$||' \
| sort \
| sed 's/^/- /')
body=$(cat <<EOF
${MARKER}
## :warning: Breaking API changes detected — maintainer review required
This PR modifies the published API diff for the following module(s):
${modules}
The committed API diff contains breaking-change markers for:
${breaking_modules}
Please review the changes in \`docs/apidiffs/current_vs_latest/\` carefully before approving.
EOF
)
else
gh pr edit "$PR_NUMBER" --repo "$REPO" --remove-label "$BREAKING_LABEL" 2>/dev/null || true
body=$(cat <<EOF
${MARKER}
## :warning: API changes detected — maintainer review required
This PR modifies the published API diff for the following module(s):
${modules}
Please review the changes in \`docs/apidiffs/current_vs_latest/\` carefully before approving.
EOF
)
fi
if [[ -n "$comment_id" ]]; then
gh api --method PATCH "repos/${REPO}/issues/comments/${comment_id}" \
--field body="$body"
else
gh pr comment "$PR_NUMBER" --repo "$REPO" --body "$body"
fi