---
version: '3'

vars:
  GIT_NOTIFY_COLOR: '#0450ec'

tasks:
  commit-msg:
    deps:
      - :lint:commit
    log:
      error: '`commit-msg` hook encountered an error!'
      start: '`commit-msg` hook running..'

  post-checkout:
    deps:
      - :install:npm:git-notify
      - :install:npm:yarnhook
    log:
      error: '`post-checkout` hook encountered an error!'
      start: '`post-checkout` hook running..'
    cmds:
      - git-notify checkout --prefix "@notify" --color "{{.GIT_NOTIFY_COLOR}}" "$GIT_PARAMS"
      - yarnhook

  post-commit:
    log:
      error: '`post-commit` hook encountered an error!'
      start: '`post-commit` hook running..'
    cmds:
      - 'true'

  post-merge:
    deps:
      - :install:npm:git-notify
      - :install:npm:yarnhook
    log:
      error: '`post-merge` hook encountered an error!'
      start: '`post-merge` hook running..'
    cmds:
      - git-notify merge --prefix "@notify" --color "{{.GIT_NOTIFY_COLOR}}" "$GIT_PARAMS"
      - yarnhook

  post-rewrite:
    deps:
      - :install:npm:git-notify
      - :install:npm:yarnhook
    log:
      error: '`post-rewrite` hook encountered an error!'
      start: '`post-rewrite` hook running..'
    cmds:
      - git-notify rewrite --prefix "@notify" --color "{{.GIT_NOTIFY_COLOR}}" "$GIT_PARAMS"
      - yarnhook

  pre-commit:
    deps:
      - :fix:json
      - :fix:misc
      - :security:gitleaks
      - :security:private-keys
    log:
      error: '`pre-commit` hook encountered an error!'
      start: '`pre-commit` hook running..'

  pre-push:
    vars:
      GITHUB_TOKEN:
        sh: echo "$GITHUB_TOKEN"
      GITLAB_TOKEN:
        sh: echo "$GITLAB_TOKEN"
    log:
      error: '`pre-push` hook encountered an error!'
      start: '`pre-push` hook running..'
    cmds:
      - |
        REMOTE={{index (split " " (trimAll "'" .CLI_ARGS)) "_0"}}
        URL={{index (split " " (trimAll "'" .CLI_ARGS)) "_1" | replace .GITLAB_TOKEN "" | replace .GITHUB_TOKEN ""}}
        if [[ $URL == http* ]]; then
          URL="$(echo $URL | sed 's/^[^@]*@/git@/')"
        fi
        HOST="$(echo $URL | sed 's/^.*@//' | sed 's/:.*$//')"
        ORG="$(echo $URL | sed 's/^.*://' | sed 's/\/.*$//')"
        PROJECT="$(echo $URL | sed 's/^[^\/]*\///' | sed 's/.[^\.]*$//')"
        IFS=' '
        while read local_ref local_sha remote_ref remote_sha; do
          # local_ref -> refs/heads/master
          # local_sha -> b14959a077c9b6ced19c29d5ba9c75ddbad5d3ea
          # remote_ref -> refs/heads/master
          # remote_sha -> 9901a2eb2f34d5744276a80c4b339a88a92a7b53
          REMOTE_BRANCH="$(echo $remote_ref | sed 's/^[^\/]*\/[^\/]*\///')"
          if [ "$(echo "$HOST" | cut -c1-8)" == 'https://' ]; then
            HOST="$(echo "$HOST" | cut -c8)"
          fi
          .config/log star "Repository -----> https://$HOST/$ORG/$PROJECT"
          if [ "$HOST" == 'gitlab.com' ]; then
            if [ "$REMOTE_BRANCH" != 'master' ]; then
              .config/log star "Open PR --------> https://$HOST/$ORG/$PROJECT/-/merge_requests/new"
            fi
            .config/log star "View commit ----> https://$HOST/$ORG/$PROJECT/-/commit/$local_sha"
          elif [ "$HOST" == 'github.com' ]; then
            if [ "$REMOTE_BRANCH" != 'master' ]; then
              .config/log star "Open PR --------> https://$HOST/$ORG/$PROJECT/pull/new/$REMOTE_BRANCH"
            fi
            .config/log star "View commit ----> https://$HOST/$ORG/$PROJECT/commit/$local_sha"
          fi
        done
