forked from evergreen-ci/evergreen
/
build.go
129 lines (112 loc) · 3.91 KB
/
build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package data
import (
"net/http"
"fmt"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/build"
"github.com/evergreen-ci/gimlet"
"github.com/pkg/errors"
)
// DBBuildConnector is a struct that implements the Build related methods
// from the Connector through interactions with the backing database.
type DBBuildConnector struct{}
// FindBuildById uses the service layer's build type to query the backing database for
// a specific build.
func (bc *DBBuildConnector) FindBuildById(buildId string) (*build.Build, error) {
b, err := build.FindOne(build.ById(buildId))
if err != nil {
return nil, err
}
if b == nil {
return nil, gimlet.ErrorResponse{
StatusCode: http.StatusNotFound,
Message: fmt.Sprintf("build with id %s not found", buildId),
}
}
return b, nil
}
// FindProjectByBranch queries the project_refs database to find the name of the
// project a given branch falls under.
func (bc *DBBuildConnector) FindProjectByBranch(branch string) (*model.ProjectRef, error) {
return model.FindOneProjectRef(branch)
}
// AbortBuild wraps the service level AbortBuild
func (bc *DBBuildConnector) AbortBuild(buildId string, user string) error {
return model.AbortBuild(buildId, user)
}
// SetBuildPriority wraps the service level method
func (bc *DBBuildConnector) SetBuildPriority(buildId string, priority int64) error {
return model.SetBuildPriority(buildId, priority)
}
// SetBuildActivated wraps the service level method
func (bc *DBBuildConnector) SetBuildActivated(buildId string, user string, activated bool) error {
return model.SetBuildActivation(buildId, activated, user)
}
// RestartBuild wraps the service level RestartBuild
func (bc *DBBuildConnector) RestartBuild(buildId string, user string) error {
return model.RestartBuildTasks(buildId, user)
}
// MockBuildConnector is a struct that implements the Build related methods
// from the Connector through interactions with the backing database.
type MockBuildConnector struct {
CachedBuilds []build.Build
CachedProjects map[string]*model.ProjectRef
CachedAborted map[string]string
FailOnChangePriority bool
FailOnAbort bool
FailOnRestart bool
}
// FindBuildById iterates through the CachedBuilds slice to find the build
// with matching id field.
func (bc *MockBuildConnector) FindBuildById(buildId string) (*build.Build, error) {
for idx := range bc.CachedBuilds {
if bc.CachedBuilds[idx].Id == buildId {
return &bc.CachedBuilds[idx], nil
}
}
return nil, gimlet.ErrorResponse{
StatusCode: http.StatusNotFound,
Message: fmt.Sprintf("build with id %s not found", buildId),
}
}
// FindProjectByBranch accesses the map of branch names to project names to find
// the project corresponding to a given branch.
func (bc *MockBuildConnector) FindProjectByBranch(branch string) (*model.ProjectRef, error) {
proj, ok := bc.CachedProjects[branch]
if !ok {
return nil, nil
}
return proj, nil
}
// AbortBuild sets the value of the input build Id in CachedAborted to true.
func (bc *MockBuildConnector) AbortBuild(buildId string, user string) error {
if bc.FailOnAbort {
return errors.New("manufactured fail")
}
bc.CachedAborted[buildId] = user
return nil
}
// SetBuildPriority throws an error if instructed
func (bc *MockBuildConnector) SetBuildPriority(buildId string, priority int64) error {
if bc.FailOnChangePriority {
return errors.New("manufactured fail")
}
return nil
}
// SetBuildActivated sets the activated and activated_by fields on the input build.
func (bc *MockBuildConnector) SetBuildActivated(buildId string, user string, activated bool) error {
b, err := bc.FindBuildById(buildId)
if err != nil {
return err
}
b.Activated = activated
b.ActivatedBy = user
return nil
}
// RestartBuild does absolutely nothing
func (bc *MockBuildConnector) RestartBuild(buildId string, user string) error {
if bc.FailOnRestart {
return errors.New("manufactured error")
}
return nil
}