name: upgrade-mainline
on:
  workflow_dispatch: {}
  schedule:
    - cron: 0 0 * * 1
jobs:
  upgrade:
    name: Upgrade
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      patch_created: ${{ steps.create_patch.outputs.patch_created }}
    env:
      CI: ""
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: mainline
      - name: PDK Init
        uses: ./.github/actions/pdk-init
      - name: Upgrade projen
        run: pnpm nx run @aws/monorepo:upgrade-projen
      - name: Upgrade dependencies
        run: pnpm projen upgrade-deps
      - name: Build
        run: pnpm build || true
      - id: create_patch
        name: Find mutations
        run: |-
          git add .
          git diff --staged --patch --exit-code > .repo.patch || echo "patch_created=true" >> $GITHUB_OUTPUT
      - if: steps.create_patch.outputs.patch_created
        name: Upload patch
        uses: actions/upload-artifact@v2
        with:
          name: .repo.patch
          path: .repo.patch
  pr:
    name: Create Pull Request
    needs: upgrade
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    if: ${{ needs.upgrade.outputs.patch_created }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          token: ${{ secrets.PROJEN_GITHUB_TOKEN }}
          ref: mainline
      - name: Download patch
        uses: actions/download-artifact@v2
        with:
          name: .repo.patch
          path: ${{ runner.temp }}
      - name: Apply patch
        run: '[ -s ${{ runner.temp }}/.repo.patch ] && git apply ${{ runner.temp }}/.repo.patch || echo "Empty patch. Skipping."'
      - name: Set git identity
        run: |-
          git config user.name "github-actions"
          git config user.email "github-actions@github.com"
      - name: Create Pull Request
        id: create-pr
        uses: peter-evans/create-pull-request@v3
        with:
          token: ${{ secrets.PROJEN_GITHUB_TOKEN }}
          commit-message: |-
            chore(deps): upgrade dependencies

            Upgrades project dependencies. See details in [workflow run].

            [Workflow Run]: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}

            ------

            *Automatically created by projen via the "upgrade-mainline" workflow*
          branch: github-actions/upgrade-mainline
          title: "chore(deps): upgrade dependencies"
          body: |-
            Upgrades project dependencies. See details in [workflow run].

            [Workflow Run]: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}

            ------

            *Automatically created by projen via the "upgrade-mainline" workflow*
          author: github-actions <github-actions@github.com>
          committer: github-actions <github-actions@github.com>
          signoff: true