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 27, 2023
1 parent 8776b89 commit 70507a0
Show file tree
Hide file tree
Showing 23 changed files with 438 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/groovy-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v2
- name: Run Tests
run: |
./gradlew test --info
./gradlew test --info --tests=TestCloseGithubIssue
- name: Upload Coverage Report
uses: codecov/[email protected]
with:
Expand Down
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.4.0'

task updateVersion {
doLast {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ package jenkins.tests
import jenkins.tests.BuildPipelineTest
import org.junit.*

class TestBuildFailureMessage extends BuildPipelineTest {
class TestBuildMessage extends BuildPipelineTest {


@Before
void setUp() {
this.registerLibTester(new BuildFailureMessageLibTester())
this.registerLibTester(new BuildMessageLibTester('pass'))
super.setUp()
def currentBuild = binding.getVariable('currentBuild')
binding.setVariable("currentBuild", currentBuild)
}

@Test
void testBuildFailureMsg() {
super.testPipeline("tests/jenkins/jobs/BuildFailureMessage_Jenkinsfile")
super.testPipeline("tests/jenkins/jobs/BuildMessage_Jenkinsfile")
}
}
46 changes: 46 additions & 0 deletions tests/jenkins/TestCloseBuildSuccessGithubIssue.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.
*/
package jenkins.tests

import jenkins.tests.BuildPipelineTest
import org.junit.Before
import org.junit.Test
import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString
import static org.hamcrest.CoreMatchers.hasItem
import static org.hamcrest.MatcherAssert.assertThat

class TestCloseBuildSuccessGithubIssue extends BuildPipelineTest {

@Override
@Before
void setUp() {
this.registerLibTester(new CloseBuildSuccessGithubIssueLibTester(['Build successful OpenSearch']))
super.setUp()
}


@Test
public void testExistingGithubIssue() {
super.testPipeline('tests/jenkins/jobs/CloseBuildSuccessGithubIssue_Jenkinsfile')
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 close bbb\nccc -R opensearch-project/OpenSearch --comment \"Closing the issue as the distribution build for OpenSearch has passed for version: **2.0.0**.\n Please see build log at www.example.com/jobs/test/123/consoleFull\", returnStdout=true}"))
}

def getCommands(method, text) {
def shCommands = helper.callStack.findAll { call ->
call.methodName == method
}.collect { call ->
callArgsToString(call)
}.findAll { command ->
command.contains(text)
}
return shCommands
}

}
51 changes: 51 additions & 0 deletions tests/jenkins/TestCloseGithubIssue.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.
*/
package jenkins.tests

import jenkins.tests.BuildPipelineTest
import org.junit.Before
import org.junit.Test
import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString
import static org.hamcrest.CoreMatchers.hasItem
import static org.hamcrest.MatcherAssert.assertThat

class TestCloseGithubIssue extends BuildPipelineTest {

@Override
@Before
void setUp() {
super.setUp()
}


@Test
void testCloseExistingGithubIssue() {
this.registerLibTester(new CloseGithubIssueLibTester(
'https://github.com/opensearch-project/opensearch-build',
'Test GH issue title',
'Test GH issue close comment',
'label101',
))
super.testPipeline("tests/jenkins/jobs/CloseGithubIssue_JenkinsFile")
// 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 label101 --json number --jq '.[0].number', returnStdout=true}"))
// assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue close bbb\nccc -R opensearch-project/opensearch-build --comment \"Test GH issue close comment\", returnStdout=true}"))
}

def getCommands(method, text) {
def shCommands = helper.callStack.findAll { call ->
call.methodName == method
}.collect { call ->
callArgsToString(call)
}.findAll { command ->
command.contains(text)
}
return shCommands
}

}
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 bbb\nccc --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 bbb\nccc --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 @@ -14,7 +14,7 @@ pipeline {
steps {
script {
try {
buildFailureMessage()
buildMessage(search: 'pass')
currentBuild.result = 'SUCCESS'
} catch (Exception err) {
//https://github.com/jenkinsci/JenkinsPipelineUnit/issues/509
Expand Down
6 changes: 6 additions & 0 deletions tests/jenkins/jobs/BuildMessage_Jenkinsfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BuildMessage_Jenkinsfile.run()
BuildMessage_Jenkinsfile.pipeline(groovy.lang.Closure)
BuildMessage_Jenkinsfile.echo(Executing on agent [label:none])
BuildMessage_Jenkinsfile.stage(notify, groovy.lang.Closure)
BuildMessage_Jenkinsfile.script(groovy.lang.Closure)
BuildMessage_Jenkinsfile.buildMessage({search=pass})
24 changes: 24 additions & 0 deletions tests/jenkins/jobs/CloseBuildSuccessGithubIssue_Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.
*/

pipeline {
agent none
stages {
stage('notify') {
steps {
script {
closeBuildSuccessGithubIssue(
message: ["Build successful OpenSearch"],
inputManifestPath: 'tests/data/opensearch-2.0.0.yml'
)
}
}
}
}
}
18 changes: 18 additions & 0 deletions tests/jenkins/jobs/CloseBuildSuccessGithubIssue_Jenkinsfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CloseBuildSuccessGithubIssue_Jenkinsfile.run()
CloseBuildSuccessGithubIssue_Jenkinsfile.pipeline(groovy.lang.Closure)
CloseBuildSuccessGithubIssue_Jenkinsfile.echo(Executing on agent [label:none])
CloseBuildSuccessGithubIssue_Jenkinsfile.stage(notify, groovy.lang.Closure)
CloseBuildSuccessGithubIssue_Jenkinsfile.script(groovy.lang.Closure)
CloseBuildSuccessGithubIssue_Jenkinsfile.closeBuildSuccessGithubIssue({message=[Build successful OpenSearch], inputManifestPath=tests/data/opensearch-2.0.0.yml})
closeBuildSuccessGithubIssue.legacySCM(groovy.lang.Closure)
closeBuildSuccessGithubIssue.library({identifier=jenkins@main, retriever=null})
closeBuildSuccessGithubIssue.readYaml({file=tests/data/opensearch-2.0.0.yml})
closeBuildSuccessGithubIssue.closeGithubIssue({repoUrl=https://github.com/opensearch-project/OpenSearch.git, issueTitle=[AUTOCUT] Distribution Build Failed for OpenSearch-2.0.0, closeComment=Closing the issue as the distribution build for OpenSearch has passed for version: **2.0.0**.
Please see build log at www.example.com/jobs/test/123/consoleFull, label=autocut,v2.0.0})
closeGithubIssue.usernamePassword({credentialsId=jenkins-github-bot-token, passwordVariable=GITHUB_TOKEN, usernameVariable=GITHUB_USER})
closeGithubIssue.withCredentials([[GITHUB_USER, GITHUB_TOKEN]], groovy.lang.Closure)
closeGithubIssue.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})
closeGithubIssue.sh({script=gh issue close bbb
ccc -R opensearch-project/OpenSearch --comment "Closing the issue as the distribution build for OpenSearch has passed for version: **2.0.0**.
Please see build log at www.example.com/jobs/test/123/consoleFull", returnStdout=true})
closeBuildSuccessGithubIssue.sleep({time=3, unit=SECONDS})
11 changes: 11 additions & 0 deletions tests/jenkins/jobs/CloseGithubIssue_JenkinsFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CloseGithubIssue_JenkinsFile.run()
CloseGithubIssue_JenkinsFile.pipeline(groovy.lang.Closure)
CloseGithubIssue_JenkinsFile.echo(Executing on agent [label:none])
CloseGithubIssue_JenkinsFile.stage(closeGithubIssue, groovy.lang.Closure)
CloseGithubIssue_JenkinsFile.script(groovy.lang.Closure)
CloseGithubIssue_JenkinsFile.closeGithubIssue({repoUrl=https://github.com/opensearch-project/opensearch-build, issueTitle=Test GH issue title, closeComment=Test GH issue close comment, label=label101})
closeGithubIssue.usernamePassword({credentialsId=jenkins-github-bot-token, passwordVariable=GITHUB_TOKEN, usernameVariable=GITHUB_USER})
closeGithubIssue.withCredentials([[GITHUB_USER, GITHUB_TOKEN]], groovy.lang.Closure)
closeGithubIssue.sh({script=gh issue list --repo https://github.com/opensearch-project/opensearch-build -S "Test GH issue title in:title" --label label101 --json number --jq '.[0].number', returnStdout=true})
closeGithubIssue.sh({script=gh issue close bbb
ccc -R opensearch-project/opensearch-build --comment "Test GH issue close comment", returnStdout=true})
26 changes: 26 additions & 0 deletions tests/jenkins/jobs/CloseGithubIssue_Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*/

pipeline {
agent none
stages {
stage('closeGithubIssue') {
steps {
script {
closeGithubIssue(
repoUrl: 'https://github.com/opensearch-project/opensearch-build',
issueTitle: 'Test GH issue title',
closeComment: 'Test GH issue close comment',
label: 'label101'
)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@
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})
5 changes: 4 additions & 1 deletion tests/jenkins/jobs/CreateGithubIssueExisting_JenkinsFile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
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})
31 changes: 0 additions & 31 deletions tests/jenkins/lib-testers/BuildFailureMessageLibTester.groovy

This file was deleted.

36 changes: 36 additions & 0 deletions tests/jenkins/lib-testers/BuildMessageLibTester.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.
*/
import static org.hamcrest.CoreMatchers.notNullValue
import static org.hamcrest.CoreMatchers.nullValue
import static org.hamcrest.MatcherAssert.assertThat

class BuildMessageLibTester extends LibFunctionTester {

private String search

public BuildMessageLibTester(search) {
this.search = search
}


void parameterInvariantsAssertions(call) {
assertThat(call.args.search.first(), notNullValue())
}

boolean expectedParametersMatcher(call) {
return call.args.search.first().toString().equals(this.search)
}

String libFunctionName() {
return 'buildMessage'
}

void configure(helper, binding){
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.
*/
import static org.hamcrest.CoreMatchers.notNullValue
import static org.hamcrest.CoreMatchers.nullValue
import static org.hamcrest.MatcherAssert.assertThat

class CloseBuildSuccessGithubIssueLibTester extends LibFunctionTester{
private List<String> message

public CloseBuildSuccessGithubIssueLibTester(message){
this.message = message
}

@Override
String libFunctionName() {
return 'closeBuildSuccessGithubIssue'
}

@Override
void parameterInvariantsAssertions(Object call) {
assertThat(call.args.message.first(), notNullValue())
}

@Override
boolean expectedParametersMatcher(Object call) {
return call.args.message.first().equals(this.message)
}

@Override
void configure(Object helper, Object binding) {
helper.registerAllowedMethod('withCredentials', [Map])
helper.registerAllowedMethod('sleep', [Map])
binding.setVariable('BUILD_URL', 'www.example.com/jobs/test/123/')
}
}
Loading

0 comments on commit 70507a0

Please sign in to comment.