name: Update generated files
on:
  workflow_dispatch: {}
  schedule:
    - cron: "0 5 * * Thu"

env:
  DEFAULT_BRANCH: main

jobs:
  update_maxpods:
    name: Update maxpods and open PR
    runs-on: ubuntu-latest
    env:
      UPDATE_BRANCH: update-maxpods
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Setup go
        uses: actions/setup-go@v2
        with:
          go-version: "^1.14"
      - name: Update maxpods
        run: make update-maxpods
      - name: Setup identity as weaveworksbot
        uses: ./.github/actions/setup-identity
        with:
          token: "${{ secrets.GITHUB_TOKEN }}"
      - name: Commit changes
        run: |
          git checkout $DEFAULT_BRANCH
          git checkout -B $UPDATE_BRANCH
          git add -u
          EDITOR=true git commit -m "Update maxpods"
          ! git diff --exit-code $DEFAULT_BRANCH HEAD
          git push --force-with-lease origin HEAD
      - uses: actions/github-script@v2.0.0
        name: Open PR to ${{env.DEFAULT_BRANCH}}
        with:
          github-token: ${{ secrets.WEAVEWORKSBOT_TOKEN }}
          script: |
            const { data: pr } = await github.pulls.create({
              ...context.repo,
              title: "Update maxpods",
              head: "${{ env.UPDATE_BRANCH }}",
              base: "${{ env.DEFAULT_BRANCH }}",
            });
            await github.issues.addLabels({
              ...context.repo,
              issue_number: pr.number,
              labels: ["kind/improvement"],
            });
  update_aws_node:
    name: Update aws-node and open PR
    runs-on: ubuntu-latest
    container: weaveworks/eksctl-build:563d25d20c1c4ee95ebd8c8e4a155e463fec1559
    env:
      UPDATE_BRANCH: update-aws-node
      GOPRIVATE: ""
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Cache go-build and mod
        uses: actions/cache@v2
        with:
          path: |
            ~/.cache/go-build/
            ~/go/pkg/mod/
          key: go-${{ hashFiles('go.sum') }}
          restore-keys: |
            go-
      - name: Update aws-node
        run: make update-aws-node
      - name: Commit changes
        run: |
          git checkout $DEFAULT_BRANCH
          git checkout -B $UPDATE_BRANCH
          git config user.name "weaveworksbot"
          git config user.email "weaveworksbot@users.noreply.github.com"
          git add -u
          EDITOR=true git commit -m "Update aws-node"
          ! git diff --exit-code $DEFAULT_BRANCH HEAD
          git push --force-with-lease origin HEAD
      - uses: actions/github-script@v2.0.0
        name: Open PR to ${{env.DEFAULT_BRANCH}}
        with:
          github-token: ${{ secrets.WEAVEWORKSBOT_TOKEN }}
          script: |
            const { data: pr } = await github.pulls.create({
              ...context.repo,
              title: "Update aws-node",
              head: "${{ env.UPDATE_BRANCH }}",
              base: "${{ env.DEFAULT_BRANCH }}",
            });
            await github.issues.addLabels({
              ...context.repo,
              issue_number: pr.number,
              labels: ["kind/improvement"],
            });
