diff --git a/.github/workflows/close_jira_ticket.yaml b/.github/workflows/close_jira_ticket.yaml new file mode 100644 index 0000000..de84064 --- /dev/null +++ b/.github/workflows/close_jira_ticket.yaml @@ -0,0 +1,72 @@ +name: Close Jira Ticket on Issue Close + +on: + issues: + types: [closed] + +jobs: + close-jira-ticket: + runs-on: ubuntu-latest + if: contains(github.event.issue.labels.*.name, 'jira-story') + steps: + - name: Extract Jira Story Key from Issue Comment + id: extract_story_key + uses: actions/github-script@v6 + with: + script: | + // Fetch all comments on the closed issue + const { data: comments } = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number + }); + + // Find the comment containing the Jira link + const jiraComment = comments.find(comment => comment.body.includes('This issue has been linked to Jira story')); + if (!jiraComment) { + core.setFailed("No Jira comment found with story key."); + return; + } + + // Extract the Jira story key from the comment + const match = jiraComment.body.match(/\[(.*?)\]\(https:\/\/cworthy\.atlassian\.net\/browse\/.*?\)/); + if (!match || match.length < 2) { + core.setFailed("Jira story key not found in the comment."); + return; + } + const storyKey = match[1]; + + // Print the extracted story key + console.log("Extracted STORY_KEY:", storyKey); + + core.setOutput("story_key", storyKey); + + - name: Verify Issue Exists and Accessible in Jira + env: + JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN_DAFYDD }} + JIRA_EMAIL: ${{ secrets.JIRA_EMAIL_DAFYDD }} + STORY_KEY: ${{ steps.extract_story_key.outputs.story_key }} + run: | + story_key="${{ steps.extract_story_key.outputs.story_key }}" + jira_domain="https://cworthy.atlassian.net" + + # Attempt to retrieve issue details and print the response directly + curl -s -X GET \ + -H "Authorization: Basic $(echo -n $JIRA_EMAIL:$JIRA_API_TOKEN | base64)" \ + -H "Content-Type: application/json" \ + "$jira_domain/rest/api/3/issue/$story_key" + + - name: Close Jira Ticket + env: + JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN_DAFYDD }} + JIRA_EMAIL: ${{ secrets.JIRA_EMAIL_DAFYDD }} + STORY_KEY: ${{ steps.extract_story_key.outputs.story_key }} + run: | + story_key="${{ steps.extract_story_key.outputs.story_key }}" + jira_domain="https://cworthy.atlassian.net" + + curl -X POST --http1.1 -v\ + -H "Authorization: Basic $(echo -n $JIRA_EMAIL:$JIRA_API_TOKEN | base64)" \ + -H "Content-Type: application/json" \ + --data '{"transition": {"id": "31"}}' \ + "$jira_domain/rest/api/3/issue/$story_key/transitions" diff --git a/.github/workflows/jira.yaml b/.github/workflows/open_jira_ticket.yaml similarity index 100% rename from .github/workflows/jira.yaml rename to .github/workflows/open_jira_ticket.yaml