forked from opensearch-project/opensearch-build-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createGithubIssue.groovy
68 lines (63 loc) · 2.95 KB
/
createGithubIssue.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
/*
* 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 GitHub issue across opensearch-project repositories.
@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.daysToReOpen <optional> - Look for a closed Github issues older than `daysToReOpen`.
*/
void call(Map args = [:]) {
label = args.label ?: 'autocut'
daysToReOpen = args.daysToReOpen ?: '3'
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\" --label ${label} --json number --jq '.[0].number'",
returnStdout: true
).trim()
def currentDayMinusDaysToReOpen = sh(
script: "date -d \"${daysToReOpen} days ago\" +'%Y-%m-%d'",
returnStdout: true
).trim()
def closedIssue = sh(
script: "gh issue list --repo ${args.repoUrl} -S \"${args.issueTitle} in:title is:closed closed:>=${currentDayMinusDaysToReOpen}\" --label ${label} --json number --jq '.[0].number'",
returnStdout: true
).trim()
if (openIssue) {
println('Issue already exists, adding a comment')
sh(
script: "gh issue comment ${openIssue} --repo ${args.repoUrl} --body \"${args.issueBody}\"",
returnStdout: true
)
}
else if (!openIssue && closedIssue) {
println("Re-opening a recently closed issue and commenting on it")
sh(
script: "gh issue reopen --repo ${args.repoUrl} ${closedIssue}",
returnStdout: true
)
sh(
script: "gh issue comment ${closedIssue} --repo ${args.repoUrl} --body \"${args.issueBody}\"",
returnStdout: true
)
}
else {
println("Creating new issue")
sh(
script: "gh issue create --title \"${args.issueTitle}\" --body \"${args.issueBody}\" --label ${label} --label \"untriaged\" --repo ${args.repoUrl}",
returnStdout: true
)
}
}
} catch (Exception ex) {
error("Unable to create GitHub issue for ${args.repoUrl}", ex.getMessage())
}
}