|
1 | 1 | package teams |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "fmt" |
| 6 | + "net/http" |
5 | 7 | "strings" |
6 | 8 |
|
7 | 9 | "github.com/containrrr/shoutrrr" |
@@ -43,19 +45,41 @@ func (p *Provider) Send(message, CliFormat string) error { |
43 | 45 | p.counter++ |
44 | 46 | for _, pr := range p.Teams { |
45 | 47 | msg := utils.FormatMessage(message, utils.SelectFormat(CliFormat, pr.TeamsFormat), p.counter) |
46 | | - webhookParts := strings.Split(pr.TeamsWebHookURL, "/webhookb2/") |
47 | | - if len(webhookParts) != 2 { |
48 | | - err := fmt.Errorf("teams: invalid webhook url for id: %s ", pr.ID) |
49 | | - TeamsErr = multierr.Append(TeamsErr, err) |
50 | | - } |
51 | | - teamsHost := strings.TrimPrefix(webhookParts[0], "https://") |
52 | | - teamsTokens := strings.ReplaceAll(webhookParts[1], "IncomingWebhook/", "") |
53 | | - url := fmt.Sprintf("teams://%s?host=%s", teamsTokens, teamsHost) |
54 | | - err := shoutrrr.Send(url, msg) |
55 | | - if err != nil { |
56 | | - err = errors.Wrap(err, fmt.Sprintf("failed to send teams notification for id: %s ", pr.ID)) |
57 | | - TeamsErr = multierr.Append(TeamsErr, err) |
58 | | - continue |
| 48 | + provider := strings.Split(pr.TeamsWebHookURL, "/")[3] |
| 49 | + |
| 50 | + // Deprecated method |
| 51 | + if provider == "webhookb2" { |
| 52 | + webhookParts := strings.Split(pr.TeamsWebHookURL, "/webhookb2/") |
| 53 | + if len(webhookParts) != 2 { |
| 54 | + err := fmt.Errorf("teams: invalid webhook url for id: %s ", pr.ID) |
| 55 | + TeamsErr = multierr.Append(TeamsErr, err) |
| 56 | + } |
| 57 | + teamsHost := strings.TrimPrefix(webhookParts[0], "https://") |
| 58 | + teamsTokens := strings.ReplaceAll(webhookParts[1], "IncomingWebhook/", "") |
| 59 | + url := fmt.Sprintf("teams://%s?host=%s", teamsTokens, teamsHost) |
| 60 | + err := shoutrrr.Send(url, msg) |
| 61 | + if err != nil { |
| 62 | + err = errors.Wrap(err, fmt.Sprintf("failed to send webhook teams notification for id: %s", pr.ID)) |
| 63 | + TeamsErr = multierr.Append(TeamsErr, err) |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + // New Power Automate method |
| 68 | + } else if provider == "workflows" { |
| 69 | + htmlMessage := strings.ReplaceAll(msg, "\n", "<br>") |
| 70 | + payload := fmt.Sprintf(`{"text": "%s"}`, htmlMessage) |
| 71 | + resp, err := http.Post(pr.TeamsWebHookURL, "application/json", bytes.NewBuffer([]byte(payload))) |
| 72 | + if err != nil { |
| 73 | + err = errors.Wrap(err, fmt.Sprintf("failed to send workflow teams notification for id: %s", pr.ID)) |
| 74 | + TeamsErr = multierr.Append(TeamsErr, err) |
| 75 | + continue |
| 76 | + } |
| 77 | + defer resp.Body.Close() |
| 78 | + if resp.StatusCode != http.StatusAccepted { |
| 79 | + err = errors.Errorf("failed to send workflow teams notification for id: %s, got status code: %d", pr.ID, resp.StatusCode) |
| 80 | + TeamsErr = multierr.Append(TeamsErr, err) |
| 81 | + continue |
| 82 | + } |
59 | 83 | } |
60 | 84 | gologger.Verbose().Msgf("teams notification sent for id: %s", pr.ID) |
61 | 85 | } |
|
0 commit comments