|
1 | 1 | name: Jira creation |
2 | 2 |
|
3 | 3 | on: |
4 | | - issues: |
5 | | - types: [opened] |
6 | | - issue_comment: |
7 | | - types: [created] |
| 4 | + issues: |
| 5 | + types: [opened] |
| 6 | + issue_comment: |
| 7 | + types: [created] |
8 | 8 |
|
9 | 9 | jobs: |
10 | | - create-issue: |
11 | | - runs-on: ubuntu-latest |
12 | | - permissions: |
13 | | - issues: write |
14 | | - if: ((github.event_name == 'issue_comment' && github.event.comment.body == 'recreate jira' && github.event.comment.user.login == 'sfc-gh-mkeller') || (github.event_name == 'issues' && github.event.pull_request.user.login != 'whitesource-for-github-com[bot]')) |
15 | | - steps: |
16 | | - - name: Checkout |
17 | | - uses: actions/checkout@v4 |
18 | | - with: |
19 | | - repository: snowflakedb/gh-actions |
20 | | - ref: jira_v1 |
21 | | - token: ${{ secrets.SNOWFLAKE_GITHUB_TOKEN }} # stored in GitHub secrets |
22 | | - path: . |
23 | | - |
24 | | - - name: Login |
25 | | - uses: atlassian/gajira-login@v3 |
26 | | - env: |
27 | | - JIRA_BASE_URL: ${{ secrets.JIRA_BASE_URL }} |
28 | | - JIRA_USER_EMAIL: ${{ secrets.JIRA_USER_EMAIL }} |
29 | | - JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }} |
30 | | - |
31 | | - - name: Create JIRA Ticket |
32 | | - id: create |
33 | | - uses: atlassian/gajira-create@v3 |
34 | | - with: |
35 | | - project: SNOW |
36 | | - issuetype: Bug |
37 | | - summary: '${{ github.event.issue.title }}' |
38 | | - description: | |
39 | | - ${{ github.event.issue.body }} \\ \\ _Created from GitHub Action_ for ${{ github.event.issue.html_url }} |
40 | | - fields: '{ "customfield_11401": {"id": "14723"}, "assignee": {"id": "712020:e527ae71-55cc-4e02-9217-1ca4ca8028a2"}, "components":[{"id":"19287"}], "labels": ["oss"], "priority": {"id": "10001"} }' |
41 | | - |
42 | | - - name: Update GitHub Issue |
43 | | - uses: ./jira/gajira-issue-update |
44 | | - env: |
45 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
46 | | - with: |
47 | | - issue_number: "{{ event.issue.id }}" |
48 | | - owner: "{{ event.repository.owner.login }}" |
49 | | - name: "{{ event.repository.name }}" |
50 | | - jira: "${{ steps.create.outputs.issue }}" |
| 10 | + create-issue: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + permissions: |
| 13 | + issues: write |
| 14 | + if: ((github.event_name == 'issue_comment' && github.event.comment.body == 'recreate jira' && github.event.comment.user.login == 'sfc-gh-mkeller') || (github.event_name == 'issues' && github.event.pull_request.user.login != 'whitesource-for-github-com[bot]')) |
| 15 | + steps: |
| 16 | + - name: Create JIRA Ticket |
| 17 | + id: create |
| 18 | + env: |
| 19 | + JIRA_BASE_URL: ${{ secrets.JIRA_BASE_URL }} |
| 20 | + JIRA_USER_EMAIL: ${{ secrets.JIRA_USER_EMAIL }} |
| 21 | + JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }} |
| 22 | + ISSUE_TITLE: ${{ github.event.issue.title }} |
| 23 | + ISSUE_BODY: ${{ github.event.issue.body }} |
| 24 | + ISSUE_URL: ${{ github.event.issue.html_url }} |
| 25 | + run: | |
| 26 | + # debug |
| 27 | + #set -x |
| 28 | + TMP_BODY=$(mktemp) |
| 29 | + trap "rm -f $TMP_BODY" EXIT |
| 30 | +
|
| 31 | + # Escape special characters in title and body |
| 32 | + TITLE=$(echo "${ISSUE_TITLE//`/\\`}" | sed 's/"/\\"/g' | sed "s/'/\\\'/g") |
| 33 | + echo "${ISSUE_BODY//`/\\`}" | sed 's/"/\\"/g' | sed "s/'/\\\'/g" > $TMP_BODY |
| 34 | + echo -e "\n\n_Created from GitHub Action_ for $ISSUE_URL" >> $TMP_BODY |
| 35 | + BODY=$(cat "$TMP_BODY") |
| 36 | +
|
| 37 | + PAYLOAD=$(jq -n \ |
| 38 | + --arg issuetitle "$TITLE" \ |
| 39 | + --arg issuebody "$BODY" \ |
| 40 | + '{ |
| 41 | + fields: { |
| 42 | + project: { key: "SNOW" }, |
| 43 | + issuetype: { name: "Bug" }, |
| 44 | + summary: $issuetitle, |
| 45 | + description: $issuebody, |
| 46 | + customfield_11401: { id: "14723" }, |
| 47 | + assignee: { id: "712020:e527ae71-55cc-4e02-9217-1ca4ca8028a2" }, |
| 48 | + components: [{ id: "19287" }], |
| 49 | + labels: ["oss"], |
| 50 | + priority: { id: "10001" } |
| 51 | + } |
| 52 | + }') |
| 53 | +
|
| 54 | + # Create JIRA issue using REST API |
| 55 | + RESPONSE=$(curl -s -X POST \ |
| 56 | + -H "Content-Type: application/json" \ |
| 57 | + -H "Accept: application/json" \ |
| 58 | + -u "$JIRA_USER_EMAIL:$JIRA_API_TOKEN" \ |
| 59 | + "$JIRA_BASE_URL/rest/api/2/issue" \ |
| 60 | + -d "$PAYLOAD") |
| 61 | +
|
| 62 | + # Extract JIRA issue key from response |
| 63 | + JIRA_KEY=$(echo "$RESPONSE" | jq -r '.key') |
| 64 | +
|
| 65 | + if [ "$JIRA_KEY" = "null" ] || [ -z "$JIRA_KEY" ]; then |
| 66 | + echo "Failed to create JIRA issue" |
| 67 | + echo "Response: $RESPONSE" |
| 68 | + echo "Request payload: $PAYLOAD" |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | +
|
| 72 | + echo "Created JIRA issue: $JIRA_KEY" |
| 73 | + echo "jira_key=$JIRA_KEY" >> $GITHUB_OUTPUT |
| 74 | +
|
| 75 | + - name: Update GitHub Issue |
| 76 | + env: |
| 77 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + REPOSITORY: ${{ github.repository }} |
| 79 | + ISSUE_NUMBER: ${{ github.event.issue.number }} |
| 80 | + JIRA_KEY: ${{ steps.create.outputs.jira_key }} |
| 81 | + ISSUE_TITLE: ${{ github.event.issue.title }} |
| 82 | + run: | |
| 83 | + TITLE=$(echo "${ISSUE_TITLE//`/\\`}" | sed 's/"/\\"/g' | sed "s/'/\\\'/g") |
| 84 | + PAYLOAD=$(jq -n \ |
| 85 | + --arg issuetitle "$TITLE" \ |
| 86 | + --arg jirakey "$JIRA_KEY" \ |
| 87 | + '{ |
| 88 | + title: ($jirakey + ": " + $issuetitle) |
| 89 | + }') |
| 90 | +
|
| 91 | + # Update Github issue title with jira id |
| 92 | + curl -s \ |
| 93 | + -X PATCH \ |
| 94 | + -H "Authorization: Bearer $GITHUB_TOKEN" \ |
| 95 | + -H "Accept: application/vnd.github+json" \ |
| 96 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 97 | + "https://api.github.com/repos/$REPOSITORY/issues/$ISSUE_NUMBER" \ |
| 98 | + -d "$PAYLOAD" |
| 99 | +
|
| 100 | + if [ "$?" != 0 ]; then |
| 101 | + echo "Failed to update GH issue. Payload was:" |
| 102 | + echo "$PAYLOAD" |
| 103 | + exit 1 |
| 104 | + fi |
0 commit comments