version: 1
policy:
  pullRequests: collaborators_quiet
tasks:
  $let:
    metadata:
      task_id: {$eval: as_slugid("decision_task")}
      github:
        $if: 'tasks_for == "github-pull-request"'
        then:
          action: "pull_request.${event.action}"
          login: ${event.pull_request.user.login}
          ref: ${event.pull_request.head.ref}
          branch: ${event.pull_request.head.ref}
          tag: ""
          sha: ${event.pull_request.head.sha}
          clone_url: ${event.pull_request.head.repo.clone_url}
        else:
          action:
            $if: 'event.ref[:10] == "refs/tags/"'
            then: "tag"
            else: "push"
          login: ${event.pusher.name}
          ref: ${event.ref}
          branch:
            $if: 'event.ref[:11] == "refs/heads/"'
            then: ${event.ref[11:]}
            else: ""
          tag:
            $if: 'event.ref[:10] == "refs/tags/"'
            then: ${event.ref[10:]}
            else: ""
          sha: ${event.after}
          clone_url: ${event.repository.clone_url}
  in:
    $let:
      decision_task:
        taskId: ${metadata.task_id}
        created: {$fromNow: ''}
        deadline: {$fromNow: '60 minutes'}

        provisionerId: "proj-deepspeech"
        workerType: "ci-decision-task"

        scopes: [
          "queue:create-task:highest:proj-deepspeech/*",
          "queue:route:index.project.deepspeech.*",
          "index:insert-task:project.deepspeech.*",
          "queue:scheduler-id:taskcluster-github",
          "generic-worker:cache:deepspeech-macos-pyenv",
          "docker-worker:capability:device:kvm"
        ]

        payload:
          maxRunTime: 600
          image: "ubuntu:18.04"

          features:
            taskclusterProxy: true

          env:
            TASK_ID: ${metadata.task_id}
            GITHUB_HEAD_USER_LOGIN: ${metadata.github.login}
            GITHUB_HEAD_USER_EMAIL: ${metadata.github.login}@users.noreply.github.com
            GITHUB_EVENT: ${metadata.github.action}
            GITHUB_HEAD_REPO_URL: ${metadata.github.clone_url}
            GITHUB_HEAD_BRANCH: ${metadata.github.branch}
            GITHUB_HEAD_TAG: ${metadata.github.tag}
            GITHUB_HEAD_REF: ${metadata.github.ref}
            GITHUB_HEAD_SHA: ${metadata.github.sha}

          command:
            - "/bin/bash"
            - "--login"
            - "-cxe"
            - >
              echo "deb http://archive.ubuntu.com/ubuntu/ bionic-updates main" > /etc/apt/sources.list.d/bionic-updates.list &&
              apt-get -qq update && apt-get -qq -y install git python3-pip curl sudo &&
              adduser --system --home /home/build-user build-user &&
              cd /home/build-user/ &&
              echo -e "#!/bin/bash\nset -xe\nenv && id && mkdir ~/DeepSpeech/ && git clone --quiet ${metadata.github.clone_url} ~/DeepSpeech/ds/ && cd ~/DeepSpeech/ds && git checkout --quiet ${metadata.github.ref}" > /tmp/clone.sh && chmod +x /tmp/clone.sh &&
              sudo -H -u build-user /bin/bash /tmp/clone.sh &&
              sudo -H -u build-user --preserve-env /bin/bash /home/build-user/DeepSpeech/ds/taskcluster/tc-schedule.sh
          artifacts:
            "public":
              type: "directory"
              path: "/tmp/artifacts/"
              expires: {$fromNow: '7 days'}

        metadata:
          name: "DeepSpeech decision task"
          description: "DeepSpeech decision task"
          owner: "${metadata.github.login}@users.noreply.github.com"
          source: "${metadata.github.clone_url}"
    in:
      $flattenDeep:
      - $if: 'tasks_for == "github-pull-request" && event["action"] in ["opened", "reopened", "synchronize"]'
        then: {$eval: decision_task}
      - $if: 'tasks_for == "github-push" && event.ref == "refs/heads/master"'
        then: {$eval: decision_task}
      - $if: 'tasks_for == "github-push" && event.ref[:10] == "refs/tags/"'
        then: {$eval: decision_task}
