pipeline:
  dependencies:
    image: node:16
    pull: true
    commands:
      - npm i
    when:
      event: [tag, push, pull_request]
      branch: [master, develop, feature/*, fix/*, translations]

  osv-detector:
    image: gitnet.fr/deblan/osv-detector:v0.10
    commands:
      - osv-detector package-lock.json
    failure: ignore

  build-js:
    image: node:16
    commands:
      - npm run build
    when:
      branch: [master, develop, feature/*, fix/*, translations]
      event: [tag, push, pull_request]

  build-translations:
    image: deblan/php:8.0
    commands:
      - php bin/generate_l10n.php
    when:
      branch: [master, develop, feature/*, fix/*, translations]
      event: [tag, push, pull_request]

  create-signature:
    image: nextcloud:25
    secrets: [app_certificate, app_public_certificate]
    environment:
      SQLITE_DATABASE: /var/www/data/data.db
      NEXTCLOUD_ADMIN_USER: admin
      NEXTCLOUD_ADMIN_PASSWORD: admin
    commands:
      - echo "$APP_CERTIFICATE" > "/tmp/side_menu.key"
      - echo "$APP_PUBLIC_CERTIFICATE" > "/tmp/side_menu.crt"
      - mkdir /tmp/app
      - cp -r README.md CHANGELOG.md appinfo css lib img l10n js src templates screenshots vendor /tmp/app
      - /usr/src/nextcloud/occ integrity:sign-app
        --privateKey=/tmp/side_menu.key
        --certificate=/tmp/side_menu.crt
        --path=/tmp/app
      - mv /tmp/app/appinfo/signature.json appinfo/
    when:
      event: [tag]

  # check-code-quality:
  #   image: sonarsource/sonar-scanner-cli
  #   secrets: [sonar_token, sonar_host, sonar_project]
  #   commands:
  #     - sonar-scanner
  #       -Dsonar.projectKey=$SONAR_PROJECT
  #       -Dsonar.sources=.
  #       -Dsonar.host.url=$SONAR_HOST
  #       -Dsonar.pullrequest.key=$CI_COMMIT_PULL_REQUEST
  #       -Dsonar.pullrequest.branch=$CI_COMMIT_SOURCE_BRANCH
  #       -Dsonar.pullrequest.base=$CI_COMMIT_TARGET_BRANCH
  #   failure: ignore
  #   when:
  #     event: [pull_request]

  create-package:
    image: deblan/php:8.0
    volumes:
      - /var/www/html/artifacts:/var/www/html/artifacts
    secrets: [app_certificate]
    commands:
      - apt-get update
      - apt-get install -y zip make
      - mkdir -p "$HOME/.nextcloud/certificates"
      - echo "$APP_CERTIFICATE" > "$HOME/.nextcloud/certificates/side_menu.key"
      - export VERSION=$(grep "<version>" appinfo/info.xml | grep -o "[0-9]*\.[0-9]*\.[0-9]*" --color=never)
      - export RELEASE_DIRECTORY="/var/www/html/artifacts/deblan/side_menu"
      - make release
    when:
      event: [tag]

  push-release:
    image: plugins/gitea-release
    volumes:
      - /var/www/html/artifacts:/var/www/html/artifacts
    settings:
      api_key:
        from_secret: gitnet_api_key
      base_url: https://gitnet.fr
      note: ${CI_COMMIT_MESSAGE}
      files: /var/www/html/artifacts/deblan/side_menu/${CI_COMMIT_TAG/v//}/*
    when:
      event: [tag]
