name: Create GitHub Pre-Release after GitLab tag mirror

on:
  push:
    tags:
      - '[0-9]+.[0-9]+.[0-9]+-alpha.[0-9]+'
      - '[0-9]+.[0-9]+.[0-9]+-beta.[0-9]+'
      - '[0-9]+.[0-9]+.[0-9]+-pre.[0-9]+'

jobs:
  release:
    runs-on: ubuntu-latest
    # OUTPUTS
    outputs:
      newtag: ${{ steps.tag.outputs.tag }}
      uploadurl: ${{ steps.create_release.outputs.upload_url }}
    steps:
      # GET LATEST CODE
      - name: Checkout latest code
        uses: actions/checkout@master
        with:
          fetch-depth: 0

      # EXTRACT TAG FROM PUSH
      - name: Get tag
        id: tag
        uses: dawidd6/action-get-tag@v1
      - name: Use tag
        run: echo ${{steps.tag.outputs.tag}}

      # GENERATE CHANGELOG, RELEASE
      - id: conventional_changelog
        uses: ardalanamini/auto-changelog@master
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Create Release
        id: create_release
        uses: actions/create-release@latest
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{steps.tag.outputs.tag}}
          body: |
            Automatic release of version: **${{steps.tag.outputs.tag}}**
            **Changes in this release:**
            ${{ steps.conventional_changelog.outputs.changelog }}
          draft: false
          prerelease: true

  main:
    needs: release
    runs-on: ubuntu-latest
    steps:
      # GET LATEST CODE
      - name: Checkout latest code
        uses: actions/checkout@master
        with:
          ref: main
          fetch-depth: 0

      # BUILD PROJECT AND PUBLISH TO PACKAGES
      - name: Set up JDK 8
        uses: actions/setup-java@v2
        with:
          distribution: 'adopt'
          java-version: '8'

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Publish to GitHub Packages
        env:
          GITHUB_ACTOR: ${{ secrets.GITHUB_ACTOR }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          NEW_VERSION=$(echo "${{ needs.release.outputs.newtag }}")
          echo "New version: ${NEW_VERSION}"
          echo "Github username: ${GITHUB_ACTOR}"
          ./gradlew about
          ./gradlew -Pversion=${NEW_VERSION} build --info -x test

      # UPLOAD ASSETS TO RELEASE
      - name: Upload Release Asset Main Jar
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url:  ${{ needs.release.outputs.uploadurl }}
          asset_path: ./build/libs/ExampleAddon-${{ needs.release.outputs.newtag }}.jar
          asset_name: ExampleAddon-${{ needs.release.outputs.newtag }}.jar
          asset_content_type: application/jar

      - name: Upload Release Asset Sources Jar
        id: upload-release-asset-sources
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url:  ${{ needs.release.outputs.uploadurl }}
          asset_path: ./build/libs/ExampleAddon-${{ needs.release.outputs.newtag }}-sources.jar
          asset_name: ExampleAddon-${{ needs.release.outputs.newtag }}-sources.jar
          asset_content_type: application/jar

      - name: Upload Release Asset Javadoc Jar
        id: upload-release-asset-javadoc
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url:  ${{ needs.release.outputs.uploadurl }}
          asset_path: ./build/libs/ExampleAddon-${{ needs.release.outputs.newtag }}-javadoc.jar
          asset_name: ExampleAddon-${{ needs.release.outputs.newtag }}-javadoc.jar
          asset_content_type: application/jar

  alternative:
    needs: release
    runs-on: ubuntu-latest
    steps:
      # GET LATEST CODE
      - name: Checkout latest code
        uses: actions/checkout@master
        with:
          ref: alternativeExtensionDeclaration
          fetch-depth: 0

      # BUILD PROJECT AND PUBLISH TO PACKAGES
      - name: Set up JDK 8
        uses: actions/setup-java@v2
        with:
          distribution: 'adopt'
          java-version: '8'

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Publish to GitHub Packages
        env:
          GITHUB_ACTOR: ${{ secrets.GITHUB_ACTOR }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          NEW_VERSION=$(echo "${{ needs.release.outputs.newtag }}")
          echo "New version: ${NEW_VERSION}"
          echo "Github username: ${GITHUB_ACTOR}"
          ./gradlew about
          ./gradlew -Pversion=${NEW_VERSION} build --info -x test

      # UPLOAD ASSETS TO RELEASE
      - name: Upload Release Asset Main Jar
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url:  ${{ needs.release.outputs.uploadurl }}
          asset_path: ./build/libs/AlternativeExtensionDeclaration-${{ needs.release.outputs.newtag }}.jar
          asset_name: AlternativeExtensionDeclaration-${{ needs.release.outputs.newtag }}.jar
          asset_content_type: application/jar

      - name: Upload Release Asset Sources Jar
        id: upload-release-asset-sources
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url:  ${{ needs.release.outputs.uploadurl }}
          asset_path: ./build/libs/AlternativeExtensionDeclaration-${{ needs.release.outputs.newtag }}-sources.jar
          asset_name: AlternativeExtensionDeclaration-${{ needs.release.outputs.newtag }}-sources.jar
          asset_content_type: application/jar

      - name: Upload Release Asset Javadoc Jar
        id: upload-release-asset-javadoc
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url:  ${{ needs.release.outputs.uploadurl }}
          asset_path: ./build/libs/AlternativeExtensionDeclaration-${{ needs.release.outputs.newtag }}-javadoc.jar
          asset_name: AlternativeExtensionDeclaration-${{ needs.release.outputs.newtag }}-javadoc.jar
          asset_content_type: application/jar

  tetris:
    needs: release
    runs-on: ubuntu-latest
    steps:
      # GET LATEST CODE
      - name: Checkout latest code
        uses: actions/checkout@master
        with:
          ref: tetris
          fetch-depth: 0

      # BUILD PROJECT AND PUBLISH TO PACKAGES
      - name: Set up JDK 8
        uses: actions/setup-java@v2
        with:
          distribution: 'adopt'
          java-version: '8'

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Publish to GitHub Packages
        env:
          GITHUB_ACTOR: ${{ secrets.GITHUB_ACTOR }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          NEW_VERSION=$(echo "${{ needs.release.outputs.newtag }}")
          echo "New version: ${NEW_VERSION}"
          echo "Github username: ${GITHUB_ACTOR}"
          ./gradlew about
          ./gradlew -Pversion=${NEW_VERSION} build --info -x test

      # UPLOAD ASSETS TO RELEASE
      - name: Upload Release Asset Main Jar
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url:  ${{ needs.release.outputs.uploadurl }}
          asset_path: ./build/libs/MiniGame-${{ needs.release.outputs.newtag }}.jar
          asset_name: MiniGame-${{ needs.release.outputs.newtag }}.jar
          asset_content_type: application/jar

      - name: Upload Release Asset Sources Jar
        id: upload-release-asset-sources
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url:  ${{ needs.release.outputs.uploadurl }}
          asset_path: ./build/libs/MiniGame-${{ needs.release.outputs.newtag }}-sources.jar
          asset_name: MiniGame-${{ needs.release.outputs.newtag }}-sources.jar
          asset_content_type: application/jar

      - name: Upload Release Asset Javadoc Jar
        id: upload-release-asset-javadoc
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url:  ${{ needs.release.outputs.uploadurl }}
          asset_path: ./build/libs/MiniGame-${{ needs.release.outputs.newtag }}-javadoc.jar
          asset_name: MiniGame-${{ needs.release.outputs.newtag }}-javadoc.jar
          asset_content_type: application/jar