|
1 | 1 | --- |
2 | 2 | name: Create JIRA ticket for new issues |
3 | 3 |
|
| 4 | + # Creates and updates jira tickets that sync with GitHub Issues events. |
4 | 5 | on: |
5 | 6 | issues: |
6 | | - types: [opened] |
7 | | - |
8 | | - permissions: |
9 | | - issues: write |
10 | | - contents: read |
| 7 | + types: [opened, reopened, closed] |
11 | 8 | jobs: |
12 | 9 | jira_task: |
13 | 10 | name: Create Jira issue |
| 11 | + if: github.event.action == 'opened' |
14 | 12 | runs-on: ubuntu-latest |
| 13 | + env: |
| 14 | + GITHUB_TOKEN: ${{ secrets.APIX_BOT_PAT }} |
15 | 15 | steps: |
16 | 16 | - name: Create JIRA ticket |
17 | 17 | id: create |
18 | 18 | shell: bash |
19 | 19 | env: |
20 | 20 | ISSUE_NUMBER: ${{ github.event.issue.number }} |
21 | | - ISSUE_BODY: ${{ github.event.issue.body }} |
22 | 21 | ISSUE_URL: ${{ github.event.issue.html_url }} |
23 | 22 | JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }} |
24 | 23 | JIRA_ASSIGNEE: ${{ secrets.ASSIGNEE_JIRA_TICKET }} |
25 | 24 | run: | |
26 | | -
|
27 | 25 | json_response=$(curl --request POST \ |
28 | 26 | --url 'https://jira.mongodb.org/rest/api/2/issue' \ |
29 | 27 | --header 'Authorization: Bearer '"${JIRA_API_TOKEN}" \ |
|
39 | 37 | "id": "12" |
40 | 38 | }, |
41 | 39 | "customfield_12751": [{ |
42 | | - "id": "22222" |
43 | | - }], |
| 40 | + "id": "25550" |
| 41 | + }], |
44 | 42 | "description": "This ticket tracks the following GitHub issue: '"${ISSUE_URL}"'.", |
45 | 43 | "components": [ |
46 | 44 | { |
47 | 45 | "id": "32194" |
48 | 46 | } |
49 | | - ], |
50 | | - "assignee": { |
51 | | - "name": "'"${JIRA_ASSIGNEE}"'" |
52 | | - } |
| 47 | + ] |
53 | 48 | } |
54 | 49 | }') |
55 | 50 | |
|
65 | 60 | issue-number: ${{ github.event.issue.number }} |
66 | 61 | body: | |
67 | 62 | Thanks for opening this issue. The ticket [${{ steps.create.outputs.jira-ticket-id }}](https://jira.mongodb.org/browse/${{ steps.create.outputs.jira-ticket-id }}) was created for internal tracking. |
| 63 | + reopen_jira_ticket: |
| 64 | + name: Reopen JIRA ticket |
| 65 | + if: github.event.action == 'reopened' |
| 66 | + runs-on: ubuntu-latest |
| 67 | + env: |
| 68 | + GITHUB_TOKEN: ${{ secrets.APIX_BOT_PAT }} |
| 69 | + steps: |
| 70 | + - name: Reopened JIRA ticket if exists |
| 71 | + run: | |
| 72 | + ISSUE_NUMBER=${{ github.event.issue.number }} |
| 73 | + JIRA_API_TOKEN=${{ secrets.JIRA_API_TOKEN }} |
| 74 | +
|
| 75 | + JIRA_QUERY="project = CLOUDP AND issuetype = Story AND resolution = Declined AND text ~ \"HELP: GitHub Issue n. $ISSUE_NUMBER\"" |
| 76 | + |
| 77 | + # URL encode the query |
| 78 | + JIRA_URL=$(echo "$JIRA_QUERY" | jq -s -R -r @uri) |
| 79 | +
|
| 80 | + json_response=$(curl -s --request GET \ |
| 81 | + --url "https://jira.mongodb.org/rest/api/2/search?fields=id&jql=${JIRA_URL}" \ |
| 82 | + --header 'Authorization: Bearer '"${JIRA_API_TOKEN}" \ |
| 83 | + --header 'Accept: application/json') |
| 84 | +
|
| 85 | + JIRA_TICKET_ID=$(echo "${json_response}" | jq -r '.issues[0].id') |
| 86 | + if [ -z "$JIRA_TICKET_ID" ]; then |
| 87 | + echo "JIRA_TICKET_ID is not defined. Exiting the script." |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | +
|
| 91 | + JIRA_REOPENED_URL="https://jira.mongodb.org/rest/api/2/issue/${JIRA_TICKET_ID}/transitions" |
| 92 | +
|
| 93 | + json_response=$(curl -s --request POST \ |
| 94 | + --url "${JIRA_REOPENED_URL}" \ |
| 95 | + --header 'Authorization: Bearer '"${JIRA_API_TOKEN}" \ |
| 96 | + --header 'Accept: application/json' \ |
| 97 | + --header 'Content-Type: application/json' \ |
| 98 | + --data '{ |
| 99 | + "transition": { |
| 100 | + "id": 3 |
| 101 | + }, |
| 102 | + "update": { |
| 103 | + "comment": [ |
| 104 | + { |
| 105 | + "add": { |
| 106 | + "body": "The related GitHub issue was reopened. Updated via automated process." |
| 107 | + } |
| 108 | + } |
| 109 | + ] |
| 110 | + } |
| 111 | + }') |
| 112 | + echo "Response: ${json_response}" |
| 113 | + close_jira_ticket: |
| 114 | + name: Close JIRA ticket |
| 115 | + if: github.event.action == 'closed' |
| 116 | + runs-on: ubuntu-latest |
| 117 | + env: |
| 118 | + GITHUB_TOKEN: ${{ secrets.APIX_BOT_PAT }} |
| 119 | + steps: |
| 120 | + - name: Close JIRA ticket if exists |
| 121 | + run: | |
| 122 | + ISSUE_NUMBER=${{ github.event.issue.number }} |
| 123 | + JIRA_API_TOKEN=${{ secrets.JIRA_API_TOKEN }} |
| 124 | +
|
| 125 | + JIRA_QUERY="project = CLOUDP AND issuetype = Story AND resolution = Unresolved AND text ~ \"HELP: GitHub Issue n. $ISSUE_NUMBER\"" |
| 126 | + |
| 127 | + # URL encode the query |
| 128 | + JIRA_URL=$(echo "$JIRA_QUERY" | jq -s -R -r @uri) |
| 129 | +
|
| 130 | + json_response=$(curl -s --request GET \ |
| 131 | + --url "https://jira.mongodb.org/rest/api/2/search?fields=id&jql=${JIRA_URL}" \ |
| 132 | + --header 'Authorization: Bearer '"${JIRA_API_TOKEN}" \ |
| 133 | + --header 'Accept: application/json') |
| 134 | +
|
| 135 | + JIRA_TICKET_ID=$(echo "${json_response}" | jq -r '.issues[0].id') |
| 136 | + if [ -z "$JIRA_TICKET_ID" ]; then |
| 137 | + echo "JIRA_TICKET_ID is not defined. Exiting the script." |
| 138 | + exit 1 |
| 139 | + fi |
| 140 | +
|
| 141 | + JIRA_CLOSE_URL="https://jira.mongodb.org/rest/api/2/issue/${JIRA_TICKET_ID}/transitions" |
| 142 | +
|
| 143 | + json_response=$(curl -s --request POST \ |
| 144 | + --url "${JIRA_CLOSE_URL}" \ |
| 145 | + --header 'Authorization: Bearer '"${JIRA_API_TOKEN}" \ |
| 146 | + --header 'Accept: application/json' \ |
| 147 | + --header 'Content-Type: application/json' \ |
| 148 | + --data '{ |
| 149 | + "transition": { |
| 150 | + "id": 1371 |
| 151 | + }, |
| 152 | + "update": { |
| 153 | + "comment": [ |
| 154 | + { |
| 155 | + "add": { |
| 156 | + "body": "The related GitHub issue was closed. Resolved via automated process." |
| 157 | + } |
| 158 | + } |
| 159 | + ] |
| 160 | + }, |
| 161 | + "fields": { |
| 162 | + "resolution": { |
| 163 | + "name": "Declined" |
| 164 | + } |
| 165 | + } |
| 166 | + }') |
| 167 | + echo "Response: ${json_response}" |
| 168 | +
|
0 commit comments