| OLD | NEW | 
|---|
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 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         "fmt" | 8         "fmt" | 
| 9         "net/http" | 9         "net/http" | 
| 10         "strings" | 10         "strings" | 
| 11         "time" | 11         "time" | 
| 12 | 12 | 
| 13         "golang.org/x/net/context" | 13         "golang.org/x/net/context" | 
| 14 | 14 | 
| 15         "github.com/luci/luci-go/common/api/buildbucket/buildbucket/v1" | 15         "github.com/luci/luci-go/common/api/buildbucket/buildbucket/v1" | 
| 16         "github.com/luci/luci-go/common/api/buildbucket/swarmbucket/v1" | 16         "github.com/luci/luci-go/common/api/buildbucket/swarmbucket/v1" | 
| 17         "github.com/luci/luci-go/milo/api/resp" | 17         "github.com/luci/luci-go/milo/api/resp" | 
|  | 18         "github.com/luci/luci-go/milo/appengine/common/model" | 
| 18         "github.com/luci/luci-go/server/auth" | 19         "github.com/luci/luci-go/server/auth" | 
| 19 ) | 20 ) | 
| 20 | 21 | 
| 21 const ( | 22 const ( | 
| 22         // StatusScheduled means a build is pending. | 23         // StatusScheduled means a build is pending. | 
| 23         StatusScheduled = "SCHEDULED" | 24         StatusScheduled = "SCHEDULED" | 
| 24         // StatusStarted means a build is executing. | 25         // StatusStarted means a build is executing. | 
| 25         StatusStarted = "STARTED" | 26         StatusStarted = "STARTED" | 
| 26         // StatusCompleted means a build is completed (successfully or not). | 27         // StatusCompleted means a build is completed (successfully or not). | 
| 27         StatusCompleted = "COMPLETED" | 28         StatusCompleted = "COMPLETED" | 
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 63         } | 64         } | 
| 64         client, err := buildbucket.New(&http.Client{Transport: t}) | 65         client, err := buildbucket.New(&http.Client{Transport: t}) | 
| 65         if err != nil { | 66         if err != nil { | 
| 66                 return nil, err | 67                 return nil, err | 
| 67         } | 68         } | 
| 68         client.BasePath = fmt.Sprintf("https://%s/api/buildbucket/v1/", server) | 69         client.BasePath = fmt.Sprintf("https://%s/api/buildbucket/v1/", server) | 
| 69         return client, nil | 70         return client, nil | 
| 70 } | 71 } | 
| 71 | 72 | 
| 72 // parseStatus converts a buildbucket build status to resp.Status. | 73 // parseStatus converts a buildbucket build status to resp.Status. | 
| 73 func parseStatus(build *buildbucket.ApiCommonBuildMessage) (resp.Status, error) 
     { | 74 func parseStatus(build *buildbucket.ApiCommonBuildMessage) (model.Status, error)
      { | 
| 74         switch build.Status { | 75         switch build.Status { | 
| 75         case StatusScheduled: | 76         case StatusScheduled: | 
| 76 »       »       return resp.NotRun, nil | 77 »       »       return model.NotRun, nil | 
| 77 | 78 | 
| 78         case StatusStarted: | 79         case StatusStarted: | 
| 79 »       »       return resp.Running, nil | 80 »       »       return model.Running, nil | 
| 80 | 81 | 
| 81         case StatusCompleted: | 82         case StatusCompleted: | 
| 82                 switch build.Result { | 83                 switch build.Result { | 
| 83                 case "SUCCESS": | 84                 case "SUCCESS": | 
| 84 »       »       »       return resp.Success, nil | 85 »       »       »       return model.Success, nil | 
| 85 | 86 | 
| 86                 case "FAILURE": | 87                 case "FAILURE": | 
| 87                         switch build.FailureReason { | 88                         switch build.FailureReason { | 
| 88                         case "BUILD_FAILURE": | 89                         case "BUILD_FAILURE": | 
| 89 »       »       »       »       return resp.Failure, nil | 90 »       »       »       »       return model.Failure, nil | 
| 90                         default: | 91                         default: | 
| 91 »       »       »       »       return resp.InfraFailure, nil | 92 »       »       »       »       return model.InfraFailure, nil | 
| 92                         } | 93                         } | 
| 93 | 94 | 
| 94                 case "CANCELED": | 95                 case "CANCELED": | 
| 95 »       »       »       return resp.InfraFailure, nil | 96 »       »       »       return model.InfraFailure, nil | 
| 96 | 97 | 
| 97                 default: | 98                 default: | 
| 98                         return 0, fmt.Errorf("unexpected buildbucket build resul
     t %q", build.Result) | 99                         return 0, fmt.Errorf("unexpected buildbucket build resul
     t %q", build.Result) | 
| 99                 } | 100                 } | 
| 100 | 101 | 
| 101         default: | 102         default: | 
| 102                 return 0, fmt.Errorf("unexpected buildbucket build status %q", b
     uild.Status) | 103                 return 0, fmt.Errorf("unexpected buildbucket build status %q", b
     uild.Status) | 
| 103         } | 104         } | 
| 104 } | 105 } | 
| 105 | 106 | 
| 106 // getChangeList tries to extract CL information from a buildbucket build. | 107 // getChangeList tries to extract CL information from a buildbucket build. | 
| 107 func getChangeList( | 108 func getChangeList( | 
| 108         build *buildbucket.ApiCommonBuildMessage, params *buildParameters, | 109         build *buildbucket.ApiCommonBuildMessage, params *buildParameters, | 
| 109         resultDetails *resultDetails) (result *resp.Commit) { | 110         resultDetails *resultDetails) (result *resp.Commit) { | 
| 110 | 111 | 
| 111         prop := ¶ms.Properties | 112         prop := ¶ms.Properties | 
| 112         switch prop.PatchStorage { | 113         switch prop.PatchStorage { | 
| 113         case "rietveld": | 114         case "rietveld": | 
| 114                 if prop.RietveldURL != "" && prop.Issue != 0 { | 115                 if prop.RietveldURL != "" && prop.Issue != 0 { | 
| 115                         result = &resp.Commit{ | 116                         result = &resp.Commit{ | 
| 116 »       »       »       »       RequestRevision: &resp.Link{Label: prop.Revision
     }, | 117 »       »       »       »       RequestRevision: resp.NewLink(prop.Revision, "")
     , | 
| 117 »       »       »       »       Changelist: &resp.Link{ | 118 »       »       »       »       Changelist: resp.NewLink( | 
| 118 »       »       »       »       »       Label: fmt.Sprintf("Rietveld CL %d", pro
     p.Issue), | 119 »       »       »       »       »       fmt.Sprintf("Rietveld CL %d", prop.Issue
     ), | 
| 119 »       »       »       »       »       URL:   fmt.Sprintf("%s/%d/#ps%d", prop.R
     ietveldURL, prop.Issue, prop.PatchSet), | 120 »       »       »       »       »       fmt.Sprintf("%s/%d/#ps%d", prop.Rietveld
     URL, prop.Issue, prop.PatchSet), | 
| 120 »       »       »       »       }, | 121 »       »       »       »       ), | 
| 121                         } | 122                         } | 
| 122                         if resultDetails.Properties.GotRevision != "" { | 123                         if resultDetails.Properties.GotRevision != "" { | 
| 123                                 // TODO(hinoka): Figure out the full URL for the
     se revisions, add it | 124                                 // TODO(hinoka): Figure out the full URL for the
     se revisions, add it | 
| 124                                 // to the URL field. | 125                                 // to the URL field. | 
| 125 »       »       »       »       result.Revision = &resp.Link{Label: resultDetail
     s.Properties.GotRevision} | 126 »       »       »       »       result.Revision = resp.NewLink(resultDetails.Pro
     perties.GotRevision, "") | 
| 126                         } | 127                         } | 
| 127                 } | 128                 } | 
| 128 | 129 | 
| 129         case "gerrit": | 130         case "gerrit": | 
| 130                 if prop.GerritPatchURL != "" && prop.GerritPatchIssue != 0 { | 131                 if prop.GerritPatchURL != "" && prop.GerritPatchIssue != 0 { | 
| 131                         path := fmt.Sprintf("%d", prop.GerritPatchIssue) | 132                         path := fmt.Sprintf("%d", prop.GerritPatchIssue) | 
| 132                         if prop.GerritPatchSet != 0 { | 133                         if prop.GerritPatchSet != 0 { | 
| 133                                 path = fmt.Sprintf("%d/%d", prop.GerritPatchIssu
     e, prop.GerritPatchSet) | 134                                 path = fmt.Sprintf("%d/%d", prop.GerritPatchIssu
     e, prop.GerritPatchSet) | 
| 134                         } | 135                         } | 
| 135                         result = &resp.Commit{ | 136                         result = &resp.Commit{ | 
| 136 »       »       »       »       Changelist: &resp.Link{ | 137 »       »       »       »       Changelist: resp.NewLink( | 
| 137 »       »       »       »       »       Label: fmt.Sprintf("Gerrit CL %d", prop.
     GerritPatchIssue), | 138 »       »       »       »       »       fmt.Sprintf("Gerrit CL %d", prop.GerritP
     atchIssue), | 
| 138 »       »       »       »       »       URL: fmt.Sprintf( | 139 »       »       »       »       »       fmt.Sprintf("%s/c/%s", prop.GerritPatchU
     RL, path), | 
| 139 »       »       »       »       »       »       "%s/c/%s", prop.GerritPatchURL, 
     path), | 140 »       »       »       »       ), | 
| 140 »       »       »       »       }, |  | 
| 141                         } | 141                         } | 
| 142                 } | 142                 } | 
| 143         } | 143         } | 
| 144 | 144 | 
| 145         if result != nil && len(params.Changes) != 0 { | 145         if result != nil && len(params.Changes) != 0 { | 
| 146                 // tryjobs have one change and it is the CL author | 146                 // tryjobs have one change and it is the CL author | 
| 147                 result.AuthorEmail = params.Changes[0].Author.Email | 147                 result.AuthorEmail = params.Changes[0].Author.Email | 
| 148         } | 148         } | 
| 149 | 149 | 
| 150         return | 150         return | 
| 151 } | 151 } | 
| OLD | NEW | 
|---|