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

  push:
    branches:
      - renovate/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.4
      - run: npm ci

      # set gOCTOKIT_OPENAPI_VERSION environment variable for all next steps
      # https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
      - run: echo "OCTOKIT_OPENAPI_VERSION=${{ github.event.client_payload.release.tag_name }}" >> $GITHUB_ENV
        if: github.event.client_payload.release.tag_name
      - run: echo "OCTOKIT_OPENAPI_VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
        if: github.event.inputs.version
      # Checking if setting the version actually works
      - run: echo "OCTOKIT_OPENAPI_VERSION => $OCTOKIT_OPENAPI_VERSION"

      - uses: gr2m/await-npm-package-version-action@v1
        with:
          package: "@octokit/openapi"
          version: ${{ env.OCTOKIT_OPENAPI_VERSION }}

      # do not update cache for renovate update
      - name: Update & generate types
        run: |
          npm run download
          npm run generate-types
        if: github.event_name != 'push'

      # 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,typescript"

      # update pull request for renovate 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,typescript"
