---
version: '3'

tasks:
  before:
    deps:
      - :install:software:git
    cmds:
      - task: commit:config
      - task: checkout
      - task: lockfiles
      - task: before:npm
    status:
      - '[ -z "$CI" ]'

  before:npm:
    deps:
      - :install:npm:pnpm
    log:
      error: Error encountered while configuring pnpm to store its cache in `.pnpm-store`
      start: Configuring pnpm to store its cache in `.pnpm-store`
      success: Successfully updated pnpm to store its cache in `.pnpm-store`
    cmds:
      - pnpm config set store-dir .pnpm-store

  checkout:
    deps:
      - :install:software:git
    log:
      error: Failed to pull latest changes
      start: Pulling latest changes
      success: Successfully pulled latest changes
    cmds:
      - cmd: git pull
        ignore_error: true
      - |
        if [ "$CI_COMMIT_REF_NAME" == 'synchronize' ]; then
          git checkout master
          git pull origin master
        else
          git checkout "$CI_COMMIT_REF_NAME"
          git pull origin "$CI_COMMIT_REF_NAME"
        fi
    status:
      - '[ -n "$NO_GIT_CREDS" ]'

  codeclimate:
    deps:
      - :install:software:docker
    log:
      error: Encountered error while running CodeClimate for CI
      start: Running CodeClimate for CI
      success: Successfully finished running CodeClimate for CI
    cmds:
      - |
        if [ -f .config/codeclimate.yml ]; then
          cp .config/codeclimate.yml .codeclimate.yml
        else
          curl -sSL https://gitlab.com/megabyte-labs/common/shared/-/raw/master/.config/codeclimate.yml > .codeclimate.yml
        fi
      - docker pull megabytelabs/codeclimate-internet
      - docker tag megabytelabs/codeclimate-internet codeclimate/codeclimate
      - task: :lint:codeclimate:load:custom-engines
      - task: codeclimate:gitlab

  codeclimate:gitlab:
    cmds:
      - docker run --rm --env CODECLIMATE_CODE="$PWD" --volume "$PWD":/code --volume /var/run/docker.sock:/var/run/docker.sock
        --volume /tmp/cc:/tmp/cc codeclimate/codeclimate analyze --dev -f json > gl-code-quality-report.json
    status:
      - '[ -z "$GITLAB_CI" ]'

  commit:
    deps:
      - :install:software:git
    vars:
      CURR_BRANCH:
        sh: git rev-parse --abbrev-ref HEAD
    log:
      error: Encountered error while pushing changes to {{.CURR_BRANCH}}
      start: Bypassing git hooks and pushing changes to {{.CURR_BRANCH}} (if there are any changes)
    cmds:
      - task: commit:config
      - task: lockfiles:clean
      - task --list > /dev/null || (echo "ERROR Invalid Taskfiles!" && exit 1)
      - git add --all
      - git diff --cached "*"
      - |
        if [[ $(git status --porcelain) ]]; then
          git commit -m "☁️ chore(automation): Applying changes from upstream repository."
          git push -q -o ci.skip origin {{.CURR_BRANCH}} || FAILED_PUSH=$?
          if [ -n "$FAILED_PUSH" ]; then
            git pull -X theirs origin {{.CURR_BRANCH}}
            task --list &> /dev/null || (echo "ERROR: Invalid Taskfiles!" && exit 1)
            git add --all
            git commit -m "☁️ chore(automation): Merging changes from upstream repository with -X theirs."
            git push --force -q -o ci.skip origin {{.CURR_BRANCH}}
          fi
        fi
    status:
      - '[ -z "$CI" ] || [ -n "$NO_GIT_CREDS" ]'

  commit:config:
    deps:
      - :install:software:git
    run: once
    log:
      error: Error configuring git
      start: Configuring git to use CI-friendly parameters
      success: Successfully configured git to use CI parameters
    cmds:
      - git remote set-url origin "https://root:$GROUP_ACCESS_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git"
      - git config user.email "$GITLAB_CI_EMAIL"
      - git config user.name "$GITLAB_CI_NAME"
    status:
      - '[ -z "$GROUP_ACCESS_TOKEN" ] || [ -z "$GITLAB_CI_EMAIL" ] || [ -z "$GITLAB_CI_NAME" ] || [ -z "$GITLAB_CI" ]'

  lockfiles:
    cmds:
      - |
        if [ -f local/package-lock.json ]; then
          cp local/package-lock.json package-lock.json
        fi
      - |
        if [ -f local/yarn.lock ]; then
          cp local/yarn.lock yarn.lock
        fi

  lockfiles:clean:
    cmds:
      - |
        if [ -f local/package-lock.json ]; then
          rm -f package-lock.json
        fi
      - |
        if [ -f local/yarn.lock ];then
          rm -f yarn.lock
        fi

  mirror:github:
    deps:
      - :donothing

  submodules:
    deps:
      - :install:software:git
    log:
      error: Encountered error while ensuring submodules are up-to-date
      start: Ensuring submodules are configured and up-to-date with their master remote
      success: Ensured submodules are up-to-date
    cmds:
      - >
        git submodule foreach 'git config user.email "$GITLAB_CI_EMAIL"; git config user.name "$GITLAB_CI_NAME";
        DEFAULT_BRANCH="$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')";
        git checkout -q "$DEFAULT_BRANCH"; git pull -q origin "$DEFAULT_BRANCH" --ff-only'
    status:
      - '[ -z "$CI" ] || [ -n "$NO_GIT_CREDS" ]'

  synchronize:
    deps:
      - commit:config
    run: once
    log:
      error: Failed to update the `synchronize` branch
      start: Synchronizing the `synchronize` branch with the `master` branch
      success: Successfully updated the `synchronize` branch
    cmds:
      - git fetch origin
      - git checkout -b synchronize || git checkout synchronize
      - git reset --hard HEAD
      - git pull -q origin master
      - |
        git push --force -q -o ci.skip origin synchronize || FAILED_SYNC=$?
        if [ -n "$FAILED_SYNC" ]; then
          task git:gitlab:protected:off -- "synchronize"
          git push --force -q -o ci.skip origin synchronize
        fi
      - task: synchronize:gitlab
      - git checkout master
      - task: :ci:github:synchronize
    status:
      - '[ -n "$NO_GIT_CREDS" ] || ([ "$FULLY_AUTOMATED_TASKS" != "true" ] && [ -z "$CI" ])'

  synchronize:gitlab:
    run: once
    cmds:
      - curl -s --request POST --form "token=${CI_JOB_TOKEN}" --form ref=master --form "variables[PIPELINE_SOURCE]=$PIPELINE_SOURCE"
        "https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/trigger/pipeline"
    status:
      - '[ -z "$GITLAB_CI" ] || [ -n "$NO_GITLAB_SYNCHRONIZE" ]'