name: Maintenance

on:
  schedule:
    - cron: "0 20 * * 1"

jobs:
  npm-upgrades:
    name: Regenerate package-lock.json
    runs-on: macos-latest
    steps:
      - name: Check out code
        uses: actions/checkout@v2

      - name: Set up Node JS
        uses: actions/setup-node@v2
        with:
          node-version: 14.15.4

      - name: Regenerate package-lock.json
        run: make -C ui npm-upgrade

      - name: Rebuild react app
        run: make -C ui build

      - name: Create Pull Request
        id: cpr
        uses: peter-evans/create-pull-request@v3.7.0
        with:
          token: ${{ secrets.GH_REPO_TOKEN }}
          author: "Łukasz Mierzwa <l.mierzwa@gmail.com>"
          commit-message: "chore(ui): upgrade all 3rd party dependencies"
          branch: npm-upgrades
          delete-branch: true
          title: "chore(ui): upgrade all 3rd party dependencies"

      - name: Check outputs
        run: |
          echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
          echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"

  go-mod-upgrades:
    name: Upgrade all Go modules
    runs-on: ubuntu-latest
    steps:
      - name: Check out code
        uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.15.7

      - name: Mock web assets
        run: make mock-assets

      - name: Upgrade all Go modules
        run: go get -u ./...

      - name: Run go mod tidy
        run: go mod tidy

      - name: Create Pull Request
        id: cpr
        uses: peter-evans/create-pull-request@v3.7.0
        with:
          token: ${{ secrets.GH_REPO_TOKEN }}
          author: "Łukasz Mierzwa <l.mierzwa@gmail.com>"
          commit-message: "chore(backend): upgrade all Go modules"
          branch: go-mod-upgrades
          delete-branch: true
          title: "chore(backend): upgrade all Go modules"

      - name: Check outputs
        run: |
          echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
          echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
