-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.yml
213 lines (197 loc) · 7.85 KB
/
action.yml
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
name: Create Issue
description: GitHub action to create Jira issues and send Slack notifications for pull requests.
inputs:
project:
description: The Jira project to add the issue to.
required: true
summary:
description: The summary of the isssue.
required: false
description:
description: A description of the isssue.
required: false
transition:
description: Transtion to apply to the issue.
default: To Do
test-case:
description: Instructions on how to test the issue.
default: Standard smoke test.
deployment-instructions:
description: Instructions on how to deploy the issue.
default: Unknown
pull-url:
description: The URL of the pull request to create the issue about.
default: ${{ github.event.pull_request.html_url }}
jira-url:
description: Base URL the Jira instance.
required: true
jira-user:
description: Email of the Jira API user.
required: true
jira-token:
description: API token of the Jira API user.
required: true
github-token:
description: GitHub access token
default: ${{ github.token }}
slack-webhook:
description: Optional Slack webhook URL to receive notifications.
required: false
runs:
using: composite
steps:
- name: Missing Pull Request
if: ${{ inputs.pull-url == '' }}
run: |
echo "::error:: Missing pull-url: pull-url is required when not running from a pull request "
exit 1
shell: bash
- name: Get Pull Request Information
id: pr-info
run: |
fields="title,number,url"
pull_info=$(gh pr view $PULL --json $fields)
echo "::debug::pull_info=$pull_info"
for field in ${fields//,/ }; do
value=$(jq -r --arg field "$field" '.[$field]' <<< $pull_info)
echo "$field=$value" >> $GITHUB_OUTPUT
done
env:
PULL: ${{ inputs.pull-url }}
GITHUB_TOKEN: ${{ inputs.github-token }}
shell: bash
- name: Check if automerging
id: automerge
run: |
echo Sleeping for 10 seconds before checking if automerge is enabled.
sleep 10
echo Checking if automerge is enabled.
automerge=$(gh pr view $PULL --json autoMergeRequest)
echo "::debug::automerge=$pull_info"
if jq -e '.autoMergeRequest == null' <<< "$automerge" > /dev/null; then
echo Automerge is not enabled
echo "mergable=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo Getting list of required status checks
rule_sets=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/rules/branches/${{ github.base_ref }})
required_checks=$(jq -c <<<"$rule_sets" '[.[]| select(.type == "required_status_checks").parameters.required_status_checks[] | .context]')
echo "::debug::required status checks : $required_checks"
filter_results() {
jq <<< "$1" \
--argjson required "$required_checks" \
'[.statusCheckRollup[] | select([.name] | inside($required))]'
}
echo Waiting for status checks to complete
empty_result_count=0
status_checks=$(gh pr view $PULL --json statusCheckRollup)
while ! filter_results "$status_checks" | jq -e '.[] | .status == "COMPLETED"' > /dev/null
do
sleep 1
echo Checking status check again
status_checks=$(gh pr view $PULL --json statusCheckRollup)
echo "::debug::checks: $(jq <<<"$status_checks" -c)"
if filter_results "$status_checks" | jq -e 'length == 0' > /dev/null; then
if (( empty_result_count < 60 )); then
empty_result_count=$((empty_result_count+1))
echo "::debug::No required checks running yet"
else
echo Exiting because no required checks have started to run after a minute.
echo "mergable=false" >> "$GITHUB_OUTPUT"
exit 0
fi
fi
done
echo Checking if pr is mergable
merge_state_status=$(gh pr view $PULL --json mergeStateStatus)
if jq -e '[.mergeStateStatus] | inside(["CLEAN", "UNSTABLE"])' <<< "$merge_state_status" > /dev/null; then
echo PR will be automerged
echo "mergable=true" >> "$GITHUB_OUTPUT"
exit 0
fi
echo PR is unmergeable, a ticket will be created
echo "mergable=false" >> "$GITHUB_OUTPUT"
env:
PULL: ${{ inputs.pull-url }}
GITHUB_TOKEN: ${{ inputs.github-token }}
shell: bash
- name: Login to jira
if: ${{ steps.automerge.outputs.mergable == 'false' }}
uses: atlassian/gajira-login@master
env:
JIRA_BASE_URL: ${{ inputs.jira-url }}
JIRA_USER_EMAIL: ${{ inputs.jira-user }}
JIRA_API_TOKEN: ${{ inputs.jira-token }}
- name: Set Jira Info
if: ${{ steps.automerge.outputs.mergable == 'false' }}
id: jira-info
env:
summary: '${{ inputs.summary }}'
description: '${{ inputs.description }}'
pr_title: '${{ steps.pr-info.outputs.title }}'
pull_url: '${{ inputs.pull-url }}'
run: |
echo "summary=${summary:-pr_title}" >> $GITHUB_OUTPUT
echo "description<<EOF" >> $GITHUB_OUTPUT
echo "${description:-pr_title}" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "test_field=${test_field:-customfield_10027}" >> $GITHUB_OUTPUT
echo "instructions_field=${instructions_field:-customfield_10028}" >> $GITHUB_OUTPUT
shell: bash
- name: Create issue
if: ${{ steps.automerge.outputs.mergable == 'false' }}
id: ticket
uses: atlassian/gajira-create@master
with:
project: ${{ inputs.project }}
issuetype: Task
summary: ${{ steps.jira-info.outputs.summary }}
description: |
${{ steps.jira-info.outputs.description }}
${{ inputs.pull-url }}
fields: |
{
"${{ steps.jira-info.outputs.test_field }}": "${{ inputs.test-case }}",
"${{ steps.jira-info.outputs.instructions_field }}": "${{ inputs.deployment-instructions }}"
}
- name: Put ticket in todo
if: ${{ steps.automerge.outputs.mergable == 'false' }}
uses: atlassian/gajira-transition@master
with:
issue: ${{ steps.ticket.outputs.issue }}
transition: ${{ inputs.transition }}
# - name: Prepend Ticket ID to Pull Request
# if: ${{ steps.automerge.outputs.mergable == 'false' }}
# run: 'gh pr edit $PULL --title "$TICKET: $OLD_TITLE"'
# env:
# PULL: ${{ inputs.pull-url }}
# TICKET: ${{ steps.ticket.outputs.issue }}
# OLD_TITLE: ${{ steps.pr-info.outputs.title }}
# GITHUB_TOKEN: ${{ inputs.github-token }}
# shell: bash
- name: Comment On Pull Request
if: ${{ steps.automerge.outputs.mergable == 'false' }}
run: gh pr comment $PULL --body "$BODY"
env:
PULL: ${{ inputs.pull-url }}
BODY: >
Created a jira ticket: [${{ steps.ticket.outputs.issue }}](${{ inputs.jira-url }}/browse/${{ steps.ticket.outputs.issue }})
GITHUB_TOKEN: ${{ inputs.github-token }}
shell: bash
- name: Send custom JSON data to Slack workflow
if: ${{ steps.automerge.outputs.mergable == 'false' && inputs.slack-webhook != '' }}
id: slack
uses: slackapi/[email protected]
with:
payload: |
{
"ticket_id": "${{ steps.ticket.outputs.issue }}",
"ticket_url": "${{ inputs.jira-url }}/browse/${{ steps.ticket.outputs.issue }}",
"pull_url": "${{ inputs.pull-url }}",
"client_id": "${{ inputs.project }}"
}
env:
SLACK_WEBHOOK_URL: ${{ inputs.slack-webhook }}