From da516132360a085431b3365d0b8c998ba2b30932 Mon Sep 17 00:00:00 2001 From: Robin Malik <8790561+robinmalik@users.noreply.github.com> Date: Sat, 21 Oct 2023 10:50:08 +0100 Subject: [PATCH] Add Adaptive Card template (not compressed) --- services/tasks/alert.go | 156 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/services/tasks/alert.go b/services/tasks/alert.go index 4f833a54..71ba1f58 100644 --- a/services/tasks/alert.go +++ b/services/tasks/alert.go @@ -20,6 +20,59 @@ const telegramTemplate = `{"chat_id": "{{ .ChatID }}","parse_mode":"HTML","text" const slackTemplate = `{ "attachments": [ { "title": "Task: {{ .Name }}", "title_link": "{{ .TaskURL }}", "text": "execution ID #{{ .TaskID }}, status: {{ .TaskResult }}!", "color": "{{ .Color }}", "mrkdwn_in": ["text"], "fields": [ { "title": "Author", "value": "{{ .Author }}", "short": true }] } ]}` +const microsoftTeamsTemplate = `{ + "type": "message", + "attachments": [ + { + "contentType": "application/vnd.microsoft.card.adaptive", + "content": { + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "type": "AdaptiveCard", + "version": "1.5", + "body": [ + { + "type": "TextBlock", + "text": "Ansible Task Template Execution by: {{ .Author }}", + }, + { + "type": "FactSet", + "facts": [ + { + "title": "Task:", + "value": "{{ .Name }}" + }, + { + "title": "Status:", + "value": "{{ .TaskResult }}" + }, + { + "title": "Task ID:", + "value": "{{ .TaskID }}" + } + ], + "separator": true + } + ], + "actions": [ + { + "type": "Action.OpenUrl", + "title": "Task URL", + "url": "http://localhost:3000{{ .TaskURL }}" + } + ], + "msteams": { + "width": "Full" + }, + "backgroundImage": { + "horizontalAlignment": "Center", + "url": "data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAABSgAAAAFCAYAAABGmwLHAAAARUlEQVR4nO3YMQEAIBDEsAM1GEErMnkZtyQSOnadd38AAAAAAAq26AAAAABAi0EJAAAAANQYlAAAAABAjUEJAAAAAHQkGWMEAh35OF6mAAAAAElFTkSuQmCC", + "fillMode": "RepeatHorizontally" + } + } + } + ] +}` + // Alert represents an alert that will be templated and sent to the appropriate service type Alert struct { TaskID string @@ -244,3 +297,106 @@ func (t *TaskRunner) sendSlackAlert() { t.Log("Can't send slack alert! Response code: " + strconv.Itoa(resp.StatusCode)) } } + +func (t *TaskRunner) sendMicrosoftTeamsAlert() { + if !util.Config.MicrosoftTeamsAlert || !t.alert { + return + } + + if t.Template.SuppressSuccessAlerts && t.Task.Status == lib.TaskSuccessStatus { + return + } + + MicrosoftTeamsUrl := util.Config.MicrosoftTeamsUrl + + var microsoftTeamsBuffer bytes.Buffer + + var version string + if t.Task.Version != nil { + version = *t.Task.Version + } else if t.Task.BuildTaskID != nil { + version = "build " + strconv.Itoa(*t.Task.BuildTaskID) + } else { + version = "" + } + + var message string + if t.Task.Message != "" { + message = "- " + t.Task.Message + } + + var author string + if t.Task.UserID != nil { + user, err := t.pool.store.GetUser(*t.Task.UserID) + if err != nil { + panic(err) + } + author = user.Name + } + + var color string + if t.Task.Status == lib.TaskSuccessStatus { + color = "good" + } else if t.Task.Status == lib.TaskFailStatus { + color = "bad" + } else if t.Task.Status == lib.TaskRunningStatus { + color = "#333CFF" + } else if t.Task.Status == lib.TaskWaitingStatus { + color = "#FFFC33" + } else if t.Task.Status == lib.TaskStoppingStatus { + color = "#BEBEBE" + } else if t.Task.Status == lib.TaskStoppedStatus { + color = "#5B5B5B" + } + + // Instantiate an alert object + alert := Alert{ + TaskID: strconv.Itoa(t.Task.ID), + Name: t.Template.Name, + TaskURL: util.Config.WebHost + "/project/" + strconv.Itoa(t.Template.ProjectID) + "/templates/" + strconv.Itoa(t.Template.ID) + "?t=" + strconv.Itoa(t.Task.ID), + TaskResult: strings.ToUpper(string(t.Task.Status)), + TaskVersion: version, + TaskDescription: message, + Author: author, + Color: color, + } + + tpl := template.New("MicrosoftTeams body template") + + tpl, err := tpl.Parse(microsoftTeamsTemplate) + if err != nil { + t.Log("Can't parse MicrosoftTeams template!") + panic(err) + } + + // The tpl.Execute(µsoftTeamsBuffer, alert) line is used to apply the data from the alert struct to the template. + // This operation fills in the placeholders in the template with the corresponding values from the alert struct + // and writes the result to the microsoftTeamsBuffer. In essence, it generates a JSON message based on the template and the data in the alert struct. + err = tpl.Execute(µsoftTeamsBuffer, alert) + if err != nil { + t.Log("Can't generate alert template!") + panic(err) + } + + // test if buffer is empty + if microsoftTeamsBuffer.Len() == 0 { + t.Log("MicrosoftTeams buffer is empty!") + return + } + + t.Log("Attempting to send MicrosoftTeams alert") + + resp, err := http.Post(MicrosoftTeamsUrl, "application/json", µsoftTeamsBuffer) + + if err != nil { + t.Log("Can't send MicrosoftTeams alert! Error: " + err.Error()) + } else if resp.StatusCode != 200 { + t.Log("Can't send MicrosoftTeams alert! Response code: " + strconv.Itoa(resp.StatusCode)) + } + + // Output to console + fmt.Println("MicrosoftTeams failed") + jsonString := microsoftTeamsBuffer.String() + fmt.Println(jsonString) + +} \ No newline at end of file