Skip to content

Commit

Permalink
Create GH label if one is missing while creating GH issues (opensearc…
Browse files Browse the repository at this point in the history
…h-project#551)

Signed-off-by: Sayali Gaikawad <[email protected]>
  • Loading branch information
gaiksaya authored Oct 25, 2024
1 parent c63a248 commit 054a0bc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
49 changes: 39 additions & 10 deletions tests/jenkins/TestCreateGithubIssue.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -58,17 +59,44 @@ 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')
runScript('tests/jenkins/jobs/CreateGithubIssue_Jenkinsfile')
assertThat(getCommands('println', ''), hasItem("Creating new issue"))
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"
))
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" --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" --json number --jq '.[0].number'""") { script ->
return [stdout: "", exitValue: 0]
}
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]
}
runScript('tests/jenkins/jobs/CreateGithubIssue_Jenkinsfile')
assertThat(getCommands('println', ''), hasItem("Creating new issue"))
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",
Expand All @@ -79,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')
runScript('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",
Expand All @@ -99,8 +128,8 @@ class TestCreateGithubIssue extends BuildPipelineTest {
"label101",
"5"
))
super.testPipeline('tests/jenkins/jobs/CreateGithubIssue_Jenkinsfile')
assertThat(getCommands('sh', 'script'), hasItem("""{script=date -d "5 days ago" +'%Y-%m-%d'}"""))
runScript('tests/jenkins/jobs/CreateGithubIssue_Jenkinsfile')
assertThat(getCommands('sh', 'script'), hasItem("{script=date -d \"5 days ago\" +'%Y-%m-%d', returnStdout=true}"))
}

@Test
Expand Down
16 changes: 16 additions & 0 deletions vars/createGithubIssue.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ void call(Map args = [:]) {
}
else {
println("Creating new issue")
List<String> 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}",
returnStdout: true
Expand Down

0 comments on commit 054a0bc

Please sign in to comment.