Skip to content

Commit 8dc983a

Browse files
Copilotarmiiller
andauthored
Add compact message mode to MS Teams integration (#148)
* Initial plan * Add compact_message option to MS Teams v3 and v4 integrations Co-authored-by: armiiller <9020194+armiiller@users.noreply.github.com> * Add tests and locale entries for compact_message option Co-authored-by: armiiller <9020194+armiiller@users.noreply.github.com> * Update the tests and spacing for the card * Update locales file --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: armiiller <9020194+armiiller@users.noreply.github.com> Co-authored-by: Austin Miller <austinrmiller1991@gmail.com>
1 parent c5c0ca5 commit 8dc983a

9 files changed

Lines changed: 243 additions & 112 deletions

File tree

app/models/pager_tree/integrations/channel/microsoft_teams/v3.rb

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ class Channel::MicrosoftTeams::V3 < Integration
77
{key: :alert_resolved, type: :boolean, default: false},
88
{key: :alert_dropped, type: :boolean, default: false},
99
{key: :outgoing_rules, type: :string, default: nil},
10-
{key: :time_zone, type: :string, default: nil}
10+
{key: :time_zone, type: :string, default: nil},
11+
{key: :compact_message, type: :boolean, default: false}
1112
]
1213
store_accessor :options, *OPTIONS.map { |x| x[:key] }.map(&:to_s), prefix: "option"
1314

@@ -22,6 +23,7 @@ class Channel::MicrosoftTeams::V3 < Integration
2223
self.option_alert_dropped ||= false
2324
self.option_outgoing_rules ||= ""
2425
self.option_time_zone ||= "UTC"
26+
self.option_compact_message ||= false
2527
end
2628

2729
def converts_to
@@ -82,40 +84,21 @@ def _alert
8284
end
8385

8486
def _blocks
87+
facts = if option_compact_message
88+
_compact_facts
89+
else
90+
_full_facts
91+
end
92+
8593
{
8694
"@type": "MessageCard",
8795
"@context": "http://schema.org/extensions",
8896
themeColor: _color,
8997
summary: _title,
9098
sections: [{
9199
activityTitle: _title,
92-
activitySubtitle: _alert.description&.try(:to_plain_text),
93-
facts: [
94-
{
95-
name: "Status",
96-
value: _alert.status.upcase
97-
},
98-
{
99-
name: "Urgency",
100-
value: _alert.urgency.upcase
101-
},
102-
{
103-
name: "Created",
104-
value: _alert.created_at.in_time_zone(option_time_zone).iso8601
105-
},
106-
{
107-
name: "Source",
108-
value: _alert.source&.name
109-
},
110-
{
111-
name: "Destinations",
112-
value: _alert.alert_destinations&.map { |d| d.destination.name }&.join(", ")
113-
},
114-
{
115-
name: "User",
116-
value: _alert.alert_responders&.where(role: :incident_commander)&.includes(account_user: :user)&.first&.account_user&.name
117-
}
118-
],
100+
activitySubtitle: option_compact_message ? nil : _alert.description&.try(:to_plain_text),
101+
facts: facts,
119102
markdown: true
120103
}],
121104
potentialAction: [
@@ -131,6 +114,48 @@ def _blocks
131114
}
132115
end
133116

117+
def _compact_facts
118+
[
119+
{
120+
name: "Status",
121+
value: _alert.status.upcase
122+
},
123+
{
124+
name: "User",
125+
value: _alert.alert_responders&.where(role: :incident_commander)&.includes(account_user: :user)&.first&.account_user&.name
126+
}
127+
]
128+
end
129+
130+
def _full_facts
131+
[
132+
{
133+
name: "Status",
134+
value: _alert.status.upcase
135+
},
136+
{
137+
name: "Urgency",
138+
value: _alert.urgency.upcase
139+
},
140+
{
141+
name: "Created",
142+
value: _alert.created_at.in_time_zone(option_time_zone).iso8601
143+
},
144+
{
145+
name: "Source",
146+
value: _alert.source&.name
147+
},
148+
{
149+
name: "Destinations",
150+
value: _alert.alert_destinations&.map { |d| d.destination.name }&.join(", ")
151+
},
152+
{
153+
name: "User",
154+
value: _alert.alert_responders&.where(role: :incident_commander)&.includes(account_user: :user)&.first&.account_user&.name
155+
}
156+
]
157+
end
158+
134159
def _title
135160
return @_title if @_title.present?
136161

app/models/pager_tree/integrations/channel/microsoft_teams/v4.rb

Lines changed: 127 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ class Channel::MicrosoftTeams::V4 < Integration
77
{key: :alert_resolved, type: :boolean, default: false},
88
{key: :alert_dropped, type: :boolean, default: false},
99
{key: :outgoing_rules, type: :string, default: nil},
10-
{key: :time_zone, type: :string, default: nil}
10+
{key: :time_zone, type: :string, default: nil},
11+
{key: :compact_message, type: :boolean, default: false}
1112
]
1213
store_accessor :options, *OPTIONS.map { |x| x[:key] }.map(&:to_s), prefix: "option"
1314

@@ -22,6 +23,7 @@ class Channel::MicrosoftTeams::V4 < Integration
2223
self.option_alert_dropped ||= false
2324
self.option_outgoing_rules ||= ""
2425
self.option_time_zone ||= "UTC"
26+
self.option_compact_message ||= false
2527
end
2628

2729
def adapter_supports_incoming?
@@ -78,6 +80,12 @@ def _alert
7880
end
7981

8082
def _blocks
83+
body = if option_compact_message
84+
_compact_body
85+
else
86+
_full_body
87+
end
88+
8189
{
8290
type: "message",
8391
attachments: [
@@ -86,105 +94,140 @@ def _blocks
8694
contentUrl: nil,
8795
content: {
8896
type: "AdaptiveCard",
89-
body: [
97+
body: body,
98+
actions: [
9099
{
91-
type: "Container",
92-
backgroundImage: _color,
93-
items: [
94-
{
95-
type: "TextBlock",
96-
size: "Large",
97-
weight: "Bolder",
98-
text: _title
99-
},
100-
{
101-
type: "ColumnSet",
102-
columns: [
103-
{
104-
type: "Column",
105-
items: [
106-
{
107-
type: "TextBlock",
108-
weight: "Bolder",
109-
text: _title,
110-
wrap: true
111-
},
112-
{
113-
type: "TextBlock",
114-
spacing: "None",
115-
text: "Created #{_alert.created_at.in_time_zone(option_time_zone).iso8601}",
116-
wrap: true
117-
}
118-
],
119-
width: "stretch"
120-
}
121-
]
122-
}
123-
]
124-
},
100+
type: "Action.OpenUrl",
101+
title: "View",
102+
url: Rails.application.routes.url_helpers.try(:alert_url, _alert, script_name: "/#{_alert.account_id}"),
103+
style: "positive"
104+
}
105+
],
106+
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
107+
version: "1.2"
108+
}
109+
}
110+
]
111+
}
112+
end
113+
114+
def _compact_body
115+
[
116+
{
117+
type: "Container",
118+
backgroundImage: _color,
119+
items: [
120+
{
121+
type: "TextBlock",
122+
size: "Large",
123+
weight: "Bolder",
124+
text: _title
125+
},
126+
{
127+
type: "FactSet",
128+
facts: [
125129
{
126-
type: "Container",
127-
items: [
128-
{
129-
type: "FactSet",
130-
facts: [
131-
{
132-
title: "Status:",
133-
value: _alert.status&.upcase
134-
}, {
135-
title: "Urgency:",
136-
value: _alert.urgency&.upcase
137-
}, {
138-
title: "Source:",
139-
value: _alert.source&.name
140-
}, {
141-
title: "Destinations:",
142-
value: _alert.alert_destinations&.map { |d| d.destination.name }&.join(", ")
143-
}, {
144-
title: "User:",
145-
value: _alert.alert_responders&.where(role: :incident_commander)&.includes(account_user: :user)&.first&.account_user&.name
146-
}
147-
],
148-
spacing: "None"
149-
}
150-
],
151-
spacing: "Medium"
130+
title: "Status:",
131+
value: _alert.status&.upcase
152132
},
153133
{
154-
type: "Container",
134+
title: "User:",
135+
value: _alert.alert_responders&.where(role: :incident_commander)&.includes(account_user: :user)&.first&.account_user&.name
136+
}
137+
],
138+
spacing: "ExtraSmall"
139+
}
140+
]
141+
}
142+
]
143+
end
144+
145+
def _full_body
146+
[
147+
{
148+
type: "Container",
149+
backgroundImage: _color,
150+
items: [
151+
{
152+
type: "TextBlock",
153+
size: "Large",
154+
weight: "Bolder",
155+
text: _title
156+
},
157+
{
158+
type: "ColumnSet",
159+
columns: [
160+
{
161+
type: "Column",
155162
items: [
156163
{
157164
type: "TextBlock",
158-
text: _alert.description&.try(:to_plain_text),
159-
wrap: true,
160-
separator: true,
161-
maxLines: 24
165+
weight: "Bolder",
166+
text: _title,
167+
wrap: true
162168
},
163169
{
164-
type: "FactSet",
165-
facts: _alert.additional_data&.map { |ad| {title: ad["label"], value: Array(ad["value"]).join(", ")} } || [],
166-
spacing: "Medium",
167-
separator: true
170+
type: "TextBlock",
171+
spacing: "None",
172+
text: "Created #{_alert.created_at.in_time_zone(option_time_zone).iso8601}",
173+
wrap: true
168174
}
169175
],
170-
spacing: "Medium",
171-
separator: true
176+
width: "stretch"
172177
}
173-
],
174-
actions: [
178+
]
179+
}
180+
]
181+
},
182+
{
183+
type: "Container",
184+
items: [
185+
{
186+
type: "FactSet",
187+
facts: [
175188
{
176-
type: "Action.OpenUrl",
177-
title: "View",
178-
url: Rails.application.routes.url_helpers.try(:alert_url, _alert, script_name: "/#{_alert.account_id}"),
179-
style: "positive"
189+
title: "Status:",
190+
value: _alert.status&.upcase
191+
}, {
192+
title: "Urgency:",
193+
value: _alert.urgency&.upcase
194+
}, {
195+
title: "Source:",
196+
value: _alert.source&.name
197+
}, {
198+
title: "Destinations:",
199+
value: _alert.alert_destinations&.map { |d| d.destination.name }&.join(", ")
200+
}, {
201+
title: "User:",
202+
value: _alert.alert_responders&.where(role: :incident_commander)&.includes(account_user: :user)&.first&.account_user&.name
180203
}
181204
],
182-
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
183-
version: "1.2"
205+
spacing: "None"
184206
}
185-
}
186-
]
187-
}
207+
],
208+
spacing: "Medium"
209+
},
210+
{
211+
type: "Container",
212+
items: [
213+
{
214+
type: "TextBlock",
215+
text: _alert.description&.try(:to_plain_text),
216+
wrap: true,
217+
separator: true,
218+
maxLines: 24
219+
},
220+
{
221+
type: "FactSet",
222+
facts: _alert.additional_data&.map { |ad| {title: ad["label"], value: Array(ad["value"]).join(", ")} } || [],
223+
spacing: "Medium",
224+
separator: true
225+
}
226+
],
227+
spacing: "Medium",
228+
separator: true
229+
}
230+
]
188231
end
189232

190233
def _title

app/views/pager_tree/integrations/channel/microsoft_teams/v3/_form_options.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
:alert_acknowledged,
1818
:alert_resolved,
1919
:alert_dropped,
20+
:compact_message,
2021
]
2122
%>
2223
<% opts.each do |opt| %>

app/views/pager_tree/integrations/channel/microsoft_teams/v3/_show_options.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
:alert_acknowledged,
3131
:alert_resolved,
3232
:alert_dropped,
33+
:compact_message,
3334
]
3435
%>
3536
<% opts.each do |opt| %>

app/views/pager_tree/integrations/channel/microsoft_teams/v4/_form_options.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
:alert_acknowledged,
1818
:alert_resolved,
1919
:alert_dropped,
20+
:compact_message,
2021
]
2122
%>
2223
<% opts.each do |opt| %>

0 commit comments

Comments
 (0)