name: Build
on:
  # Trigger the workflow on pushes to only the 'main' branch (this avoids duplicate checks being run e.g. for dependabot pull requests)
  push:
    branches: [ main ]
  # Trigger the workflow on any pull request
  pull_request:

jobs:
  # Run Gradle Wrapper Validation Action to verify the wrapper's checksum
  gradleValidation:
    name: Gradle Wrapper
    runs-on: ubuntu-latest
    steps:

      # Check out current repository
      - name: Fetch Sources
        uses: actions/checkout@v2.3.4

      # Validate wrapper
      - name: Gradle Wrapper Validation
        uses: gradle/wrapper-validation-action@v1.0.4
  build:
    name: Build
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.properties.outputs.version }}
      changelog: ${{ steps.properties.outputs.changelog }}
    steps:

      # Check out current repository
      - name: Fetch Sources
        uses: actions/checkout@v2

      - name: Download JBR
        uses: carlosperate/download-file-action@v2
        with:
          file-url: 'https://cache-redirector.jetbrains.com/intellij-jbr/jbr-17.0.5-linux-x64-b653.14.tar.gz'
          file-name: 'jbr-17.0.5.tar.gz'

      - name: Setup Java
        uses: actions/setup-java@v3
        with:
          java-version: 17
          distribution: jdkfile
          jdkFile: jbr-17.0.5.tar.gz
          cache: gradle

      # Set environment variables
      - name: Export Properties
        id: properties
        shell: bash
        run: |
          PROPERTIES="$(./gradlew properties -q)"
          VERSION="$(echo "$PROPERTIES" | grep "^version:" | cut -f2- -d ' ')"
          NAME="$(echo "$PROPERTIES" | grep "^name:" | cut -f2- -d ' ')"
          CHANGELOG="$(./gradlew getChangelog --unreleased --no-header --console=plain -q)"
          CHANGELOG="${CHANGELOG//'%'/'%25'}"
          CHANGELOG="${CHANGELOG//$'\n'/'%0A'}"
          CHANGELOG="${CHANGELOG//$'\r'/'%0D'}"
          echo "version=$VERSION"
          echo "name=$NAME"
          echo "changelog=$CHANGELOG"
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "name=$NAME" >> $GITHUB_OUTPUT
          echo "changelog=$CHANGELOG" >> $GITHUB_OUTPUT
      # Build artifact using buildPlugin Gradle task
      - name: Build
        run: ./gradlew build

      # Store built plugin as an artifact for downloading
      - name: Upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: "${{ steps.properties.outputs.name }} - ${{ steps.properties.outputs.version }}"
          path: ./**/build/libs/*

  # Prepare a draft release for GitHub Releases page for the manual verification
  # If accepted and published, release workflow would be triggered
  releaseDraft:
    name: Release Draft
    if: github.event_name != 'pull_request'
    needs: build
    runs-on: ubuntu-latest
    steps:

      # Check out current repository
      - name: Fetch Sources
        uses: actions/checkout@v2.3.4

      # Remove old release drafts by using the curl request for the available releases with draft flag
      - name: Remove Old Release Drafts
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh api repos/{owner}/{repo}/releases \
            --jq '.[] | select(.draft == true) | .id' \
            | xargs -I '{}' gh api -X DELETE repos/{owner}/{repo}/releases/{}
      # Create new release draft - which is not publicly visible and requires manual acceptance
      - name: Create Release Draft
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release create v${{ needs.build.outputs.version }} \
            --draft \
            --title "v${{ needs.build.outputs.version }}" \
            --notes "${{ needs.build.outputs.changelog }}"