# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches: [ main ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - name: Get version
        id: get_version
        run: |
          echo "::set-output name=version::$(grep -P -o '(?<=## \[).*(?=\])' -m1 CHANGELOG.md)"
      
      - name: Extract release notes
        id: extract-release-notes
        if: github.ref == 'refs/heads/main'
        uses: ffurrer2/extract-release-notes@v1
          
      - name: Tag
        id: create_tag
        if: github.ref == 'refs/heads/main'
        uses: mathieudutour/github-tag-action@v5.5
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
          custom_tag: ${{ steps.get_version.outputs.version }} 
          create_annotated_tag: True
          tag_prefix: ''

      - name: Build PDF
        id: build_pdf
        if: github.ref == 'refs/heads/main'
        uses: avattathil/asciidoctor-action@master
        with:
          program: "asciidoctor-pdf -a allow-uri-read -D . spec/TestFormatSpec.adoc"
      
      - name: Create Release
        id: create_release
        if: github.ref == 'refs/heads/main'
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
        with:
          release_branch: refs/heads/main
          release_name: ${{ steps.get_version.outputs.version }}
          tag_name: ${{ steps.get_version.outputs.version }}
          body: ${{ steps.extract-release-notes.outputs.release_notes }}
          draft: false
          prerelease: false

      - name: Upload Release Asset
        id: upload-release-asset 
        if: github.ref == 'refs/heads/main'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }} 
          asset_path: ./TestFormatSpec.pdf
          asset_name: TestFormatSpec.pdf
          asset_content_type: application/pdf
