Skip to content

Code Coverage

Code Coverage #6185

Workflow file for this run

name: Code Coverage
on:
push:
branches:
- main
pull_request:
branches:
- main
# have the ability to trigger this workflow manually
workflow_dispatch:
jobs:
build:
name: Unit Test and SonarCloud Scan
runs-on: ubuntu-latest
env:
ANSIBLE_AI_DATABASE_HOST: localhost
ANSIBLE_AI_DATABASE_NAME: wisdom
ANSIBLE_AI_DATABASE_PASSWORD: wisdom
ANSIBLE_AI_DATABASE_USER: wisdom
DJANGO_SETTINGS_MODULE: ansible_ai_connect.main.settings.development
ENABLE_ANSIBLE_LINT_POSTPROCESS: True
PYTHONUNBUFFERED: 1
SECRET_KEY: somesecret
services:
postgres:
image: docker.io/library/postgres:alpine
env:
POSTGRES_USER: wisdom
POSTGRES_PASSWORD: wisdom
POSTGRES_DB: wisdom
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
##############
# Python tests
##############
- name: Setup Django server
uses: ./.github/actions/setup-django-server
with:
start-server: 'false'
- name: Running Unit Tests (Python)
run: |
coverage run --rcfile=setup.cfg -m ansible_ai_connect.manage test ansible_ai_connect
coverage xml
coverage report --rcfile=setup.cfg --format=markdown > code-coverage-results.md
# See https://sonarsource.atlassian.net/browse/SONARPY-1203
- name: Fix paths in coverage file
run: |
sed -i 's,/home/runner/work/ansible-wisdom-service/ansible-wisdom-service/,/github/workspace/,g' coverage.xml
##################
# TypeScript tests
##################
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: '21.x'
cache: 'npm'
cache-dependency-path: ./ansible_ai_connect_admin_portal/package-lock.json
- name: Install Dependencies (TypeScript)
run: npm ci
working-directory: ./ansible_ai_connect_admin_portal
- name: Running Unit Tests (TypeScript)
run: npm run test
working-directory: ./ansible_ai_connect_admin_portal
############################
# TypeScript tests (chatbot)
############################
- name: Use Node.js (chatbot)
uses: actions/setup-node@v3
with:
node-version: '21.x'
cache: 'npm'
cache-dependency-path: ./ansible_ai_connect_chatbot/package-lock.json
- name: Install Dependencies (TypeScript) (chatbot)
run: npm install
working-directory: ./ansible_ai_connect_chatbot
- name: Install Chromium dependencies (chatbot)
run: npx playwright install-deps chromium
working-directory: ./ansible_ai_connect_chatbot
- name: Install Chromium (chatbot)
run: npx playwright install chromium
working-directory: ./ansible_ai_connect_chatbot
- name: Running Unit Tests with code coverage (TypeScript) (chatbot)
run: npm run coverage
working-directory: ./ansible_ai_connect_chatbot
#####################
# SonarCloud coverage
#####################
- name: SonarCloud Scan
uses: SonarSource/sonarqube-scan-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
####################
# OpenAPI file check
####################
- name: Start Django server for OpenAPI generation
shell: bash
run: |
make run-server &
sleep 10
make create-cachetable
- name: Ensure the OpenAPI file is up to date
run: |
make update-openapi-schema
git diff --exit-code -- tools/openapi-schema/ansible-ai-connect-service.yaml
git diff --exit-code -- tools/openapi-schema/ansible-ai-connect-service.json
- name: Validate OpenAPI schema
continue-on-error: true
run: |
python3 -c "
from openapi_spec_validator import validate_url
from openapi_spec_validator.validation.validators import OpenAPIV30SpecValidator
validate_url('http://localhost:8000/api/schema/', cls=OpenAPIV30SpecValidator)
"
- name: Upload OpenAPI spec artifact (for sync on push to main)
if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/main'
uses: actions/upload-artifact@v4
with:
name: openapi-schema
path: tools/openapi-schema/ansible-ai-connect-service.json
retention-days: 1
sync-to-central-repo:
name: Sync spec to central repo
needs: [build]
if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Download spec artifact from component workflow
uses: actions/download-artifact@v4
with:
name: openapi-schema
path: ./component-spec
- name: Verify spec file exists
run: |
SPEC_FILE="./component-spec/ansible-ai-connect-service.json"
if [ ! -f "$SPEC_FILE" ]; then
echo "❌ Spec file not found at $SPEC_FILE"
echo "Contents of artifact:"
ls -la ./component-spec/
exit 1
fi
echo "✅ Found spec file at $SPEC_FILE"
- name: Checkout spec repo
uses: actions/checkout@v4
with:
repository: ansible-automation-platform/aap-openapi-specs
ref: ${{ github.ref_name }}
path: spec-repo
token: ${{ secrets.OPENAPI_SPEC_SYNC_TOKEN }}
- name: Check if branch exists in spec repo
id: check_branch
working-directory: spec-repo
run: |
BRANCH="${{ github.ref_name }}"
# Check if branch exists locally (already checked out)
if git rev-parse --verify HEAD >/dev/null 2>&1; then
echo "✅ Branch '$BRANCH' exists in spec repo"
echo "branch_exists=true" >> $GITHUB_OUTPUT
else
echo "❌ Branch '$BRANCH' does NOT exist in spec repo"
echo "branch_exists=false" >> $GITHUB_OUTPUT
fi
- name: Fail if branch doesn't exist
if: steps.check_branch.outputs.branch_exists != 'true'
run: |
echo "##[error]❌ Branch '${{ github.ref_name }}' does not exist in the central spec repository."
echo "##[error]Expected branch: ${{ github.ref_name }}"
echo "##[error]This branch must be created in the spec repo before specs can be synced."
exit 1
- name: Compare specs
id: compare
run: |
COMPONENT_SPEC="./component-spec/ansible-ai-connect-service.json"
SPEC_REPO_FILE="spec-repo/lightspeed.json"
# Check if spec file exists in spec repo
if [ ! -f "$SPEC_REPO_FILE" ]; then
echo "Spec file doesn't exist in spec repo - will create new file"
echo "has_diff=true" >> $GITHUB_OUTPUT
echo "is_new_file=true" >> $GITHUB_OUTPUT
else
# Compare files
if diff -q "$COMPONENT_SPEC" "$SPEC_REPO_FILE" > /dev/null; then
echo "✅ No differences found - specs are identical"
echo "has_diff=false" >> $GITHUB_OUTPUT
else
echo "📝 Differences found - spec has changed"
echo "has_diff=true" >> $GITHUB_OUTPUT
echo "is_new_file=false" >> $GITHUB_OUTPUT
fi
fi
- name: Update spec file
if: steps.compare.outputs.has_diff == 'true'
run: |
cp "./component-spec/ansible-ai-connect-service.json" "spec-repo/lightspeed.json"
echo "✅ Updated spec-repo/lightspeed.json"
- name: Create PR in spec repo
if: steps.compare.outputs.has_diff == 'true'
working-directory: spec-repo
env:
GH_TOKEN: ${{ secrets.OPENAPI_SPEC_SYNC_TOKEN }}
run: |
# Configure git
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Create branch for PR
SHORT_SHA="${{ github.sha }}"
SHORT_SHA="${SHORT_SHA:0:7}"
BRANCH_NAME="update-lightspeed-${{ github.ref_name }}-${SHORT_SHA}"
git checkout -b "$BRANCH_NAME"
# Add and commit changes
git add "lightspeed.json"
if [ "${{ steps.compare.outputs.is_new_file }}" == "true" ]; then
COMMIT_MSG="Add Lightspeed OpenAPI spec for ${{ github.ref_name }}"
else
COMMIT_MSG="Update Lightspeed OpenAPI spec for ${{ github.ref_name }}"
fi
git commit -m "$COMMIT_MSG
Synced from ${{ github.repository }}@${{ github.sha }}
Source branch: ${{ github.ref_name }}
Co-Authored-By: github-actions[bot] <github-actions[bot]@users.noreply.github.com>"
# Push branch
git push origin "$BRANCH_NAME"
# Create PR
PR_TITLE="[${{ github.ref_name }}] Update Lightspeed spec from merged commit"
PR_BODY="## Summary
Automated OpenAPI spec sync from component repository merge.
**Source:** ${{ github.repository }}@${{ github.sha }}
**Branch:** \`${{ github.ref_name }}\`
**Component:** \`lightspeed\`
**Spec File:** \`lightspeed.json\`
## Changes
$(if [ "${{ steps.compare.outputs.is_new_file }}" == "true" ]; then echo "- 🆕 New spec file created"; else echo "- 📝 Spec file updated with latest changes"; fi)
## Source Commit
\`\`\`
${{ github.event.head_commit.message }}
\`\`\`
---
🤖 This PR was automatically generated by the OpenAPI spec sync workflow."
gh pr create \
--title "$PR_TITLE" \
--body "$PR_BODY" \
--base "${{ github.ref_name }}" \
--head "$BRANCH_NAME"
echo "✅ Created PR in spec repo"
- name: Report results
if: always()
run: |
if [ "${{ steps.compare.outputs.has_diff }}" == "true" ]; then
echo "📝 Spec sync completed - PR created in spec repo"
else
echo "✅ Spec sync completed - no changes needed"
fi