-
Notifications
You must be signed in to change notification settings - Fork 86
/
accumulator.go
219 lines (194 loc) · 6.93 KB
/
accumulator.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package accumulator
import (
"strings"
"time"
"github.com/buildbuddy-io/buildbuddy/proto/build_event_stream"
"github.com/buildbuddy-io/buildbuddy/proto/command_line"
"github.com/buildbuddy-io/buildbuddy/server/build_event_protocol/invocation_format"
"github.com/buildbuddy-io/buildbuddy/server/util/timeutil"
gitutil "github.com/buildbuddy-io/buildbuddy/server/util/git"
)
const (
repoURLFieldName = "repoURL"
branchNameFieldName = "branchName"
commitSHAFieldName = "commitSHA"
roleFieldName = "role"
commandFieldName = "command"
patternFieldName = "pattern"
workflowIDFieldName = "workflowID"
actionNameFieldName = "actionName"
)
var (
buildMetadataFieldMapping = map[string]string{
"REPO_URL": repoURLFieldName,
"BRANCH_NAME": branchNameFieldName,
"COMMIT_SHA": commitSHAFieldName,
"ROLE": roleFieldName,
}
)
// BEValues is an in-memory data structure created for each new stream of build
// events. Every event in the stream is passed through BEValues, which extracts
// common values that many functions are interested in. By holding a reference
// to BEValues, those functions can obtain those common values without
// duplicating the BES parsing logic.
//
// N.B. Commonly extracted values should be added here. BEValues is held in
// memory for the life of the stream, so it should not save every single event
// in full (that data lives in blobstore).
type BEValues struct {
valuesMap map[string]string
invocationID string
sawWorkspaceStatusEvent bool
buildStartTime time.Time
}
func NewBEValues(invocationID string) *BEValues {
return &BEValues{
invocationID: invocationID,
valuesMap: make(map[string]string, 0),
sawWorkspaceStatusEvent: false,
}
}
func (v *BEValues) AddEvent(event *build_event_stream.BuildEvent) {
switch p := event.Payload.(type) {
case *build_event_stream.BuildEvent_Started:
v.handleStartedEvent(event)
case *build_event_stream.BuildEvent_StructuredCommandLine:
v.populateWorkspaceInfoFromStructuredCommandLine(p.StructuredCommandLine)
case *build_event_stream.BuildEvent_BuildMetadata:
v.populateWorkspaceInfoFromBuildMetadata(p.BuildMetadata)
case *build_event_stream.BuildEvent_WorkspaceStatus:
v.populateWorkspaceInfoFromWorkspaceStatus(p.WorkspaceStatus)
v.sawWorkspaceStatusEvent = true
case *build_event_stream.BuildEvent_WorkflowConfigured:
v.handleWorkflowConfigured(p.WorkflowConfigured)
}
}
func (v *BEValues) InvocationID() string {
return v.invocationID
}
func (v *BEValues) StartTime() time.Time {
return v.buildStartTime
}
func (v *BEValues) RepoURL() string {
return v.getStringValue(repoURLFieldName)
}
func (v *BEValues) BranchName() string {
return v.getStringValue(branchNameFieldName)
}
func (v *BEValues) CommitSHA() string {
return v.getStringValue(commitSHAFieldName)
}
func (v *BEValues) Role() string {
return v.getStringValue(roleFieldName)
}
func (v *BEValues) Command() string {
return v.getStringValue(commandFieldName)
}
func (v *BEValues) Pattern() string {
return v.getStringValue(patternFieldName)
}
func (v *BEValues) WorkflowID() string {
return v.getStringValue(workflowIDFieldName)
}
func (v *BEValues) ActionName() string {
return v.getStringValue(actionNameFieldName)
}
func (v *BEValues) WorkspaceIsLoaded() bool {
return v.sawWorkspaceStatusEvent
}
func (v *BEValues) getStringValue(fieldName string) string {
if existing, ok := v.valuesMap[fieldName]; ok {
return existing
}
return ""
}
func (v *BEValues) setStringValue(fieldName, proposedValue string) bool {
if fieldName == repoURLFieldName {
proposedValue = gitutil.StripRepoURLCredentials(proposedValue)
}
existing, ok := v.valuesMap[fieldName]
if ok && existing != "" {
return false
}
v.valuesMap[fieldName] = proposedValue
return true
}
func (v *BEValues) handleStartedEvent(event *build_event_stream.BuildEvent) {
v.setStringValue(commandFieldName, event.GetStarted().Command)
v.setStringValue(patternFieldName, patternFromEvent(event))
v.buildStartTime = timeutil.FromMillis(event.GetStarted().GetStartTimeMillis())
}
func (v *BEValues) populateWorkspaceInfoFromStructuredCommandLine(commandLine *command_line.CommandLine) {
for _, section := range commandLine.Sections {
if list := section.GetOptionList(); list == nil {
continue
}
for _, option := range section.GetOptionList().Option {
if option.OptionName != "ENV" && option.OptionName != "client_env" {
continue
}
parts := strings.Split(option.OptionValue, "=")
if len(parts) != 2 {
continue
}
environmentVariable := parts[0]
value := parts[1]
switch environmentVariable {
case "CIRCLE_REPOSITORY_URL", "GITHUB_REPOSITORY", "BUILDKITE_REPO", "TRAVIS_REPO_SLUG", "GIT_URL", "CI_REPOSITORY_URL", "REPO_URL":
v.setStringValue(repoURLFieldName, value)
case "CIRCLE_BRANCH", "GITHUB_HEAD_REF", "BUILDKITE_BRANCH", "TRAVIS_BRANCH", "GIT_BRANCH", "CI_COMMIT_BRANCH":
v.setStringValue(branchNameFieldName, value)
case "GITHUB_REF":
// GITHUB_REF can contain tag information instead of branch information,
// so we have to check that this is a branch before attempting to strip
// the prefix. Additionally, if this is a pull request, we want to use
// the value from GITHUB_HEAD_REF instead, so if the branch name is
// already populated, we don't overwrite it.
if strings.HasPrefix(value, "refs/heads/") && v.getStringValue(branchNameFieldName) == "" {
v.setStringValue(branchNameFieldName, strings.TrimPrefix(value, "refs/heads/"))
}
case "CIRCLE_SHA1", "GITHUB_SHA", "BUILDKITE_COMMIT", "TRAVIS_COMMIT", "GIT_COMMIT", "CI_COMMIT_SHA", "COMMIT_SHA":
v.setStringValue(commitSHAFieldName, value)
case "CI":
v.setStringValue(roleFieldName, "CI")
case "CI_RUNNER":
v.setStringValue(roleFieldName, "CI_RUNNER")
}
}
}
}
func (v *BEValues) populateWorkspaceInfoFromBuildMetadata(metadata *build_event_stream.BuildMetadata) {
for mdKey, mdVal := range metadata.Metadata {
if fieldName := buildMetadataFieldMapping[mdKey]; fieldName != "" {
v.setStringValue(fieldName, mdVal)
}
}
}
func (v *BEValues) populateWorkspaceInfoFromWorkspaceStatus(workspace *build_event_stream.WorkspaceStatus) {
for _, item := range workspace.Item {
if item.Key == "REPO_URL" {
v.setStringValue(repoURLFieldName, item.Value)
}
if item.Key == "BRANCH_NAME" {
v.setStringValue(branchNameFieldName, item.Value)
}
if item.Key == "COMMIT_SHA" {
v.setStringValue(commitSHAFieldName, item.Value)
}
}
}
func (v *BEValues) handleWorkflowConfigured(wfc *build_event_stream.WorkflowConfigured) {
v.setStringValue(workflowIDFieldName, wfc.GetWorkflowId())
v.setStringValue(actionNameFieldName, wfc.GetActionName())
}
func patternFromEvent(event *build_event_stream.BuildEvent) string {
for _, child := range event.Children {
switch c := child.Id.(type) {
case *build_event_stream.BuildEventId_Pattern:
{
return invocation_format.ShortFormatPatterns(c.Pattern.Pattern)
}
}
}
return ""
}