forked from opensearch-project/opensearch-build-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradleCheckFlakyTestGitHubIssue.groovy
88 lines (78 loc) · 4.08 KB
/
gradleCheckFlakyTestGitHubIssue.groovy
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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/** Library to create/edit/skip Flaky Test Report GitHub issue for OpenSearch repo.
@param Map args = [:] args A map of the following parameters
@param args.repoUrl <required> - GitHub repository URL to create issue
@param args.issueTitle <required> - GitHub issue title
@param args.issueBody <required> - GitHub issue body
@param args.label <optional> - GitHub issue label to be attached along with 'untriaged'. Defaults to autocut.
@param args.issueEdit <optional> - Updates the body of the issue, the default if not passed is to add a comment.
@param args.issueBodyFile <optional> - GitHub issue body from an `.md` file
*/
import gradlecheck.ParseMarkDownTable
import gradlecheck.MarkdownComparator
void call(Map args = [:]) {
label = args.label ?: 'autocut,>test-failure,flaky-test'
try {
withCredentials([usernamePassword(credentialsId: 'jenkins-github-bot-token', passwordVariable: 'GITHUB_TOKEN', usernameVariable: 'GITHUB_USER')]) {
def openIssue = sh(
script: "gh issue list --repo ${args.repoUrl} -S \"${args.issueTitle} in:title\" --json number --jq '.[0].number'",
returnStdout: true
).trim()
def closedIssue = sh(
script: "gh issue list --repo ${args.repoUrl} -S \"${args.issueTitle} in:title is:closed\" --json number --jq '.[0].number'",
returnStdout: true
).trim()
if (openIssue) {
println('Issue already exists, editing the issue body')
sh(
script: "gh issue edit ${openIssue} --repo ${args.repoUrl} --body-file \"${args.issueBodyFile}\"",
returnStdout: true
)
}
else if (!openIssue && closedIssue) {
def existingIssueBody = sh(
script: "gh issue list --repo ${args.repoUrl} -S \"${args.issueTitle} in:title is:closed\" --json body --jq '.[0].body'",
returnStdout: true
).trim()
def existingTable = new ParseMarkDownTable(existingIssueBody).parseMarkdownTableRows()
def markdownTable = new ParseMarkDownTable(readFile(args.issueBodyFile)).parseMarkdownTableRows()
def differences = new MarkdownComparator(markdownTable, existingTable).markdownComparison()
if (!differences) {
println("Not Re-opening the issue as the no change in the Flaky report after the issue is closed")
} else {
println "Differences found:"
differences.each { diffRow ->
println "Git Reference: ${diffRow['Git Reference']}, " +
"Merged Pull Request: ${diffRow['Merged Pull Request']}, " +
"Build Details: ${diffRow['Build Details']}, " +
"Test Name: ${diffRow['Test Name']}"
}
sh(
script: "gh issue reopen --repo ${args.repoUrl} ${closedIssue}",
returnStdout: true
)
sh(
script: "gh issue edit ${closedIssue} --repo ${args.repoUrl} --body-file \"${args.issueBodyFile}\"",
returnStdout: true
)
}
}
else {
println("Creating new issue")
sh(
script: "gh issue create --title \"${args.issueTitle}\" --body-file \"${args.issueBodyFile}\" --label \"${label}\" --label \"untriaged\" --repo ${args.repoUrl}",
returnStdout: true
)
}
}
} catch (Exception ex) {
error("Unable to create GitHub issue for ${args.repoUrl}", ex.getMessage())
}
}