# -*- indent-tabs-mode: nil -*-
#
# Continuous Integration configuration for Orca
#
# Full documentation for Gitlab CI: https://docs.gitlab.com/ee/ci/
#
# Introduction to Gitlab CI: https://docs.gitlab.com/ee/ci/quick_start/index.html

# Include the parameters we need from Freedesktop CI Templates,
# plus the GNOME release-service.
include:
  - local: 'ci/container_builds.yml'
  - component: gitlab.gnome.org/GNOME/citemplates/release-service@master
    inputs:
      job-stage: release
      dist-job-name: "build-release-tarball"
      tarball-artifact-path: "${TARBALL_ARTIFACT_PATH}"

# Stages in the CI pipeline in which jobs will be run
stages:
  - container-build
  - build
  - format
  - lint
  - pages
  - release

# Enable merge request pipelines and avoid duplicate pipelines
# https://docs.gitlab.com/ee/ci/yaml/index.html#switch-between-branch-pipelines-and-merge-request-pipelines
workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS && $CI_PIPELINE_SOURCE == "push"
      when: never
    - if: '$CI_COMMIT_TAG'
    - if: '$CI_COMMIT_BRANCH'

variables:
  MESON_BUILD_DIR: _build
  TARBALL_ARTIFACT_PATH: "${MESON_BUILD_DIR}/meson-dist/${CI_PROJECT_NAME}-${CI_COMMIT_TAG}.tar.xz"

# Main build job; installs at-spi2-core and builds/installs Orca.
#
# The build is done in a _build directory; see build_and_install.sh.
#
# At the end, prepares the Mallard docs with translations in _build and makes them
# ready for rendering into HTML.  The rendering is done in the "pages" job below.
build-opensuse-x86_64:
  stage: build
  extends:
    - '.orca_opensuse_tumbleweed_x86_64'
    - '.fdo.distribution-image@opensuse'
  needs:
    - job: opensuse-container@x86_64
      artifacts: false
  script:
    - bash -x ci/install_atspi.sh
    - bash -x ci/build_and_install.sh
    - bash -x ci/prepare_translated_docs.sh
  artifacts:
    paths:
      - _build/help
    expire_in: 1 day

# Linters: Ruff and Mypy
#
# The configurations are in pyproject.toml.
linters:
  stage: lint
  extends:
    - '.orca_opensuse_tumbleweed_x86_64'
    - '.fdo.distribution-image@opensuse'
  needs:
    - job: opensuse-container@x86_64
      artifacts: false
    - job: auto-format
      optional: true
  script:
    - source ci/env.sh
    - status=0
    - codespell src/orca || status=1
    - ruff format --check src/orca || status=1
    - ruff check src/orca || status=1
    - mypy src/orca || status=1
    - exit $status
  allow_failure: true

# Auto-format code with ruff and commit back to the branch.
# Only runs on merge requests from branches in this project (not forks),
# since the CI_JOB_TOKEN cannot push to external repositories.
# If changes are pushed, exits with failure to stop this pipeline;
# a new pipeline will start with the formatted code.
auto-format:
  stage: format
  extends:
    - '.orca_opensuse_tumbleweed_x86_64'
    - '.fdo.distribution-image@opensuse'
  needs:
    - job: opensuse-container@x86_64
      artifacts: false
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TITLE =~ /^Draft:/'
      when: never
    # Only run for MRs from branches in this project, not forks (CI token can't push to forks)
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_SOURCE_PROJECT_PATH == $CI_MERGE_REQUEST_PROJECT_PATH'
  script:
    - source ci/env.sh
    - ruff format src/orca
    - ruff check --fix src/orca || true
    - |
      if [ -n "$(git status --porcelain)" ]; then
        git stash
        git config user.name "GitLab CI"
        git config user.email "ci@gitlab.gnome.org"
        git remote set-url origin "https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.gnome.org/${CI_MERGE_REQUEST_SOURCE_PROJECT_PATH}.git"
        git fetch origin "${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"
        git checkout "${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"
        git stash pop
        git add -A
        git commit -m "style: auto-format with ruff"
        git push origin "${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"
        echo "Pushed formatting changes. A new pipeline will run."
        exit 1
      else
        echo "No formatting changes needed"
      fi

# Build a tarball for the release if we have a tag.
build-release-tarball:
  stage: build
  extends:
    - '.orca_opensuse_tumbleweed_x86_64'
    - '.fdo.distribution-image@opensuse'
  needs:
    - job: opensuse-container@x86_64
      artifacts: false
  rules:
    - if: '$CI_COMMIT_TAG'
      when: always
    - when: never
  script:
    - bash -x ci/install_atspi.sh
    - meson setup "${MESON_BUILD_DIR}"
    - meson dist -C "${MESON_BUILD_DIR}"
  artifacts:
    name: "${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
    when: always
    paths:
      - "${TARBALL_ARTIFACT_PATH}"

# Renders the documentation from Mallard into HTML, and puts it in public/
#
# Assumes that a previous build job put the translated docs in _build/help/{language_name}
# for all available languages.
render-mallard-docs:
  stage: build
  extends:
    - '.orca_opensuse_tumbleweed_x86_64'
    - '.fdo.distribution-image@opensuse'
  needs:
    - job: opensuse-container@x86_64
      artifacts: false
    - job: build-opensuse-x86_64   # this one puts the translated docs in _build/help
  script:
    - bash -x ci/render_mallard_docs.sh
  artifacts:
    paths:
      - public
    expire_in: 1 day

# Takes the public/ generated in the render-mallard-docs job and publishes it via GitLab pages.
pages:
  stage: pages
  extends:
    - '.orca_opensuse_tumbleweed_x86_64'
    - '.fdo.distribution-image@opensuse'
  needs:
    - job: render-mallard-docs
  script:
    # This is so that GitLab will see that there is something to do, as the job
    # needs nothing except the artifacts from the render-mallard-docs job.
    - echo
  artifacts:
    paths:
      - public
    expire_in: 1 day
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

