#
# Copyright (c) 2019-2021 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
#   Red Hat, Inc. - initial API and implementation

name: Release Che Machine Exec

on:
  workflow_dispatch:
    # Inputs the workflow accepts.
    inputs:
      version:
        description: 'The version that is going to be released. Should be in format 7.y.z'
        required: true
      noCommit:
        description: 'If true, will not commit the version bump changes'
        default: ''
      forceRecreateTags:
        description: If true, tags will be recreated. Use with caution
        required: false
        default: 'false'
jobs:
  build:
    name: Create Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Check existing tags
        run: |
          set +e
          RECREATE_TAGS=${{ github.event.inputs.forceRecreateTags }}
          VERSION=${{ github.event.inputs.version }}
          EXISTING_TAG=$(git ls-remote --exit-code origin refs/tags/${VERSION})
          if [[ -n ${EXISTING_TAG} ]]; then
            if [[ ${RECREATE_TAGS} == "true" ]]; then
              echo "[INFO] Removing tag for ${VERSION} version. New tag will be recreated during release."
              git push origin :$VERSION
            else
              echo "[ERROR] Cannot proceed with release - tag ${EXISTING_TAG} already exists."
              exit 1
            fi
          else
            echo "[INFO] No existing tags detected for $VERSION"
          fi
      - name: Login to docker.io
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}
          registry: docker.io
      - name: Login to quay.io
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.QUAY_USERNAME }}
          password: ${{ secrets.QUAY_PASSWORD }}
          registry: quay.io
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1    
      - name: Create Release
        run: |
          git config --global user.name "Mykhailo Kuznietsov"
          git config --global user.email "mkuznets@redhat.com"
          export GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
          NO_COMMIT=${{ github.event.inputs.noCommit}}
          if [[ $NO_COMMIT == "true" ]]; then
            NO_COMMIT="--no-commit"
          else
            NO_COMMIT=
          fi
          /bin/bash make-release.sh --version ${{ github.event.inputs.version }} --trigger-release $NO_COMMIT
      - name: Create failure MM message
        if: ${{ failure() }}
        run: |
          echo "{\"text\":\":no_entry_sign: Che Machine Exec ${{ github.event.inputs.version }} release has failed: https://github.com/eclipse-che/che-machine-exec/actions/workflows/release.yml\"}" > mattermost.json
      - name: Create success MM message
        run: |
          echo "{\"text\":\":white_check_mark: Che Machine Exec ${{ github.event.inputs.version }} has been released: https://quay.io/eclipse/che-machine-exec:${{ github.event.inputs.version }}\"}" > mattermost.json
      - name: Send MM message
        if: ${{ success() }} || ${{ failure() }}
        uses: mattermost/action-mattermost-notify@1.0.2
        env:
          MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_WEBHOOK_URL }}
          MATTERMOST_CHANNEL: eclipse-che-releases
          MATTERMOST_USERNAME: che-bot

