Skip to content

Commit 16bc0ed

Browse files
author
Pavel Alexeev
committed
DATA-2799 Fix error with jira field specification by pair
1 parent 72a5401 commit 16bc0ed

File tree

4 files changed

+90
-11
lines changed

4 files changed

+90
-11
lines changed

src/main/groovy/info/hubbitus/DTO/AlertContext.groovy

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,20 @@ class AlertContext {
122122
Map<String, JiraFieldMap> res = alert.params
123123
.findAll{it.key.startsWith(JIRA_FIELD_KEY_PREFIX) }
124124
.collectEntries { param ->
125-
println(param)
126125
JiraFieldMap field = new JiraFieldMap(name: param.key - JIRA_FIELD_KEY_PREFIX)
127126

128127
switch (true) {
129128
case field.name.startsWith('name__'): // Name/value pair. E.g. jira__field__name__2: 'Итоговый результат'/jira__field__value__2: 'Some result description (описание результата)'
130-
field.name = field.name - 'name__'
131-
field.rawValue = alert.params."${JIRA_FIELD_KEY_PREFIX}__value__${field.name}"
129+
String valueKey = "${JIRA_FIELD_KEY_PREFIX}value__${field.name - 'name__'}"
130+
field.name = param.value
131+
field.rawValue = alert.params[valueKey]
132132
field.meta = (CimFieldInfo)jiraMetaFields.find{ CimFieldInfo it -> field.name == it.name}
133133
if (!field.meta) {
134-
throw new IllegalStateException("Name/value pair used for get field: field.name=[${field.name}], field.value[${field.value}], but no metadata field found for this pair! Please check specification")
134+
throw new IllegalStateException("Name/value pair used to get field: field.name=[${field.name}], field.value[${field.value}], but no metadata field found for this pair! Please check specification")
135135
}
136136
break
137-
138-
case field.name.startsWith('value__'):
139-
break
137+
case field.name.startsWith('value__'): // Pair to the 'name__', skipping
138+
return
140139

141140
case field.name.startsWith('customId'):
142141
def customFieldId = field.name - 'customId__'

src/main/groovy/info/hubbitus/JiraService.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class JiraService {
9999

100100
def commentIssue(Issue issue, AlertContext alerting){
101101
String comment = alerting.field(JIRA__COMMENT_IN_PRESENT_ISSUES.key)
102-
log.info("Add comment on the issue ${jiraURL}browse/${issue.key} «${issue.summary}»: ${comment}")
102+
log.info("Add comment on the issue [${jiraURL}browse/${issue.key} «${issue.summary}»]: ${comment}")
103103
issueClient.addComment(
104104
issue.getCommentsUri(),
105105
Comment.valueOf(comment)
@@ -108,7 +108,7 @@ class JiraService {
108108

109109
BasicIssue createIssue(AlertContext alerting){
110110
BasicIssue res = issueClient.createIssue(createIssueInput(alerting)).claim()
111-
log.info("Issue created: ${res.key}")
111+
log.info("Issue created: ${jiraURL}browse/${res.key}")
112112
return res
113113
}
114114

@@ -156,7 +156,7 @@ class JiraService {
156156
}
157157

158158
/**
159-
* @link com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder#toListOfComplexIssueInputFieldValueWithSingleKey(java.lang.Iterable, java.lang.String). Copy/paste. Unfortunately that is provate
159+
* @link com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder#toListOfComplexIssueInputFieldValueWithSingleKey(java.lang.Iterable, java.lang.String). Copy/paste. Unfortunately that is private
160160
**/
161161
private static <T> Iterable<ComplexIssueInputFieldValue> toListOfComplexIssueInputFieldValueWithSingleKey(final Iterable<T> items, final String key) {
162162
return Iterables.transform(items, new Function<T, ComplexIssueInputFieldValue>() {

src/test/java/info/hubbitus/AlertControllerTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void testPingEndpoint() {
2929
}
3030

3131
@Test
32-
void testSimpleJiraClient() throws IOException {
32+
void testJiraClientSmall() throws IOException {
3333
given()
3434
.contentType("application/json")
3535
.request().body(contentResourceFile("/alert-sample.small.json5"))
@@ -39,6 +39,25 @@ void testSimpleJiraClient() throws IOException {
3939
.statusCode(200);
4040
}
4141

42+
/**
43+
* Just similar to {@see testJiraClientSmall}, but in JSON used pair:
44+
* "jira__field__name__1": "Component/s",
45+
* "jira__field__value__1": "DQ-issues+alerts, DevOps+infrastructure",
46+
* instead of simple:
47+
* "jira__field__component_s": "DQ-issues+alerts, DevOps+infrastructure"
48+
* @throws IOException
49+
*/
50+
@Test
51+
void testJiraClientSmallFieldPairs() throws IOException {
52+
given()
53+
.contentType("application/json")
54+
.request().body(contentResourceFile("/alert-sample.small.field_pairs.json5"))
55+
.when()
56+
.post("/alert")
57+
.then()
58+
.statusCode(200);
59+
}
60+
4261
/* *********************
4362
* Helper methods
4463
******************** */
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"receiver": "jira-data",
3+
"status": "firing",
4+
"alerts": [
5+
{
6+
"status": "firing",
7+
"labels": {
8+
"alertname": "DataTest1",
9+
"code": "200",
10+
"global_label_1": "2",
11+
"instance": "localhost:9090",
12+
"jira__field__priority": "High",
13+
"jira__issue_type_name": "Alert",
14+
"jira__project_key": "DATA",
15+
"job": "prometheus",
16+
"qaz1_label": "qaz1_label_value",
17+
"qaz2_label": "qaz2_label_value",
18+
"severity": "warning"
19+
},
20+
"annotations": {
21+
"description": "Some description QAZ2\nof DataTest0 alert\nVALUE: 3\n",
22+
"jira__field__assignee": "plalexeev",
23+
"jira__field__name__1": "Component/s",
24+
"jira__field__value__1": "DQ-issues+alerts, DevOps+infrastructure",
25+
"jira__issue_type_name": "Alert",
26+
"jira__project_key": "DATA",
27+
"qaz1_annotation": "qaz1_annotation_value",
28+
"jira__field__labels": "label_one, labelTwo, label:three, severity:${context.field('severity')}",
29+
"summary": "DataTest1 summary",
30+
"value": "3",
31+
"jira__comment_in_present_issues": "Looks like error happened again!"
32+
},
33+
"startsAt": "2023-11-05T23:05:29.64Z",
34+
"endsAt": "0001-01-01T00:00:00Z",
35+
"generatorURL": "http://89cffcee4bdb:9090/graph?g0.expr=promhttp_metric_handler_requests_total+%3E+1&g0.tab=1",
36+
"fingerprint": "97584ea0f197fa9a"
37+
}
38+
],
39+
"groupLabels": {
40+
"instance": "localhost:9090"
41+
},
42+
"commonLabels": {
43+
"code": "200",
44+
"instance": "localhost:9090",
45+
"jira__field__priority": "High",
46+
"jira__issue_type_name": "Alert",
47+
"jira__project_key": "DATA",
48+
"job": "prometheus",
49+
"qaz1_label": "qaz1_label_value",
50+
"severity": "warning"
51+
},
52+
"commonAnnotations": {
53+
"jira__field__assignee": "plalexeev",
54+
"qaz1_annotation": "qaz1_annotation_value"
55+
},
56+
"externalURL": "http://f856ff453070:9093",
57+
"version": "4",
58+
"groupKey": "{}:{instance=\"localhost:9090\"}",
59+
"truncatedAlerts": 0
60+
}
61+

0 commit comments

Comments
 (0)