Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add library to add and remove GH issue labels #377

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions tests/jenkins/TestUpdateGitHubIssueLabels.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* 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.CoreMatchers.hasItems
import static org.hamcrest.CoreMatchers.not
import static org.hamcrest.MatcherAssert.assertThat
import static org.junit.jupiter.api.Assertions.assertThrows


class TestUpdateGitHubIssueLabels extends BuildPipelineTest {

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

@Test
void testIssueDoesNotExist() {
this.registerLibTester(new updateGitHubIssueLabelsLibTester(
"https://github.com/opensearch-project/opensearch-build",
"Test GH issue title",
"label101,label102",
"add"
))
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]
}
super.testPipeline('tests/jenkins/jobs/UpdateGitHubIssueLabels_Jenkinsfile')
assertThat(getCommands('println', ''), hasItem('No open issues found for https://github.com/opensearch-project/opensearch-build'))
}

@Test
void testLabelCreation() {
this.registerLibTester(new updateGitHubIssueLabelsLibTester(
"https://github.com/opensearch-project/opensearch-build",
"Test GH issue title",
"label101,label102",
"add"
))
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/UpdateGitHubIssueLabels_Jenkinsfile')
assertThat(getCommands('sh', 'script'), hasItem("{script=gh label create label101 --repo https://github.com/opensearch-project/opensearch-build, returnStdout=true}"))
}

@Test
void testExistingLabelsAddition() {
this.registerLibTester(new updateGitHubIssueLabelsLibTester(
"https://github.com/opensearch-project/opensearch-build",
"Test GH issue title",
"label101,label102",
"add"
))
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: "67", 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: "label101", exitValue: 0]
}
helper.addShMock("""gh label list --repo https://github.com/opensearch-project/opensearch-build -S label102 --json name --jq '.[0].name'""") { script ->
return [stdout: "label102", exitValue: 0]
}
runScript('tests/jenkins/jobs/UpdateGitHubIssueLabels_Jenkinsfile')
assertThat(getCommands('sh', 'script'), not(hasItem('{script=gh label create label101 --repo https://github.com/opensearch-project/opensearch-build, returnStdout=true}')))
assertThat(getCommands('sh', 'script'), not(hasItem('{script=gh label create label102 --repo https://github.com/opensearch-project/opensearch-build, returnStdout=true}')))
assertThat(getCommands('sh', 'script'), hasItems('{script=gh issue edit 67 -R https://github.com/opensearch-project/opensearch-build --add-label \"label101\", returnStdout=true}', '{script=gh issue edit 67 -R https://github.com/opensearch-project/opensearch-build --add-label \"label102\", returnStdout=true}'))
}

@Test
void testSkippingLabelCreationForRemove() {
this.registerLibTester(new updateGitHubIssueLabelsLibTester(
"https://github.com/opensearch-project/opensearch-build",
"Test GH issue title",
"label101",
"remove"
))
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/UpdateGitHubIssueLabels_Removal_Jenkinsfile')
assertThat(getCommands('println', 'label'), hasItem('label101 label does not exist. Skipping the label removal'))
}

@Test
void testLabelRemoval() {
this.registerLibTester(new updateGitHubIssueLabelsLibTester(
"https://github.com/opensearch-project/opensearch-build",
"Test GH issue title",
"label101",
"remove"
))
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: "67", 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: "label101", exitValue: 0]
}
runScript('tests/jenkins/jobs/UpdateGitHubIssueLabels_Removal_Jenkinsfile')
assertThat(getCommands('sh', 'script'), hasItem('{script=gh issue edit 67 -R https://github.com/opensearch-project/opensearch-build --remove-label \"label101\", returnStdout=true}'))
}

@Test
void testFailure(){
this.registerLibTester(new updateGitHubIssueLabelsLibTester(
"https://github.com/opensearch-project/opensearch-build",
"Test GH issue title",
"label101,label102",
"add"
))
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: "Wrong credentials", exitValue: 127]
}
assertThrows(Exception) {
runScript('tests/jenkins/jobs/UpdateGitHubIssueLabels_Jenkinsfile')
}
assertThat(getCommands('error', ''), hasItem('Unable to edit GitHub issue for https://github.com/opensearch-project/opensearch-build, Script returned error code: 127'))
}
@Test
void testAction(){
this.registerLibTester(new updateGitHubIssueLabelsLibTester(
"https://github.com/opensearch-project/opensearch-build",
"Test GH issue title",
"label101",
"delete"
))
runScript('tests/jenkins/jobs/UpdateGitHubIssueLabels_Delete_Jenkinsfile')
assertThat(getCommands('error', ''), hasItem("Invalid action 'delete' specified. Valid values: add, remove"))
assertJobStatusFailure()
}

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

26 changes: 26 additions & 0 deletions tests/jenkins/jobs/UpdateGitHubIssueLabels_Delete_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('updateGitHubIssueLabels') {
steps {
script {
updateGitHubIssueLabels(
repoUrl: "https://github.com/opensearch-project/opensearch-build",
issueTitle: "Test GH issue title",
label: "label101",
action: "delete"
)
}
}
}
}
}
26 changes: 26 additions & 0 deletions tests/jenkins/jobs/UpdateGitHubIssueLabels_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('updateGitHubIssueLabels') {
steps {
script {
updateGitHubIssueLabels(
repoUrl: "https://github.com/opensearch-project/opensearch-build",
issueTitle: "Test GH issue title",
label: "label101,label102",
action: "add"
)
}
}
}
}
}
10 changes: 10 additions & 0 deletions tests/jenkins/jobs/UpdateGitHubIssueLabels_Jenkinsfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
UpdateGitHubIssueLabels_Jenkinsfile.run()
UpdateGitHubIssueLabels_Jenkinsfile.pipeline(groovy.lang.Closure)
UpdateGitHubIssueLabels_Jenkinsfile.echo(Executing on agent [label:none])
UpdateGitHubIssueLabels_Jenkinsfile.stage(updateGitHubIssueLabels, groovy.lang.Closure)
UpdateGitHubIssueLabels_Jenkinsfile.script(groovy.lang.Closure)
UpdateGitHubIssueLabels_Jenkinsfile.updateGitHubIssueLabels({repoUrl=https://github.com/opensearch-project/opensearch-build, issueTitle=Test GH issue title, label=label101,label102, action=add})
updateGitHubIssueLabels.usernamePassword({credentialsId=jenkins-github-bot-token, passwordVariable=GITHUB_TOKEN, usernameVariable=GITHUB_USER})
updateGitHubIssueLabels.withCredentials([[GITHUB_USER, GITHUB_TOKEN]], groovy.lang.Closure)
updateGitHubIssueLabels.sh({script=gh issue list --repo https://github.com/opensearch-project/opensearch-build -S "Test GH issue title in:title" --json number --jq '.[0].number', returnStdout=true})
updateGitHubIssueLabels.println(No open issues found for https://github.com/opensearch-project/opensearch-build)
26 changes: 26 additions & 0 deletions tests/jenkins/jobs/UpdateGitHubIssueLabels_Removal_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('updateGitHubIssueLabels') {
steps {
script {
updateGitHubIssueLabels(
repoUrl: "https://github.com/opensearch-project/opensearch-build",
issueTitle: "Test GH issue title",
label: "label101",
action: "remove"
)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 updateGitHubIssueLabelsLibTester extends LibFunctionTester {

private String repoUrl
private String issueTitle
private String label
private String action

public updateGitHubIssueLabelsLibTester(repoUrl, issueTitle, label, action){
this.repoUrl = repoUrl
this.issueTitle = issueTitle
this.label = label
this.action = action
}

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

@Override
void parameterInvariantsAssertions(Object call) {
assertThat(call.args.repoUrl.first(), notNullValue())
assertThat(call.args.issueTitle.first(), notNullValue())
assertThat(call.args.label.first(), notNullValue())
assertThat(call.args.action.first(), notNullValue())
}

@Override
boolean expectedParametersMatcher(Object call) {
return call.args.repoUrl.first().equals(this.repoUrl)
&& call.args.issueTitle.first().equals(this.issueTitle)
&& call.args.label.first().equals(this.label)
&& call.args.action.first().equals(this.action)
}

@Override
void configure(Object helper, Object binding) {
helper.registerAllowedMethod('withCredentials', [Map])
}
}
Loading
Loading