| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package buildbucket |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "strings" |
| 10 "time" |
| 11 |
| 12 "golang.org/x/net/context" |
| 13 |
| 14 "github.com/luci/luci-go/appengine/cmd/milo/resp" |
| 15 "github.com/luci/luci-go/appengine/gaeauth/client" |
| 16 "github.com/luci/luci-go/common/api/buildbucket/buildbucket/v1" |
| 17 "github.com/luci/luci-go/common/transport" |
| 18 ) |
| 19 |
| 20 const ( |
| 21 // StatusScheduled means a build is pending. |
| 22 StatusScheduled = "SCHEDULED" |
| 23 // StatusStarted means a build is executing. |
| 24 StatusStarted = "STARTED" |
| 25 // StatusCompleted means a build is completed (successfully or not). |
| 26 StatusCompleted = "COMPLETED" |
| 27 ) |
| 28 |
| 29 // ParseTags parses buildbucket build tags to a map. |
| 30 // Ignores tags that doesn't have colon (we don't have them in practice because |
| 31 // buildbucket validates tags). |
| 32 func ParseTags(tags []string) map[string]string { |
| 33 result := make(map[string]string, len(tags)) |
| 34 for _, t := range tags { |
| 35 parts := strings.SplitN(t, ":", 2) |
| 36 if len(parts) == 2 { |
| 37 result[parts[0]] = parts[1] |
| 38 } |
| 39 } |
| 40 return result |
| 41 } |
| 42 |
| 43 func newClient(c context.Context, server string) (*buildbucket.Service, error) { |
| 44 c, _ = context.WithTimeout(c, 60*time.Second) |
| 45 c = client.UseServiceAccountTransport(c, nil, nil) |
| 46 client, err := buildbucket.New(transport.GetClient(c)) |
| 47 if err != nil { |
| 48 return nil, err |
| 49 } |
| 50 client.BasePath = fmt.Sprintf("https://%s/api/buildbucket/v1/", server) |
| 51 return client, nil |
| 52 } |
| 53 |
| 54 // parseStatus converts a buildbucket build status to resp.Status. |
| 55 func parseStatus(build *buildbucket.ApiBuildMessage) (resp.Status, error) { |
| 56 switch build.Status { |
| 57 case StatusScheduled: |
| 58 return resp.NotRun, nil |
| 59 |
| 60 case StatusStarted: |
| 61 return resp.Running, nil |
| 62 |
| 63 case StatusCompleted: |
| 64 switch build.Result { |
| 65 case "SUCCESS": |
| 66 return resp.Success, nil |
| 67 |
| 68 case "FAILURE": |
| 69 switch build.FailureReason { |
| 70 case "BUILD_FAILURE": |
| 71 return resp.Failure, nil |
| 72 default: |
| 73 return resp.InfraFailure, nil |
| 74 } |
| 75 |
| 76 case "CANCELED": |
| 77 return resp.InfraFailure, nil |
| 78 |
| 79 default: |
| 80 return 0, fmt.Errorf("unexpected buildbucket build resul
t %q", build.Result) |
| 81 } |
| 82 |
| 83 default: |
| 84 return 0, fmt.Errorf("unexpected buildbucket build status %q", b
uild.Status) |
| 85 } |
| 86 } |
| 87 |
| 88 // getChangeList tries to extract CL information from a buildbucket build. |
| 89 func getChangeList(build *buildbucket.ApiBuildMessage, params *buildParameters,
resultDetails *resultDetails) *resp.Commit { |
| 90 var result *resp.Commit |
| 91 |
| 92 prop := ¶ms.Properties |
| 93 switch prop.PatchStorage { |
| 94 case "rietveld": |
| 95 if prop.RietveldURL != "" && prop.Issue != 0 { |
| 96 result = &resp.Commit{ |
| 97 Revision: resultDetails.Properties.GotRev
ision, |
| 98 RequestRevision: prop.Revision, |
| 99 Changelist: &resp.Link{ |
| 100 Label: fmt.Sprintf("Rietveld CL %d", pro
p.Issue), |
| 101 URL: fmt.Sprintf("%s/%d/#ps%d", prop.R
ietveldURL, prop.Issue, prop.PatchSet), |
| 102 }, |
| 103 } |
| 104 } |
| 105 |
| 106 case "gerrit": |
| 107 if prop.GerritURL != "" && prop.GerritChangeNumber != 0 { |
| 108 result = &resp.Commit{ |
| 109 Revision: prop.GerritPatchRef, |
| 110 RequestRevision: prop.GerritPatchRef, |
| 111 Changelist: &resp.Link{ |
| 112 Label: fmt.Sprintf("Gerrit CL %d", prop.
GerritChangeNumber), |
| 113 URL: prop.GerritChangeURL, |
| 114 }, |
| 115 } |
| 116 } |
| 117 } |
| 118 |
| 119 if result != nil && len(params.Changes) != 0 { |
| 120 // tryjobs have one change and it is the CL author |
| 121 result.AuthorEmail = params.Changes[0].Author.Email |
| 122 } |
| 123 |
| 124 return result |
| 125 } |
| OLD | NEW |