Skip to content

Commit

Permalink
Merge pull request #47 from Snowflake-Labs/use-json-string-for-issue-…
Browse files Browse the repository at this point in the history
…desc

Use beautified JSON string for JIRA issue description.
  • Loading branch information
sfc-gh-pkommini authored Dec 13, 2022
2 parents 77f506d + 9abf663 commit 4cf4544
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 18 deletions.
5 changes: 5 additions & 0 deletions ef_jira.tf
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ resource "snowflake_function" "jira_handler" {
local.results_schema,
snowflake_external_function.snowalert_jira_api[0].name,
])
json_beautify_function = join(".", [
local.snowalert_database_name,
local.data_schema,
snowflake_function.json_beautify_with_indent.name,
])
}
)

Expand Down
59 changes: 59 additions & 0 deletions functions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,62 @@ javascript
module.snowalert_grants
]
}

resource "snowflake_function" "json_beautify_with_indent" {
provider = snowflake.security_alerting_role

database = local.snowalert_database_name
schema = local.data_schema
name = "JSON_BEAUTIFY"

arguments {
name = "STR"
type = "STRING"
}

arguments {
name = "INDENT"
type = "DOUBLE"
}

return_type = "STRING"
language = "javascript"
statement = <<javascript
try {
return JSON.stringify(JSON.parse(STR), null, INDENT);
} catch (err) {
return STR
}
javascript

depends_on = [
module.snowalert_grants
]
}

resource "snowflake_function" "json_beautify_without_indent" {
provider = snowflake.security_alerting_role

database = local.snowalert_database_name
schema = local.data_schema
name = "JSON_BEAUTIFY"

arguments {
name = "STR"
type = "STRING"
}

return_type = "STRING"
language = "javascript"
statement = <<javascript
${join(".", [
local.snowalert_database_name,
local.data_schema,
snowflake_function.json_beautify_with_indent.name,
])}.(STR, 2)
javascript

depends_on = [
module.snowalert_grants
]
}
38 changes: 20 additions & 18 deletions handler_functions_sql/jira_handler_v2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@ ${jira_api_function}(
'name', COALESCE(payload['issue_type'], '${default_jira_issue_type}')
),
'summary', alert['TITLE']::STRING,
'description', '|| Field || Value ||\r\n'
|| '|| Alert ID || ' || alert['ID']::STRING || ' ||\r\n'
|| '|| Query ID || ' || alert['QUERY_ID']::STRING || ' ||\r\n'
|| '|| Query Name || ' || alert['QUERY_NAME']::STRING || ' ||\r\n'
|| '|| Environment || ' || alert['ENVIRONMENT']::STRING || ' ||\r\n'
|| '|| Sources || ' || alert['SOURCES']::STRING || ' ||\r\n'
|| '|| Categories || ' || COALESCE(alert['CATEGORIES']::STRING, '-') || ' ||\r\n'
|| '|| Actor || ' || alert['ACTOR']::STRING || ' ||\r\n'
|| '|| Object || ' || alert['OBJECT']::STRING || ' ||\r\n'
|| '|| Action || ' || alert['ACTION']::STRING || ' ||\r\n'
|| '|| Title || ' || alert['TITLE']::STRING || ' ||\r\n'
|| '|| Event Time || ' || alert['EVENT_TIME']::STRING || ' ||\r\n'
|| '|| Alert Time || ' || alert['ALERT_TIME']::STRING || ' ||\r\n'
|| '|| Detector || ' || alert['DETECTOR']::STRING || ' ||\r\n'
|| '|| Severity || ' || alert['SEVERITY']::STRING || ' ||\r\n'
|| '|| Description || ' || alert['DESCRIPTION']::STRING || ' ||\r\n'
|| '|| Event Data || ' || alert['EVENT_DATA']::STRING || ' ||\r\n'

'description', ${json_beautify_function}(TO_JSON(
OBJECT_CONSTRUCT(
'Alert ID', alert['ID']::STRING,
'Query ID', alert['QUERY_ID']::STRING,
'Query Name', alert['QUERY_NAME']::STRING,
'Environment', alert['ENVIRONMENT']::STRING,
'Sources', alert['SOURCES']::STRING,
'Categories', COALESCE(alert['CATEGORIES']::STRING, '-'),
'Actor', alert['ACTOR']::STRING,
'Object', alert['OBJECT']::STRING,
'Action', alert['ACTION']::STRING,
'Title', alert['TITLE']::STRING,
'Event Time', alert['EVENT_TIME']::STRING,
'Alert Time', alert['ALERT_TIME']::STRING,
'Detector', alert['DETECTOR']::STRING,
'Severity', alert['SEVERITY']::STRING,
'Description', alert['DESCRIPTION']::STRING,
'Event Data', alert['EVENT_DATA']::STRING
), 4)
)
)
)
),
Expand Down

0 comments on commit 4cf4544

Please sign in to comment.