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

variables:
  - &rust_image "rust:1.81"
  - &rust_nightly_image "rustlang/rust:nightly"
  - &install_pnpm "corepack enable pnpm"
  - &install_binstall "wget -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"
  - install_diesel_cli: &install_diesel_cli
      - apt-get update && apt-get install -y postgresql-client
      - cargo install diesel_cli --no-default-features --features postgres
      - export PATH="$CARGO_HOME/bin:$PATH"
  - &slow_check_paths
    - event: pull_request
      path:
        include: [
            # rust source code
            "crates/**",
            "src/**",
            "**/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: tmknom/prettier:3.2.5
    commands:
      - prettier -c . '!**/volumes' '!**/dist' '!target' '!**/translations' '!api_tests/pnpm-lock.yaml'
    when:
      - event: pull_request

  toml_fmt:
    image: tamasfe/taplo:0.9.3
    commands:
      - taplo format --check
    when:
      - event: pull_request

  sql_fmt:
    image: backplane/pgformatter
    commands:
      - ./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
    commands:
      - rustup component add rustfmt
      - cargo +nightly fmt -- --check
    when:
      - event: pull_request

  cargo_shear:
    image: *rust_nightly_image
    commands:
      - *install_binstall
      - cargo binstall -y cargo-shear
      - cargo shear
    when:
      - event: pull_request

  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

  cargo_clippy:
    image: *rust_image
    environment:
      CARGO_HOME: .cargo_home
    commands:
      - rustup component add clippy
      - cargo clippy --workspace --tests --all-targets -- -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
      LEMMY_TEST_FAST_FEDERATION: "1"
      LEMMY_CONFIG_LOCATION: ../../config/config.hjson
    commands:
      - cargo test --workspace --no-fail-fast
    when: *slow_check_paths

  check_ts_bindings:
    image: *rust_image
    environment:
      CARGO_HOME: .cargo_home
    commands:
      - ./scripts/ts_bindings_check.sh
    when:
      - event: pull_request

  # 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
    commands:
      - cargo check --package lemmy_api_common
    when: *slow_check_paths

  lemmy_api_common_doesnt_depend_on_diesel:
    image: *rust_image
    environment:
      CARGO_HOME: .cargo_home
    commands:
      - "! cargo tree -p lemmy_api_common --no-default-features -i diesel"
    when: *slow_check_paths

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

  check_defaults_hjson_updated:
    image: *rust_image
    environment:
      CARGO_HOME: .cargo_home
    commands:
      - ./scripts/update_config_defaults.sh config/defaults_current.hjson
      - diff config/defaults.hjson config/defaults_current.hjson
    when: *slow_check_paths

  check_diesel_schema:
    image: *rust_image
    environment:
      CARGO_HOME: .cargo_home
      DATABASE_URL: postgres://lemmy:password@database:5432/lemmy
    commands:
      - <<: *install_diesel_cli
      - cp crates/db_schema/src/schema.rs tmp.schema
      - diesel migration run
      - diff tmp.schema crates/db_schema/src/schema.rs
    when: *slow_check_paths

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

  check_diesel_migration:
    # TODO: use willsquire/diesel-cli image when shared libraries become optional in lemmy_server
    image: *rust_image
    environment:
      LEMMY_DATABASE_URL: postgres://lemmy:password@database:5432/lemmy
      RUST_BACKTRACE: "1"
      CARGO_HOME: .cargo_home
      DATABASE_URL: postgres://lemmy:password@database:5432/lemmy
      PGUSER: lemmy
      PGPASSWORD: password
      PGHOST: database
      PGDATABASE: lemmy
    commands:
      # Install diesel_cli
      - <<: *install_diesel_cli
      # Run all migrations
      - diesel migration run
      - psql -c "DROP SCHEMA IF EXISTS r CASCADE;"
      - pg_dump --no-owner --no-privileges --no-table-access-method --schema-only --no-sync -f before.sqldump
      # Make sure that the newest migration is revertable without the `r` schema
      - diesel migration redo
      # Run schema setup twice, which fails on the 2nd time if `DROP SCHEMA IF EXISTS r CASCADE` drops the wrong things
      - alias lemmy_schema_setup="target/lemmy_server --disable-scheduled-tasks --disable-http-server --disable-activity-sending"
      - lemmy_schema_setup
      - lemmy_schema_setup
      # Make sure that the newest migration is revertable with the `r` schema
      - diesel migration redo
      # Check for changes in the schema, which would be caused by an incorrect migration
      - psql -c "DROP SCHEMA IF EXISTS r CASCADE;"
      - pg_dump --no-owner --no-privileges --no-table-access-method --schema-only --no-sync -f after.sqldump
      - diff before.sqldump after.sqldump
    when: *slow_check_paths

  check_db_perf_tool:
    image: *rust_image
    environment:
      LEMMY_DATABASE_URL: postgres://lemmy:password@database:5432/lemmy
      RUST_BACKTRACE: "1"
      CARGO_HOME: .cargo_home
    commands:
      # same as scripts/db_perf.sh but without creating a new database server
      - cargo run --package lemmy_db_perf -- --posts 10 --read-post-pages 1
    when: *slow_check_paths

  run_federation_tests:
    image: node:22-bookworm-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 api-test
    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

  publish_release_docker:
    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, linux/arm64
      build_args:
        - RUST_RELEASE_MODE=release
      tag: ${CI_COMMIT_TAG}
    when:
      - event: tag

  nightly_build:
    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,linux/arm64
      build_args:
        - RUST_RELEASE_MODE=release
      tag: dev
    when:
      - event: cron

  # using https://github.com/pksunkara/cargo-workspaces
  publish_to_crates_io:
    image: *rust_image
    environment:
      CARGO_API_TOKEN:
        from_secret: cargo_api_token
    commands:
      - *install_binstall
      # Install cargo-workspaces
      - cargo binstall -y cargo-workspaces
      - cp -r migrations crates/db_schema/
      - cargo workspaces publish --token "$CARGO_API_TOKEN" --from-git --allow-dirty --no-verify --allow-branch "${CI_COMMIT_TAG}" --yes custom "${CI_COMMIT_TAG}"
    when:
      - event: tag

  notify_on_build:
    image: alpine:3
    commands:
      - apk add curl
      - "curl -d'Lemmy CI build ${CI_PIPELINE_STATUS}: ${CI_PIPELINE_URL}' ntfy.sh/lemmy_drone_ci"
    when:
      - event: [pull_request, tag]
        status: [failure, success]

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

services:
  database:
    # 15-alpine image necessary because of diesel tests
    image: pgautoupgrade/pgautoupgrade:15-alpine
    environment:
      POSTGRES_USER: lemmy
      POSTGRES_PASSWORD: password