name: Continuous Integration

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    env: 
      CI_COMMIT_MESSAGE: Continuous Integration Build Artifacts
      CI_COMMIT_AUTHOR: ${{ github.event.repository.name }} Continuous Integration
    steps:
    - uses: actions/checkout@v6
      with:
        ref: ${{ github.event.pull_request.head.ref }}
        token: ${{ secrets.WORKFLOW_GIT_ACCESS_TOKEN }}
        fetch-depth: 0 # checkout full GIT history for auto-changelog

    - name: Get GIT tags for auto-changelog generation (startingVersion config option)
      run: git fetch --tags origin

    - name: Set environment variable "commit-message"
      run: echo "commit-message=$(git log -1 --pretty=format:'%s')" >> $GITHUB_ENV
    - name: Display environment variable "commit-message"
      run: echo "commit-message=${{ env.commit-message }}"

    - name: Set environment variable "commit-author"
      run: echo "commit-author=$(git log -1 --pretty=format:'%an')" >> $GITHUB_ENV
    - name: Display environment variable "commit-author"
      run: echo "commit-author=${{ env.commit-author }}"

    - name: Set environment variable "is-auto-commit"
      if: env.commit-message == env.CI_COMMIT_MESSAGE && env.commit-author == env.CI_COMMIT_AUTHOR
      run: echo "is-auto-commit=true" >> $GITHUB_ENV
    - name: Display environment variable "is-auto-commit"
      run: echo "is-auto-commit=${{ env.is-auto-commit }}"
    - name: Info message on CI auto commit 
      if: env.is-auto-commit
      run: echo "Continuous Integration Auto Commit - The following steps will be skipped"

    - uses: actions/setup-node@v6
      if: env.is-auto-commit == false
      with:
        node-version-file: '.nvmrc'
    - name: (Main) Install nodes packages
      if: env.is-auto-commit == false
      run: npm ci
    - name: (Main) Build package (lint, test, build, package, merge)
      if: env.is-auto-commit == false
      run: npm run package

    - name: (Main) Archive test code coverage results
      if: env.is-auto-commit == false
      uses: actions/upload-artifact@v7
      with:
        name: test-code-coverage-report
        path: coverage
        retention-days: 31
        if-no-files-found: error
    - name: (Main) Archive documentation
      if: env.is-auto-commit == false
      uses: actions/upload-artifact@v7
      with:
        name: documentation
        path: docs
        retention-days: 31
        if-no-files-found: error
    - name: (Main) Archive CHANGELOG.md
      if: env.is-auto-commit == false
      uses: actions/upload-artifact@v7
      with:
        name: changelog
        path: CHANGELOG.md
        retention-days: 31
        if-no-files-found: error
    - name: (Main) Archive build artifacts distribution-production
      if: env.is-auto-commit == false
      uses: actions/upload-artifact@v7
      with:
        name: distribution-production
        path: dist
        retention-days: 31
        if-no-files-found: error
    - name: (Main) Archive build artifacts distribution-development
      if: env.is-auto-commit == false
      uses: actions/upload-artifact@v7
      with:
        name: distribution-development
        path: devdist
        retention-days: 31
        if-no-files-found: error

    - name: (Example) Install nodes packages
      if: env.is-auto-commit == false
      working-directory: example
      run: npm ci
    - name: (Example) Build package (lint, test, build, package, merge)
      if: env.is-auto-commit == false
      working-directory: example
      run: npm run package

    - name: (Example) Archive test code coverage results
      if: env.is-auto-commit == false
      uses: actions/upload-artifact@v7
      with:
        name: example-test-code-coverage-report
        path: example/coverage
        retention-days: 31

    - name: (Example Servlet) Set up JDK 25
      if: env.is-auto-commit == false
      uses: actions/setup-java@v5
      with:
        distribution: 'adopt'
        java-version: 25
        cache: 'maven'
    - name: (Example Servlet) Build with Maven
      if: env.is-auto-commit == false
      working-directory: example/search-example-servlet
      run: mvn -B package --file pom.xml

    - name: Display event name 
      run: echo "github.event_name=${{ github.event_name }}"
    - name: Commit build artifacts (dist, devdist, docs, coverage)
      # Don't run again on already pushed auto commit. Don't run on pull request events.
      # Git pull before add/commit/push to reduce race conditions on parallel builds
      if: env.is-auto-commit == false && github.event_name != 'pull_request'
      run: |
        git config --global user.name '${{ env.CI_COMMIT_AUTHOR }}'
        git config --global user.email "7671054+JohT@users.noreply.github.com"
        git fetch origin
        git status
        git commit -a -m "${{ env.CI_COMMIT_MESSAGE }}"
        git status
        git rebase --strategy-option=theirs origin/main --verbose
        git status
        git push --verbose