Skip to content

Commit

Permalink
feat(yolo): send user actions to slack
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Feb 4, 2019
1 parent 6b93d30 commit 2d71712
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/berty/staff/tools/release
require (
github.com/Masterminds/sprig v2.17.1+incompatible
github.com/aokoli/goutils v1.1.0 // indirect
github.com/ashwanthkumar/slack-go-webhook v0.0.0-20181208062437-4a19b1a876b7
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/gobuffalo/packd v0.0.0-20181212173646-eca3b8fd6687
github.com/gobuffalo/packr v1.22.0
Expand All @@ -18,7 +19,9 @@ require (
github.com/labstack/gommon v0.2.7 // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/moul/http2curl v1.0.0 // indirect
github.com/oxtoacart/bpool v0.0.0-20150712133111-4e1c5567d7c2
github.com/parnurzeal/gorequest v0.2.15 // indirect
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v0.0.3
github.com/valyala/bytebufferpool v1.0.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/ajg/form v0.0.0-20160822230020-523a5da1a92f/go.mod h1:uL1WgH+h2mgNtvB
github.com/aokoli/goutils v1.1.0 h1:jy4ghdcYvs5EIoGssZNslIASX5m+KNMfyyKvRQ0TEVE=
github.com/aokoli/goutils v1.1.0/go.mod h1:SijmP0QR8LtwsmDs8Yii5Z/S4trXFGFC2oO5g9DP+DQ=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/ashwanthkumar/slack-go-webhook v0.0.0-20181208062437-4a19b1a876b7 h1:15SC3LmDbVGJ4e17A9/hXW94BPjlefvcg+Am5/Q6sL4=
github.com/ashwanthkumar/slack-go-webhook v0.0.0-20181208062437-4a19b1a876b7/go.mod h1:97O1qkjJBHSSaWJxsTShRIeFy0HWiygk+jnugO9aX3I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/cockroachdb/cockroach-go v0.0.0-20181001143604-e0a95dfd547c/go.mod h1:XGLbWH/ujMcbPbhZq52Nv6UrCghb1yGn//133kEsvDk=
github.com/codegangsta/negroni v1.0.0/go.mod h1:v0y3T5G7Y1UlFfyxFn/QLRU4a2EuNau2iZY63YTKWo0=
Expand Down Expand Up @@ -278,6 +280,8 @@ github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/monoculum/formam v0.0.0-20180901015400-4e68be1d79ba/go.mod h1:RKgILGEJq24YyJ2ban8EO0RUVSJlF1pGsEvoLEACr/Q=
github.com/moul/http2curl v1.0.0 h1:dRMWoAtb+ePxMlLkrCbAqh4TlPHXvoGUSQ323/9Zahs=
github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ=
github.com/nicksnyder/go-i18n v1.10.0/go.mod h1:HrK7VCrbOvQoUAQ7Vpy7i87N7JZZZ7R2xBGjv0j365Q=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand All @@ -286,6 +290,8 @@ github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/oxtoacart/bpool v0.0.0-20150712133111-4e1c5567d7c2 h1:CXwSGu/LYmbjEab5aMCs5usQRVBGThelUKBNnoSOuso=
github.com/oxtoacart/bpool v0.0.0-20150712133111-4e1c5567d7c2/go.mod h1:L3UMQOThbttwfYRNFOWLLVXMhk5Lkio4GGOtw5UrxS0=
github.com/parnurzeal/gorequest v0.2.15 h1:oPjDCsF5IkD4gUk6vIgsxYNaSgvAnIh1EJeROn3HdJU=
github.com/parnurzeal/gorequest v0.2.15/go.mod h1:3Kh2QUMJoqw3icWAecsyzkpY7UzRfDhbRdTjtNwNiUE=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
49 changes: 49 additions & 0 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
"strings"
"time"

slack "github.com/ashwanthkumar/slack-go-webhook"
"github.com/hako/durafmt"
circleci "github.com/jszwedko/go-circleci"
"github.com/labstack/echo"
"github.com/labstack/echo-contrib/session"
)

const (
Expand All @@ -27,6 +29,7 @@ const (
IOS_YOLO_JOB = "client.rn.ios-beta"
ANDROID_STAFF_JOB = "client.rn.android"
ANDROID_YOLO_JOB = "client.rn.android-beta"
SLACK_WEBHOOK_URL = "https://hooks.slack.com/services/T2AJ2MM5Z/BFX8ZASKW/***REMOVED***"
)

var reIPA = regexp.MustCompile("/([^/]+).ipa$")
Expand Down Expand Up @@ -90,7 +93,52 @@ func (s *Server) getVersion(arts []*circleci.Artifact, kind string) (string, err
return "", fmt.Errorf("no version found")
}

func sendUserActionToSlack(c echo.Context, action string, color string) {
sessAuth, err := session.Get(authSessionCookieName, c)
if err != nil {
c.Logger().Warn("failed to parse cookie:", err.Error())
//c.Redirect(http.StatusTemporaryRedirect, "/oauth/logout")
return
}

attachment := slack.Attachment{}
profile := sessAuth.Values["profile"].(map[string]interface{})
if profile["picture"] != nil {
profilePicture := profile["picture"].(string)
attachment.ThumbnailUrl = &profilePicture
}
//username := fmt.Sprintf("%s - %s (%s)", profile["nickname"].(string), profile["name"].(string), profile["sub"].(string))
//attachment.AddField(slack.Field{Title: "action", Value: action, Short: true})
//attachment.AddField(slack.Field{Title: "author", Value: username, Short: true})
//attachment.AddField(slack.Field{Title: "ip", Value: c.RealIP(), Short: true})
//attachment.AddField(slack.Field{Title: "path", Value: c.Path(), Short: true})
//attachment.AddField(slack.Field{Title: "groups", Value: strings.Join(profile["groups"].([]string), ","), Short: true})
attachment.AddField(slack.Field{Title: "user-agent", Value: c.Request().UserAgent(), Short: true})
attachment.AddField(slack.Field{Title: "auth", Value: fmt.Sprintf("nickname=%q sub=%q", profile["nickname"].(string), profile["sub"].(string)), Short: true})
attachment.Color = &color

payload := slack.Payload{
Text: fmt.Sprintf("%s (%s)", action, c.Path()),
//Username: "yolo",
Username: profile["name"].(string),
Channel: "#yolologs",
IconEmoji: ":yolo:",
//IconUrl: profile["picture"].(string),
Attachments: []slack.Attachment{attachment},
}
go func() { // do it asynchronously to avoid blocking
if err := slack.Send(SLACK_WEBHOOK_URL, "", payload); len(err) > 0 {
c.Logger().Warn("failed to send user action to slack: ", fmt.Sprintf("%v", err))
}
}()
}

func sendUserErrorToSlack(c echo.Context, err error) {
sendUserActionToSlack(c, fmt.Sprintf("error: %v", err), "#ff0000")
}

func (s *Server) GetIPA(c echo.Context) error {
sendUserActionToSlack(c, "iOS download", "#0000ff")
id := c.Param("*")
arts, err := s.client.GetArtifacts(id, true)
if err != nil {
Expand All @@ -115,6 +163,7 @@ func (s *Server) GetIPA(c echo.Context) error {
}

func (s *Server) GetAPK(c echo.Context) error {
sendUserActionToSlack(c, "Android download", "#00ff00")
id := c.Param("*")
arts, err := s.client.GetArtifacts(id, true)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func NewServer(cfg *ServerConfig) (*Server, error) {
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.Gzip())
e.HTTPErrorHandler = func(err error, c echo.Context) {
sendUserErrorToSlack(c, err)
e.DefaultHTTPErrorHandler(err, c)
}
templatesBox := packr.NewBox("../templates")
s := &Server{
client: cfg.Client,
Expand Down

0 comments on commit 2d71712

Please sign in to comment.