Skip to content

Commit dcbf6e1

Browse files
committed
feat: add commit status for playwright/backend test
1 parent b96c61f commit dcbf6e1

File tree

3 files changed

+170
-11
lines changed

3 files changed

+170
-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: 49 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:
@@ -31,7 +36,19 @@ jobs:
3136
backend-tests:
3237
runs-on: ubuntu-latest
3338
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
39+
outputs:
40+
start_time: ${{ steps.run-tests.outputs.timestamp }}
41+
status: ${{ steps.set-pending-status.outcome }}
3442
steps:
43+
- name: Set pending commit status
44+
id: set-pending-status
45+
run: |
46+
echo "timestamp=$(date +%s)" >> $GITHUB_OUTPUT
47+
curl -X POST -H "Authorization: Bearer ${{ github.token }}" \
48+
-H "Accept: application/vnd.github+json" \
49+
https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.sha }} \
50+
-d "{\"state\": \"pending\", \"context\": \"Backend Tests : ${{env.BASE_URL}}\", \"target_url\": \"https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}\"}"
51+
3552
- name: Checkout code
3653
uses: actions/checkout@v4
3754

@@ -43,6 +60,7 @@ jobs:
4360

4461
- name: Run Backend Test
4562
working-directory: tests/govtool-backend
63+
id: run-tests
4664
run: |
4765
python -m pip install --upgrade pip
4866
pip install -r requirements.txt
@@ -55,22 +73,23 @@ jobs:
5573
fi
5674
python ./setup.py
5775
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 }}
6276
6377
- name: Upload report
6478
if: always()
6579
uses: actions/upload-artifact@v4
6680
with:
6781
name: allure-results
6882
path: tests/govtool-backend/allure-results
83+
env:
84+
NETWORK: ${{ inputs.network || vars.NETWORK }}
85+
KUBER_API_KEY: ${{ secrets.KUBER_API_KEY }}
6986

7087
publish-report:
7188
runs-on: ubuntu-latest
7289
if: always() && needs.backend-tests.result != 'skipped'
7390
needs: backend-tests
91+
outputs:
92+
report_number: ${{ steps.report-details.outputs.report_number }}
7493
steps:
7594
- uses: actions/checkout@v4
7695
- name: Download results
@@ -134,6 +153,29 @@ jobs:
134153
folder: build
135154
target-folder: ${{ env.REPORT_NAME }}
136155

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

.github/workflows/test_integration_playwright.yml

Lines changed: 46 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,15 +31,28 @@ on:
2631
workflow_run:
2732
workflows: ["Build and deploy GovTool test stack"]
2833
types: [completed]
34+
2935

3036
jobs:
3137
integration-tests:
3238
runs-on: ubuntu-latest
3339
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
40+
outputs:
41+
start_time: ${{ steps.set-pending-status.outputs.timestamp }}
42+
status: ${{ steps.run-test.outcome }}
3443
defaults:
3544
run:
3645
working-directory: tests/govtool-frontend/playwright
3746
steps:
47+
- name: Set pending commit status
48+
id: set-pending-status
49+
run: |
50+
echo "timestamp=$(date +%s)" >> $GITHUB_OUTPUT
51+
curl -X POST -H "Authorization: Bearer ${{ github.token }}" \
52+
-H "Accept: application/vnd.github+json" \
53+
https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.sha }} \
54+
-d "{\"state\": \"pending\", \"context\": \"Playwright Tests : ${{env.HOST_URL}}\", \"target_url\": \"https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}\"}"
55+
3856
- uses: actions/checkout@v4
3957
- uses: actions/setup-node@v4
4058
with:
@@ -58,6 +76,7 @@ jobs:
5876
run: npx playwright install --with-deps
5977

6078
- name: Run tests
79+
id: run-test
6180
run: |
6281
mkdir -p ./lib/_mock
6382
chmod +w ./lib/_mock
@@ -95,7 +114,6 @@ jobs:
95114
path: tests/govtool-frontend/playwright/lock_logs.txt
96115

97116
env:
98-
HOST_URL: https://${{inputs.deployment || 'govtool.cardanoapi.io' }}
99117
API_URL: https://${{inputs.deployment || 'govtool.cardanoapi.io' }}/api
100118
DOCS_URL: ${{ vars.DOCS_URL }}
101119
KUBER_API_KEY: ${{secrets.KUBER_API_KEY}}
@@ -111,6 +129,8 @@ jobs:
111129
runs-on: ubuntu-latest
112130
if: always() && needs.integration-tests.result != 'skipped'
113131
needs: integration-tests
132+
outputs:
133+
report_number: ${{ steps.report-details.outputs.report_number }}
114134
steps:
115135
- uses: actions/checkout@v4
116136
- name: Download report
@@ -175,6 +195,28 @@ jobs:
175195
folder: build
176196
target-folder: ${{ env.REPORT_NAME }}
177197

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

0 commit comments

Comments
 (0)