-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
FakeJiraRunner.groovy
89 lines (74 loc) · 2.55 KB
/
FakeJiraRunner.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
package com.ceilfors.jenkins.plugins.jiratrigger.integration
import groovy.json.JsonSlurper
import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient
/**
* @author ceilfors
*/
class FakeJiraRunner implements JiraRunner {
public static final String CUSTOM_FIELD_NAME = 'My Customer Custom Field'
private final RESTClient restClient
private int id = 1
private final Map<String, FakeIssue> issueMap = [:]
FakeJiraRunner(JenkinsRunner jenkinsRunner) {
restClient = createRestClient(jenkinsRunner.webhookUrl)
}
@Override
String createIssue() {
createIssue('')
}
@Override
String createIssue(String description) {
String issueKey = "TEST-$id"
id++
issueMap[issueKey] = new FakeIssue(issueKey: issueKey, description: description)
issueKey
}
@Override
void updateDescription(String issueKey, String description) {
issueMap[issueKey].description = description
Map body = createPostBody('updateDescription', issueKey)
body.changelog.items[0].toString = description
restClient.post(body: body)
}
@Override
void updateStatus(String issueKey, String status) {
Map body = createPostBody('updateStatus', issueKey)
body.changelog.items[0].toString = status
restClient.post(body: body)
}
@Override
void updateCustomField(String issueKey, String fieldName, String value) {
Map body = createPostBody('updateCustomField', issueKey)
body.changelog.items[0].field = fieldName
body.changelog.items[0].toString = value
restClient.post(body: body)
}
@Override
void addComment(String issueKey, String comment) {
Map body = createPostBody('addComment', issueKey)
body.comment.body = comment
restClient.post(body: body)
}
@Override
boolean validateIssueKey(String issueKey, String jqlFilter) {
true
}
private RESTClient createRestClient(String jenkinsUrl) {
new RESTClient(jenkinsUrl, ContentType.JSON)
}
protected Map createPostBody(String method, String issueKey) {
def slurper = new JsonSlurper()
Map body = slurper.parse(new FileReader(new File(this.class.getResource("${method}.json").toURI()))) as Map
body.issue.key = issueKey
body.issue.fields.description = issueMap[issueKey].description
body
}
protected RESTClient getRestClient() {
restClient
}
protected static class FakeIssue {
String issueKey
String description
}
}