image: python:3.10

variables:
  CONTAINER_IMAGE: ${CI_PROJECT_PATH}:${CI_COMMIT_SHORT_SHA}

stages:
  - test
  - package
  - publish

.tox: &tox
  script:
    - pip install tox
    - tox -e ${TOXENV:-$CI_JOB_NAME}
  stage: test

lint:
  <<: *tox

# Let run the test on debian bookworm since we need to use system-wide apt_pkg.
py3:
  stage: test
  image: debian:bookworm
  script:
  - apt update
  - apt install -y tox python3-apt python3-venv git
  - python3 -m venv .venv
  - source .venv/bin/activate
  - pip install tox
  - tox --version
  - tox -e py3
  artifacts:
    paths:
      - coverage.xml
      - xunit.xml

wrap-and-sort:
  stage: test
  image: buildpack-deps:stable
  script:
  - apt update
  - apt -y --no-install-recommends install devscripts python3-debian
  - CHANGES=$(wrap-and-sort -astN)
  - if [[ ! -z "$CHANGES" ]]; then echo "$CHANGES"; exit 1; fi

sonar:
  allow_failure: true
  image: sonarsource/sonar-scanner-cli
  needs:
    - py3
  script:
    - sonar-scanner -X -Dsonar.python.coverage.reportPaths=coverage*.xml -Dsonar.python.xunit.reportPaths=xunit*.xml -Dsonar.host.url=$SONAR_URL -Dsonar.login=$SONAR_TOKEN -Dsonar.projectKey=$CI_PROJECT_NAME -Dsonar.projectVersion=$(git describe) -Dsonar.qualitygate.wait=true
  stage: test
  variables:
    SONAR_PROJECT_BASE_DIR: "${CI_PROJECT_DIR}"

package:dsc:
  stage: package
  needs: []
  image: buildpack-deps:unstable
  variables:
    GIT_DEPTH: 50  # GitLab runner will fetch last 50 commits
  script: 
    - rm -f ../*.debian.tar.xz ../*.orig.tar.xz ../*.dsc
    # Install build dependencies
    - apt -U -y --no-install-recommends install tox
    - apt build-dep -y --no-install-recommends -P nocheck,nodoc .
    # Update changelog for automatic build
    - git config user.email "$GITLAB_USER_EMAIL"
    - git config user.name "$GITLAB_USER_NAME"
    - (tox -e changelog && git commit -m "automatic build" CHANGELOG.md debian/changelog) || true
    # Create original tarball ".orig.tar.xz"
    - debian/rules gentarball
    # Build debian source package ".dsc"
    - dpkg-buildpackage -S --no-check-builddeps
    - mkdir -p ./dist/
    - mv ../*.debian.tar.xz ../*.orig.tar.xz ../*.dsc ./dist/
  artifacts:
    paths:
      - ./dist/*

package:deb:
  stage: package
  needs: 
    - package:dsc
  image: buildpack-deps:$DIST
  variables:
     GIT_STRATEGY: none  # GitLab runner will NOT clone/fetch repo
  parallel:
    matrix:
      - DIST: [forky]
  script: |
    set -x
    set -e
    rm -f *.deb
    # Install build dependencies
    apt update
    apt -y --no-install-recommends install lintian
    ls -1 ./dist/*.dsc | while read -r FILE
    do
      rm -rf builddir
      dpkg-source -x ${FILE} builddir/
      pushd builddir/
      apt build-dep -y --no-install-recommends .
      # Build debian binary package ".deb"
      dpkg-buildpackage -b
      popd
      lintian --info --display-info --pedantic *.deb ${FILE}
      mkdir -p ./dist/${DIST}
      mv *.deb ./dist/${DIST}/
    done
  artifacts:
    paths:
      - ./dist/*

publish:dsc:
  needs:
    - package:dsc
  only:
    variables:
      - $NEXUS_USR
      - $NEXUS_PWD
  script: |
    set -e
    set -x
    # Loop on dsc files
    ls -1 ./dist/*.dsc | while read -r FILE
    do
      # Define where to send files.
      FILENAME="${FILE##*/}" 
      PKGNAME="${FILENAME%%_*}"
      TMP="${FILENAME#*_}"
      VERSION="${TMP%.dsc}"
      ARCHIVE_URL="$NEXUS_URL/repository/archive/debian/$PKGNAME/$VERSION"
      # For each file in dsc, upload it to nexus
      grep -A99 "^Files:" "$FILE" | grep "^ " | awk '{print $3}' | while read -r ASSET
      do
        curl --fail -u "$NEXUS_USR:$NEXUS_PWD" \
          --upload-file "./dist/$ASSET" \
          "$ARCHIVE_URL/$(basename "$ASSET")"
      done
      # Upload the .dsc itself
      curl --fail -u "$NEXUS_USR:$NEXUS_PWD" \
        --upload-file "$FILE" \
        "$ARCHIVE_URL/$(basename "$FILE")"
    done
  stage: publish

publish:deb:
  needs:
    - package:deb
  only:
    variables:
      - $NEXUS_USR
      - $NEXUS_PWD
  parallel:
    matrix:
      - DIST: [forky]
  script: |
    set -e
    set -x
    if [[ "$CI_COMMIT_TAG" == *.+([0-9]) ]]; then
      APT_URL="$NEXUS_URL/repository/apt-release-${DIST}/"
      APT_COMPONENT=${DIST}
    else
      APT_URL="$NEXUS_URL/repository/apt-dev-${DIST}/"
      APT_COMPONENT=${DIST}-dev
    fi
    # Upload binaries
    ls -1 ./dist/$DIST/*.deb | while read -r FILE
    do
      curl --fail -u "$NEXUS_USR:$NEXUS_PWD" -H "Content-Type: multipart/form-data" --data-binary "@$FILE" "$APT_URL"
      sleep 2
      ARCH=$(echo "$FILE" | grep -oP '(?<=_)[^_]+(?=\.deb$)')
      curl --fail "${APT_URL}dists/$APT_COMPONENT/main/binary-${ARCH}/Packages" | grep "$(basename $FILE)"
    done
  stage: publish
