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)
}
// PostBuild restarts a build
func PostBuild(c *gin.Context) {
remote_ := remote.FromContext(c)
repo := session.Repo(c)

View file

@ -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 {

View file

@ -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