Skip to content

Commit

Permalink
Add a comment to the latest
Browse files Browse the repository at this point in the history
Signed-off-by: Prudhvi Godithi <[email protected]>
  • Loading branch information
prudhvigodithi committed Jul 26, 2023
1 parent 8776b89 commit 9fd97be
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ jacocoTestReport {
}
}

String version = '5.2.0'
String version = '5.3.0'

task updateVersion {
doLast {
Expand Down
5 changes: 4 additions & 1 deletion tests/jenkins/TestCreateBuildFailureGithubIssue.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class TestCreateBuildFailureGithubIssue extends BuildPipelineTest {
@Test
public void testExistingGithubIssue() {
super.testPipeline('tests/jenkins/jobs/CreateBuildFailureGithubIssue_Jenkinsfile', 'tests/jenkins/jobs/CreateBuildFailureGithubExistingIssueCheck_Jenkinsfile')
assertThat(getCommands('println', ''), hasItem('Issue already exists in the repository, skipping.'))
assertThat(getCommands('println', ''), hasItem('Issue already exists, adding a comment.'))
assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue list --repo https://github.com/opensearch-project/OpenSearch.git -S \"[AUTOCUT] Distribution Build Failed for OpenSearch-2.0.0 in:title\" --label autocut,v2.0.0, returnStdout=true}"))
assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue list --repo https://github.com/opensearch-project/OpenSearch.git -S \"[AUTOCUT] Distribution Build Failed for OpenSearch-2.0.0 in:title\" --label autocut,v2.0.0 --json number --jq '.[0].number', returnStdout=true}"))
assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue comment \nbbb\nccc\n --repo https://github.com/opensearch-project/OpenSearch.git --body \"***Received Error***: **Error building OpenSearch, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component OpenSearch --snapshot**.\n The distribution build for OpenSearch has failed for version: 2.0.0.\n Please see build log at www.example.com/jobs/test/123/consoleFull\", returnStdout=true}"))
}

def getCommands(method, text) {
Expand Down
8 changes: 5 additions & 3 deletions tests/jenkins/TestCreateGithubIssue.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ class TestCreateGithubIssue extends BuildPipelineTest {
this.registerLibTester(new CreateGithubIssueLibTester(
'https://github.com/opensearch-project/opensearch-build',
'Test GH issue title',
'Test GH issue body'
'Test GH issue body',
))
super.testPipeline('tests/jenkins/jobs/CreateGithubIssueExisting_JenkinsFile')
assertThat(getCommands('println', ''), hasItem('Issue already exists in the repository, skipping.'))
assertThat(getCommands('sh', 'script'), hasItem('{script=gh issue list --repo https://github.com/opensearch-project/opensearch-build -S "Test GH issue title in:title" --label autocut, returnStdout=true}'))
assertThat(getCommands('println', ''), hasItem('Issue already exists, adding a comment.'))
assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue list --repo https://github.com/opensearch-project/opensearch-build -S \"Test GH issue title in:title\" --label autocut, returnStdout=true}"))
assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue list --repo https://github.com/opensearch-project/opensearch-build -S \"Test GH issue title in:title\" --label autocut --json number --jq '.[0].number', returnStdout=true}"))
assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue comment \nbbb\nccc\n --repo https://github.com/opensearch-project/opensearch-build --body \"Test GH issue body\", returnStdout=true}"))
}

def getCommands(method, text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@
createGithubIssue.usernamePassword({credentialsId=jenkins-github-bot-token, passwordVariable=GITHUB_TOKEN, usernameVariable=GITHUB_USER})
createGithubIssue.withCredentials([[GITHUB_USER, GITHUB_TOKEN]], groovy.lang.Closure)
createGithubIssue.sh({script=gh issue list --repo https://github.com/opensearch-project/OpenSearch.git -S "[AUTOCUT] Distribution Build Failed for OpenSearch-2.0.0 in:title" --label autocut,v2.0.0, returnStdout=true})
createGithubIssue.println(Issue already exists in the repository, skipping.)
createGithubIssue.println(Issue already exists, adding a comment.)
createGithubIssue.sh({script=gh issue list --repo https://github.com/opensearch-project/OpenSearch.git -S "[AUTOCUT] Distribution Build Failed for OpenSearch-2.0.0 in:title" --label autocut,v2.0.0 --json number --jq '.[0].number', returnStdout=true})
createGithubIssue.sh({script=gh issue comment
bbb
ccc
--repo https://github.com/opensearch-project/OpenSearch.git --body "***Received Error***: **Error building OpenSearch, retry with: ./build.sh manifests/2.2.0/opensearch-2.2.0.yml --component OpenSearch --snapshot**.
The distribution build for OpenSearch has failed for version: 2.0.0.
Please see build log at www.example.com/jobs/test/123/consoleFull", returnStdout=true})
createBuildFailureGithubIssue.sleep({time=3, unit=SECONDS})
7 changes: 6 additions & 1 deletion tests/jenkins/jobs/CreateGithubIssueExisting_JenkinsFile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
createGithubIssue.usernamePassword({credentialsId=jenkins-github-bot-token, passwordVariable=GITHUB_TOKEN, usernameVariable=GITHUB_USER})
createGithubIssue.withCredentials([[GITHUB_USER, GITHUB_TOKEN]], groovy.lang.Closure)
createGithubIssue.sh({script=gh issue list --repo https://github.com/opensearch-project/opensearch-build -S "Test GH issue title in:title" --label autocut, returnStdout=true})
createGithubIssue.println(Issue already exists in the repository, skipping.)
createGithubIssue.println(Issue already exists, adding a comment.)
createGithubIssue.sh({script=gh issue list --repo https://github.com/opensearch-project/opensearch-build -S "Test GH issue title in:title" --label autocut --json number --jq '.[0].number', returnStdout=true})
createGithubIssue.sh({script=gh issue comment
bbb
ccc
--repo https://github.com/opensearch-project/opensearch-build --body "Test GH issue body", returnStdout=true})
15 changes: 12 additions & 3 deletions vars/buildFailureMessage.groovy → vars/buildMessage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/** Library to find a pattern in jenkins build log.
@param Map args = [:] args A map of the following parameters
@param args.search <required> - Use 'pass' to get the components passed and 'fail' for components failed.
*/
import com.cloudbees.groovy.cps.NonCPS
import org.apache.commons.io.IOUtils
@NonCPS
def call(){
String ERROR_STRING = "Error building"
def call(Map args = [:]){
String QUERY_STRING = ''
if (args.search == 'fail') {
QUERY_STRING = "Error building"
} else if (args.search == 'pass') {
QUERY_STRING = "Build successful"
}
List<String> message = []
Reader performance_log = currentBuild.getRawBuild().getLogReader()
String logContent = IOUtils.toString(performance_log)
Expand All @@ -19,7 +28,7 @@ def call(){
logContent.eachLine() { line ->
line=line.replace("\"", "")
//Gets the exact match for Error building
def java.util.regex.Matcher match = (line =~ /$ERROR_STRING.*/)
def java.util.regex.Matcher match = (line =~ /$QUERY_STRING.*/)
if (match.find()) {
line=match[0]
message.add(line)
Expand Down
44 changes: 44 additions & 0 deletions vars/closeBuildSuccessGithubIssue.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 close GitHub issue across opensearch-project repositories.
@param Map args = [:] args A map of the following parameters
@param args.message <required> - message retrieved from buildMessage() method.
@param args.inputManifestPath <required> - Path to input manifest.
*/
void call(Map args = [:]) {
lib = library(identifier: 'jenkins@main', retriever: legacySCM(scm))
def passMessages = args.message
List<String> passedComponents = []
else {
for (message in passMessages.unique()) {
java.util.regex.Matcher match = (message =~ /(?<=\bBuild successful\s).*/)
String matched = match[0]
println(matched.split(" ")[0].trim())
passedComponents.add(matched.split(" ")[0].trim())
}

def yamlFile = readYaml(file: args.inputManifestPath)
def currentVersion = yamlFile.build.version

for (component in yamlFile.components) {
if (passedComponents.contains(component.name)) {
println("Component ${component.name} passed, closing github issue")
ghIssueBody = """Closing the issue as the distribution build for ${component.name} has passed for version: ${currentVersion}.
Please see build log at ${BUILD_URL}consoleFull""".stripIndent()
closeGithubIssue(
repoUrl: component.repository,
issueTitle: "[AUTOCUT] Distribution Build Failed for ${component.name}-${currentVersion}",
closeComment: ghIssueBody,
label: "autocut,v${currentVersion}"
)
sleep(time:3, unit:'SECONDS')
}
}
}
}
37 changes: 37 additions & 0 deletions vars/closeGithubIssue.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 close 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.closeComment <required> - GitHub issue leave a closing comment
@param args.label <optional> - GitHub issue label to be attached along with 'untriaged'. Defaults to autocut.
*/
void call(Map args = [:]) {
label = args.label ?: 'autocut'
try {
withCredentials([usernamePassword(credentialsId: 'jenkins-github-bot-token', passwordVariable: 'GITHUB_TOKEN', usernameVariable: 'GITHUB_USER')]) {
def issues = sh(
script: "gh issue list --repo ${args.repoUrl} -S \"${args.issueTitle} in:title\" --label ${label}",
returnStdout: true
)
def issuesNumber = sh(
script: "gh issue list --repo ${args.repoUrl} -S \"${args.issueTitle} in:title\" --label ${label} --json number --jq '.[0].number'",
returnStdout: true
)
sh(
script: "gh issue close ${issuesNumber} --repo ${args.repoUrl} --comment \"${args.closeComment}\"",
returnStdout: true
)
}
} catch (Exception ex) {
error("Unable to close GitHub issue for ${args.repoUrl}", ex.getMessage())
}
}
10 changes: 9 additions & 1 deletion vars/createGithubIssue.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ void call(Map args = [:]) {
)

if (issues) {
println('Issue already exists in the repository, skipping.')
println('Issue already exists, adding a comment.')
def issuesNumber = sh(
script: "gh issue list --repo ${args.repoUrl} -S \"${args.issueTitle} in:title\" --label ${label} --json number --jq '.[0].number'",
returnStdout: true
)
sh(
script: "gh issue comment ${issuesNumber} --repo ${args.repoUrl} --body \"${args.issueBody}\"",
returnStdout: true
)
}
else {
sh(
Expand Down

0 comments on commit 9fd97be

Please sign in to comment.