forked from opensearch-project/opensearch-build-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestCreateGithubIssue.groovy
132 lines (121 loc) · 6.91 KB
/
TestCreateGithubIssue.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* 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
import com.lesfurets.jenkins.unit.*
class TestCreateGithubIssue extends BuildPipelineTest {
@Override
@Before
void setUp() {
super.setUp()
}
@Test
void testCreateGithubIssueComment() {
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" --label label101 --json number --jq '.[0].number'""") { script ->
return [stdout: "22", 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 ->
return [stdout: "", exitValue: 0]
}
super.testPipeline('tests/jenkins/jobs/CreateGithubIssue_Jenkinsfile')
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}"""))
}
void testCreateGithubIssueCreate() {
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" --label label101 --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 ->
return [stdout: "", exitValue: 0]
}
super.testPipeline('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}"))
}
void testCreateGithubIssueReOpen() {
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" --label label101 --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 ->
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}"))
}
void testCreateGithubIssueReOpenWithDays() {
this.registerLibTester(new CreateGithubIssueLibTester(
"https://github.com/opensearch-project/opensearch-build",
"Test GH issue title",
"Test GH issue body",
"label101",
"5"
))
super.testPipeline('tests/jenkins/jobs/CreateGithubIssue_Jenkinsfile')
assertThat(getCommands('sh', 'script'), hasItem("""{script=date -d "5 days ago" +'%Y-%m-%d'}"""))
}
@Test
void testCreateGithubIssueWithBodyFile() {
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 ->
return [stdout: "22", 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 ->
return [stdout: "", exitValue: 0]
}
super.testPipeline('tests/jenkins/jobs/EditGithubIssue_Jenkinsfile', 'tests/jenkins/jobs/EditGithubIssue_Jenkinsfile_IssueBody')
assertThat(getCommands('println', ''), hasItem("Issue already exists, editing the issue body"))
assertThat(getCommands('sh', 'script'), hasItem("""{script=gh issue edit bbb\nccc --repo https://github.com/opensearch-project/opensearch-build --body-file issueBody.md, 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
}
}