# TODO: The when: platform conditionals aren't working currently
# See https://github.com/woodpecker-ci/woodpecker/issues/1677

variables:
  # When updating the rust version here, be sure to update versions in `docker/Dockerfile`
  # as well. Otherwise release builds can fail if Lemmy or dependencies rely on new Rust
  # features. In particular the ARM builder image needs to be updated manually in the repo below:
  # https://github.com/raskyld/lemmy-cross-toolchains
  # Also be sure to change the version in `rust-toolchain.toml`
  - &rust_image "rust:1.95"
  - &rust_nightly_image "rustlang/rust:nightly"
  - &install_pnpm "npm install -g corepack@latest && corepack enable pnpm"
  - &install_binstall "wget -q -O- https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-x86_64-unknown-linux-musl.tgz | tar -xvz -C /usr/local/cargo/bin"
  - &slow_check_paths
    - event: pull_request
      path:
        include: [
            # rust source code
            "crates/**",
            "**/Cargo.toml",
            "Cargo.lock",
            # database migrations
            "migrations/**",
            # typescript tests
            "api_tests/**",
            # config files and scripts used by ci
            ".woodpecker.yml",
            ".rustfmt.toml",
            "scripts/update_config_defaults.sh",
            "diesel.toml",
            ".gitmodules",
          ]

steps:
  prepare_repo:
    image: alpine:3
    commands:
      - apk add git
      - git submodule init
      - git submodule update
    when:
      - event: [pull_request, tag]

  prettier_check:
    image: jauderho/prettier:3.7.4-alpine
    commands:
      - prettier -c . '!**/volumes' '!**/dist' '!target' '!**/translations' '!api_tests/pnpm-lock.yaml'
    when:
      - event: pull_request

  check_typos:
    image: *rust_image
    commands:
      - *install_binstall
      - cargo binstall -y typos-cli
      - typos --exclude crates/email/ --exclude crates/apub/apub/assets/ --exclude migrations --exclude readmes
    when:
      - event: pull_request

  bash_fmt:
    image: alpine:3
    commands:
      - apk add shfmt
      - shfmt -i 2 -d */**.bash
      - shfmt -i 2 -d */**.sh
    when:
      - event: pull_request

  toml_fmt:
    image: ghcr.io/shaddydc/taplo
    commands:
      - taplo format --check
    when:
      - event: pull_request

  sql_fmt:
    image: *rust_image
    commands:
      - apt-get install perl make bash
      - ./scripts/alpine_install_pg_formatter.sh
      - ./scripts/sql_format_check.sh
    when:
      - event: pull_request

  cargo_fmt:
    image: *rust_nightly_image
    environment:
      # store cargo data in repo folder so that it gets cached between steps
      CARGO_HOME: .cargo_home
      RUSTUP_HOME: .rustup_home
    commands:
      - rustup component add rustfmt --toolchain nightly
      - cargo +nightly fmt -- --check
    when:
      - event: pull_request

  cargo_shear:
    image: *rust_nightly_image
    commands:
      - *install_binstall
      - cargo binstall -y cargo-shear --disable-strategies compile
      - cargo shear --deny-warnings
    when:
      - event: pull_request

  api_tests_lint:
    image: node:24-trixie-slim
    commands:
      - *install_pnpm
      - cd api_tests/
      - pnpm i
      - pnpm lint
    when: *slow_check_paths

  ignored_files:
    image: alpine:3
    commands:
      - apk add git
      - IGNORED=$(git ls-files --cached -i --exclude-standard)
      - if [[ "$IGNORED" ]]; then echo "Ignored files present:\n$IGNORED\n"; exit 1; fi
    when:
      - event: pull_request

  no_empty_files:
    image: alpine:3
    commands:
      # Makes sure there are no files smaller than 2 bytes
      # Don't use completely empty, as some editors use newlines
      - EMPTY_FILES=$(find crates migrations api_tests/src config -type f -size -2c)
      - if [[ "$EMPTY_FILES" ]]; then echo "Empty files present:\n$EMPTY_FILES\n"; exit 1; fi
    when:
      - event: pull_request

  cargo_build:
    image: *rust_image
    environment:
      CARGO_HOME: .cargo_home
      RUSTUP_HOME: .rustup_home
    commands:
      - cargo build --all-features
      - mv target/debug/lemmy_server target/lemmy_server
    when: *slow_check_paths

  # `DROP OWNED` doesn't work for default user
  create_database_user:
    image: pgautoupgrade/pgautoupgrade:18-alpine
    environment:
      PGUSER: postgres
      PGPASSWORD: password
      PGHOST: database
      PGDATABASE: lemmy
    commands:
      - psql -c "CREATE USER lemmy WITH PASSWORD 'password' SUPERUSER;"
    when: *slow_check_paths

  cargo_clippy:
    image: *rust_image
    environment:
      CARGO_HOME: .cargo_home
      RUSTUP_HOME: .rustup_home
    commands:
      - rustup component add clippy
      - cargo clippy --workspace --tests --all-targets --all-features -- -D warnings
    when: *slow_check_paths

  cargo_test:
    image: *rust_image
    environment:
      LEMMY_DATABASE_URL: postgres://lemmy:password@database:5432/lemmy
      RUST_BACKTRACE: "1"
      CARGO_HOME: .cargo_home
      RUSTUP_HOME: .rustup_home
      LEMMY_TEST_FAST_FEDERATION: "1"
      LEMMY_CONFIG_LOCATION: /woodpecker/src/github.com/LemmyNet/lemmy/config/config.hjson
    commands:
      # Install pg_dump for the schema setup test (must match server version)
      - apt update && apt install -y lsb-release
      - sh -c 'echo "deb [signed-by=/usr/share/keyrings/postgres-keyring.gpg] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
      - wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /usr/share/keyrings/postgres-keyring.gpg
      - apt update && apt install -y postgresql-client-18
      # Run tests (if they fail, try again)
      - cargo test --workspace || cargo test --workspace
    when: *slow_check_paths

  # make sure api builds with default features (used by other crates relying on lemmy api)
  check_api_common_default_features:
    image: *rust_image
    environment:
      CARGO_HOME: .cargo_home
      RUSTUP_HOME: .rustup_home
    commands:
      - cargo check --package lemmy_api_common
    when: *slow_check_paths

  check_disallowed_dependencies:
    image: *rust_image
    environment:
      CARGO_HOME: .cargo_home
      RUSTUP_HOME: .rustup_home
    commands:
      - "! cargo tree -p lemmy_api_common --no-default-features -i diesel"
      - "! cargo tree -i aws-lc-sys"
      - "! cargo tree -i extism"
      - "cargo tree --all-features -i extism"
    when: *slow_check_paths

  check_crates_io_publish:
    image: *rust_image
    environment:
      CARGO_HOME: .cargo_home
      RUSTUP_HOME: .rustup_home
    commands:
      - cargo publish --workspace --dry-run
    when: *slow_check_paths

  lemmy_api_common_works_with_wasm:
    image: *rust_image
    environment:
      CARGO_HOME: .cargo_home
      RUSTUP_HOME: .rustup_home
    commands:
      - "rustup target add wasm32-unknown-unknown"
      - "cargo check --target wasm32-unknown-unknown -p lemmy_api_common"
    when: *slow_check_paths

  check_diesel_schema:
    image: *rust_image
    environment:
      LEMMY_DATABASE_URL: postgres://lemmy:password@database:5432/lemmy
      DATABASE_URL: postgres://lemmy:password@database:5432/lemmy
      RUST_BACKTRACE: "1"
      CARGO_HOME: .cargo_home
      RUSTUP_HOME: .rustup_home
    commands:
      - *install_binstall
      - cp crates/db_schema_file/src/schema.rs tmp.schema
      - target/lemmy_server migration --all run
      - apt-get update && apt-get install -y postgresql-client
      - cargo binstall diesel_cli@2.3.2 -y --disable-strategies compile
      - export PATH="$CARGO_HOME/bin:$PATH"
      - diesel print-schema
      - diff tmp.schema crates/db_schema_file/src/schema.rs
    when: *slow_check_paths

  run_federation_tests:
    image: node:24-trixie-slim
    environment:
      LEMMY_DATABASE_URL: postgres://lemmy:password@database:5432
      DO_WRITE_HOSTS_FILE: "1"
    commands:
      - *install_pnpm
      - apt-get update && apt-get install -y bash curl postgresql-client
      - bash api_tests/prepare-drone-federation-test.sh
      - cd api_tests/
      - pnpm i
      - pnpm lint
      # Unfortunately these tests are unstable on slower CI machines, so try it a few times.
      - pnpm api-test-multiple
    when: *slow_check_paths

  federation_tests_server_output:
    image: alpine:3
    commands:
      # `|| true` prevents this step from appearing to fail if the server output files don't exist
      - cat target/log/lemmy_*.out || true
      - "# If you can't see all output, then use the download button"
    when:
      - event: pull_request
        status: [failure]

  # Only publish arm builds on non-pre-release builds
  publish_release_docker_arm64:
    image: woodpeckerci/plugin-docker-buildx
    settings:
      repo: dessalines/lemmy
      dockerfile: docker/Dockerfile
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password
      platforms: linux/arm64
      build_args:
        RUST_RELEASE_MODE: release
      build_args_from_env:
        - CI_PIPELINE_EVENT
      tag: ${CI_COMMIT_TAG=nightly}
    when:
      - event: [tag, cron]
        # Ensures that its a full semver release, and not a pre-release one like 1.0.0-alpha.1
        evaluate: '"${CI_COMMIT_TAG}" matches ''^([0-9]+)\\\.([0-9]+)\\\.([0-9]+)(-beta\\\.[0-9]+)?$'''

  # Only publish arm builds on non-pre-release builds
  publish_release_docker_amd64:
    image: woodpeckerci/plugin-docker-buildx
    settings:
      repo: dessalines/lemmy
      dockerfile: docker/Dockerfile
      username:
        from_secret: docker_username
      password:
        from_secret: docker_password
      platforms: linux/amd64
      build_args:
        RUST_RELEASE_MODE: release
      build_args_from_env:
        - CI_PIPELINE_EVENT
      tag: ${CI_COMMIT_TAG=nightly}
    when:
      - event: [tag, cron]

  # lemmy container doesnt run as root so we need to change permissions to let it copy the binary
  chmod_for_native_binary:
    image: alpine:3
    commands:
      - chmod 777 .
    when:
      - event: tag

  # extract lemmy binary from newly built docker image into workspace folder
  extract_native_binary:
    image: dessalines/lemmy:${CI_COMMIT_TAG=default}
    commands:
      - cp /usr/local/bin/lemmy_server .
    when:
      - event: tag

  prepare_native_binary:
    image: alpine:3
    commands:
      - sha256sum lemmy_server > sha256sum.txt
      - gzip lemmy_server
    when:
      - event: tag

  # https://woodpecker-ci.org/plugins/Release
  publish_native_binary:
    image: woodpeckerci/plugin-release
    settings:
      files:
        - lemmy_server.gz
        - sha256sum.txt
      title: ${CI_COMMIT_TAG}
      prerelease: true
      api-key:
        from_secret: github_token
    when:
      - event: tag

  publish_to_crates_io:
    image: *rust_image
    environment:
      CARGO_API_TOKEN:
        from_secret: cargo_api_token
    commands:
      - *install_binstall
      - cp -r migrations crates/db_schema/
      - cargo publish --workspace --token "$CARGO_API_TOKEN" --allow-dirty --no-verify
    when:
      - event: tag

  notify_success:
    image: alpine:3
    commands:
      - apk add curl
      - "curl -H'Title: ✔️ ${CI_REPO_NAME}/${CI_COMMIT_SOURCE_BRANCH}' -d'${CI_PIPELINE_URL}' ntfy.sh/lemmy_drone_ci"
    when:
      - event: pull_request
        status: [success]

  notify_failure:
    image: alpine:3
    commands:
      - apk add curl
      - "curl -H'Title: ❌ ${CI_REPO_NAME}/${CI_COMMIT_SOURCE_BRANCH}' -d'${CI_PIPELINE_URL}' ntfy.sh/lemmy_drone_ci"
    when:
      - event: pull_request
        status: [failure]

  notify_on_tag_deploy:
    image: alpine:3
    commands:
      - apk add curl
      - "curl -H'Title: ${CI_REPO_NAME}:${CI_COMMIT_TAG} deployed' -d'${CI_PIPELINE_URL}' ntfy.sh/lemmy_drone_ci"
    when:
      event: tag

services:
  database:
    image: pgautoupgrade/pgautoupgrade:18-alpine
    environment:
      POSTGRES_DB: lemmy
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
    when:
      - event: pull_request
    # Woodpecker's services ending is now causing pipeline failures:
    # https://github.com/woodpecker-ci/woodpecker/issues/6372
    failure: ignore
