diff --git a/flow/event.go b/flow/event.go deleted file mode 100644 index 6d8b99f..0000000 --- a/flow/event.go +++ /dev/null @@ -1,54 +0,0 @@ -package flow - -import ( - "time" -) - -const ( - statusQueued = "QUEUED" - statusWorking = "WORKING" - statusSuccess = "SUCCESS" - statusFailure = "FAILURE" -) - -// Event is Cloud Build events published to Cloud Pub/Sub -type Event struct { - ID string `json:"id"` - ProjectID string `json:"projectId"` - Status string `json:"status"` - Timeout string `json:"timeout"` - LogURL string `json:"logUrl"` - StartTime *time.Time `json:"startTime"` - FinishTime *time.Time `json:"finishTime"` - - TriggerID string `json:"buildTriggerId"` - - EventSource `json:"source"` - Artifacts `json:"artifacts"` -} - -type EventSource struct { - EventRepo `json:"repoSource"` -} - -type EventRepo struct { - RepoName string `json:"repoName"` - TagName *string `json:"tagName"` - BranchName *string `json:"branchName"` -} - -type Artifacts struct { - Images []string `json:"images"` -} - -func (e Event) isFinished() bool { - return (e.FinishTime != nil) -} - -func (e Event) isSuuccess() bool { - return (e.Status == statusSuccess) -} - -func (e Event) isTriggerdBuld() bool { - return (e.TriggerID != "") -} diff --git a/flow/process.go b/flow/process.go index fa0abc8..49541a1 100644 --- a/flow/process.go +++ b/flow/process.go @@ -7,6 +7,7 @@ import ( "os" "strings" + "github.com/sakajunquality/cloud-pubsub-events/cloudbuildevent" "github.com/sakajunquality/flow/gitbot" "github.com/sakajunquality/flow/slackbot" ) @@ -19,19 +20,19 @@ type PullRequest struct { err error } -func (f *Flow) process(ctx context.Context, e Event) error { - if !e.isFinished() { // Notify only the finished +func (f *Flow) process(ctx context.Context, e cloudbuildevent.Event) error { + if !e.IsFinished() { // Notify only the finished fmt.Fprintf(os.Stdout, "Build hasn't finished\n") return nil } - app, err := getApplicationByEventTriggerID(e.TriggerID) + app, err := getApplicationByEventTriggerID(*e.TriggerID) if err != nil { fmt.Fprintf(os.Stdout, "No app is configured for %s\n", e.TriggerID) return nil } - if !e.isSuuccess() { // CloudBuild Failure + if !e.IsSuuccess() { // CloudBuild Failure return f.notifyFalure(e, "", nil) } @@ -117,7 +118,7 @@ func (f *Flow) createRelasePR(ctx context.Context, version string, a Application return *prURL, nil } -func (f *Flow) notifyRelasePR(e Event, prs PullRequests, app *Application) error { +func (f *Flow) notifyRelasePR(e cloudbuildevent.Event, prs PullRequests, app *Application) error { var prURL string for _, pr := range prs { @@ -143,12 +144,12 @@ func (f *Flow) notifyRelasePR(e Event, prs PullRequests, app *Application) error return slackbot.NewSlackMessage(f.slackBotToken, cfg.SlackNotifiyChannel, d).Post() } -func (f *Flow) notifyDeploy(e Event) error { +func (f *Flow) notifyDeploy(e cloudbuildevent.Event) error { d := slackbot.MessageDetail{ IsSuccess: true, IsPrNotify: false, LogURL: e.LogURL, - AppName: e.RepoName, + AppName: *e.RepoName, TagName: e.TagName, BranchName: e.BranchName, } @@ -156,7 +157,7 @@ func (f *Flow) notifyDeploy(e Event) error { return slackbot.NewSlackMessage(f.slackBotToken, cfg.SlackNotifiyChannel, d).Post() } -func (f *Flow) notifyFalure(e Event, errorMessage string, app *Application) error { +func (f *Flow) notifyFalure(e cloudbuildevent.Event, errorMessage string, app *Application) error { d := slackbot.MessageDetail{ IsSuccess: false, LogURL: e.LogURL, diff --git a/flow/subscribe.go b/flow/subscribe.go index b82d07f..ff057e9 100644 --- a/flow/subscribe.go +++ b/flow/subscribe.go @@ -2,13 +2,13 @@ package flow import ( "context" - "encoding/json" "fmt" "log" "os" "sync" "cloud.google.com/go/pubsub" + "github.com/sakajunquality/cloud-pubsub-events/cloudbuildevent" ) var ( @@ -31,9 +31,8 @@ func (f *Flow) subscribe(ctx context.Context, errCh chan error) { } err := subscription.Receive(ctx, func(ctx context.Context, msg *pubsub.Message) { - var e Event - - if err := json.Unmarshal(msg.Data, &e); err != nil { + e, err := cloudbuildevent.ParseMessage(msg.Data) + if err != nil { fmt.Fprintf(os.Stderr, "Error: could not decode message data: %#v", msg) msg.Ack() return diff --git a/go.mod b/go.mod index 2002df8..8374a8d 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/nlopes/slack v0.4.0 github.com/pkg/errors v0.8.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/sakajunquality/cloud-pubsub-events v0.0.0-20190117094524-e3828a247582 github.com/stretchr/testify v1.2.2 // indirect go.opencensus.io v0.17.0 // indirect golang.org/x/net v0.0.0-20181017193950-04a2e542c03f // indirect diff --git a/go.sum b/go.sum index cec6fea..1cc52f4 100644 --- a/go.sum +++ b/go.sum @@ -39,6 +39,8 @@ github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/sakajunquality/cloud-pubsub-events v0.0.0-20190117094524-e3828a247582 h1:icL5nNiXcEVS61weueCXUI7O7mN4jQXIjjHU/OHyeQE= +github.com/sakajunquality/cloud-pubsub-events v0.0.0-20190117094524-e3828a247582/go.mod h1:ge90hWT8vT100pJ3wdBk9uYRyQ+KXj91+CuCFA9j5R8= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= go.opencensus.io v0.17.0 h1:2Cu88MYg+1LU+WVD+NWwYhyP0kKgRlN9QjWGaX0jKTE=