name: create-release

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version'
        required: true

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
          token: ${{ secrets.REPO_ACCESS }}

      - name: Setup Node
        uses: actions/setup-node@v2.1.2
        with:
          node-version: '14'

      - name: Bump version
        run: |
          jq --arg v ${{ github.event.inputs.version }} '.version = $v' package.json > tmp && mv tmp package.json
          jq --arg v ${{ github.event.inputs.version }} '.version = $v' package-lock.json > tmp && mv tmp package-lock.json

      - name: Build
        run: |
          npm ci

      - name: Generate changelog
        run: npx @code-chris/changelog-generator --release-version ${{ github.event.inputs.version }} --github-handle ${{ github.repository }} --commit-output commits.md

      - name: Commit files
        id: commit
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git commit -m "chore: release ${{ github.event.inputs.version }}" -a

      - name: Push changes
        uses: ad-m/github-push-action@v0.6.0
        with:
          github_token: ${{ secrets.REPO_ACCESS }}
          branch: master

      - name: Create release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.event.inputs.version }}
          release_name: ${{ github.event.inputs.version }}
          body_path: commits.md
          prerelease: ${{ contains(github.event.inputs.version, 'alpha') || contains(github.event.inputs.version, 'beta') || contains(github.event.inputs.version, 'rc') }}
