Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: test commit status and queue management #2919

Merged
merged 3 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions .github/scripts/set_commit_status.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

# Ensure required environment variables are set
if [ -z "$GITHUB_REPOSITORY" ] || [ -z "$GITHUB_SHA" ] || [ -z "$GITHUB_TOKEN" ] || [ -z "$GITHUB_RUN_ID" ]; then
echo "Missing required environment variables!"
exit 1
fi

# Retrieve necessary variables from workflow
START_TIME=${START_TIME:-$(date +%s)}
TEST_STATUS=${TEST_STATUS:-"failure"}
REPORT_NUMBER=${REPORT_NUMBER:-1}
REPORT_NAME=${REPORT_NAME:-"govtool-frontend"}
HOST_URL=${HOST_URL:-"https://govtool.cardanoapi.io"}
CONTEXT="Playwright Tests : $HOST_URL"

if [[ "$REPORT_NAME" == "govtool-backend" ]]; then
CONTEXT="Backend Tests : $BASE_URL"
fi

# Parse Allure JSON results
if [ -z "$(ls -A allure-results/*.json 2>/dev/null)" ]; then
echo "No Allure JSON results found!"
PASSED=0
FAILED=0
TOTAL=0
else
PASSED=$(jq -s '[.[] | select(.status == "passed")] | length' allure-results/*.json)
FAILED=$(jq -s '[.[] | select(.status == "failed")] | length' allure-results/*.json)
TOTAL=$((PASSED + FAILED))
fi



# Calculate test duration
CURRENT_TIME=$(date +%s)
TEST_DURATION_SECONDS=$((CURRENT_TIME - START_TIME))
TEST_DURATION_MINUTES=$((TEST_DURATION_SECONDS / 60))
TEST_DURATION_HOURS=$((TEST_DURATION_MINUTES / 60))

if [ "$TEST_DURATION_HOURS" -gt 0 ]; then
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")"
elif [ "$TEST_DURATION_MINUTES" -gt 0 ]; then
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")"
else
TEST_DURATION="${TEST_DURATION_SECONDS} second$( [ "$TEST_DURATION_SECONDS" -ne 1 ] && echo "s")"
fi

# Determine target URL based on environment
case "$GH_PAGES" in
"intersectMBO/govtool-test-reports") TARGET_URL="https://intersectmbo.github.io/govtool-test-reports/${REPORT_NAME}/${REPORT_NUMBER}" ;;
"cardanoapi/govtool-test-reports") TARGET_URL="https://cardanoapi.github.io/govtool-test-reports/${REPORT_NAME}/${REPORT_NUMBER}" ;;
*) TARGET_URL="https://cardanoapi.github.io/govtool-test-reports/${REPORT_NAME}/${REPORT_NUMBER}" ;;
esac

# Determine test result message
if [[ "$TEST_STATUS" == "success" ]]; then
DESCRIPTION="Tests passed in ${TEST_DURATION}"
elif [[ "$TEST_STATUS" == "failure" && "$TOTAL" -ne 0 ]]; then
DESCRIPTION="Tests failed in ${TEST_DURATION}: Passed ${PASSED}, Failed ${FAILED} out of ${TOTAL}"
else
DESCRIPTION="⚠️ Tests execution failed :$TEST_STATUS"
TEST_STATUS="error"
TARGET_URL="https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
fi



# Send commit status update to GitHub
curl -X POST -H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${GITHUB_REPOSITORY}/statuses/${GITHUB_SHA} \
-d "{\"state\": \"${TEST_STATUS}\", \"context\": \"${CONTEXT}\", \"description\": \"${DESCRIPTION}\", \"target_url\": \"${TARGET_URL}\"}"

echo "Commit status updated successfully!"
61 changes: 54 additions & 7 deletions .github/workflows/test_backend.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
name: Backend Test

permissions:
contents: read
checks: write
statuses: write

on:
workflow_dispatch:
inputs:
Expand Down Expand Up @@ -27,14 +32,31 @@ on:
workflows: ["Build and deploy GovTool test stack"]
types: [completed]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false

jobs:
backend-tests:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
outputs:
start_time: ${{ steps.set-pending-status.outputs.timestamp }}
status: ${{ steps.run-tests.outcome }}
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set pending commit status
id: set-pending-status
run: |
echo "timestamp=$(date +%s)" >> $GITHUB_OUTPUT
curl -X POST -H "Authorization: Bearer ${{ github.token }}" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.sha }} \
-d "{\"state\": \"pending\", \"context\": \"Backend Tests : ${{env.BASE_URL}}\", \"target_url\": \"https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}\"}"


- name: Set up Python
uses: actions/setup-python@v4
with:
Expand All @@ -43,6 +65,7 @@ jobs:

- name: Run Backend Test
working-directory: tests/govtool-backend
id: run-tests
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
Expand All @@ -55,22 +78,23 @@ jobs:
fi
python ./setup.py
python -m pytest --alluredir allure-results
env:
BASE_URL: https://${{inputs.deployment || 'govtool.cardanoapi.io/api' }}
NETWORK: ${{ inputs.network || vars.NETWORK }}
KUBER_API_KEY: ${{ secrets.KUBER_API_KEY }}

- name: Upload report
if: always()
uses: actions/upload-artifact@v4
with:
name: allure-results
path: tests/govtool-backend/allure-results
env:
NETWORK: ${{ inputs.network || vars.NETWORK }}
KUBER_API_KEY: ${{ secrets.KUBER_API_KEY }}

publish-report:
runs-on: ubuntu-latest
if: always() && needs.backend-tests.result != 'skipped'
needs: backend-tests
outputs:
report_number: ${{ steps.report-details.outputs.report_number }}
steps:
- uses: actions/checkout@v4
- name: Download results
Expand Down Expand Up @@ -134,6 +158,29 @@ jobs:
folder: build
target-folder: ${{ env.REPORT_NAME }}

env:
REPORT_NAME: govtool-backend
GH_PAGES: ${{vars.GH_PAGES}}
publish-status:
runs-on: ubuntu-latest
if: always()
needs: [backend-tests, publish-report]
steps:
- uses: actions/checkout@v4
- name: Download results
uses: actions/download-artifact@v4
with:
name: allure-results
path: allure-results
- name: Set Commit Status
if: always()
run: |
chmod +x .github/scripts/set_commit_status.sh
.github/scripts/set_commit_status.sh
env:
START_TIME: ${{ needs.backend-tests.outputs.start_time }}
TEST_STATUS: ${{ needs.backend-tests.outputs.status }}
REPORT_NUMBER: ${{ needs.publish-report.outputs.report_number }}
GITHUB_TOKEN: ${{ github.token }}

env:
BASE_URL: https://${{inputs.deployment || 'govtool.cardanoapi.io/api' }}
REPORT_NAME: govtool-backend
GH_PAGES: ${{vars.GH_PAGES}}
53 changes: 49 additions & 4 deletions .github/workflows/test_integration_playwright.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
name: Integration Test [Playwright]

permissions:
contents: read
checks: write
statuses: write

on:
workflow_dispatch:
inputs:
Expand All @@ -26,16 +31,32 @@ on:
workflow_run:
workflows: ["Build and deploy GovTool test stack"]
types: [completed]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false

jobs:
integration-tests:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
outputs:
start_time: ${{ steps.set-pending-status.outputs.timestamp }}
status: ${{ steps.run-test.outcome }}
defaults:
run:
working-directory: tests/govtool-frontend/playwright
steps:
- uses: actions/checkout@v4
- name: Set pending commit status
id: set-pending-status
run: |
echo "timestamp=$(date +%s)" >> $GITHUB_OUTPUT
curl -X POST -H "Authorization: Bearer ${{ github.token }}" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.sha }} \
-d "{\"state\": \"pending\", \"context\": \"Playwright Tests : ${{env.HOST_URL}}\", \"target_url\": \"https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}\"}"

- uses: actions/setup-node@v4
with:
node-version: "18"
Expand All @@ -58,6 +79,7 @@ jobs:
run: npx playwright install --with-deps

- name: Run tests
id: run-test
run: |
mkdir -p ./lib/_mock
chmod +w ./lib/_mock
Expand Down Expand Up @@ -95,7 +117,6 @@ jobs:
path: tests/govtool-frontend/playwright/lock_logs.txt

env:
HOST_URL: https://${{inputs.deployment || 'govtool.cardanoapi.io' }}
API_URL: https://${{inputs.deployment || 'govtool.cardanoapi.io' }}/api
DOCS_URL: ${{ vars.DOCS_URL }}
KUBER_API_KEY: ${{secrets.KUBER_API_KEY}}
Expand All @@ -111,6 +132,8 @@ jobs:
runs-on: ubuntu-latest
if: always() && needs.integration-tests.result != 'skipped'
needs: integration-tests
outputs:
report_number: ${{ steps.report-details.outputs.report_number }}
steps:
- uses: actions/checkout@v4
- name: Download report
Expand Down Expand Up @@ -175,6 +198,28 @@ jobs:
folder: build
target-folder: ${{ env.REPORT_NAME }}

env:
REPORT_NAME: govtool-frontend
GH_PAGES: ${{vars.GH_PAGES}}
publish-status:
runs-on: ubuntu-latest
if: always()
needs: [integration-tests, publish-report]
steps:
- uses: actions/checkout@v4
- name: Download results
uses: actions/download-artifact@v4
with:
name: allure-results
path: allure-results
- name: Set Commit Status
if: always()
run: |
chmod +x .github/scripts/set_commit_status.sh
.github/scripts/set_commit_status.sh
env:
START_TIME: ${{ needs.integration-tests.outputs.start_time }}
TEST_STATUS: ${{ needs.integration-tests.outputs.status }}
REPORT_NUMBER: ${{ needs.publish-report.outputs.report_number }}
GITHUB_TOKEN: ${{ github.token }}
env:
HOST_URL: https://${{inputs.deployment || 'govtool.cardanoapi.io' }}
REPORT_NAME: govtool-frontend
GH_PAGES: ${{vars.GH_PAGES}}