name: Zenhub
on:
  repository_dispatch:
    types: [zenhub-cmd]
  schedule:
    - cron: "0 0 * * *"
jobs:
  day:
    if: github.event.client_payload.slash_command == null || github.event.client_payload.slash_command.arg1 == 'day'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: "3.8"
      - uses: dschep/install-pipenv-action@v1
      - id: cache
        uses: actions/cache@master
        with:
          path: .venv
          key: |
            pipenv-${{ hashFiles('**/Pipfile.lock') }}
          restore-keys: |
            pipenv-
      - run: |
          pipenv install --dev
        env:
          PIPENV_VENV_IN_PROJECT: true
      - id: day
        uses: JasonEtco/create-an-issue@v2
        env:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
        with:
          filename: .github/zenhub/day.md
          assignees: shunkakinoki
      - id: morning
        uses: JasonEtco/create-an-issue@v2
        env:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
        with:
          filename: .github/zenhub/morning.md
          assignees: shunkakinoki
      - id: noon
        uses: JasonEtco/create-an-issue@v2
        env:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
        with:
          filename: .github/zenhub/noon.md
          assignees: shunkakinoki
      - id: night
        uses: JasonEtco/create-an-issue@v2
        env:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
        with:
          filename: .github/zenhub/night.md
          assignees: shunkakinoki
      - run: |
          pipenv run python .github/scripts/zenhub.py -a day -p 1 -i ${{ steps.day.outputs.number }}
          pipenv run python .github/scripts/zenhub.py -a session -p 1 -d ${{ steps.day.outputs.number }} -i ${{ steps.morning.outputs.number }}
          pipenv run python .github/scripts/zenhub.py -a session -p 2 -d ${{ steps.day.outputs.number }} -i ${{ steps.noon.outputs.number }}
          pipenv run python .github/scripts/zenhub.py -a session -p 3 -d ${{ steps.day.outputs.number }} -i ${{ steps.night.outputs.number }}
        env:
          ZENHUB_API_TOKEN: ${{ secrets.ZENHUB_API_TOKEN }}
  week:
    if: github.event.client_payload.slash_command.arg1 == 'week'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: "3.8"
      - uses: dschep/install-pipenv-action@v1
      - id: cache
        uses: actions/cache@master
        with:
          path: .venv
          key: |
            pipenv-${{ hashFiles('**/Pipfile.lock') }}
          restore-keys: |
            pipenv-
      - run: |
          pipenv install --dev
        env:
          PIPENV_VENV_IN_PROJECT: true
      - id: week
        uses: JasonEtco/create-an-issue@v2
        env:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
        with:
          filename: .github/zenhub/week.md
          assignees: shunkakinoki
      - id: current-time
        uses: gerred/actions/current-time@master
      - uses: actions/github-script@v2
        with:
          github-token: ${{ secrets.ACCESS_TOKEN }}
          script: |
            github.issues.createMilestone({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              title: "Pioneer Week",
              due_on: $TIME
            })
        env:
          TIME: ${{ steps.current-time.outputs.time }}
