-
Notifications
You must be signed in to change notification settings - Fork 61
219 lines (188 loc) · 6.82 KB
/
analyze-pr-changes.yml
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
name: Analyze PR Changes
on: [pull_request]
jobs:
analyze:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Environment Variables
shell: bash -eo pipefail {0}
run: |
BEFORE_SHA=$(git merge-base "$BASE_SHA" "$HEAD_SHA")
AFTER_SHA=${HEAD_SHA}
CLUSTER_SPEC=/tmp/opensearch-openapi-CLUSTER.yaml
BEFORE_SPEC=/tmp/opensearch-openapi-${BEFORE_SHA}.yaml
AFTER_SPEC=/tmp/opensearch-openapi-${AFTER_SHA}.yaml
BEFORE_COVERAGE=/tmp/coverage-api-${BEFORE_SHA}.json
AFTER_COVERAGE=/tmp/coverage-api-${AFTER_SHA}.json
COVERAGE_DIFF=/tmp/coverage-api-${BEFORE_SHA}-${AFTER_SHA}-DIFF.json
vars=(
BEFORE_SHA
AFTER_SHA
CLUSTER_SPEC
BEFORE_SPEC
AFTER_SPEC
BEFORE_COVERAGE
AFTER_COVERAGE
COVERAGE_DIFF
)
{
for var in "${vars[@]}"
do
echo "${var}=${!var}"
done
} | tee "$GITHUB_ENV"
env:
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
- name: Dump OpenSearch Cluster's API
shell: bash -eo pipefail {0}
run: |
docker build coverage --tag opensearch-with-api-plugin
docker run \
--name opensearch \
--rm -d \
-p 9200:9200 -p 9600:9600 \
-e "discovery.type=single-node" \
-e OPENSEARCH_INITIAL_ADMIN_PASSWORD="$OPENSEARCH_PASSWORD" \
opensearch-with-api-plugin
npm install
npm run dump-cluster-spec -- --opensearch-insecure --output $CLUSTER_SPEC
docker stop opensearch
env:
OPENSEARCH_PASSWORD: BobgG7YrtsdKf9M
- name: Checkout BEFORE Spec
shell: bash -eo pipefail {0}
run: git checkout $BEFORE_SHA
- name: Build BEFORE Spec
shell: bash -eo pipefail {0}
run: |
npm install
npm run merge -- --source ./spec --output $BEFORE_SPEC
- name: Checkout AFTER Spec
shell: bash -eo pipefail {0}
run: git checkout $AFTER_SHA
- name: Build AFTER Spec
shell: bash -eo pipefail {0}
run: |
npm install
npm run merge -- --source ./spec --output $AFTER_SPEC
- name: Calculate Coverage
shell: bash -eo pipefail {0}
run: |
npm run coverage:spec -- \
--cluster $CLUSTER_SPEC \
--specification $BEFORE_SPEC \
--output $BEFORE_COVERAGE
npm run coverage:spec -- \
--cluster $CLUSTER_SPEC \
--specification $AFTER_SPEC \
--output $AFTER_COVERAGE
jq . $AFTER_COVERAGE
jq --slurp '
[ .[].counts ]
| {
"before": (.[0]),
"after": (.[1]),
"covered_delta": (.[1].covered - .[0].covered),
"covered_pct_delta": ((.[1].covered_pct - .[0].covered_pct) * 100 | round / 100),
"uncovered_delta": (.[1].uncovered - .[0].uncovered),
"uncovered_pct_delta": ((.[1].uncovered_pct - .[0].uncovered_pct) * 100 | round / 100),
"specified_but_not_provided_delta": (.[1].specified_but_not_provided - .[0].specified_but_not_provided)
}
' \
$BEFORE_COVERAGE \
$AFTER_COVERAGE \
| tee $COVERAGE_DIFF
- name: Upload Coverage Data
id: upload-coverage
uses: actions/upload-artifact@v4
with:
name: coverage-api
path: |
/tmp/coverage-api-*.json
- name: Install openapi-changes
shell: bash -eo pipefail {0}
run: npm install --global @pb33f/openapi-changes
- name: Generate API Changes HTML Report
shell: bash -eo pipefail {0}
run: openapi-changes html-report --no-logo --no-color $BEFORE_SPEC $AFTER_SPEC
- name: Upload API Changes HTML Report
id: upload-api-changes-report
uses: actions/upload-artifact@v4
with:
name: api-changes-report
path: |
report.html
/tmp/opensearch-openapi-*.yaml
- name: Generate API Changes Summary
shell: bash -eo pipefail {0}
run: |
if ! openapi-changes summary --no-logo --no-color --markdown $BEFORE_SPEC $AFTER_SPEC >output.md ; then
if ! grep -q 'breaking changes discovered' output.md ; then
cat output.md >/dev/stderr
exit 1
fi
fi
gawk '
BEGIN {
RS = "(\r|\n|\r\n)"
WAS_BLANK = 0
HAD_CHANGES = 0
}
/^starting work/ || /^Building original model/ || /^Date:/ || /^SPEC:/ || /^ERROR:/ || /^DONE:/ {
next
}
/^[[:space:]]*$/ {
WAS_BLANK = 1
next
}
WAS_BLANK {
WAS_BLANK = 0
print ""
}
{
HAD_CHANGES = 1
print
}
END {
if (!HAD_CHANGES) {
print "**NO CHANGES**\n"
}
}
' output.md | tee changes-summary.md
- name: Construct Comment Data Payload
shell: bash -eo pipefail {0}
run: |
jq \
--arg pr_number ${PR_NUMBER} \
--arg before_sha ${BEFORE_SHA} \
--arg after_sha ${AFTER_SHA} \
--arg api_changes_report_url "${API_CHANGES_REPORT_URL}" \
--rawfile api_changes_summary ./changes-summary.md \
--slurpfile api_coverage $COVERAGE_DIFF \
--null-input '
{
"pr_number": ($pr_number),
"comment_identifier": "# Changes Analysis",
"template_name": "pr-change-analysis",
"template_data": {
"before_sha": ($before_sha),
"after_sha": ($after_sha),
"api_changes_report_url": ($api_changes_report_url),
"api_changes_summary": ($api_changes_summary),
"api_coverage": ($api_coverage[0])
}
}
' | tee pr-comment.json
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
API_CHANGES_REPORT_URL: ${{ steps.upload-api-changes-report.outputs.artifact-url }}
- name: Upload PR Comment Payload
uses: actions/upload-artifact@v4
with:
name: pr-comment
path: pr-comment.json