Skip to content

Commit

Permalink
add endpoint to send test notification to slack (#1730)
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes authored Oct 1, 2024
1 parent 54908fe commit 4b0d0f0
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
96 changes: 96 additions & 0 deletions ee/drift/controllers/notifications.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package controllers

import (
"bytes"
"encoding/json"
"fmt"
"github.com/diggerhq/digger/ee/drift/dbmodels"
"github.com/gin-gonic/gin"
"log"
"net/http"
)

func sendTestSlackWebhook(webhookURL string) error {
payload := map[string]interface{}{
"blocks": []map[string]interface{}{
{
"type": "section",
"fields": []map[string]string{
{"type": "mrkdwn", "text": "*Project*"},
{"type": "mrkdwn", "text": "*Status*"},
},
},
{"type": "divider"},
{
"type": "section",
"fields": []map[string]string{
{"type": "mrkdwn", "text": "<https://driftapp.digger.dev|Dev environment>"},
{"type": "mrkdwn", "text": ":large_yellow_circle: Drift detected"},
},
},
{"type": "divider"},
{
"type": "section",
"fields": []map[string]string{
{"type": "mrkdwn", "text": "<https://driftapp.digger.dev|Staging environment>"},
{"type": "mrkdwn", "text": ":white_circle: Acknowledged drift"},
},
},
{"type": "divider"},
{
"type": "section",
"fields": []map[string]string{
{"type": "mrkdwn", "text": "<https://driftapp.digger.dev|Prod environment>"},
{"type": "mrkdwn", "text": ":large_green_circle: No drift"},
},
},
{"type": "divider"},
},
}

jsonPayload, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("error marshalling JSON: %v", err)
}

resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonPayload))
if err != nil {
return fmt.Errorf("error sending POST request: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("non-OK HTTP status: %s", resp.Status)
}

return nil
}

type TestSlackNotificationForOrgRequest struct {
OrgId string `json:"org_id"`
}

func (mc MainController) SendTestSlackNotificationForOrg(c *gin.Context) {
var request TestSlackNotificationForOrgRequest
err := c.BindJSON(&request)
if err != nil {
log.Printf("Error binding JSON: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error binding JSON"})
return
}
orgId := request.OrgId

os := dbmodels.DB.Query.OrgSetting
orgSettings, err := dbmodels.DB.Query.OrgSetting.Where(os.OrgID.Eq(orgId)).First()

slackNotificationUrl := orgSettings.SlackNotificationURL

err = sendTestSlackWebhook(slackNotificationUrl)
if err != nil {
log.Printf("Error sending slack notification: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error sending slack notification"})
return
}

c.String(200, "ok")
}
1 change: 1 addition & 0 deletions ee/drift/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func main() {

r.POST("/repos/:repo/projects/:projectName/jobs/:jobId/set-status", middleware.JobTokenAuth(), controller.SetJobStatusForProject)

r.POST("/_internal/send_test_slack_notification_for_org", middleware.WebhookAuth(), controller.SendTestSlackNotificationForOrg)
r.POST("/_internal/process_drift", middleware.WebhookAuth(), controller.ProcessAllDrift)
r.POST("/_internal/process_drift_for_org", middleware.WebhookAuth(), controller.ProcessDriftForOrg)
r.POST("/_internal/trigger_drift_for_project", middleware.WebhookAuth(), controller.TriggerDriftRunForProject)
Expand Down

0 comments on commit 4b0d0f0

Please sign in to comment.