-
Notifications
You must be signed in to change notification settings - Fork 4
180 lines (169 loc) · 7.04 KB
/
jira-release-version.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
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
name: Create and Release Jira Version
on:
workflow_call:
inputs:
projectID:
description: 'The Jira Project ID'
required: true
type: string
projectKey:
description: 'The Jira Project Key'
required: true
type: string
jiraVersionPrefix:
description: 'The Jira Project Key'
required: true
type: string
secrets:
jiraToken:
required: true
description: 'The Jira API Token'
jobs:
find_released_prs:
runs-on: ubuntu-latest
outputs:
prBodyArray: ${{ steps.fetchReleasedPRs.outputs.prBodyArray }}
steps:
- name: Fetch PRs with Jira References
id: fetchReleasedPRs
uses: actions/github-script@v6
with:
script: |
const releaseBody = `${{ github.event.release.body }}`;
const ownerAndRepo = '${{ github.repository }}';
const regex = new RegExp(`\\(\\[#(\\d+)\\]\\(https:\\/\\/github\\.com\\/${ownerAndRepo}\\/issues\\/\\d+\\)\\)`, 'g')
const issueMatches = [...releaseBody.matchAll(regex)];
const prNumbers = issueMatches.map((match) => match[1]);
const [owner, repo] = ownerAndRepo.split('/');
const results = await Promise.all(prNumbers.map(pullNumber => {
return github.rest.issues.get({
owner,
repo,
issue_number: pullNumber,
});
}));
const prBodyArray = results.filter(r => r.data.body != null).map(r => JSON.stringify(r.data.body));
console.log(`Found ${prBodyArray.length} PRs referenced in release body`);
core.setOutput('prBodyArray', prBodyArray);
create_jira_version:
runs-on: ubuntu-latest
needs: find_released_prs
if: ${{ needs.find_released_prs.outputs.prBodyArray != '[]' }}
outputs:
jiraVersionName: ${{ steps.createJiraVersion.outputs.jiraVersionName }}
jiraVersionId: ${{ steps.createJiraVersion.outputs.jiraVersionId }}
steps:
- name: Create Jira Version
id: createJiraVersion
uses: actions/github-script@v6
with:
script: |
const versionTag = '${{ github.event.release.tag_name }}';
const jiraVersion = `${{ inputs.jiraVersionPrefix }} - ${versionTag}`;
const projectId = ${{ inputs.projectID }};
// create the version but don't release it until after associating relevant issues because the Slack automation
// will be triggered when the version is released and it relies on the issues being associated with the release
const response = await fetch(
'https://czi-tech.atlassian.net/rest/api/3/version',
{
method: 'POST',
body: JSON.stringify({
name: jiraVersion,
projectId,
}),
headers: {
Authorization: 'Basic ${{ secrets.jiraToken }}',
'Content-Type': 'application/json',
},
}
);
const responseJson = await response.json();
if (response.status >= 400) {
throw new Error(`Failed to create Jira Version "${jiraVersion}" in project "${projectId}": ${responseJson.errorMessages.join(', ')}`);
}
console.log(`Created Jira Version "${jiraVersion}" in project "${projectId}"`);
core.setOutput('jiraVersionName', jiraVersion);
core.setOutput('jiraVersionId', responseJson.id);
update_issue_fix_version:
runs-on: ubuntu-latest
needs: [find_released_prs, create_jira_version]
strategy:
matrix:
pullRequestBody: ${{ fromJson(needs.find_released_prs.outputs.prBodyArray) }}
if: ${{ needs.find_released_prs.outputs.prBodyArray != '[]' }}
steps:
- uses: actions/checkout@v3
- name: Find Jira Marker
id: findJiraMarker
uses: chanzuckerberg/github-actions/.github/actions/[email protected]
with:
pullRequestBody: ${{ matrix.pullRequestBody }}
projectKey: ${{ inputs.projectKey }}
- name: Update Fix Version of Referenced Jira Issue
uses: actions/github-script@v6
with:
script: |
const jiraIssueRef = '${{ steps.findJiraMarker.outputs.jiraIssueReference }}'
if (!jiraIssueRef) {
return;
}
const jiraVersionName = '${{ needs.create_jira_version.outputs.jiraVersionName }}';
console.log(`Updating Jira issue ${jiraIssueRef} with Fix Version: ${jiraVersionName}`)
const response = await fetch(
`https://czi-tech.atlassian.net/rest/api/3/issue/${jiraIssueRef}`,
{
method: 'PUT',
body: JSON.stringify({
update: {
fixVersions: [{
add: {
name: jiraVersionName,
},
}],
},
}),
headers: {
Authorization: 'Basic ${{ secrets.jiraToken }}',
'Content-Type': 'application/json',
},
}
);
if (response.status >= 400) {
const responseJson = await response.json();
throw new Error(`Failed to update Fix Version in Jira issue ${jiraIssueRef}: ${responseJson.errorMessages.join(', ')}`);
}
console.log(`Successfully updated Fix Version in Jira issue ${jiraIssueRef}, response code: ${response.status}`);
release_jira_version:
needs:
- create_jira_version
- update_issue_fix_version
runs-on: ubuntu-latest
steps:
- name: Release Jira Version
id: createJiraVersion
uses: actions/github-script@v6
with:
script: |
const releaseDate = new Date().toISOString().slice(0, 10); // YYYY-MM-DD
const jiraVersionId = '${{ needs.create_jira_version.outputs.jiraVersionId }}';
const jiraVersionName = '${{ needs.create_jira_version.outputs.jiraVersionName }}';
const projectId = ${{ inputs.projectID }};
const response = await fetch(
`https://czi-tech.atlassian.net/rest/api/3/version/${jiraVersionId}`,
{
method: 'PUT',
body: JSON.stringify({
released: true,
releaseDate,
}),
headers: {
Authorization: 'Basic ${{ secrets.jiraToken }}',
'Content-Type': 'application/json',
},
}
);
const responseJson = await response.json();
if (response.status >= 400) {
throw new Error(`Failed to release Jira Version "${jiraVersionName}" in project "${projectId}": ${responseJson.errorMessages.join(', ')}`);
}
console.log(`Released Jira Version "${jiraVersionName}" in project "${projectId}", release date: ${releaseDate}`);