name: Update
on:
  repository_dispatch:
    types: ["octokit/openapi release"]

  push:
    branches:
      - dependabot/npm_and_yarn/openapi-typescript-*

  workflow_dispatch:
    inputs:
      version:
        description: "Version of https://www.npmjs.com/package/@octokit/openapi"
        required: true

jobs:
  update:
    runs-on: ubuntu-latest
    if: github.event_name == 'workflow_dispatch' || github.event.client_payload.action == 'published'
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2.1.3
      - run: npm ci
      # do not update cache for dependabot update
      - run: |
          npm run download
          npm run generate-types
        if: github.event_name == 'repository_dispatch'
        env:
          VERSION: ${{ github.event.client_payload.release.tag_name }}
      - run: |
          npm run download
          npm run generate-types
        if: github.event_name == 'workflow_dispatch'
        env:
          VERSION: ${{ github.event.inputs.version }}

      # create/update pull request for dispatch event update
      - name: Create Pull Request
        if: github.event_name != 'push'
        uses: gr2m/create-or-update-pull-request-action@v1.x
        env:
          GITHUB_TOKEN: ${{ secrets.OCTOKITBOT_PAT }}
        with:
          title: "🚧 OpenAPI types changed"
          body: |
            Make sure to update the commits so that the merge results in helpful release notes, see [Merging the Pull Request & releasing a new version](https://github.com/octokit/rest.js/blob/master/CONTRIBUTING.md#merging-the-pull-request--releasing-a-new-version).

            In general

            - Avoid breaking changes at all costs
            - If there are no typescript changes, use `build: cache` as commit message
            - If there are there are only updates, use `fix: ...`
            - If there are any new additions, use `feat: ...`
            - If there are breaking changes, keep the previous ones and deprecate them. Only if there is no other way, add `BREAKING CHANGE: ...` to the commit body (not subject!) to trigger a breaking change.
          branch: "openapi-update"
          commit-message: "WIP"
          author: "Octokit Bot <33075676+octokitbot@users.noreply.github.com>"
          labels: "maintenance"

      # update pull request for dependabot update
      - name: Create Pull Request
        if: github.event_name == 'push'
        uses: gr2m/create-or-update-pull-request-action@v1.x
        env:
          GITHUB_TOKEN: ${{ secrets.OCTOKITBOT_PAT }}
        with:
          title: "🚧 OpenAPI types changed"
          body: |
            Make sure to update the commits so that the merge results in helpful release notes, see [Merging the Pull Request & releasing a new version](https://github.com/octokit/rest.js/blob/master/CONTRIBUTING.md#merging-the-pull-request--releasing-a-new-version).

            In general

            - Avoid breaking changes at all costs
            - If there are no typescript changes, use `build: cache` as commit message
            - If there are there are only updates, use `fix: ...`
            - If there are any new additions, use `feat: ...`
            - If there are breaking changes, keep the previous ones and deprecate them. Only if there is no other way, add `BREAKING CHANGE: ...` to the commit body (not subject!) to trigger a breaking change.
          branch: "${{ github.ref }}"
          commit-message: "WIP"
          author: "Octokit Bot <33075676+octokitbot@users.noreply.github.com>"
          labels: "maintenance"
