Standard Github actions for pull request

name: "qovery - clone"

permissions:
  actions: write
  checks: write
  contents: write
  deployments: write
  id-token: write
  issues: write
  discussions: write
  packages: write
  pages: write
  pull-requests: write
  repository-projects: write
  security-events: write
  statuses: write

on:
  pull_request:
    branches: [ master ]
  workflow_dispatch:
jobs:
  qovery-clone:
    # check Pull request has a ridp label (If not dont create a environment)
    if: >-
        contains(github.event.pull_request.labels.*.name, 'idp')
    runs-on: ubuntu-latest
    steps:
      - name: check out
        uses: actions/checkout@v3

      - name: Extract branch name
        shell: bash
        run: |
          branch=$(echo "${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" | tr '[:upper:]' '[:lower:]')
          echo "branch=$branch" >> "$GITHUB_OUTPUT"
        id: extract_branch

      - name: Qovery Install
        run: |
          curl -s https://get.qovery.com | bash
        env:
          QOVERY_CLI_ACCESS_TOKEN: ${{ secrets.QOVERY }}

      - name: Qovery Clone
        run: |
          qovery environment clone \
          --organization "x" \
          --project "x" \
          --environment "entity-store" \
          --new-environment-name ${{ steps.extract_branch.outputs.branch }}
        env:
          QOVERY_CLI_ACCESS_TOKEN: ${{ secrets.QOVERY }}

      # No matter what, we want to make sure the branchs are updated!
      - name: Qovery Branch Update Terraform
        run: |
          qovery lifecycle update \
          --organization "x" \
          --project "x" \
          --environment ${{ steps.extract_branch.outputs.branch }} \
          --lifecycle "Entity Store - Terraform" \
          --branch $GITHUB_HEAD_REF \
        if: ${{ !cancelled() }}
        env:
          QOVERY_CLI_ACCESS_TOKEN: ${{ secrets.QOVERY }}

      - name: Qovery Branch Update Helm
        run: |
          qovery helm update \
          --organization "x" \
          --project "x" \
          --environment ${{ steps.extract_branch.outputs.branch }} \
          --helm "Entity Store - Helm" \
          --chart_git_commit_branch $GITHUB_HEAD_REF
        if: ${{ !cancelled() }}
        env:
          QOVERY_CLI_ACCESS_TOKEN: ${{ secrets.QOVERY }}

      - name: Qovery Deploy
        run: |
          qovery environment deploy \
          --organization "x" \
          --project "x" \
          --environment ${{ steps.extract_branch.outputs.branch }} \
          --watch
        if: ${{ !cancelled() }}
        env:
          QOVERY_CLI_ACCESS_TOKEN: ${{ secrets.QOVERY }}

      - uses: actions/github-script@v6
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: 'Qovery environment deployed go to ingress: ${{ steps.extract_branch.outputs.branch }}-entity-store-api.opencorporates.dev'
            })