# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: Java CI with Maven

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]
  schedule:
    - cron: "0 4 * * 6" # create a release every Saturday at 4:00 UTC
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-24.04
    strategy:
      matrix:
        # test against latest update of each major Java version:
        java: [ 8, 11, 17, 21, 25 ]
    name: Build with Java ${{ matrix.java }}
    if: github.event_name == 'push' || github.event_name == 'pull_request'
    steps:
      - uses: actions/checkout@v4
      - name: Setup java
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: ${{ matrix.java }}
          java-package: jdk+fx
          cache: 'maven'
      - name: Build with Maven
        run: mvn -B package

  new_release_version:
    name: Increment Release Version
    runs-on: ubuntu-latest
    permissions:
      contents: write  # Needed to commit changes
    if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
    timeout-minutes: 30
    steps:
      - id: checkout
        name: Checkout sources
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}  # Checking out GIT repo using PAT allows to re-triggering the workflow
          persist-credentials: false  # Ensures the supplied token is used for subsequent Git operations
      - id: extract_version
        name: Extract Version
        run: |
          #------------------------------------------------------
          # getting current release version no from Maven Central
          #------------------------------------------------------               
          curr_version=$(curl -gL https://oss.sonatype.org/content/repositories/releases/com/actelion/research/openchemlib/maven-metadata.xml -s | grep release | sed -n 's/.*<release>\(.*\)<\/release>.*/\1/p')
          IFS='.- ' read -r -a tokens <<< ${curr_version}
          curr_year=${tokens[0]}
          curr_month=${tokens[1]}
          curr_build=${tokens[2]}
          echo curr_version: ${curr_version}
          #------------------------------------------------------
          # creating a new version no
          #------------------------------------------------------
          new_year=$(date +%Y)
          new_month=$(date +%m)
          new_month=${new_month#0}
          new_build=0
          if [ "$new_year" == "$curr_year" ] && [ "$new_month" == "$curr_month" ]; then
          new_build=$((curr_build+1))
          fi
          new_version=${new_year}.${new_month}.${new_build}
          echo new_version: ${new_version}
          #------------------------------------------------------
          
          echo "new_version=${new_version}" >> $GITHUB_ENV
      - id: set_release_version
        name: Set next release version
        run: |
          mvn versions:set -DnewVersion=${new_version} -DgenerateBackupPoms=false
          git config user.name "GitHub Actions"
          git config user.email "github-actions@users.noreply.github.com"
          git remote set-url origin https://x-access-token:${{ secrets.PERSONAL_ACCESS_TOKEN }}@github.com/Actelion/openchemlib.git
          git commit -am "Incremented version to ${new_version}"
          git push

  deploy:
    name: Deploy with Java 8
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    permissions:
      contents: write  # Needed to commit changes
    timeout-minutes: 30
    steps:
      - id: checkout
        name: Checkout sources
        uses: actions/checkout@v4
      - id: setup_jdk
        name: Set up JDK 8
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '8'
          java-package: jdk+fx
          cache: maven
          server-id: central # Value of the distributionManagement/repository/id field of the pom.xml
          server-username: MAVEN_USERNAME # env variable for username in deploy
          server-password: MAVEN_PASSWORD # env variable for token in deploy
          gpg-private-key: ${{ secrets.GPG_SECRET_KEY }} # Value of the GPG private key to import
          gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase
      - id: extract_version
        name: Extract Version
        run: |
          #------------------------------------------------------
          # getting current version no from pom.xml
          #------------------------------------------------------               
          curr_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
          IFS='.- ' read -r -a tokens <<< ${curr_version}
          curr_year=${tokens[0]}
          curr_month=${tokens[1]}
          curr_build=${tokens[2]}
          echo curr_version: ${curr_version}
          # make version available to the next steps
          echo "curr_version=${curr_version}" >> $GITHUB_ENV
          #------------------------------------------------------
          # creating a new snapshot version no
          #------------------------------------------------------
          new_build=$((curr_build+1))

          new_version=${curr_year}.${curr_month}.${new_build}-SNAPSHOT
          echo new_version: ${new_version}
          # make version available to the next steps
          echo "new_version=${new_version}" >> $GITHUB_ENV
          #------------------------------------------------------
      - id: publish_artifacts
        name: Publish Artifacts
        env:
          MAVEN_USERNAME: ${{ secrets.CENTRAL_USERNAME }}
          MAVEN_PASSWORD: ${{ secrets.CENTRAL_TOKEN }}
          MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_SECRET_KEY_PASSWORD }}
        run: |
          mvn \
            --no-transfer-progress \
            --batch-mode \
            --activate-profiles enable-sign-artifacts \
            deploy
      - id: create_tag
        name: Create Tag
        if: ${{ !contains(env.curr_version,'SNAPSHOT') }}
        run: |
          git config user.name "GitHub Actions"
          git config user.email "github-actions@users.noreply.github.com"
          git tag ${curr_version}
          git push origin ${curr_version}
      - id: create-release
        name: Create Release
        if: ${{ !contains(env.curr_version,'SNAPSHOT') }}
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ env.curr_version }}
          files: |
            target/*.jar
            LICENSE
      - id: set_snapshot_version
        name: Set next snapshot Version
        if: ${{ !contains(env.curr_version,'SNAPSHOT') }}
        run: |
          mvn versions:set -DnewVersion=${new_version} -DgenerateBackupPoms=false
          git config user.name "GitHub Actions"
          git config user.email "github-actions@users.noreply.github.com"
          git commit -am "Incremented version to ${new_version}"
          git push
