Factored to a better place

This commit is contained in:
Laszlo Fogas
2019-06-01 12:52:02 +02:00
parent 21d6552b4b
commit ddabcde3e3
3 changed files with 32 additions and 22 deletions

View File

@@ -393,6 +393,7 @@ func GetBuildQueue(c *gin.Context) {
c.JSON(200, out) c.JSON(200, out)
} }
// PostBuild restarts a build
func PostBuild(c *gin.Context) { func PostBuild(c *gin.Context) {
remote_ := remote.FromContext(c) remote_ := remote.FromContext(c)
repo := session.Repo(c) repo := session.Repo(c)

View File

@@ -142,31 +142,18 @@ func PostHook(c *gin.Context) {
} }
} }
// fetch the build file from the database // fetch the build file from the remote
confb, err := remote.FileBackoff(remote_, user, repo, build, repo.Config) remoteYamlConfig, err := remote.FileBackoff(remote_, user, repo, build, repo.Config)
if err != nil { if err != nil {
logrus.Errorf("error: %s: cannot find %s in %s: %s", repo.FullName, repo.Config, build.Ref, err) logrus.Errorf("error: %s: cannot find %s in %s: %s", repo.FullName, repo.Config, build.Ref, err)
c.AbortWithError(404, err) c.AbortWithError(404, err)
return return
} }
sha := shasum(confb) conf, err := findOrPersistPipelineConfig(repo, remoteYamlConfig)
conf, err := Config.Storage.Config.ConfigFind(repo, sha)
if err != nil { if err != nil {
conf = &model.Config{ logrus.Errorf("failure to find or persist build config for %s. %s", repo.FullName, err)
RepoID: repo.ID, c.AbortWithError(500, err)
Data: string(confb), return
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
}
}
} }
build.ConfigID = conf.ID build.ConfigID = conf.ID
@@ -177,9 +164,9 @@ func PostHook(c *gin.Context) {
} }
// verify the branches can be built vs skipped // verify the branches can be built vs skipped
branches, err := yaml.ParseString(conf.Data) parsedPipelineConfig, err := yaml.ParseString(conf.Data)
if err == nil { 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") c.String(200, "Branch does not match restrictions defined in yaml")
return return
} }
@@ -197,7 +184,6 @@ func PostHook(c *gin.Context) {
} }
} }
build.Trim()
err = store.CreateBuild(c, build, build.Procs...) err = store.CreateBuild(c, build, build.Procs...)
if err != nil { if err != nil {
logrus.Errorf("failure to save commit for %s. %s", repo.FullName, err) 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) 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) { func setBuildProcs(build *model.Build, buildItems []*buildItem) {
pcounter := len(buildItems) pcounter := len(buildItems)
for _, item := range buildItems { for _, item := range buildItems {

View File

@@ -72,6 +72,7 @@ func (db *datastore) GetBuildQueue() ([]*model.Feed, error) {
} }
func (db *datastore) CreateBuild(build *model.Build, procs ...*model.Proc) error { func (db *datastore) CreateBuild(build *model.Build, procs ...*model.Proc) error {
build.Trim()
id, err := db.incrementRepoRetry(build.RepoID) id, err := db.incrementRepoRetry(build.RepoID)
if err != nil { if err != nil {
return err return err