on:
  issues:
    types: [labeled]

permissions:
  contents: read

name: JIRA Sync

jobs:
  sync:
    name: Sync to JIRA
    permissions:
      issues: write # for actions/github-script to create comments
    runs-on: ubuntu-latest
    steps:    
      - name: Login
        uses: atlassian/gajira-login@ca13f8850ea309cf44a6e4e0c49d9aa48ac3ca4c # v3
        env:
          JIRA_BASE_URL: ${{ secrets.JIRA_BASE_URL }}
          JIRA_USER_EMAIL: ${{ secrets.JIRA_USER_EMAIL }}
          JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }}

      - name: Search
        if: github.event.action == 'labeled'
        id: search
        uses: tomhjp/gh-action-jira-search@04700b457f317c3e341ce90da5a3ff4ce058f2fa # v0.2.2
        with:
          # cf[10089] is Issue Link (use JIRA API to retrieve)
          jql: 'project = "HPR" AND cf[10089] = "${{ github.event.issue.html_url }}"'


      - name: Set type
        id: set-ticket-type
        run: |
          # Questions are not tracked in JIRA at this time.
          if [[ "${{ contains(github.event.issue.labels.*.name, 'question') }}" == "true" ]]; then
            echo "type=Invalid" >> $GITHUB_OUTPUT
          else
            # Properly labeled GH issues are assigned the standard "GH Issue" type upon creation.
            echo "type=GH Issue" >> $GITHUB_OUTPUT
          fi

      - name: Set labels
        id: set-ticket-labels 
        run: |
          if [[ "${{ contains(github.event.issue.labels.*.name, 'bug') }}" == "true" ]]; then
            echo "labels=[\"bug\"]" >> $GITHUB_OUTPUT
          elif [[ "${{ contains(github.event.issue.labels.*.name, 'enhancement') }}" == "true" ]]; then
            echo "labels=[\"enhancement\"]" >> $GITHUB_OUTPUT
          else
            echo "labels=[]" >> $GITHUB_OUTPUT
          fi

      - name: Validate ticket
        if: steps.set-ticket-type.outputs.type == 'Invalid'
        run: |
          echo "Questions are not being synced to JIRA at this time."
          echo "If the issue is a bug or an enhancement please remove the question label and reapply the 'sync to jira' label."

      - name: Create ticket
        id: create-ticket
        if: steps.search.outputs.issue == '' && github.event.label.name == 'sync to jira' && steps.set-ticket-type.outputs.type != 'Invalid'
        uses: atlassian/gajira-create@1ff0b6bd115a780592b47bfbb63fc4629132e6ec # v3
        with:
          project: HPR
          issuetype: "${{ steps.set-ticket-type.outputs.type }}"
          summary: "${{ github.event.repository.name }}: ${{ github.event.issue.title }}"
          description: "${{ github.event.issue.body }}\n\n_Created from GitHub by ${{ github.actor }}._"
          # The field customfield_10089 refers to the Issue Link field in JIRA. 
          fields: '{ "customfield_10089": "${{ github.event.issue.html_url }}", "components": [{ "name": "core" }], "labels": ${{ steps.set-ticket-labels.outputs.labels }} }'

      - name: Add tracking comment
        if: steps.create-ticket.outputs.issue != '' && steps.set-ticket-type.outputs.type != 'Invalid'
        uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `
                This issue has been synced to JIRA for planning.
                JIRA ID: [${{ steps.create-ticket.outputs.issue }}](https://hashicorp.atlassian.net/browse/${{steps.create-ticket.outputs.issue}})`
            })

