---
version: '3'

tasks:
  exportall:
    deps:
      - exportall:github
      - exportall:gitlab

  exportall:github:
    vars:
      SLUG:
        sh: jq -r '.blueprint.slug' package.json
    env:
      GH_CURL_AUTH:
        sh: |
          echo "Authorization: token $GITHUB_TOKEN"
    cmds:
      - git issue exportall github {{.GITHUB_ORG}} {{.SLUG}}
    status:
      - '[ -z "$GITHUB_TOKEN" ]'

  exportall:gitlab:
    vars:
      GITLAB_PATH:
        sh: jq -r '.blueprint.repository.gitlab' package.json | sed 's/https:\/\/gitlab.com\///' | sed 's/\/[^\/]*$//' | sed 's/\//%2F/g'
      GITLAB_SLUG:
        sh: jq -r '.blueprint.repository.gitlab' package.json | sed 's/.*\/\([^\/]*\)$/\1/'
    env:
      GL_CURL_AUTH:
        sh: |
          echo "PRIVATE-TOKEN: $GITLAB_TOKEN"
    cmds:
      - git issue exportall gitlab {{.GITLAB_PATH}} {{.GITLAB_SLUG}}
    status:
      - '[ -z "$GITLAB_TOKEN" ]'

  import:
    deps:
      - import:github
      - import:gitlab

  import:github:
    vars:
      SLUG:
        sh: jq -r '.blueprint.slug' package.json
    env:
      GH_CURL_AUTH:
        sh: |
          echo "Authorization: token $GITHUB_TOKEN"
    cmds:
      - git issue import github {{.GITHUB_ORG}} {{.SLUG}}
    status:
      - '[ -z "$GITHUB_TOKEN" ]'

  import:gitlab:
    vars:
      GITLAB_PATH:
        sh: jq -r '.blueprint.repository.gitlab' package.json | sed 's/https:\/\/gitlab.com\///' | sed 's/\/[^\/]*$//' | sed 's/\//%2F/g'
      GITLAB_SLUG:
        sh: jq -r '.blueprint.repository.gitlab' package.json | sed 's/.*\/\([^\/]*\)$/\1/'
    env:
      GL_CURL_AUTH:
        sh: |
          echo "PRIVATE-TOKEN: $GITLAB_TOKEN"
    cmds:
      - git issue import gitlab {{.GITLAB_PATH}} {{.GITLAB_SLUG}}
    status:
      - '[ -z "$GITLAB_TOKEN" ]'

  init:
    cmds:
      - cmd: git issue init
        ignore_error: true

  synchronize:
    deps:
      - :install:software:git-issue:admin
    cmds:
      - task: init
      - task: import
      - task: exportall