Skip to content

Commit 4e186ca

Browse files
authored
Adapt pull request workflow to work properly for PRs from forked repositories (#169)
Resolves #168
1 parent 4516be1 commit 4e186ca

File tree

6 files changed

+144
-71
lines changed

6 files changed

+144
-71
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: Upload and compare benchmark results
2+
3+
on:
4+
workflow_run:
5+
types: [completed]
6+
workflows: ['Running benchmark']
7+
8+
jobs:
9+
upload-benchmark-results:
10+
if: (github.event.workflow_run.event == 'push' || github.event.workflow_run.event == 'pull_request') && github.repository == 'OptimumCode/json-schema-validator'
11+
runs-on: ubuntu-latest
12+
env:
13+
RESULTS_DIR: bench-results
14+
permissions:
15+
# deployments permission to deploy GitHub pages website
16+
deployments: write
17+
# contents permission to update benchmark contents in gh-pages branch
18+
contents: write
19+
# pull-requests permission to create comments on PR in case of alert
20+
pull-requests: write
21+
strategy:
22+
# to make sure results are submitted one by one
23+
max-parallel: 1
24+
matrix:
25+
include:
26+
- artifact-pattern: 'bench-result-*'
27+
results-name: KMP JSON schema validator
28+
alert: true
29+
- artifact-pattern: 'bench-comparison-result-*'
30+
results-name: Compare KMP JSON schema validator
31+
alert: false
32+
name: 'Process benchmark results for ${{ matrix.results-name }}'
33+
steps:
34+
- name: 'Checkout Repository'
35+
uses: actions/checkout@v4
36+
- name: Download benchmark results
37+
uses: actions/download-artifact@v4
38+
with:
39+
pattern: ${{ matrix.artifact-pattern }}
40+
path: ${{ env.RESULTS_DIR }}
41+
merge-multiple: true
42+
run-id: ${{ github.event.workflow_run.id }}
43+
- name: Show downloaded artifacts
44+
run: tree ${{ env.RESULTS_DIR }}
45+
- name: Prepare and join benchmark reports
46+
id: prep
47+
run: |
48+
for report in $(find ./${{ env.RESULTS_DIR }} -type f -name "*.json")
49+
do
50+
file_name=$(basename "$report")
51+
platform="${file_name%.*}"
52+
jq "[ .[] | .benchmark |= \"${platform}.\" + ltrimstr(\"io.github.optimumcode.json.schema.benchmark.\") | .params |= map_values(. |= split(\"/\")[-1]) ]" $report > ${{ env.RESULTS_DIR }}/$platform.json
53+
done
54+
AGGREGATED_REPORT=aggregated.json
55+
# Joined reports looks like this: [[{},{}], [{},{}]]
56+
# We need to transform them into this: [{},{}]
57+
ls ${{ env.RESULTS_DIR }}/*.json
58+
jq -s '[ .[] | .[] ]' ${{ env.RESULTS_DIR }}/*.json > $AGGREGATED_REPORT
59+
echo "report=$AGGREGATED_REPORT" >> $GITHUB_OUTPUT
60+
- name: Store benchmark result
61+
uses: benchmark-action/github-action-benchmark@v1
62+
with:
63+
name: ${{ matrix.results-name }}
64+
tool: 'jmh'
65+
output-file-path: ${{ steps.prep.outputs.report }}
66+
alert-comment-cc-users: "@OptimumCode"
67+
comment-on-alert: ${{ matrix.alert }}
68+
summary-always: true
69+
alert-threshold: '150%'
70+
fail-threshold: '200%'
71+
max-items-in-chart: 50
72+
github-token: ${{ secrets.GITHUB_TOKEN }}
73+
# Push and deploy GitHub pages branch automatically only if run in main repo and not in PR
74+
auto-push: ${{ github.event.workflow_run.event == 'push' }}
75+
76+

.github/workflows/benchmark.yml

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -138,71 +138,3 @@ jobs:
138138
with:
139139
name: bench-comparison-result-${{ matrix.os }}
140140
path: ${{ env.BENCHMARK_RESULTS }}/comparison/**/*.json
141-
142-
upload-benchmark-results:
143-
if: (github.event_name == 'push' || github.event_name == 'pull_request') && github.repository == 'OptimumCode/json-schema-validator'
144-
needs:
145-
- benchmark-matrix
146-
runs-on: ubuntu-latest
147-
env:
148-
RESULTS_DIR: bench-results
149-
permissions:
150-
# deployments permission to deploy GitHub pages website
151-
deployments: write
152-
# contents permission to update benchmark contents in gh-pages branch
153-
contents: write
154-
# pull-requests permission to create comments on PR in case of alert
155-
pull-requests: write
156-
strategy:
157-
# to make sure results are submitted one by one
158-
max-parallel: 1
159-
matrix:
160-
include:
161-
- artifact-pattern: 'bench-result-*'
162-
results-name: KMP JSON schema validator
163-
alert: true
164-
- artifact-pattern: 'bench-comparison-result-*'
165-
results-name: Compare KMP JSON schema validator
166-
alert: false
167-
name: 'Process benchmark results for ${{ matrix.results-name }}'
168-
steps:
169-
- name: 'Checkout Repository'
170-
uses: actions/checkout@v4
171-
- name: Download benchmark results
172-
uses: actions/download-artifact@v4
173-
with:
174-
pattern: ${{ matrix.artifact-pattern }}
175-
path: ${{ env.RESULTS_DIR }}
176-
merge-multiple: true
177-
- name: Show downloaded artifacts
178-
run: tree ${{ env.RESULTS_DIR }}
179-
- name: Prepare and join benchmark reports
180-
id: prep
181-
run: |
182-
for report in $(find ./${{ env.RESULTS_DIR }} -type f -name "*.json")
183-
do
184-
file_name=$(basename "$report")
185-
platform="${file_name%.*}"
186-
jq "[ .[] | .benchmark |= \"${platform}.\" + ltrimstr(\"io.github.optimumcode.json.schema.benchmark.\") | .params |= map_values(. |= split(\"/\")[-1]) ]" $report > ${{ env.RESULTS_DIR }}/$platform.json
187-
done
188-
AGGREGATED_REPORT=aggregated.json
189-
# Joined reports looks like this: [[{},{}], [{},{}]]
190-
# We need to transform them into this: [{},{}]
191-
ls ${{ env.RESULTS_DIR }}/*.json
192-
jq -s '[ .[] | .[] ]' ${{ env.RESULTS_DIR }}/*.json > $AGGREGATED_REPORT
193-
echo "report=$AGGREGATED_REPORT" >> $GITHUB_OUTPUT
194-
- name: Store benchmark result
195-
uses: benchmark-action/github-action-benchmark@v1
196-
with:
197-
name: ${{ matrix.results-name }}
198-
tool: 'jmh'
199-
output-file-path: ${{ steps.prep.outputs.report }}
200-
alert-comment-cc-users: "@OptimumCode"
201-
comment-on-alert: ${{ matrix.alert }}
202-
summary-always: true
203-
alert-threshold: '150%'
204-
fail-threshold: '200%'
205-
max-items-in-chart: 50
206-
github-token: ${{ secrets.GITHUB_TOKEN }}
207-
# Push and deploy GitHub pages branch automatically only if run in main repo and not in PR
208-
auto-push: ${{ github.event_name != 'pull_request' }}

.github/workflows/build-and-test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ on:
88
description: "enabled uploading coverage report to codecov"
99
required: false
1010
default: false
11+
collect-code-coverage:
12+
type: boolean
13+
description: "enables collecting coverage reports and uploading them as artifacts"
14+
required: false
15+
default: false
1116
secrets:
1217
CODECOV_TOKEN:
1318
description: "token to upload codecov report"
@@ -50,6 +55,7 @@ jobs:
5055
run-on: ubuntu-latest
5156
task: linuxAllTest
5257
upload-code-coverage: ${{ inputs.upload-code-coverage }}
58+
collect-code-coverage: ${{ inputs.collect-code-coverage }}
5359
secrets:
5460
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
5561
check-macos:

.github/workflows/check.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ on:
1616
description: "enabled uploading coverage report to codecov"
1717
required: false
1818
default: false
19+
collect-code-coverage:
20+
type: boolean
21+
description: "enables collecting coverage reports and uploading them as artifacts"
22+
required: false
23+
default: false
1924
secrets:
2025
CODECOV_TOKEN:
2126
description: "token to upload codecov report"
@@ -66,3 +71,9 @@ jobs:
6671
uses: codecov/codecov-action@v4
6772
with:
6873
token: ${{ secrets.CODECOV_TOKEN }}
74+
- name: Collect coverage reports
75+
if: inputs.collect-code-coverage && github.actor != 'dependabot[bot]'
76+
uses: actions/upload-artifact@v4
77+
with:
78+
name: coverage-reports
79+
path: '**/build/reports/kover/report.xml'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Upload code coverage results
2+
3+
on:
4+
workflow_run:
5+
workflows: ['Check the PR']
6+
types: [completed]
7+
8+
jobs:
9+
upload:
10+
runs-on: ubuntu-latest
11+
if: github.event.workflow_run.actor.name != 'dependabot[bot]' && github.repository == 'OptimumCode/json-schema-validator'
12+
steps:
13+
- name: 'Checkout Repository'
14+
uses: actions/checkout@v4
15+
with:
16+
ref: ${{ github.event.workflow_run.head_branch }}
17+
- name: Download benchmark results
18+
uses: actions/download-artifact@v4
19+
with:
20+
name: coverage-reports
21+
path: reports/
22+
run-id: ${{ github.event.workflow_run.id }}
23+
- name: Download PR number
24+
uses: actions/download-artifact@v4
25+
with:
26+
name: pr-number
27+
path: '.'
28+
run-id: ${{ github.event.workflow_run.id }}
29+
- id: trigger
30+
run: echo "pr-number=$(cat pr_number)" >> GITHUB_OUTPUT
31+
- name: Test pull_requests objects
32+
run: echo ${{ github.event.workflow_run.pull_requests }}
33+
- name: Upload coverage reports to Codecov
34+
uses: codecov/codecov-action@v4
35+
with:
36+
override_pr: ${{ steps.trigger.outputs.pr-number }}
37+
token: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/pull_request.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ jobs:
5353
check-pr:
5454
uses: ./.github/workflows/build-and-test.yml
5555
with:
56-
upload-code-coverage: true
57-
secrets:
58-
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
56+
collect-code-coverage: true
5957
danger-check:
6058
runs-on: ubuntu-latest
6159
permissions:
@@ -71,4 +69,17 @@ jobs:
7169
dangerfile: Dangerfile.df.kts
7270
env:
7371
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
72+
store-pr-number:
73+
runs-on: ubuntu-latest
74+
steps:
75+
- name: Save PR number
76+
env:
77+
PR_NUMBER: ${{ github.event.number }}
78+
run: |
79+
mkdir -p ./pr
80+
echo $PR_NUMBER > ./pr/pr_number
81+
- uses: actions/upload-artifact@v4
82+
with:
83+
name: pr-number
84+
path: pr/
7485

0 commit comments

Comments
 (0)