forked from crazy-max/diun
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
168 additions
and
41 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package slack | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
"text/template" | ||
"time" | ||
|
||
"github.com/crazy-max/diun/internal/model" | ||
"github.com/crazy-max/diun/internal/notif/notifier" | ||
"github.com/nlopes/slack" | ||
) | ||
|
||
// Client represents an active slack notification object | ||
type Client struct { | ||
*notifier.Notifier | ||
cfg model.NotifSlack | ||
app model.App | ||
} | ||
|
||
// New creates a new slack notification instance | ||
func New(config model.NotifSlack, app model.App) notifier.Notifier { | ||
return notifier.Notifier{ | ||
Handler: &Client{ | ||
cfg: config, | ||
app: app, | ||
}, | ||
} | ||
} | ||
|
||
// Name returns notifier's name | ||
func (c *Client) Name() string { | ||
return "slack" | ||
} | ||
|
||
// Send creates and sends a webhook notification with an entry | ||
func (c *Client) Send(entry model.NotifEntry) error { | ||
var textBuf bytes.Buffer | ||
textTpl := template.Must(template.New("text").Parse("<!channel> Docker tag `{{ .Image.Domain }}/{{ .Image.Path }}:{{ .Image.Tag }}` {{ if (eq .Status \"new\") }}newly added{{ else }}updated{{ end }}.")) | ||
if err := textTpl.Execute(&textBuf, entry); err != nil { | ||
return err | ||
} | ||
|
||
color := "#4caf50" | ||
if entry.Status == model.ImageStatusUpdate { | ||
color = "#0054ca" | ||
} | ||
|
||
return slack.PostWebhook(c.cfg.WebhookURL, &slack.WebhookMessage{ | ||
Attachments: []slack.Attachment{slack.Attachment{ | ||
Color: color, | ||
AuthorName: "Diun", | ||
AuthorSubname: "github.com/crazy-max/diun", | ||
AuthorLink: "https://github.com/crazy-max/diun", | ||
AuthorIcon: "https://raw.githubusercontent.com/crazy-max/diun/master/.res/diun.png", | ||
Text: textBuf.String(), | ||
Footer: fmt.Sprintf("%s © %d %s %s", c.app.Author, time.Now().Year(), c.app.Name, c.app.Version), | ||
Fields: []slack.AttachmentField{ | ||
{ | ||
Title: "Provider", | ||
Value: entry.Provider, | ||
Short: false, | ||
}, | ||
{ | ||
Title: "Created", | ||
Value: entry.Manifest.Created.Format("Jan 02, 2006 15:04:05 UTC"), | ||
Short: false, | ||
}, | ||
{ | ||
Title: "Digest", | ||
Value: entry.Manifest.Digest.String(), | ||
Short: false, | ||
}, | ||
{ | ||
Title: "Platform", | ||
Value: fmt.Sprintf("%s/%s", entry.Manifest.Os, entry.Manifest.Architecture), | ||
Short: false, | ||
}, | ||
}, | ||
Ts: json.Number(strconv.FormatInt(time.Now().Unix(), 10)), | ||
}}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters