---
# --------------------------------------------------------------------------------------
# Global
# --------------------------------------------------------------------------------------
include:
  - local: common.yml

stages:
  - manifest-integration

variables:
  MANIFEST_FILE: "default.xml"
  # The BB_RECIPE_NAME is used for projects referenced in the SRCREV file
  # to match the repository and the bitbake recipe name.
  # We set it here to none, as every project needing it
  # has to specify it in its own gitlab-ci.yml file.
  # The BB_RECIPE_NAME is passed to the python scripts below anyway, but not
  # used for projects referenced in the manifest file.
  BB_RECIPE_NAME: none

workflow:
  rules:
    # Explicitly allow externally triggered pipelines in every case
    - if: $CI_PIPELINE_SOURCE == "pipeline" || $CI_PIPELINE_SOURCE == "api"
    # Do not run pipelines on forked projects.
    # The pipelines would not work anyway because of the users permissions.
    # There are two cases catched here:
    # 1. The project is forked into someones gitlab namespace and a MR to
    #    include a change into this forked project is created. In this case
    #    is the CI_PROJECT_ROOT_NAMESPACE not seco-ne but the
    #    namespace the fork lives in.
    # 2. The MR from the forked project is created to merge the change into this
    #    the project in the seco-ne namespace (customer sending
    #    change to us). Here the the IDs used below differ.
    #
    - if: $CI_PROJECT_ROOT_NAMESPACE != "seco-ne"
      when: never
    - if: $CI_MERGE_REQUEST_SOURCE_PROJECT_ID != $CI_MERGE_REQUEST_PROJECT_ID
      when: never
    # FIXME: Unfortunately we cannot use variables in regular expressions due to a
    # GitLab limitation: https://gitlab.com/gitlab-org/gitlab/-/issues/209904
    # As soon as this get fixed, use the regex based rules below instead of checking
    # against the MASTER_BRANCH variable.
    # Run pipeline if target branch of the merge request has an integration target, i.e.
    # INTEGRATION contains a line beginning with the target branch followed by a colon.
    # This also implies that the pipeline runs in merge request context only.
    # - if: $INTEGRATION =~ /^$CI_MERGE_REQUEST_TARGET_BRANCH_NAME:/m
    - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $MASTER_BRANCH
    # Run pipeline on target branch after merging a merge request.
    # - if: $INTEGRATION =~ /^$CI_COMMIT_BRANCH:/m
    - if: $CI_COMMIT_BRANCH == $MASTER_BRANCH

.skip-for-gitlab-ci-integrations:
  rules:
    - if: $CI_COMMIT_REF_NAME !~ /^integrate\/gitlab-ci\/.*/

# --------------------------------------------------------------------------------------
# Manifest integration jobs
# --------------------------------------------------------------------------------------
generate-pipelines:
  extends:
    - .infrastructure
    - .skip-for-gitlab-ci-integrations
  stage: manifest-integration
  script:
    # The job generation script implicitly passes the OS environment to the template, so
    # that the template has access to all GitLab CI variables. Hence there is no need
    # to explicitly pass any of them as command line arguments.
    - .gitlab-ci/scripts/generate_job_from_template.py
        --template=.gitlab-ci/manifest-integration-pipelines.yml.jinja2
        > manifest-integration-pipelines.yml
  artifacts:
    expire_in: 4 weeks
    paths:
      - manifest-integration-pipelines.yml

trigger-pipelines:
  extends:
    - .skip-for-gitlab-ci-integrations
  stage: manifest-integration
  needs:
    - generate-pipelines
  trigger:
    include:
      - artifact: manifest-integration-pipelines.yml
        job: generate-pipelines
    strategy: depend

check:
  extends:
    - .infrastructure
    - .skip-for-gitlab-ci-integrations
  stage: manifest-integration
  rules:
    - if: $CI_MERGE_REQUEST_IID && $CI_COMMIT_REF_NAME !~ /^integrate\/gitlab-ci\/.*/
  needs:
    - trigger-pipelines
  allow_failure: true
  script:
    - cd ${CI_PROJECT_DIR}
    # Loop over all integrations and check each integration branch
    - while read -r integration; do
        SOURCE_BRANCH=$(echo $integration | cut -d':' -f1);
        TARGET_PROJECT=$(echo $integration | cut -d':' -f2);
        TARGET_BRANCH=$(echo $integration | cut -d':' -f3);
        if [[ "$SOURCE_BRANCH" == "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ]]; then
          .gitlab-ci/scripts/check_if_integration_branch_is_up_to_date.py
            --gitlab-url=${CI_SERVER_URL}
            --token=${GITBOT_TOKEN}
            --target-project=${TARGET_PROJECT}
            --target-branch=${TARGET_BRANCH}
            --source-project=${CI_PROJECT_PATH}
            --merge-request=${CI_MERGE_REQUEST_IID}
            ;
        fi;
      done <<< "$INTEGRATION"

yamllint:
  extends: .yamllint
  stage: manifest-integration
