Skip to content

Commit 3d0099c

Browse files
authored
Merge pull request #2919 from IntersectMBO/feat/test-commit-status
Feat: test commit status and queue management
2 parents 9ab9ada + a6bbd99 commit 3d0099c

File tree

3 files changed

+178
-11
lines changed

3 files changed

+178
-11
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
3+
# Ensure required environment variables are set
4+
if [ -z "$GITHUB_REPOSITORY" ] || [ -z "$GITHUB_SHA" ] || [ -z "$GITHUB_TOKEN" ] || [ -z "$GITHUB_RUN_ID" ]; then
5+
echo "Missing required environment variables!"
6+
exit 1
7+
fi
8+
9+
# Retrieve necessary variables from workflow
10+
START_TIME=${START_TIME:-$(date +%s)}
11+
TEST_STATUS=${TEST_STATUS:-"failure"}
12+
REPORT_NUMBER=${REPORT_NUMBER:-1}
13+
REPORT_NAME=${REPORT_NAME:-"govtool-frontend"}
14+
HOST_URL=${HOST_URL:-"https://govtool.cardanoapi.io"}
15+
CONTEXT="Playwright Tests : $HOST_URL"
16+
17+
if [[ "$REPORT_NAME" == "govtool-backend" ]]; then
18+
CONTEXT="Backend Tests : $BASE_URL"
19+
fi
20+
21+
# Parse Allure JSON results
22+
if [ -z "$(ls -A allure-results/*.json 2>/dev/null)" ]; then
23+
echo "No Allure JSON results found!"
24+
PASSED=0
25+
FAILED=0
26+
TOTAL=0
27+
else
28+
PASSED=$(jq -s '[.[] | select(.status == "passed")] | length' allure-results/*.json)
29+
FAILED=$(jq -s '[.[] | select(.status == "failed")] | length' allure-results/*.json)
30+
TOTAL=$((PASSED + FAILED))
31+
fi
32+
33+
34+
35+
# Calculate test duration
36+
CURRENT_TIME=$(date +%s)
37+
TEST_DURATION_SECONDS=$((CURRENT_TIME - START_TIME))
38+
TEST_DURATION_MINUTES=$((TEST_DURATION_SECONDS / 60))
39+
TEST_DURATION_HOURS=$((TEST_DURATION_MINUTES / 60))
40+
41+
if [ "$TEST_DURATION_HOURS" -gt 0 ]; then
42+
TEST_DURATION="${TEST_DURATION_HOURS} hour$( [ "$TEST_DURATION_HOURS" -ne 1 ] && echo "s"), $((TEST_DURATION_MINUTES % 60)) minute$( [ "$((TEST_DURATION_MINUTES % 60))" -ne 1 ] && echo "s"), and $((TEST_DURATION_SECONDS % 60)) second$( [ "$((TEST_DURATION_SECONDS % 60))" -ne 1 ] && echo "s")"
43+
elif [ "$TEST_DURATION_MINUTES" -gt 0 ]; then
44+
TEST_DURATION="${TEST_DURATION_MINUTES} minute$( [ "$TEST_DURATION_MINUTES" -ne 1 ] && echo "s") and $((TEST_DURATION_SECONDS % 60)) second$( [ "$((TEST_DURATION_SECONDS % 60))" -ne 1 ] && echo "s")"
45+
else
46+
TEST_DURATION="${TEST_DURATION_SECONDS} second$( [ "$TEST_DURATION_SECONDS" -ne 1 ] && echo "s")"
47+
fi
48+
49+
# Determine target URL based on environment
50+
case "$GH_PAGES" in
51+
"intersectMBO/govtool-test-reports") TARGET_URL="https://intersectmbo.github.io/govtool-test-reports/${REPORT_NAME}/${REPORT_NUMBER}" ;;
52+
"cardanoapi/govtool-test-reports") TARGET_URL="https://cardanoapi.github.io/govtool-test-reports/${REPORT_NAME}/${REPORT_NUMBER}" ;;
53+
*) TARGET_URL="https://cardanoapi.github.io/govtool-test-reports/${REPORT_NAME}/${REPORT_NUMBER}" ;;
54+
esac
55+
56+
# Determine test result message
57+
if [[ "$TEST_STATUS" == "success" ]]; then
58+
DESCRIPTION="Tests passed in ${TEST_DURATION}"
59+
elif [[ "$TEST_STATUS" == "failure" && "$TOTAL" -ne 0 ]]; then
60+
DESCRIPTION="Tests failed in ${TEST_DURATION}: Passed ${PASSED}, Failed ${FAILED} out of ${TOTAL}"
61+
else
62+
DESCRIPTION="⚠️ Tests execution failed :$TEST_STATUS"
63+
TEST_STATUS="error"
64+
TARGET_URL="https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
65+
fi
66+
67+
68+
69+
# Send commit status update to GitHub
70+
curl -X POST -H "Authorization: Bearer ${GITHUB_TOKEN}" \
71+
-H "Accept: application/vnd.github+json" \
72+
https://api.github.com/repos/${GITHUB_REPOSITORY}/statuses/${GITHUB_SHA} \
73+
-d "{\"state\": \"${TEST_STATUS}\", \"context\": \"${CONTEXT}\", \"description\": \"${DESCRIPTION}\", \"target_url\": \"${TARGET_URL}\"}"
74+
75+
echo "Commit status updated successfully!"

.github/workflows/test_backend.yml

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
name: Backend Test
22

3+
permissions:
4+
contents: read
5+
checks: write
6+
statuses: write
7+
38
on:
49
workflow_dispatch:
510
inputs:
@@ -27,14 +32,31 @@ on:
2732
workflows: ["Build and deploy GovTool test stack"]
2833
types: [completed]
2934

35+
concurrency:
36+
group: ${{ github.workflow }}-${{ github.ref }}
37+
cancel-in-progress: false
38+
3039
jobs:
3140
backend-tests:
3241
runs-on: ubuntu-latest
3342
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
43+
outputs:
44+
start_time: ${{ steps.set-pending-status.outputs.timestamp }}
45+
status: ${{ steps.run-tests.outcome }}
3446
steps:
3547
- name: Checkout code
3648
uses: actions/checkout@v4
3749

50+
- name: Set pending commit status
51+
id: set-pending-status
52+
run: |
53+
echo "timestamp=$(date +%s)" >> $GITHUB_OUTPUT
54+
curl -X POST -H "Authorization: Bearer ${{ github.token }}" \
55+
-H "Accept: application/vnd.github+json" \
56+
https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.sha }} \
57+
-d "{\"state\": \"pending\", \"context\": \"Backend Tests : ${{env.BASE_URL}}\", \"target_url\": \"https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}\"}"
58+
59+
3860
- name: Set up Python
3961
uses: actions/setup-python@v4
4062
with:
@@ -43,6 +65,7 @@ jobs:
4365

4466
- name: Run Backend Test
4567
working-directory: tests/govtool-backend
68+
id: run-tests
4669
run: |
4770
python -m pip install --upgrade pip
4871
pip install -r requirements.txt
@@ -55,22 +78,23 @@ jobs:
5578
fi
5679
python ./setup.py
5780
python -m pytest --alluredir allure-results
58-
env:
59-
BASE_URL: https://${{inputs.deployment || 'govtool.cardanoapi.io/api' }}
60-
NETWORK: ${{ inputs.network || vars.NETWORK }}
61-
KUBER_API_KEY: ${{ secrets.KUBER_API_KEY }}
6281
6382
- name: Upload report
6483
if: always()
6584
uses: actions/upload-artifact@v4
6685
with:
6786
name: allure-results
6887
path: tests/govtool-backend/allure-results
88+
env:
89+
NETWORK: ${{ inputs.network || vars.NETWORK }}
90+
KUBER_API_KEY: ${{ secrets.KUBER_API_KEY }}
6991

7092
publish-report:
7193
runs-on: ubuntu-latest
7294
if: always() && needs.backend-tests.result != 'skipped'
7395
needs: backend-tests
96+
outputs:
97+
report_number: ${{ steps.report-details.outputs.report_number }}
7498
steps:
7599
- uses: actions/checkout@v4
76100
- name: Download results
@@ -134,6 +158,29 @@ jobs:
134158
folder: build
135159
target-folder: ${{ env.REPORT_NAME }}
136160

137-
env:
138-
REPORT_NAME: govtool-backend
139-
GH_PAGES: ${{vars.GH_PAGES}}
161+
publish-status:
162+
runs-on: ubuntu-latest
163+
if: always()
164+
needs: [backend-tests, publish-report]
165+
steps:
166+
- uses: actions/checkout@v4
167+
- name: Download results
168+
uses: actions/download-artifact@v4
169+
with:
170+
name: allure-results
171+
path: allure-results
172+
- name: Set Commit Status
173+
if: always()
174+
run: |
175+
chmod +x .github/scripts/set_commit_status.sh
176+
.github/scripts/set_commit_status.sh
177+
env:
178+
START_TIME: ${{ needs.backend-tests.outputs.start_time }}
179+
TEST_STATUS: ${{ needs.backend-tests.outputs.status }}
180+
REPORT_NUMBER: ${{ needs.publish-report.outputs.report_number }}
181+
GITHUB_TOKEN: ${{ github.token }}
182+
183+
env:
184+
BASE_URL: https://${{inputs.deployment || 'govtool.cardanoapi.io/api' }}
185+
REPORT_NAME: govtool-backend
186+
GH_PAGES: ${{vars.GH_PAGES}}

.github/workflows/test_integration_playwright.yml

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
name: Integration Test [Playwright]
22

3+
permissions:
4+
contents: read
5+
checks: write
6+
statuses: write
7+
38
on:
49
workflow_dispatch:
510
inputs:
@@ -26,16 +31,32 @@ on:
2631
workflow_run:
2732
workflows: ["Build and deploy GovTool test stack"]
2833
types: [completed]
34+
35+
concurrency:
36+
group: ${{ github.workflow }}-${{ github.ref }}
37+
cancel-in-progress: false
2938

3039
jobs:
3140
integration-tests:
3241
runs-on: ubuntu-latest
3342
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
43+
outputs:
44+
start_time: ${{ steps.set-pending-status.outputs.timestamp }}
45+
status: ${{ steps.run-test.outcome }}
3446
defaults:
3547
run:
3648
working-directory: tests/govtool-frontend/playwright
3749
steps:
3850
- uses: actions/checkout@v4
51+
- name: Set pending commit status
52+
id: set-pending-status
53+
run: |
54+
echo "timestamp=$(date +%s)" >> $GITHUB_OUTPUT
55+
curl -X POST -H "Authorization: Bearer ${{ github.token }}" \
56+
-H "Accept: application/vnd.github+json" \
57+
https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.sha }} \
58+
-d "{\"state\": \"pending\", \"context\": \"Playwright Tests : ${{env.HOST_URL}}\", \"target_url\": \"https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}\"}"
59+
3960
- uses: actions/setup-node@v4
4061
with:
4162
node-version: "18"
@@ -58,6 +79,7 @@ jobs:
5879
run: npx playwright install --with-deps
5980

6081
- name: Run tests
82+
id: run-test
6183
run: |
6284
mkdir -p ./lib/_mock
6385
chmod +w ./lib/_mock
@@ -95,7 +117,6 @@ jobs:
95117
path: tests/govtool-frontend/playwright/lock_logs.txt
96118

97119
env:
98-
HOST_URL: https://${{inputs.deployment || 'govtool.cardanoapi.io' }}
99120
API_URL: https://${{inputs.deployment || 'govtool.cardanoapi.io' }}/api
100121
DOCS_URL: ${{ vars.DOCS_URL }}
101122
KUBER_API_KEY: ${{secrets.KUBER_API_KEY}}
@@ -111,6 +132,8 @@ jobs:
111132
runs-on: ubuntu-latest
112133
if: always() && needs.integration-tests.result != 'skipped'
113134
needs: integration-tests
135+
outputs:
136+
report_number: ${{ steps.report-details.outputs.report_number }}
114137
steps:
115138
- uses: actions/checkout@v4
116139
- name: Download report
@@ -175,6 +198,28 @@ jobs:
175198
folder: build
176199
target-folder: ${{ env.REPORT_NAME }}
177200

178-
env:
179-
REPORT_NAME: govtool-frontend
180-
GH_PAGES: ${{vars.GH_PAGES}}
201+
publish-status:
202+
runs-on: ubuntu-latest
203+
if: always()
204+
needs: [integration-tests, publish-report]
205+
steps:
206+
- uses: actions/checkout@v4
207+
- name: Download results
208+
uses: actions/download-artifact@v4
209+
with:
210+
name: allure-results
211+
path: allure-results
212+
- name: Set Commit Status
213+
if: always()
214+
run: |
215+
chmod +x .github/scripts/set_commit_status.sh
216+
.github/scripts/set_commit_status.sh
217+
env:
218+
START_TIME: ${{ needs.integration-tests.outputs.start_time }}
219+
TEST_STATUS: ${{ needs.integration-tests.outputs.status }}
220+
REPORT_NUMBER: ${{ needs.publish-report.outputs.report_number }}
221+
GITHUB_TOKEN: ${{ github.token }}
222+
env:
223+
HOST_URL: https://${{inputs.deployment || 'govtool.cardanoapi.io' }}
224+
REPORT_NAME: govtool-frontend
225+
GH_PAGES: ${{vars.GH_PAGES}}

0 commit comments

Comments
 (0)