forked from hazelcast/hazelcast
-
Notifications
You must be signed in to change notification settings - Fork 0
276 lines (265 loc) · 11.6 KB
/
assign-to-test-failure.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
name: Assign to test failure
on:
issues:
types: labeled
env:
ORGANIZATION_NAME : "${{ github.repository_owner }}"
BUILD_URL: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
jobs:
assign-to-test-failure:
if: contains(github.event.label.name, 'Test-Failure') && join(github.issue.assignees) == ''
runs-on: ubuntu-latest
steps:
- name: Find commit from failure body or use master branch
id: find-failure-commit-reference
uses: actions/github-script@v6
continue-on-error: true
with:
result-encoding: string
script: |
//Extract text from function - between /*! and */ characters
function parseAsString(f) {
return f.toString().
replace(/^[^\/]+\/\*!?/, '').
replace(/\*\/[^\/]+$/, '');
}
// Insert GH variable as multi-line comment into function to support special characters (due to no escaping from GH)
// Proper text value is extracted by parseAsString function
const testDescription = parseAsString(function() {/*!
${{ github.event.issue.body }}
*/});
//Regex: "commit: <match>"
const commitRegex = /commit\s([^)\s]+)/;
const match = testDescription.match(commitRegex);
let checkoutRef = "master"
if (match && match[1]) {
checkoutRef = match[1].trim();
}
console.log(`Using ${checkoutRef} as git checkout reference`);
return checkoutRef
- name: Checkout
uses: actions/checkout@v2
continue-on-error: true
with:
fetch-depth: 0
ref: ${{ steps.find-failure-commit-reference.outputs.result }}
- name: Find path to affected test
id: find-affected-test
uses: actions/github-script@v6
continue-on-error: true
with:
result-encoding: string
script: |
//Extract text between /*! and */
function parseAsString(f) {
return f.toString().
replace(/^[^\/]+\/\*!?/, '').
replace(/\*\/[^\/]+$/, '')
}
function replaceQuotes(text) {
//Regex: ' or " from beginning or end
return text.replace(/^['"]|['"]$/g, '');
}
function extractNameFromTitleWithoutPackage(title){
//Check if title contains class with Test suffix
//Regex: Capitalized word, ending with Test and match word only (\b)
const match = title.match(/[A-Z]\w+Test*\b/)
if (match) {
return replaceQuotes(match[0]) + ".java"
}
return '';
}
function extractNameFromPackageString(testRef) {
//Check if contains class name
if (testRef.toLowerCase() !== testRef) {
const testPath = testRef.split(".").join("/")
//Remove testName/case from name
//Regex: Everything before and capitalized word
const match = testRef.match(/(.*?[A-Z]\w*)/)
return replaceQuotes(match ? match[1] : testRef) + ".java"
}
//Return package path
return replaceQuotes(testRef.split(".").join("/"))
}
// Insert GH variable as multi-line comment into function to support special characters (due to no escaping from GH)
// Proper text value is extracted by parseAsString function
const title = parseAsString(function() {/*!
${{ github.event.issue.title }}
*/}).trim()
const packageRef = title.split(" ").find(ref => ref.indexOf("com.hazelcast")!== -1)
if (packageRef !== undefined) {
return extractNameFromPackageString(packageRef)
} else {
return extractNameFromTitleWithoutPackage(title)
}
- name: Find the last commit for failed test
if: steps.find-affected-test.outputs.result != ''
id: find-last-commit
continue-on-error: true
run: |
FILENAME=${{steps.find-affected-test.outputs.result}}
FOUND_FILE=""
LAST_COMMIT_DATE=""
FILES=$(find . -wholename "*$FILENAME*" -type f)
for FILE in $FILES; do
COMMIT_DATE=$(git log -1 --pretty=format:"%ad" --date=iso -- "$FILE")
if [[ "$COMMIT_DATE" > "$LAST_COMMIT_DATE" ]]; then
LAST_COMMIT_DATE=$COMMIT_DATE
FOUND_FILE=$FILE
fi
done
LAST_COMMIT=$(git log -1 --pretty=format:"%h" -- "$FOUND_FILE")
echo "Found last commit: $LAST_COMMIT for $FOUND_FILE file"
echo "FOUND_FILE=$FOUND_FILE" >> "$GITHUB_OUTPUT"
echo "LAST_COMMIT=$LAST_COMMIT" >> "$GITHUB_OUTPUT"
- name: Find author of the commit
if: steps.find-affected-test.outputs.result != ''
uses: actions/github-script@v6
id: find-author-username
continue-on-error: true
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
result-encoding: string
script: |
const commitSHA = "${{ steps.find-last-commit.outputs.LAST_COMMIT }}"
const commitDetails = await github.rest.repos.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
ref: commitSHA,
});
const username = commitDetails.data.author.login
console.log(`Found username: ${username} for commit: ${commitSHA}`)
return username
- name: Check if author belongs to organization
if: steps.find-affected-test.outputs.result != ''
id: author-is-organization-member
uses: actions/github-script@v6
continue-on-error: true
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
result-encoding: string
script: |
const organization = "${{ env.ORGANIZATION_NAME }}"
const username = "${{ steps.find-author-username.outputs.result }}"
let authorBelongsToOrganization = false
try {
const response = await github.request('GET /orgs/{org}/members/{username}', {
org: organization,
username: username,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
if (response.status == 204) {
authorBelongsToOrganization = true
console.log(`Author: ${username} - is organization member`);
} else {
console.log(`Author: ${username} is not a member of the organization.`);
}
} catch (error) {
console.log(`Author: ${username} is not a member of the organization. Details: ${error}`);
}
return authorBelongsToOrganization;
- name: Summarize
uses: actions/github-script@v6
id : summary
with:
result-encoding: json
script: |
//Extract text between /*! and */
function parseAsString(f) {
return f.toString().
replace(/^[^\/]+\/\*!?/, '').
replace(/\*\/[^\/]+$/, '')
}
// Insert GH variable as multi-line comment into function to support special characters (due to no escaping from GH)
// Proper text value is extracted by parseAsString function
const title = parseAsString(function() {/*!
${{ github.event.issue.title }}
*/}).trim();
const summary = {
issue : {
key : "${{ github.event.issue.number }}",
title : title
},
checkoutRef : "${{ steps.find-failure-commit-reference.outputs.result }}",
test : {
initialPath : "${{ steps.find-affected-test.outputs.result }}",
foundFile : "${{ steps.find-last-commit.outputs.FOUND_FILE }}",
lastCommit : {
sha : "${{ steps.find-last-commit.outputs.LAST_COMMIT }}",
author : {
username : "${{ steps.find-author-username.outputs.result }}",
belongsToOrganization : "${{ steps.author-is-organization-member.outputs.result }}"
}
}
}
}
console.log(JSON.stringify(summary, null, 2))
return summary
- name: One of previous steps was failed
uses: actions/github-script@v6
id: run-for-failure
if: contains(steps.*.outcome, 'failure')
with:
result-encoding: string
script: |
console.log("One of previous steps was failed")
return true
- name: Evaluate suggested assignement
uses: actions/github-script@v6
id: evaluate-suggested-assignement
with:
result-encoding: json
script: |
const issueAuthor = "${{ github.event.issue.user.login }}"
if("${{steps.run-for-failure.outputs.result}}" !== "") {
return {
comment: `Assigned @${issueAuthor} due to failed step in github action`,
finalAssignement: issueAuthor
}
}
if ("${{ fromJson(steps.summary.outputs.result).test.foundFile }}" === "") {
return {
comment: `Assigned @${issueAuthor} due to not found reference to code in the issue `,
finalAssignement: issueAuthor
}
}
const commit = "${{ fromJson(steps.summary.outputs.result).test.lastCommit.sha }}"
const commitAuthor = "${{ fromJson(steps.summary.outputs.result).test.lastCommit.author.username }}"
if ("${{ fromJson(steps.summary.outputs.result).test.lastCommit.author.belongsToOrganization }}" !== "true") {
return {
comment: `Assigned @${issueAuthor}, because identified author (${commitAuthor}) of last commit (${commit}) is not a member of the organization`,
finalAssignement: issueAuthor
}
}
//Proper case
return {
comment: `Assigned @${commitAuthor} due to being the author of last commit (${commit})`,
finalAssignement: commitAuthor
}
- name: Assign user
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
await github.rest.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: "${{ fromJson(steps.summary.outputs.result).issue.key }}",
assignees: ["${{ fromJson(steps.evaluate-suggested-assignement.outputs.result).finalAssignement }}"]
});
- name: Comment on issue
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const comment = "${{ fromJson(steps.evaluate-suggested-assignement.outputs.result).comment }}"
const buildUrl = "${{ env.BUILD_URL }}"
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `${comment}
Details: ${buildUrl}`
})