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