Skip to content

Commit

Permalink
Merge pull request #18 from sakajunquality/feature/delete-subscription
Browse files Browse the repository at this point in the history
delete Pull subscription
  • Loading branch information
sakajunquality authored Jan 1, 2020
2 parents 3392e4e + 9381413 commit e297415
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 216 deletions.
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ COPY go.sum .
RUN go mod download

COPY . .
RUN go build -o /go/bin/flowd ./cmd/flowd/main.go
RUN go build -o /go/bin/server ./cmd/server/main.go
RUN go build -o /go/bin/server

FROM run
COPY --from=build /go/bin/flowd /usr/local/bin/flowd
COPY --from=build /go/bin/server /usr/local/bin/server
CMD ["server"]
45 changes: 0 additions & 45 deletions cmd/flowd/main.go

This file was deleted.

3 changes: 1 addition & 2 deletions config-example.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
applications:
- name: example
trigger_id: xxxxxxxxxxxxxxxx
image_tag: gcr.io/$PROJECT_ID/foo
source_owner: sakajunquality
source_name: example-app
manifest_owner: sakajunquality
manifest_name: example-deployment
manifest_base_branch: master
image_tag: gcr.io/$PROJECT_ID/hoge
manifests:
- env: dev
files:
Expand Down
7 changes: 2 additions & 5 deletions flow/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ type Config struct {
}

type Application struct {
Name string `yaml:"name"`

TriggerID string `yaml:"trigger_id"`

Name string `yaml:"name"`
SourceOwner string `yaml:"source_owner"`
SourceName string `yaml:"source_name"`
ManifestOwner string `yaml:"manifest_owner"`
Expand All @@ -21,7 +18,7 @@ type Application struct {
RewriteVersion bool `yaml:"rewrite_version"`
RewriteNewTag bool `yaml:"rewrite_new_tag"`

ImageName string `yaml:"image_tag"`
Image string `yaml:"image_tag"`
Manifests []Manifest `yaml:"manifests"`
}

Expand Down
29 changes: 0 additions & 29 deletions flow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,6 @@ func New(c *Config) (*Flow, error) {
return f, nil
}

func (f *Flow) Start(ctx context.Context, errCh chan error) {
pubsubClient, err := pubsub.NewClient(ctx, f.projectID)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating pubsub client: %v.\n", err)
}

// Create Cloud Pub/Sub topic if not exist
topic := pubsubClient.Topic(pubsubTopicID)
exists, err := topic.Exists(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Error checking for topic: %v.\n", err)

}

// Create topic subscription
subscription = pubsubClient.Subscription(subName)
exists, err = subscription.Exists(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Error checking for subscription: %v.\n", err)
}
if !exists {
if _, err = pubsubClient.CreateSubscription(ctx, subName, pubsub.SubscriptionConfig{Topic: topic}); err != nil {
fmt.Fprintf(os.Stderr, "Failed to create subscription: %v.\n", err)
}
}

go f.subscribe(ctx, errCh)
}

func (f *Flow) ProcessGCREvent(ctx context.Context, e gcrevent.Event) error {
if e.Action != gcrevent.ActionInsert {
return nil
Expand Down
70 changes: 2 additions & 68 deletions flow/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"context"
"errors"
"fmt"
"os"
"strings"

"github.com/sakajunquality/cloud-pubsub-events/cloudbuildevent"
"github.com/sakajunquality/flow/gitbot"
"github.com/sakajunquality/flow/slackbot"
)
Expand All @@ -20,34 +18,6 @@ type PullRequest struct {
err error
}

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
}

if e.TriggerID == nil {
return errors.New("Only the triggered build is supported")
}

app, err := getApplicationByEventTriggerID(*e.TriggerID)
if err != nil {
return fmt.Errorf("No app is configured for %s", *e.TriggerID)
}

if !e.IsSuuccess() { // CloudBuild Failure
return f.notifyFalure(e, "", nil)
}

version, err := getVersionFromImage(e.Images)
if err != nil {
return f.notifyFalure(e, fmt.Sprintf("Could not ditermine version from image: %s", err), nil)
}

prs := f.generatePRs(ctx, app, version)
return f.notifyReleasePR(e.Images[0], version, prs, app)
}

func (f *Flow) processImage(ctx context.Context, image, version string) error {
app, err := getApplicationByImage(image)
if err != nil {
Expand Down Expand Up @@ -122,7 +92,7 @@ func (f *Flow) createReleasePR(ctx context.Context, version string, a Applicatio
release := gitbot.NewRelease(*repo, a.Name, m.Env, version, prBody)

for _, filePath := range m.Files {
release.AddChanges(filePath, fmt.Sprintf("%s:.*", a.ImageName), fmt.Sprintf("%s:%s", a.ImageName, version))
release.AddChanges(filePath, fmt.Sprintf("%s:.*", a.Image), fmt.Sprintf("%s:%s", a.Image, version))
if a.RewriteVersion {
release.AddChanges(filePath, "version: .*", fmt.Sprintf("version: %s", version))
}
Expand All @@ -135,8 +105,6 @@ func (f *Flow) createReleasePR(ctx context.Context, version string, a Applicatio
// Add Commit Author
release.AddAuthor(cfg.GitAuthor.Name, cfg.GitAuthor.Email)

fmt.Printf("%#v", release)

// Create a release PullRequest
prURL, err := release.Create(ctx, f.githubToken)
if err != nil {
Expand Down Expand Up @@ -169,21 +137,6 @@ func (f *Flow) notifyReleasePR(image, version string, prs PullRequests, app *App
return slackbot.NewSlackMessage(f.slackBotToken, cfg.SlackNotifiyChannel, d).Post()
}

func (f *Flow) notifyFalure(e cloudbuildevent.Event, errorMessage string, app *Application) error {
d := slackbot.MessageDetail{
IsSuccess: false,
ErrorMessage: errorMessage,
TagName: e.TagName,
BranchName: e.BranchName,
}

if app != nil {
d.AppName = app.Name
}

return slackbot.NewSlackMessage(f.slackBotToken, cfg.SlackNotifiyChannel, d).Post()
}

func getApplicationByEventRepoName(eventRepoName string) (*Application, error) {
for _, app := range cfg.ApplicationList {
// CloudBuild Repo Names
Expand All @@ -194,30 +147,11 @@ func getApplicationByEventRepoName(eventRepoName string) (*Application, error) {
return nil, errors.New("No application found for " + eventRepoName)
}

func getApplicationByEventTriggerID(eventTriggerID string) (*Application, error) {
for _, app := range cfg.ApplicationList {
if eventTriggerID == app.TriggerID {
return &app, nil
}
}
return nil, errors.New("No application found for " + eventTriggerID)
}

func getApplicationByImage(image string) (*Application, error) {
for _, app := range cfg.ApplicationList {
if image == app.ImageName {
if image == app.Image {
return &app, nil
}
}
return nil, errors.New("No application found for image " + image)
}

// Retrieve Docker Image tag from the built image
func getVersionFromImage(images []string) (string, error) {
if len(images) < 1 {
return "", errors.New("no images found")
}
// does not support multiple images
tags := strings.Split(images[0], ":")
return tags[1], nil
}
64 changes: 0 additions & 64 deletions flow/subscribe.go

This file was deleted.

File renamed without changes.

0 comments on commit e297415

Please sign in to comment.