# SPDX-FileCopyrightText: 2010 - 2024 the Friendica project
#
# SPDX-License-Identifier: CC0-1.0

# The code standard check is just triggered for PRs and pushes to non-stable branches of Friendica
when:
  branch:
    exclude: [ stable ]
  event: [ pull_request, push ]

steps:
  restore_cache:
    image: meltwater/drone-cache:dev
    settings:
      backend: "filesystem"
      restore: true
      cache_key: '{{ .Repo.Name }}_phpcs_{{ arch }}_{{ os }}'
      archive_format: "gzip"
      mount:
        - '.composer'
    volumes:
      - /tmp/drone-cache:/tmp/cache
  composer_install:
    image: composer
    commands:
      - export COMPOSER_HOME=.composer
      - ./bin/composer.phar run cs:install
  rebuild_cache:
    image: meltwater/drone-cache:dev
    settings:
      backend: "filesystem"
      rebuild: true
      cache_key: '{{ .Repo.Name }}_phpcs_{{ arch }}_{{ os }}'
      archive_format: "gzip"
      mount:
        - '.composer'
    volumes:
      - /tmp/drone-cache:/tmp/cache
  check:
    image: php:8.3
    commands:
      - echo "**** Use bin/dev/fix-codestyle.sh in case of errors ****"
      - apt-get update -q
      - DEBIAN_FRONTEND=noninteractive apt-get install -q -y git
      - if [ ! -z "$${CI_COMMIT_PULL_REQUEST}" ]; then
          git fetch --no-tags origin ${CI_COMMIT_TARGET_BRANCH};
          CHANGED_FILES="$(git diff --name-only --diff-filter=ACMRTUXB $(git merge-base FETCH_HEAD origin/${CI_COMMIT_TARGET_BRANCH})..${CI_COMMIT_SHA})";
        else
          CHANGED_FILES="$(git diff --name-only --diff-filter=ACMRTUXB ${CI_COMMIT_SHA})";
        fi
      - if ! echo "$${CHANGED_FILES}" | grep -qE "^(\\.php-cs-fixer(\\.dist)?\\.php|composer\\.lock)$"; then
          EXTRA_ARGS=$(printf -- '--path-mode=intersection\n--\n%s' "$${CHANGED_FILES}");
        else
          EXTRA_ARGS='';
        fi
      - ./bin/dev/php-cs-fixer/vendor/bin/php-cs-fixer check --config=.php-cs-fixer.dist.php -v --diff --using-cache=no $${EXTRA_ARGS}