# Python Django
# Test a Django project on multiple versions of Python.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/python

# NOTES:
#
# Display name of each step should be prefixed with one of the following:
#   CR-QC: for quality control measures.
#   CR-BUILD: for build-related tasks.
#   CR-DEPLOY: for publication or deployment.
#   [no prefix]: for unrelated CI setup/tooling.
#
# Use PowerShell Core for any utility scripts so they are re-usable across
# Windows, macOS, and Linux.
#


trigger:
  - main


stages:
- stage: Unit_Tests
  displayName: Unit Tests

  jobs:
  - job: pytest
    displayName: pytest
    pool:
      vmImage: 'ubuntu-latest'
    strategy:
      matrix:
        py3.8_wag5.0:
          PYTHON_VERSION: '3.8'
          WAGTAIL_VERSION: '5.0.*'
          TEMPLATE: 'basic'
        py3.9_wag5.0:
          PYTHON_VERSION: '3.9'
          WAGTAIL_VERSION: '5.0.*'
          TEMPLATE: 'basic'
        py3.10_wag5.0:
          PYTHON_VERSION: '3.10'
          WAGTAIL_VERSION: '5.0.*'
          TEMPLATE: 'basic'
        py3.11_wag5.1:
          PYTHON_VERSION: '3.11'
          WAGTAIL_VERSION: '5.1.*'
          TEMPLATE: 'basic'
        py3.12_wag5.2_basic:
          PYTHON_VERSION: '3.12'
          WAGTAIL_VERSION: '5.2.*'
          TEMPLATE: 'basic'
        py3.12_wag5.2_pro:
          PYTHON_VERSION: '3.12'
          WAGTAIL_VERSION: '5.2.*'
          TEMPLATE: 'pro'

    steps:
    - task: UsePythonVersion@0
      displayName: 'Use Python version'
      inputs:
        versionSpec: '$(PYTHON_VERSION)'
        architecture: 'x64'

    - script: python -m pip install -r requirements-ci.txt wagtail==$(WAGTAIL_VERSION)
      displayName: 'CR-QC: Install coderedcms from local repo'

    - script: coderedcms start testproject --template $(TEMPLATE)
      displayName: 'CR-QC: Create starter project from template'

    - script: |
        cd testproject/
        touch requirements-dev.txt
        python -m pip install -r requirements-dev.txt
        python manage.py makemigrations --check
      displayName: 'CR-QC: Check migrations'

    - pwsh: ./ci/run-tests.ps1
      displayName: 'CR-QC: Run unit tests'

    - task: PublishTestResults@2
      displayName: 'Publish unit test report'
      condition: succeededOrFailed()
      inputs:
        testResultsFiles: '**/test-*.xml'
        testRunTitle: 'Publish test results for Python $(python.version)'

    - task: PublishCodeCoverageResults@1
      displayName: 'Publish code coverage report'
      condition: succeededOrFailed()
      inputs:
        codeCoverageTool: Cobertura
        summaryFileLocation: '$(System.DefaultWorkingDirectory)/coverage.xml'


- stage: Static_Analysis
  displayName: Static Analysis
  dependsOn: Unit_Tests
  condition: succeeded('Unit_Tests')
  jobs:
  - job: linters
    displayName: Linters
    pool:
      vmImage: 'ubuntu-latest'

    steps:
    - task: UsePythonVersion@0
      displayName: 'Use Python version'
      inputs:
        versionSpec: '3.12'
        architecture: 'x64'

    - script: python -m pip install -r requirements-ci.txt
      displayName: 'CR-QC: Install coderedcms from local repo'

    - pwsh: ./ci/spellcheck.ps1
      displayName: 'CR-QC: Spelling'

    - script: black --check .
      displayName: 'CR-QC: Black'

    - script: flake8 .
      displayName: 'CR-QC: Flake8'

  - job: codecov
    displayName: Code Coverage
    pool:
      vmImage: 'ubuntu-latest'

    steps:
    - task: DownloadBuildArtifacts@0
      displayName: 'Download code coverage from current build'
      inputs:
        buildType: 'current'
        project: '$(System.TeamProjectId)'
        pipeline: '$(System.DefinitionId)'
        downloadType: 'specific'
        downloadPath: '$(Agent.WorkFolder)/current-artifacts'

    - pwsh: ./ci/compare-codecov.ps1 -wd $Env:WorkDir
      displayName: 'CR-QC: Compare code coverage'
      failOnStderr: false
      env:
        WorkDir: $(Agent.WorkFolder)

  - job: docs
    displayName: Documentation
    pool:
      vmImage: 'ubuntu-latest'

    steps:
    - task: UsePythonVersion@0
      displayName: 'Use Python version'
      inputs:
        versionSpec: '3.12'
        architecture: 'x64'

    - script: python -m pip install -r requirements-ci.txt
      displayName: 'CR-QC: Install coderedcms from local repo'

    - pwsh: ./ci/make-docs.ps1
      displayName: 'CR-QC: Build documentation'