From ddabcde3e3e1fdc2f4f001683a30a210b633ebc6 Mon Sep 17 00:00:00 2001 From: Laszlo Fogas Date: Sat, 1 Jun 2019 12:52:02 +0200 Subject: [PATCH] Factored to a better place --- server/build.go | 1 + server/hook.go | 52 ++++++++++++++++++++++----------------- store/datastore/builds.go | 1 + 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/server/build.go b/server/build.go index f2833fe02..33161c3c6 100644 --- a/server/build.go +++ b/server/build.go @@ -393,6 +393,7 @@ func GetBuildQueue(c *gin.Context) { c.JSON(200, out) } +// PostBuild restarts a build func PostBuild(c *gin.Context) { remote_ := remote.FromContext(c) repo := session.Repo(c) diff --git a/server/hook.go b/server/hook.go index 2f6440fa6..3bf02e456 100644 --- a/server/hook.go +++ b/server/hook.go @@ -142,31 +142,18 @@ func PostHook(c *gin.Context) { } } - // fetch the build file from the database - confb, err := remote.FileBackoff(remote_, user, repo, build, repo.Config) + // fetch the build file from the remote + remoteYamlConfig, err := remote.FileBackoff(remote_, user, repo, build, repo.Config) if err != nil { logrus.Errorf("error: %s: cannot find %s in %s: %s", repo.FullName, repo.Config, build.Ref, err) c.AbortWithError(404, err) return } - sha := shasum(confb) - conf, err := Config.Storage.Config.ConfigFind(repo, sha) + conf, err := findOrPersistPipelineConfig(repo, remoteYamlConfig) if err != nil { - conf = &model.Config{ - RepoID: repo.ID, - Data: string(confb), - Hash: sha, - } - err = Config.Storage.Config.ConfigCreate(conf) - if err != nil { - // retry in case we receive two hooks at the same time - conf, err = Config.Storage.Config.ConfigFind(repo, sha) - if err != nil { - logrus.Errorf("failure to find or persist build config for %s. %s", repo.FullName, err) - c.AbortWithError(500, err) - return - } - } + logrus.Errorf("failure to find or persist build config for %s. %s", repo.FullName, err) + c.AbortWithError(500, err) + return } build.ConfigID = conf.ID @@ -177,9 +164,9 @@ func PostHook(c *gin.Context) { } // verify the branches can be built vs skipped - branches, err := yaml.ParseString(conf.Data) + parsedPipelineConfig, err := yaml.ParseString(conf.Data) if err == nil { - if !branches.Branches.Match(build.Branch) && build.Event != model.EventTag && build.Event != model.EventDeploy { + if !parsedPipelineConfig.Branches.Match(build.Branch) && build.Event != model.EventTag && build.Event != model.EventDeploy { c.String(200, "Branch does not match restrictions defined in yaml") return } @@ -197,7 +184,6 @@ func PostHook(c *gin.Context) { } } - build.Trim() err = store.CreateBuild(c, build, build.Procs...) if err != nil { logrus.Errorf("failure to save commit for %s. %s", repo.FullName, err) @@ -277,6 +263,28 @@ func PostHook(c *gin.Context) { queueBuild(build, repo, buildItems) } +func findOrPersistPipelineConfig(repo *model.Repo, remoteYamlConfig []byte) (*model.Config, error) { + sha := shasum(remoteYamlConfig) + conf, err := Config.Storage.Config.ConfigFind(repo, sha) + if err != nil { + conf = &model.Config{ + RepoID: repo.ID, + Data: string(remoteYamlConfig), + Hash: sha, + } + err = Config.Storage.Config.ConfigCreate(conf) + if err != nil { + // retry in case we receive two hooks at the same time + conf, err = Config.Storage.Config.ConfigFind(repo, sha) + if err != nil { + return nil, err + } + } + } + + return conf, nil +} + func setBuildProcs(build *model.Build, buildItems []*buildItem) { pcounter := len(buildItems) for _, item := range buildItems { diff --git a/store/datastore/builds.go b/store/datastore/builds.go index 5a347fc4f..ed7977152 100644 --- a/store/datastore/builds.go +++ b/store/datastore/builds.go @@ -72,6 +72,7 @@ func (db *datastore) GetBuildQueue() ([]*model.Feed, error) { } func (db *datastore) CreateBuild(build *model.Build, procs ...*model.Proc) error { + build.Trim() id, err := db.incrementRepoRetry(build.RepoID) if err != nil { return err