# -------------------------------------------------------------------------------------
#
# Copyright (c) 2021, WSO2 Inc. (http://www.wso2.com). All Rights Reserved.
#
# This software is the property of WSO2 Inc. and its suppliers, if any.
# Dissemination of any information or reproduction of any material contained
# herein in any form is strictly forbidden, unless permitted by WSO2 expressly.
# You may not alter or remove any copyright or other notice from copies of this content.
#
# --------------------------------------------------------------------------------------

# This workflow will initiate the process for delivering hotfixes.

name: Hotfix Delivery

on:
  # There are not automatic triggers and only accept manual triggers.
  workflow_dispatch:
    inputs:
      # Take the version as an input which contain bugs.
      bug_version:
        # The label that displays for the developer.
        description: 'Enter the version that needs a hotfix (e.g. 0.1.0).'
        # Make it a mandatory input.
        required: true

jobs:
  build:
    # The workflow will run on a virtual environment with Ubuntu latest OS.
    runs-on: ubuntu-latest

    steps:
      # Checkout task to fetch all history of the source repo with fetch-depth: 0.
      - name: Checkout Source Repo
        id: checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      # Task for updating POMs and creating a new branch.
      - name: Pom Update and Branch Creation
        run: |

          # Set github profile configurations.
          echo "Set github profile config."
          git config user.email "iam-cloud@wso2.com"
          git config user.name "wso2-iam-cloud-bot"

          # Read the input value for the version.
          echo "Read the input value for the version."
          VERSION=${{ github.event.inputs.bug_version }}
          # Remove the prefix 'v' from the version input (if it has).
          VERSION_WITHOUT_V=${VERSION#v}
          echo "Version without 'v': ${VERSION_WITHOUT_V}"
          # Append the prefix 'v' to the version.
          VERSION="v"$VERSION_WITHOUT_V
          echo "Required version to be fixed: ${VERSION}"

          # Check whether the provided version is available and stop the workflow if not.
          echo "Check whether the provided version is available."
          IS_TAG_EXIST=$(git tag -l ${VERSION} | wc -l)
          if [ $IS_TAG_EXIST -eq 0 ]; then
            echo "Provided version is not available!"
            exit 1
          fi

          # Check whether the required branch is already available and stop the workflow if it is already available.
          echo "Check whether the required branch is already there."
          IS_BRANCH_EXIST=$(git ls-remote --heads origin hotfix-${VERSION_WITHOUT_V} | wc -l)
          if [ $IS_BRANCH_EXIST -eq 1 ]; then
            echo "Required branch is already exist!"
            exit 1
          fi

          # Create the new branch based from the given version.
          echo "Create the new branch for the fix - hotfix-${VERSION_WITHOUT_V}."
          git checkout -b hotfix-${VERSION_WITHOUT_V} ${VERSION}

          # Update versions in POM files.
          echo "Update versions in pom files."
          mvn versions:set -DnewVersion=${VERSION_WITHOUT_V}-hotfix-1-SNAPSHOT

          # Change the SCM tag to HEAD in root POM.
          echo "Change SCM tag to HEAD in root pom.xml."
          sed -i 's/<tag>'${VERSION}'<\/tag>/<tag>HEAD<\/tag>/g' pom.xml

          # Add only modified files and commit changes.
          echo "Add only modified files."
          git add -u
          echo "Commit changes and push to the new branch."
          git commit -m "Creating a new branch for the Hotfix delivery - ${VERSION_WITHOUT_V}."
          git push origin hotfix-${VERSION_WITHOUT_V}
