stages:
  - test
  - build
  - release


Linux:
  stage: test
  allow_failure: true
  variables:
    GIT_SUBMODULE_STRATEGY: recursive
  tags:
    - autoscale
  image: debian
  script:
    - apt-get update && apt-get -y upgrade
    - apt-get -y install build-essential curl jq make python3 python3-dev python3-pip wget unzip git pigz
    - make fetch
    - sh -c "./scripts/check-patchfail.sh"
    - make test-linux
  artifacts:
    paths:
      - librewolf-*.tar.bz2
      - mozconfig.txt

macOS:
  stage: test
  allow_failure: true
  variables:
    GIT_SUBMODULE_STRATEGY: recursive
  when: manual
  tags:
    - autoscale
  image: debian
  script:
    - apt-get update && apt-get -y upgrade
    - apt-get -y install build-essential curl jq make python3 python3-dev python3-pip wget unzip git pigz
    - make fetch
    - sh -c "./scripts/check-patchfail.sh"
    - make test-macos
  artifacts:
    paths:
      - librewolf-*.dmg
      - mozconfig.txt

Windows:
  stage: test
  allow_failure: true
  variables:
    GIT_SUBMODULE_STRATEGY: recursive
  when: manual
  tags:
    - autoscale
  image: debian
  script:
    - apt-get update && apt-get -y upgrade
    - apt-get -y install build-essential curl jq make python3 python3-dev python3-pip wget unzip git pigz
    - make fetch
    - sh -c "./scripts/check-patchfail.sh"
    - make test-windows
  artifacts:
    paths:
      - librewolf-*.zip
      - mozconfig.txt





Build:
  stage: build
  when: manual
  variables:
    GIT_SUBMODULE_STRATEGY: recursive
  script:
    - apt-get update && apt-get -y upgrade
    - apt-get -y install pigz
    - make all
    - make check-fuzz
    - echo VERSION=$(cat version) >> variables.env
    - echo RELEASE=$(cat release) >> variables.env
    - echo BUILD_JOB_ID=$CI_JOB_ID >> variables.env
  artifacts:
    paths:
      - librewolf-*.source.tar.gz
      - librewolf-*.source.tar.gz.sha256sum
      - librewolf-*.source.tar.gz.sha512sum
      - librewolf-*.source.tar.gz.sig
      - patchfail-fuzz.out
    reports:
      dotenv: variables.env

Release:
  stage: release
  when: manual
  allow_failure: false
  image: ubuntu
  needs:
    - job: "Build"
      artifacts: true
  only:
    - main
  except:
    - merge_requests
  before_script:
    - apt-get update
    - apt-get install -y curl jq
    - curl -L --output /usr/local/bin/release-cli "https://gitlab.com/gitlab-org/release-cli/-/package_files/163559016/download"
    - chmod +x /usr/local/bin/release-cli
  script:
    - |
      curl \
        --header "JOB-TOKEN: $CI_JOB_TOKEN" \
        --upload-file librewolf-$VERSION-$RELEASE.source.tar.gz \
        "$CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/librewolf-source/$VERSION-$RELEASE/librewolf-$VERSION-$RELEASE.source.tar.gz"
    - |
      curl \
        --header "JOB-TOKEN: $CI_JOB_TOKEN" \
        --upload-file librewolf-$VERSION-$RELEASE.source.tar.gz.sha256sum \
        "$CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/librewolf-source/$VERSION-$RELEASE/librewolf-$VERSION-$RELEASE.source.tar.gz.sha256sum"
    - |
      curl \
        --header "JOB-TOKEN: $CI_JOB_TOKEN" \
        --upload-file librewolf-$VERSION-$RELEASE.source.tar.gz.sha512sum \
        "$CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/librewolf-source/$VERSION-$RELEASE/librewolf-$VERSION-$RELEASE.source.tar.gz.sha512sum"
    - |
      curl \
        --header "JOB-TOKEN: $CI_JOB_TOKEN" \
        --upload-file librewolf-$VERSION-$RELEASE.source.tar.gz.sig \
        "$CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/librewolf-source/$VERSION-$RELEASE/librewolf-$VERSION-$RELEASE.source.tar.gz.sig"
    - |
      curl -X 'POST' \
        "https://codeberg.org/api/v1/repos/librewolf/source/releases?token=$CODEBERG_TOKEN" \
        -H 'accept: application/json' \
        -H 'Content-Type: application/json' \
        -d "{
        \"name\": \"$VERSION-$RELEASE\",
        \"tag_name\": \"$VERSION-$RELEASE\",
        \"body\": \"[librewolf-$VERSION-$RELEASE.source.tar.gz]($CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/librewolf-source/$VERSION-$RELEASE/librewolf-$VERSION-$RELEASE.source.tar.gz)\n[librewolf-$VERSION-$RELEASE.source.tar.gz.sha256sum]($CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/librewolf-source/$VERSION-$RELEASE/librewolf-$VERSION-$RELEASE.source.tar.gz.sha256sum)\n[librewolf-$VERSION-$RELEASE.source.tar.gz.sig]($CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/librewolf-source/$VERSION-$RELEASE/librewolf-$VERSION-$RELEASE.source.tar.gz.sig)\n\n[View on GitLab](https://gitlab.com/librewolf-community/browser/source/-/releases/$VERSION-$RELEASE)\"
      }"

  release:
    tag_name: "$VERSION-$RELEASE"
    description: "## LibreWolf Source Release v$VERSION-$RELEASE\n\n- \n\n(Built on GitLab by job [$BUILD_JOB_ID](https://gitlab.com/librewolf-community/browser/source/-/jobs/$BUILD_JOB_ID))"
    assets:
      links:
        - name: librewolf-$VERSION-$RELEASE.source.tar.gz
          link_type: package
          url: $CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/librewolf-source/$VERSION-$RELEASE/librewolf-$VERSION-$RELEASE.source.tar.gz
        - name: librewolf-$VERSION-$RELEASE.source.tar.gz.sha256sum
          link_type: other
          url: $CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/librewolf-source/$VERSION-$RELEASE/librewolf-$VERSION-$RELEASE.source.tar.gz.sha256sum
        - name: librewolf-$VERSION-$RELEASE.source.tar.gz.sha512sum
          link_type: other
          url: $CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/librewolf-source/$VERSION-$RELEASE/librewolf-$VERSION-$RELEASE.source.tar.gz.sha512sum
        - name: librewolf-$VERSION-$RELEASE.source.tar.gz.sig
          link_type: other
          url: $CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic/librewolf-source/$VERSION-$RELEASE/librewolf-$VERSION-$RELEASE.source.tar.gz.sig
