Skip to content

Commit 53f8296

Browse files
committed
Added GH action to sync issue with JIRA
1 parent e1c9af3 commit 53f8296

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
name: "Sync GitHub issue to Jira"
2+
description: 'This GitHub action creates Jira issue from GitHub issue when GitHub issue is labeled with "jira" label.'
3+
inputs:
4+
webhook-url:
5+
description: >
6+
Jira integration webhook URL.
7+
Store it as a secret as anyone who has access to it will be able to post to your Jira board.
8+
required: true
9+
label:
10+
description: "Label which will trigger a Jira import."
11+
required: true
12+
default: "jira"
13+
14+
runs:
15+
using: "composite"
16+
steps:
17+
- name: restrict action to labelled issues and issue comments
18+
run: |
19+
set -eux
20+
21+
echo "NeedsJiraUpdate=false" >> $GITHUB_ENV
22+
23+
if [ ${{ github.event_name }} != "issues" ] && [ ${{ github.event_name }} != "issue_comment" ]; then
24+
echo "This action only work on issue events. Please use on: issues or issue_comment to use this action."
25+
exit 1
26+
fi
27+
28+
if [ ${{ github.event.issue.pull_request }} ]; then
29+
echo "This action only work on issues, not pull requests."
30+
exit 0
31+
fi
32+
33+
# Issue creation with label will trigger 2 events and run twice: one create, one labelled.
34+
# let just focus on labelling then for creating issues Jira-side.
35+
if [ ${{ github.event_name }} == "issues" ] && [ ${{ github.event.action }} == "opened" ]; then
36+
echo "Ignoring creation of issues as a label will trigger a second event."
37+
exit 0
38+
fi
39+
40+
# We only operate on labelled issues or issues that are just unlabeled with our desired label
41+
## check if one label of labels is our jira label
42+
toconsider=${{ contains(github.event.issue.labels.*.name, inputs.label) }}
43+
## second chance, this has just been unlabeled and needs to be deleted on Jira
44+
if [ ${{ github.event.action }} == "unlabeled" ] && [ ${{ github.event.label.name }} == ${{ inputs.label }} ]; then
45+
toconsider=true
46+
fi
47+
if [ "${toconsider}" == false ]; then
48+
echo "Our desired label not found on issue or not unlabeled, skipping"
49+
exit 0
50+
fi
51+
52+
# And finally, for the "labeled" event, we are only interested if the new added label is our desired one.
53+
if [ ${{ github.event.action }} == "labeled" ] && [ ${{ github.event.label.name }} != ${{ inputs.label }} ]; then
54+
echo "Not interested in this action, skipping"
55+
exit 0
56+
fi
57+
58+
# last one wins
59+
echo "NeedsJiraUpdate=true" >> $GITHUB_ENV
60+
shell: bash
61+
62+
- name: "Update jira"
63+
if: ${{ env.NeedsJiraUpdate == 'true' }}
64+
env:
65+
# ID is the html url to keep a link between systems as there is no way to force an ID on Jira side.
66+
id: ${{ github.event.issue.html_url }}
67+
title: ${{ github.event.issue.title }}
68+
body: ${{ github.event.issue.body }}
69+
author: ${{ github.event.issue.user.login }}
70+
commentAuthor: ${{ github.actor }}
71+
comment: ${{ github.event.comment.body }}
72+
run: |
73+
set -eux
74+
75+
# Convert markdown to JIRA using mistletoe package which is available starting with impish.
76+
# Since GH runners only have LTS versions it's safe to only check for focal which doesn't have the package.
77+
if [ $(lsb_release -c -s) == "focal" ]; then
78+
echo "Converting Markdown to JIRA is only possible starting with Ubuntu 22.04 (jammy). Pushing verbatim content to JIRA..."
79+
else
80+
TMPDIR=$(mktemp -d)
81+
trap 'rm -rf -- "$TMPDIR"' EXIT
82+
83+
sudo apt install -y python3-mistletoe
84+
echo ${body} > $TMPDIR/body.md
85+
echo ${comment} > $TMPDIR/comment.md
86+
body=$(PYTHONPATH=/usr/share/doc/python3-mistletoe mistletoe -r examples.jira_renderer.JIRARenderer $TMPDIR/body.md)
87+
comment=$(PYTHONPATH=/usr/share/doc/python3-mistletoe mistletoe -r examples.jira_renderer.JIRARenderer $TMPDIR/comment.md)
88+
fi
89+
90+
description="${body}
91+
92+
Opened by ${author}."
93+
commentContent=""
94+
95+
# Choose Jira action based on event type and action.
96+
action=""
97+
if [ ${{ github.event_name }} == "issues" ]; then
98+
action=Update
99+
if [ ${{ github.event.action }} == "labeled" ]; then
100+
action=Create
101+
elif [ ${{ github.event.action }} == "reopened" ]; then
102+
action=Reopen
103+
elif [ ${{ github.event.action }} == "deleted" ] || [ ${{ github.event.action }} == "unlabeled" ]; then
104+
# Note: deleting issue from GH is not supported ATM as there is no more label attached. unlabeled is supported.
105+
action=Delete
106+
elif [ ${{ github.event.action }} == "closed" ]; then
107+
action=Close
108+
fi
109+
else
110+
action=AddComment
111+
if [ ${{ github.event.action }} == "deleted" ]; then
112+
echo "Deleting comment on Jira is not supported ATM, skipping."
113+
exit 0
114+
fi
115+
# For now, editing comments will add a new one on Jira.
116+
commentContent="From ${commentAuthor}:
117+
${comment}"
118+
fi
119+
120+
echo "PUSHING: $id $action $title $description $commentContent"
121+
122+
# Push to Jira as a json data format.
123+
data=$(jq -n \
124+
--arg id "$id" \
125+
--arg action "$action" \
126+
--arg title "$title" \
127+
--arg description "$description" \
128+
--arg commentContent "$commentContent" \
129+
'{data: {id: $id, action: $action, title: $title, description: $description, commentContent: $commentContent}}')
130+
curl -X POST -H 'Content-type: application/json' --data "${data}" '${{ inputs.webhook-url }}'
131+
132+
shell: bash

.github/workflows/sync_with_jira.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Sync GitHub issues to Jira example
2+
on: [issues, issue_comment]
3+
4+
jobs:
5+
sync-issues:
6+
name: Sync issues to Jira
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: ./github/actions/sync_with_jira/
11+
with:
12+
webhook-url: https://automation.atlassian.com/pro/hooks/01e3a9679be4d3e0e0fb7c1654d5bb603b0111a0

0 commit comments

Comments
 (0)