88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
|
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||
|
// Use of this source code is governed by the Drone Non-Commercial License
|
||
|
// that can be found in the LICENSE file.
|
||
|
|
||
|
package manager
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
|
||
|
"github.com/drone/drone/core"
|
||
|
|
||
|
"github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
type updater struct {
|
||
|
Builds core.BuildStore
|
||
|
Events core.Pubsub
|
||
|
Repos core.RepositoryStore
|
||
|
Steps core.StepStore
|
||
|
Stages core.StageStore
|
||
|
Webhook core.WebhookSender
|
||
|
}
|
||
|
|
||
|
func (u *updater) do(ctx context.Context, step *core.Step) error {
|
||
|
logger := logrus.WithFields(
|
||
|
logrus.Fields{
|
||
|
"step.status": step.Status,
|
||
|
"step.name": step.Name,
|
||
|
"step.id": step.ID,
|
||
|
},
|
||
|
)
|
||
|
|
||
|
err := u.Steps.Update(noContext, step)
|
||
|
if err != nil {
|
||
|
logger.WithError(err).Warnln("manager: cannot update step")
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
stage, err := u.Stages.Find(noContext, step.StageID)
|
||
|
if err != nil {
|
||
|
logger.WithError(err).Warnln("manager: cannot find stage")
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
build, err := u.Builds.Find(noContext, stage.BuildID)
|
||
|
if err != nil {
|
||
|
logger.WithError(err).Warnln("manager: cannot find build")
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
repo, err := u.Repos.Find(noContext, build.RepoID)
|
||
|
if err != nil {
|
||
|
logger.WithError(err).Warnln("manager: cannot find repo")
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
stages, err := u.Stages.ListSteps(noContext, build.ID)
|
||
|
if err != nil {
|
||
|
logger.WithError(err).Warnln("manager: cannot list stages")
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
repo.Build = build
|
||
|
repo.Build.Stages = stages
|
||
|
data, _ := json.Marshal(repo)
|
||
|
err = u.Events.Publish(noContext, &core.Message{
|
||
|
Repository: repo.Slug,
|
||
|
Visibility: repo.Visibility,
|
||
|
Data: data,
|
||
|
})
|
||
|
if err != nil {
|
||
|
logger.WithError(err).Warnln("manager: cannot publish build event")
|
||
|
}
|
||
|
|
||
|
payload := &core.WebhookData{
|
||
|
Event: core.WebhookEventBuild,
|
||
|
Action: core.WebhookActionUpdated,
|
||
|
Repo: repo,
|
||
|
Build: build,
|
||
|
}
|
||
|
err = u.Webhook.Send(noContext, payload)
|
||
|
if err != nil {
|
||
|
logger.WithError(err).Warnln("manager: cannot send global webhook")
|
||
|
}
|
||
|
return nil
|
||
|
}
|