From c48b88b76b409111a63055520b2252ed1fecd7ef Mon Sep 17 00:00:00 2001 From: Sayali Gaikawad Date: Fri, 25 Oct 2024 13:51:24 -0700 Subject: [PATCH] Fix test cases Signed-off-by: Sayali Gaikawad --- tests/jenkins/TestCreateGithubIssue.groovy | 28 ++++++++++++---------- vars/createGithubIssue.groovy | 27 +++++++++++---------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/tests/jenkins/TestCreateGithubIssue.groovy b/tests/jenkins/TestCreateGithubIssue.groovy index 5965e14c..fb1f8d6d 100644 --- a/tests/jenkins/TestCreateGithubIssue.groovy +++ b/tests/jenkins/TestCreateGithubIssue.groovy @@ -47,7 +47,8 @@ class TestCreateGithubIssue extends BuildPipelineTest { assertThat(getCommands('println', ''), hasItem("Issue already exists, adding a comment")) 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}""")) } - + + @Test void testCreateGithubIssueCreate() { this.registerLibTester(new CreateGithubIssueLibTester( "https://github.com/opensearch-project/opensearch-build", @@ -58,10 +59,10 @@ class TestCreateGithubIssue extends BuildPipelineTest { helper.addShMock("date -d \"5 days ago\" +'%Y-%m-%d'") { script -> return [stdout: "2023-10-24", exitValue: 0] } - helper.addShMock("""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'""") { script -> + helper.addShMock("""gh issue list --repo https://github.com/opensearch-project/opensearch-build -S "Test GH issue title in:title" --json number --jq '.[0].number'""") { script -> return [stdout: "", exitValue: 0] } - helper.addShMock("""gh issue list --repo https://github.com/opensearch-project/opensearch-build -S "Test GH issue title in:title is:closed closed:>=2023-10-24" --label label101 --json number --jq '.[0].number'""") { script -> + helper.addShMock("""gh issue list --repo https://github.com/opensearch-project/opensearch-build -S "Test GH issue title in:title is:closed closed:>=2023-10-24" --json number --jq '.[0].number'""") { script -> return [stdout: "", exitValue: 0] } super.testPipeline('tests/jenkins/jobs/CreateGithubIssue_Jenkinsfile') @@ -69,31 +70,33 @@ class TestCreateGithubIssue extends BuildPipelineTest { assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue create --title \"Test GH issue title\" --body \"Test GH issue body\" --label \"label101\" --label \"untriaged\" --repo https://github.com/opensearch-project/opensearch-build, returnStdout=true}")) } + @Test void testCreateGithubIssueCreateWithMissingLabel() { this.registerLibTester(new CreateGithubIssueLibTester( "https://github.com/opensearch-project/opensearch-build", "Test GH issue title", "Test GH issue body", - "label101,missedOne" + "label101" )) helper.addShMock("date -d \"5 days ago\" +'%Y-%m-%d'") { script -> return [stdout: "2023-10-24", exitValue: 0] } - helper.addShMock("""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'""") { script -> + helper.addShMock("""gh issue list --repo https://github.com/opensearch-project/opensearch-build -S "Test GH issue title in:title" --json number --jq '.[0].number'""") { script -> return [stdout: "", exitValue: 0] } - helper.addShMock("""gh issue list --repo https://github.com/opensearch-project/opensearch-build -S "Test GH issue title in:title is:closed closed:>=2023-10-24" --label label101 --json number --jq '.[0].number'""") { script -> + helper.addShMock("""gh issue list --repo https://github.com/opensearch-project/opensearch-build -S "Test GH issue title in:title is:closed closed:>=2023-10-24" --json number --jq '.[0].number'""") { script -> return [stdout: "", exitValue: 0] } - helper.addShMock("""gh label list --repo https://github.com/opensearch-project/opensearch-build -S "missedOne" --json name --jq '.[0].name'""") { script -> + helper.addShMock("""gh label list --repo https://github.com/opensearch-project/opensearch-build -S "label101" --json name --jq '.[0].name'""") { script -> return [stdout: "no labels in opensearch-project/opensearch-build matched your search", exitValue: 0] } super.testPipeline('tests/jenkins/jobs/CreateGithubIssue_Jenkinsfile') assertThat(getCommands('println', ''), hasItem("Creating new issue")) - assertThat(getCommands('sh', 'script'), hasItem("{script=gh label create missedOne --repo https://github.com/opensearch-project/opensearch-build, returnStdout=true}")) + assertThat(getCommands('sh', 'script'), hasItem("{script=gh label create label101 --repo https://github.com/opensearch-project/opensearch-build, returnStdout=true}")) assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue create --title \"Test GH issue title\" --body \"Test GH issue body\" --label \"label101\" --label \"untriaged\" --repo https://github.com/opensearch-project/opensearch-build, returnStdout=true}")) } + @Test void testCreateGithubIssueReOpen() { this.registerLibTester(new CreateGithubIssueLibTester( "https://github.com/opensearch-project/opensearch-build", @@ -104,18 +107,19 @@ class TestCreateGithubIssue extends BuildPipelineTest { helper.addShMock("date -d \"5 days ago\" +'%Y-%m-%d'") { script -> return [stdout: "2023-10-24", exitValue: 0] } - helper.addShMock("""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'""") { script -> + helper.addShMock("""gh issue list --repo https://github.com/opensearch-project/opensearch-build -S "Test GH issue title in:title" --json number --jq '.[0].number'""") { script -> return [stdout: "", exitValue: 0] } - helper.addShMock("""gh issue list --repo https://github.com/opensearch-project/opensearch-build -S "Test GH issue title in:title is:closed closed:>=2023-10-24" --label label101 --json number --jq '.[0].number'""") { script -> + helper.addShMock("""gh issue list --repo https://github.com/opensearch-project/opensearch-build -S "Test GH issue title in:title is:closed closed:>=2023-10-24" --json number --jq '.[0].number'""") { script -> return [stdout: "22", exitValue: 0] } super.testPipeline('tests/jenkins/jobs/CreateGithubIssue_Jenkinsfile') assertThat(getCommands('println', ''), hasItem("Re-opening a recently closed issue and commenting on it")) assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue reopen --repo https://github.com/opensearch-project/opensearch-build 22, returnStdout=true}")) - assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue comment 22 --repo https://github.com/opensearch-project/opensearch-build --body \"Test GH issue body\", returnStdout=true}")) + assertThat(getCommands('sh', 'script'), hasItem("{script=gh issue comment 22 --repo https://github.com/opensearch-project/opensearch-build --body \"Test GH issue body\", returnStdout=true}")) } + @Test void testCreateGithubIssueReOpenWithDays() { this.registerLibTester(new CreateGithubIssueLibTester( "https://github.com/opensearch-project/opensearch-build", @@ -125,7 +129,7 @@ class TestCreateGithubIssue extends BuildPipelineTest { "5" )) super.testPipeline('tests/jenkins/jobs/CreateGithubIssue_Jenkinsfile') - assertThat(getCommands('sh', 'script'), hasItem("""{script=date -d "5 days ago" +'%Y-%m-%d'}""")) + assertThat(getCommands('sh', 'script'), hasItem("{script=date -d \"5 days ago\" +'%Y-%m-%d', returnStdout=true}")) } @Test diff --git a/vars/createGithubIssue.groovy b/vars/createGithubIssue.groovy index f30f10f7..84b356a0 100644 --- a/vars/createGithubIssue.groovy +++ b/vars/createGithubIssue.groovy @@ -76,18 +76,21 @@ void call(Map args = [:]) { } else { println("Creating new issue") - def labelName = sh( - script: "gh label list --repo ${args.repoUrl} -S ${i} --json name --jq '.[0].name'", - returnStdout: true - ).trim() - if (labelName.equals(i.trim())) { - println("Label ${i} already exists. Skipping label creation") - } else { - println("${i} label is missing. Creating the missing label") - sh( - script: "gh label create ${i} --repo ${args.repoUrl}", - returnStdout: true - ) + List allLabels = Arrays.asList(label.split(',')) + allLabels.each { i -> + def labelName = sh( + script: "gh label list --repo ${args.repoUrl} -S ${i} --json name --jq '.[0].name'", + returnStdout: true + ).trim() + if (labelName.equals(i.trim())) { + println("Label ${i} already exists. Skipping label creation") + } else { + println("${i} label is missing. Creating the missing label") + sh( + script: "gh label create ${i} --repo ${args.repoUrl}", + returnStdout: true + ) + } } sh( script: "gh issue create --title \"${args.issueTitle}\" ${bodyOption} --label \"${label}\" --label \"untriaged\" --repo ${args.repoUrl}",