# ----------------------------------------------------------------------------
# Example workflow distributed with the jira-sdlc plugin
# (https://github.com/kantorv/jira-sdlc-tools). It ships as an example to
# copy into your own repo's .github/workflows/ and adapt — it is not
# installed with the plugin.
#
#   Guide:         https://kantorv.github.io/jira-sdlc-tools/docs/state-transitions-with-github-actions
#   All examples:  https://kantorv.github.io/jira-sdlc-tools/docs/applications
#   Source:        https://github.com/kantorv/jira-sdlc-tools/blob/main/docs/github-automations/example-workflows/jira_issue_transition_on_branch.yml
# ----------------------------------------------------------------------------

name: Transition Jira Issue on Branch Creation

on:
  create

jobs:
  transition-jira:
    if: false
    #if: github.ref_type == 'branch' && (startsWith(github.ref_name, 'feature/') || startsWith(github.ref_name, 'hotfix/'))
    runs-on: ubuntu-latest

    steps:
      - name: Extract Jira issue key
        id: jira
        shell: bash
        run: |
          BRANCH="${{ github.ref_name }}"

          if [[ "$BRANCH" =~ ^(feature|hotfix)/([A-Z]+-[0-9]+)- ]]; then
            echo "key=${BASH_REMATCH[2]}" >> "$GITHUB_OUTPUT"
          else
            echo "No Jira issue found in branch '$BRANCH'. Skipping."
            exit 0
          fi

      - name: Transition to In Progress
        env:
          JIRA_URL: ${{ secrets.JIRA_ACCOUNT_URL }}
          JIRA_EMAIL: ${{ secrets.JIRA_ACCOUNT_EMAIL }}
          JIRA_TOKEN: ${{ secrets.JIRA_ISSUE_TRANSITION_TOKEN }}
          KEY: ${{ steps.jira.outputs.key }}
        shell: bash
        run: |
          set -euo pipefail
          # Mirrors STATUS_TODO / STATUS_IN_PROGRESS from .jst/jira-sdlc-tools.env.
          SOURCE="To Do"
          TARGET="In Progress"

          # The secret may carry a scheme; the REST host needs a bare host.
          SITE="${JIRA_URL#*://}"

          # A scoped API token is rejected by Basic auth on the site domain
          # (*.atlassian.net) — it only works through the api.atlassian.com
          # gateway, which is addressed by cloud id. Resolve the cloud id from
          # the site's public tenant_info endpoint so no extra secret is needed.
          CLOUD_ID=$(curl -fsSL "https://$SITE/_edge/tenant_info" | jq -r '.cloudId')
          if [ -z "$CLOUD_ID" ] || [ "$CLOUD_ID" = "null" ]; then
            echo "::error::Could not resolve cloudId from https://$SITE/_edge/tenant_info"
            exit 1
          fi
          BASE="https://api.atlassian.com/ex/jira/$CLOUD_ID/rest/api/3"
          api() { curl -sSfL -u "$JIRA_EMAIL:$JIRA_TOKEN" -H "Accept: application/json" "$@"; }

          CUR=$(api "$BASE/issue/$KEY?fields=status" | jq -r '.fields.status.name')
          echo "Issue $KEY is currently '$CUR'."
          # Only advance To Do → In Progress; never regress from a later status.
          if [ "$CUR" != "$SOURCE" ]; then
            echo "Not in '$SOURCE' (currently '$CUR') — skipping to avoid regressing."
            exit 0
          fi

          # The REST API only accepts a transition id, not a status name, so
          # resolve it explicitly here.
          TID=$(api "$BASE/issue/$KEY/transitions" \
            | jq -r --arg t "$TARGET" 'first(.transitions[] | select(.to.name == $t) | .id) // empty')
          if [ -z "$TID" ]; then
            echo "::error::No available transition to '$TARGET' from '$CUR' for $KEY."
            exit 1
          fi

          api -X POST -H "Content-Type: application/json" \
            -d "{\"transition\":{\"id\":\"$TID\"}}" "$BASE/issue/$KEY/transitions"
          echo "Transitioned $KEY from $SOURCE to $TARGET."
