Merge pull request #1584 from robinmalik/develop

Add: Support for Microsoft Team Channel Alerts (via Adaptive Card webhook)
This commit is contained in:
Denis Gukov 2024-03-02 18:35:11 +01:00 committed by GitHub
commit ebb0c2c286
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 162 additions and 1 deletions

View File

@ -70,6 +70,11 @@ func InteractiveSetup(conf *util.ConfigType) {
askValue("Slack Webhook URL", "", &conf.SlackUrl)
}
askConfirmation("Enable Microsoft Team Channel alerts?", false, &conf.MicrosoftTeamsAlert)
if conf.MicrosoftTeamsAlert {
askValue("Microsoft Teams Webhook URL", "", &conf.MicrosoftTeamsUrl)
}
askConfirmation("Enable LDAP authentication?", false, &conf.LdapEnable)
if conf.LdapEnable {
askValue("LDAP server host", "localhost:389", &conf.LdapServer)

View File

@ -78,6 +78,7 @@ func (t *TaskRunner) SetStatus(status lib.TaskStatus) {
if status == lib.TaskSuccessStatus || status == lib.TaskFailStatus {
t.sendTelegramAlert()
t.sendSlackAlert()
t.sendMicrosoftTeamsAlert()
}
}

View File

@ -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": "{{ .TaskURL }}"
}
],
"msteams": {
"width": "Full"
},
"backgroundImage": {
"horizontalAlignment": "Center",
"url": "data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAABSgAAAAFCAYAAABGmwLHAAAARklEQVR4nO3YMQEAIBDEsANPSMC/AbzwMm5JJHTseuf+AAAAAAAUbNEBAAAAgBaDEgAAAACoMSgBAAAAgBqDEgAAAADoSDL8RAJfcbcsoQAAAABJRU5ErkJggg==",
"fillMode": "RepeatHorizontally"
}
}
}
]
}`
// Alert represents an alert that will be templated and sent to the appropriate service
type Alert struct {
TaskID string
@ -244,3 +297,102 @@ 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(&microsoftTeamsBuffer, 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(&microsoftTeamsBuffer, 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", &microsoftTeamsBuffer)
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))
}
t.Log("MicrosoftTeams alert sent successfully")
}

View File

@ -162,12 +162,15 @@ type ConfigType struct {
LdapMappings ldapMappings `json:"ldap_mappings"`
LdapNeedTLS bool `json:"ldap_needtls" env:"SEMAPHORE_LDAP_NEEDTLS"`
// telegram and slack alerting
// Telegram, Slack and Microsoft Teams alerting
TelegramAlert bool `json:"telegram_alert" env:"SEMAPHORE_TELEGRAM_ALERT"`
TelegramChat string `json:"telegram_chat" env:"SEMAPHORE_TELEGRAM_CHAT"`
TelegramToken string `json:"telegram_token" env:"SEMAPHORE_TELEGRAM_TOKEN"`
SlackAlert bool `json:"slack_alert" env:"SEMAPHORE_SLACK_ALERT"`
SlackUrl string `json:"slack_url" env:"SEMAPHORE_SLACK_URL"`
MicrosoftTeamsAlert bool `json:"microsoft_teams_alert" env:"SEMAPHORE_MICROSOFT_TEAMS_ALERT"`
MicrosoftTeamsUrl string `json:"microsoft_teams_url" env:"SEMAPHORE_MICROSOFT_TEAMS_URL"`
// oidc settings
OidcProviders map[string]OidcProvider `json:"oidc_providers"`