Skip to content

Commit

Permalink
chore: update logs2notifications to use machinery
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Aug 14, 2024
1 parent 88af552 commit e07a355
Show file tree
Hide file tree
Showing 27 changed files with 61 additions and 953 deletions.
2 changes: 1 addition & 1 deletion services/actions-handler/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/cheshir/go-mq/v2 v2.0.1
github.com/uselagoon/machinery v0.0.19
github.com/uselagoon/machinery v0.0.29
gopkg.in/matryer/try.v1 v1.0.0-20150601225556-312d2599e12e
)

Expand Down
4 changes: 2 additions & 2 deletions services/actions-handler/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,8 @@ github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/uselagoon/machinery v0.0.19 h1:1R0DWe0Fz2cRhzisnM86SQJXFy+DO9d+9gEa2maRwC4=
github.com/uselagoon/machinery v0.0.19/go.mod h1:NbgtEofjK2XY0iUpk9aMYazIo+W/NI56+UF72jv8zVY=
github.com/uselagoon/machinery v0.0.29 h1:invFIPv1Z1xCt8/1ilbiNDuAEPrb+AUO21BnNG+CX8c=
github.com/uselagoon/machinery v0.0.29/go.mod h1:X0qguIO9skumMhhT0ap5CKHulKgYzy3TiIn+xlwiFQc=
github.com/vishvananda/netlink v0.0.0-20181108222139-023a6dafdcdf/go.mod h1:+SR5DhBJrl6ZM7CoCKvpw5BKroDKQ+PJqOg65H/2ktk=
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netlink v1.1.1-0.20201029203352-d40f9887b852/go.mod h1:twkDnbuQxJYemMlGd4JFIcuhgX83tXhKS2B/PRMpOho=
Expand Down
2 changes: 1 addition & 1 deletion services/backup-handler/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/google/uuid v1.3.0
github.com/isayme/go-amqp-reconnect v0.0.0-20210303120416-fc811b0bcda2
github.com/streadway/amqp v1.0.0
github.com/uselagoon/machinery v0.0.28
github.com/uselagoon/machinery v0.0.29
)

require (
Expand Down
4 changes: 2 additions & 2 deletions services/backup-handler/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo=
github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/uselagoon/machinery v0.0.28 h1:6eChYCr6b4kaiBBaUsD0skTC/y6RNodLblJOIiwKyZg=
github.com/uselagoon/machinery v0.0.28/go.mod h1:+cKZerqQzyvPf4lM0ec1jFgDmeM5jYd9W/0EjHmIY8I=
github.com/uselagoon/machinery v0.0.29 h1:invFIPv1Z1xCt8/1ilbiNDuAEPrb+AUO21BnNG+CX8c=
github.com/uselagoon/machinery v0.0.29/go.mod h1:X0qguIO9skumMhhT0ap5CKHulKgYzy3TiIn+xlwiFQc=
8 changes: 5 additions & 3 deletions services/logs2notifications/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ go 1.21
require (
github.com/aws/aws-sdk-go v1.41.11
github.com/cheshir/go-mq/v2 v2.0.1
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/machinebox/graphql v0.2.3-0.20181106130121-3a9253180225
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2
github.com/slack-go/slack v0.9.5
github.com/uselagoon/machinery v0.0.29
gopkg.in/mail.v2 v2.3.1
)

require (
github.com/NeowayLabs/wabbit v0.0.0-20210927194032-73ad61d1620e // indirect
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/guregu/null v4.0.0+incompatible // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/matryer/is v1.4.0 // indirect
github.com/machinebox/graphql v0.2.3-0.20181106130121-3a9253180225 // indirect
github.com/pborman/uuid v1.2.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rabbitmq/amqp091-go v1.7.0 // indirect
Expand Down
15 changes: 12 additions & 3 deletions services/logs2notifications/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denverdino/aliyungo v0.0.0-20190125010748-a747050bb1ba/go.mod h1:dV8lFg6daOBZbT6/BDGIz6Y3WFGn8juu6G+CQ6LHtl0=
github.com/dgrijalva/jwt-go v0.0.0-20170104182250-a601269ab70c/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=
Expand Down Expand Up @@ -412,6 +411,8 @@ github.com/gogo/protobuf v1.3.0/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXP
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -509,6 +510,8 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgf
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/guregu/null v4.0.0+incompatible h1:4zw0ckM7ECd6FNNddc3Fu4aty9nTlpkkzH7dPn4/4Gw=
github.com/guregu/null v4.0.0+incompatible/go.mod h1:ePGpQaN9cw0tj45IR5E5ehMvsFlLlQZAkkOXZurJ3NM=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v0.0.0-20141028054710-7554cd9344ce/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand All @@ -525,6 +528,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down Expand Up @@ -847,6 +852,8 @@ github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/uselagoon/machinery v0.0.29 h1:invFIPv1Z1xCt8/1ilbiNDuAEPrb+AUO21BnNG+CX8c=
github.com/uselagoon/machinery v0.0.29/go.mod h1:X0qguIO9skumMhhT0ap5CKHulKgYzy3TiIn+xlwiFQc=
github.com/vishvananda/netlink v0.0.0-20181108222139-023a6dafdcdf/go.mod h1:+SR5DhBJrl6ZM7CoCKvpw5BKroDKQ+PJqOg65H/2ktk=
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netlink v1.1.1-0.20201029203352-d40f9887b852/go.mod h1:twkDnbuQxJYemMlGd4JFIcuhgX83tXhKS2B/PRMpOho=
Expand Down Expand Up @@ -1036,8 +1043,9 @@ golang.org/x/net v0.0.0-20210825183410-e898025ed96a/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211209124913-491a49abca63/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -1161,8 +1169,9 @@ golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220405210540-1e041c57c461/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (h *Messaging) sendEmailMessage(emoji, color, subject, event, project, emai
}

}
log.Println(fmt.Sprintf("Sent %s message to email for project %s", event, project))
log.Printf("Sent %s message to email for project %s", event, project)
}

func getEmailEvent(msgEvent string) (string, string, string, error) {
Expand Down
23 changes: 10 additions & 13 deletions services/logs2notifications/internal/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (

mq "github.com/cheshir/go-mq/v2"
"github.com/matryer/try"
"github.com/uselagoon/lagoon/services/logs2notifications/internal/lagoon"
lclient "github.com/uselagoon/lagoon/services/logs2notifications/internal/lagoon/client"
"github.com/uselagoon/lagoon/services/logs2notifications/internal/lagoon/jwt"
"github.com/uselagoon/lagoon/services/logs2notifications/internal/schema"

"github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
"github.com/uselagoon/machinery/api/schema"
"github.com/uselagoon/machinery/utils/jwt"
)

// RabbitBroker .
Expand All @@ -31,6 +32,7 @@ type LagoonAPI struct {
TokenSigningKey string `json:"tokenSigningKey"`
JWTSubject string `json:"subject"`
JWTIssuer string `json:"issuer"`
Version string `json:"version"`
}

// Action is the structure of an action that is received via the message queue.
Expand All @@ -40,11 +42,6 @@ type Action struct {
Data map[string]interface{} `json:"data"` // contains the payload for the action, this could be any json so using a map
}

type messaging interface {
Consumer()
Publish(string, []byte)
}

// Messaging is used for the config and client information for the messaging queue.
type Messaging struct {
Config mq.Config
Expand Down Expand Up @@ -204,7 +201,7 @@ func (h *Messaging) Consumer() {

go func() {
for err := range messageQueue.Error() {
log.Println(fmt.Sprintf("Caught error from message queue: %v", err))
log.Printf("Caught error from message queue: %v", err)
}
}()

Expand All @@ -217,7 +214,7 @@ func (h *Messaging) Consumer() {
message.Ack(false) // ack to remove from queue
})
if err != nil {
log.Println(fmt.Sprintf("Failed to set handler to consumer `%s`: %v", "items-queue", err))
log.Printf("Failed to set handler to consumer `%s`: %v", "items-queue", err)
}
<-forever
}
Expand Down Expand Up @@ -282,7 +279,7 @@ func (h *Messaging) processMessage(message []byte) {
}

func (h *Messaging) getProjectNotifictions(ctx context.Context, projectName string) (*schema.Project, error) {
token, err := jwt.OneMinuteAdminToken(h.LagoonAPI.TokenSigningKey, h.LagoonAPI.JWTAudience, h.LagoonAPI.JWTSubject, h.LagoonAPI.JWTIssuer)
token, err := jwt.GenerateAdminToken(h.LagoonAPI.TokenSigningKey, h.LagoonAPI.JWTAudience, h.LagoonAPI.JWTSubject, h.LagoonAPI.JWTIssuer, time.Now().Unix(), 60)
if err != nil {
// the token wasn't generated
if h.EnableDebug {
Expand All @@ -291,7 +288,7 @@ func (h *Messaging) getProjectNotifictions(ctx context.Context, projectName stri
return nil, err
}
// get all notifications for said project
l := lclient.New(h.LagoonAPI.Endpoint, token, "logs2notifications", false)
l := lclient.New(h.LagoonAPI.Endpoint, "actions-handler", h.LagoonAPI.Version, &token, false)
projectNotifications, err := lagoon.NotificationsForProject(ctx, projectName, l)
if err != nil {
log.Println(err)
Expand Down
13 changes: 0 additions & 13 deletions services/logs2notifications/internal/handler/main_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
package handler

import (
"bytes"
"encoding/json"
"io/ioutil"
"reflect"
"testing"

mq "github.com/cheshir/go-mq/v2"
)

func checkEqual(t *testing.T, got, want interface{}, msgs ...interface{}) {
if !reflect.DeepEqual(got, want) {
buf := bytes.Buffer{}
buf.WriteString("got:\n[%v]\nwant:\n[%v]\n")
for _, v := range msgs {
buf.WriteString(v.(string))
}
t.Errorf(buf.String(), got, want)
}
}

func TestProcessing(t *testing.T) {
config := mq.Config{}
graphQLConfig := LagoonAPI{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func (h *Messaging) sendMicrosoftTeamsMessage(emoji, color, webhook, event, proj

teamsPayloadBytes, _ := json.Marshal(teamsPayload)
req, err := http.NewRequest("POST", webhook, bytes.NewBuffer(teamsPayloadBytes))
if err != nil {
log.Printf("Error sending message to microsoft teams for project %s: %v", project, err)
return
}
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
Expand All @@ -124,7 +128,7 @@ func (h *Messaging) sendMicrosoftTeamsMessage(emoji, color, webhook, event, proj
return
}
defer resp.Body.Close()
log.Println(fmt.Sprintf("Sent %s message to microsoft teams for project %s", event, project))
log.Printf("Sent %s message to microsoft teams for project %s", event, project)
}

func getMicrosoftTeamsEvent(msgEvent string) (string, string, string, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func (h *Messaging) sendRocketChatMessage(emoji, color, appID, channel, webhook,
}
jsonBytes, _ := json.Marshal(data)
req, err := http.NewRequest("POST", webhook, bytes.NewBuffer(jsonBytes))
if err != nil {
log.Printf("Error sending message to rocketchat channel %s for project %s: %v", channel, project, err)
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Length", fmt.Sprintf("%d", len(jsonBytes)))

Expand All @@ -130,7 +134,7 @@ func (h *Messaging) sendRocketChatMessage(emoji, color, appID, channel, webhook,
return
}
defer resp.Body.Close()
log.Println(fmt.Sprintf("Sent %s message to rocketchat channel %s for project %s", event, channel, project))
log.Printf("Sent %s message to rocketchat channel %s for project %s", event, channel, project)
}

func getRocketChatEvent(msgEvent string) (string, string, string, error) {
Expand Down
10 changes: 4 additions & 6 deletions services/logs2notifications/internal/handler/s3_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/uselagoon/lagoon/services/logs2notifications/internal/helpers"
"github.com/uselagoon/machinery/utils/namespace"
)

// MessageType .
Expand Down Expand Up @@ -41,7 +41,7 @@ func (h *Messaging) SendToS3(notification *Notification, msgType MessageType) {
if notification.Meta.Environment != "" {
filePath = fmt.Sprintf("tasklogs/%s/%s/%s-%s.txt",
notification.Project,
helpers.ShortenEnvironment(notification.Project, helpers.MakeSafe(notification.Meta.Environment)),
namespace.ShortenEnvironment(notification.Project, namespace.MakeSafe(notification.Meta.Environment)),
notification.Meta.Task.ID,
notification.Meta.RemoteID,
)
Expand All @@ -56,8 +56,7 @@ func (h *Messaging) SendToS3(notification *Notification, msgType MessageType) {

// UploadFileS3
func (h *Messaging) uploadFileS3(message, fileName string) {
var forcePath bool
forcePath = true
forcePath := true
session, err := session.NewSession(&aws.Config{
Region: aws.String(h.S3FilesRegion),
Endpoint: aws.String(h.S3FilesOrigin),
Expand All @@ -81,6 +80,5 @@ func (h *Messaging) uploadFileS3(message, fileName string) {
if err != nil {
log.Println(err)
}
log.Println(fmt.Sprintf("Uploaded file %s", fileName))
return
log.Printf("Uploaded file %s\n", fileName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (h *Messaging) sendSlackMessage(emoji, color, appID, channel, webhook, even
log.Printf("Error sending message to slack channel %s for project %s: %v", channel, project, err)
return
}
log.Println(fmt.Sprintf("Sent %s message to slack channel %s for project %s", event, channel, project))
log.Printf("Sent %s message to slack channel %s for project %s", event, channel, project)
}

func getSlackEvent(msgEvent string) (string, string, string, error) {
Expand Down
18 changes: 8 additions & 10 deletions services/logs2notifications/internal/handler/webhook_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http"
"strings"

"github.com/uselagoon/lagoon/services/logs2notifications/internal/schema"
"github.com/uselagoon/machinery/api/schema"
)

// WebhookData .
Expand All @@ -24,17 +24,21 @@ type WebhookData struct {
}

// SendToWebhook .
func (h *Messaging) SendToWebhook(notification *Notification, webhook schema.NotificationWebhook) {
func (h *Messaging) SendToWebhook(notification *Notification, webhook schema.AddNotificationWebhookInput) {
message, err := h.processWebhookTemplate(notification)
if err != nil {
return
}
h.sendWebhookMessage(notification.Meta.ProjectName, *message, webhook)
}

func (h *Messaging) sendWebhookMessage(project string, data WebhookData, webhook schema.NotificationWebhook) {
func (h *Messaging) sendWebhookMessage(project string, data WebhookData, webhook schema.AddNotificationWebhookInput) {
message, _ := json.Marshal(data)
req, err := http.NewRequest("POST", webhook.Webhook, bytes.NewBuffer(message))
if err != nil {
log.Printf("Error sending message to webhook: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Length", fmt.Sprintf("%d", len(message)))

Expand All @@ -45,13 +49,7 @@ func (h *Messaging) sendWebhookMessage(project string, data WebhookData, webhook
return
}
defer resp.Body.Close()
log.Println(fmt.Sprintf("Sent %s message to webhook", data.Event))
if err != nil {
log.Printf("Error sending message to webhook for project %s: %v", project, err)
return
}
defer resp.Body.Close()
log.Println(fmt.Sprintf("Sent %s message to webhook for project %s", data.Event, project))
log.Printf("Sent %s message to webhook for project %s", data.Event, project)
}

// processWebhookTemplate .
Expand Down
Loading

0 comments on commit e07a355

Please sign in to comment.