| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package buildbucket | 5 package buildbucket |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/json" | 8 "encoding/json" |
| 9 "errors" | |
| 10 "fmt" | 9 "fmt" |
| 11 "net/http" | 10 "net/http" |
| 12 "strings" | 11 "strings" |
| 13 | 12 |
| 14 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
| 15 | 14 |
| 16 "github.com/luci/gae/service/datastore" | 15 "github.com/luci/gae/service/datastore" |
| 17 bucketApi "github.com/luci/luci-go/common/api/buildbucket/buildbucket/v1
" | 16 bucketApi "github.com/luci/luci-go/common/api/buildbucket/buildbucket/v1
" |
| 18 "github.com/luci/luci-go/common/clock" | 17 "github.com/luci/luci-go/common/clock" |
| 18 "github.com/luci/luci-go/common/errors" |
| 19 "github.com/luci/luci-go/common/logging" | 19 "github.com/luci/luci-go/common/logging" |
| 20 "github.com/luci/luci-go/common/retry/transient" | 20 "github.com/luci/luci-go/common/retry/transient" |
| 21 "github.com/luci/luci-go/common/tsmon/field" | 21 "github.com/luci/luci-go/common/tsmon/field" |
| 22 "github.com/luci/luci-go/common/tsmon/metric" | 22 "github.com/luci/luci-go/common/tsmon/metric" |
| 23 "github.com/luci/luci-go/milo/api/resp" | 23 "github.com/luci/luci-go/milo/api/resp" |
| 24 "github.com/luci/luci-go/milo/buildsource/swarming" | 24 "github.com/luci/luci-go/milo/buildsource/swarming" |
| 25 "github.com/luci/luci-go/milo/common" | 25 "github.com/luci/luci-go/milo/common" |
| 26 "github.com/luci/luci-go/milo/common/model" | 26 "github.com/luci/luci-go/milo/common/model" |
| 27 "github.com/luci/luci-go/server/router" | 27 "github.com/luci/luci-go/server/router" |
| 28 ) | 28 ) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 46 Hostname string | 46 Hostname string |
| 47 } | 47 } |
| 48 | 48 |
| 49 var ( | 49 var ( |
| 50 errNoLogLocation = errors.New("log_location tag not found") | 50 errNoLogLocation = errors.New("log_location tag not found") |
| 51 errNoProject = errors.New("project tag not found") | 51 errNoProject = errors.New("project tag not found") |
| 52 ) | 52 ) |
| 53 | 53 |
| 54 type parameters struct { | 54 type parameters struct { |
| 55 BuilderName string `json:"builder_name"` | 55 BuilderName string `json:"builder_name"` |
| 56 Properties string `json:"properties"` | |
| 57 } | 56 } |
| 58 | 57 |
| 59 func isLUCI(build *bucketApi.ApiCommonBuildMessage) bool { | 58 func isLUCI(build *bucketApi.ApiCommonBuildMessage) bool { |
| 60 // All luci buckets are assumed to be prefixed with luci. | 59 // All luci buckets are assumed to be prefixed with luci. |
| 61 return strings.HasPrefix(build.Bucket, "luci.") | 60 return strings.HasPrefix(build.Bucket, "luci.") |
| 62 } | 61 } |
| 63 | 62 |
| 64 // PubSubHandler is a webhook that stores the builds coming in from pubsub. | 63 // PubSubHandler is a webhook that stores the builds coming in from pubsub. |
| 65 func PubSubHandler(ctx *router.Context) { | 64 func PubSubHandler(ctx *router.Context) { |
| 66 err := pubSubHandlerImpl(ctx.Context, ctx.Request) | 65 err := pubSubHandlerImpl(ctx.Context, ctx.Request) |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 entry.respBuild.SummarizeTo(&bs) | 167 entry.respBuild.SummarizeTo(&bs) |
| 169 } | 168 } |
| 170 logging.Debugf(c, "Created build summary: %#v", bs) | 169 logging.Debugf(c, "Created build summary: %#v", bs) |
| 171 // Make datastore flakes transient errors | 170 // Make datastore flakes transient errors |
| 172 return transient.Tag.Apply(datastore.Put(c, &bs)) | 171 return transient.Tag.Apply(datastore.Put(c, &bs)) |
| 173 } | 172 } |
| 174 | 173 |
| 175 func handlePubSubBuild(c context.Context, data *psMsg) error { | 174 func handlePubSubBuild(c context.Context, data *psMsg) error { |
| 176 host := data.Hostname | 175 host := data.Hostname |
| 177 build := &data.Build | 176 build := &data.Build |
| 177 // We only care about the "builder_name" key from the parameter. |
| 178 p := parameters{} | 178 p := parameters{} |
| 179 err := json.Unmarshal([]byte(build.ParametersJson), &p) | 179 err := json.Unmarshal([]byte(build.ParametersJson), &p) |
| 180 if err != nil { | 180 if err != nil { |
| 181 » » logging.WithError(err).Errorf(c, "could not unmarshal build para
meters") | 181 » » err = errors.Annotate( |
| 182 » » » err, "could not unmarshal build parameters %s", build.Pa
rametersJson).Err() |
| 182 buildCounter.Add(c, 1, build.Bucket, isLUCI(build), build.Status
, "Rejected") | 183 buildCounter.Add(c, 1, build.Bucket, isLUCI(build), build.Status
, "Rejected") |
| 183 // Permanent error, since this is probably a type of build we do
not recognize. | 184 // Permanent error, since this is probably a type of build we do
not recognize. |
| 184 return err | 185 return err |
| 185 } | 186 } |
| 186 logging.Debugf(c, "Received from %s: build %s/%s (%s)\n%s", | 187 logging.Debugf(c, "Received from %s: build %s/%s (%s)\n%s", |
| 187 host, build.Bucket, p.BuilderName, build.Status, build) | 188 host, build.Bucket, p.BuilderName, build.Status, build) |
| 188 if !isLUCI(build) { | 189 if !isLUCI(build) { |
| 189 logging.Infof(c, "This is not a luci build, ignoring") | 190 logging.Infof(c, "This is not a luci build, ignoring") |
| 190 buildCounter.Add(c, 1, build.Bucket, isLUCI(build), build.Status
, "Rejected") | 191 buildCounter.Add(c, 1, build.Bucket, isLUCI(build), build.Status
, "Rejected") |
| 191 return nil | 192 return nil |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 logging.WithError(err).Errorf(c, "could not parse pubsub message
string") | 228 logging.WithError(err).Errorf(c, "could not parse pubsub message
string") |
| 228 return err | 229 return err |
| 229 } | 230 } |
| 230 if err := json.Unmarshal(bData, &data); err != nil { | 231 if err := json.Unmarshal(bData, &data); err != nil { |
| 231 logging.WithError(err).Errorf(c, "could not parse pubsub message
data") | 232 logging.WithError(err).Errorf(c, "could not parse pubsub message
data") |
| 232 return err | 233 return err |
| 233 } | 234 } |
| 234 | 235 |
| 235 return handlePubSubBuild(c, &data) | 236 return handlePubSubBuild(c, &data) |
| 236 } | 237 } |
| OLD | NEW |