| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. | 1 // Copyright 2016 The LUCI Authors. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 "golang.org/x/net/context" | 22 "golang.org/x/net/context" |
| 23 "google.golang.org/grpc" | 23 "google.golang.org/grpc" |
| 24 "google.golang.org/grpc/codes" | 24 "google.golang.org/grpc/codes" |
| 25 | 25 |
| 26 "github.com/golang/protobuf/ptypes/timestamp" | 26 "github.com/golang/protobuf/ptypes/timestamp" |
| 27 "github.com/luci/gae/service/datastore" | 27 "github.com/luci/gae/service/datastore" |
| 28 "github.com/luci/luci-go/common/iotools" | 28 "github.com/luci/luci-go/common/iotools" |
| 29 "github.com/luci/luci-go/common/logging" | 29 "github.com/luci/luci-go/common/logging" |
| 30 milo "github.com/luci/luci-go/milo/api/proto" | 30 milo "github.com/luci/luci-go/milo/api/proto" |
| 31 "github.com/luci/luci-go/milo/common" |
| 31 "github.com/luci/luci-go/server/auth" | 32 "github.com/luci/luci-go/server/auth" |
| 32 ) | 33 ) |
| 33 | 34 |
| 34 // Service is a service implementation that displays BuildBot builds. | 35 // Service is a service implementation that displays BuildBot builds. |
| 35 type Service struct{} | 36 type Service struct{} |
| 36 | 37 |
| 37 var errNotFoundGRPC = grpc.Errorf(codes.NotFound, "Master Not Found") | 38 var errNotFoundGRPC = grpc.Errorf(codes.NotFound, "Master Not Found") |
| 38 | 39 |
| 39 // GetBuildbotBuildJSON implements milo.BuildbotServer. | 40 // GetBuildbotBuildJSON implements milo.BuildbotServer. |
| 40 func (s *Service) GetBuildbotBuildJSON(c context.Context, req *milo.BuildbotBuil
dRequest) ( | 41 func (s *Service) GetBuildbotBuildJSON(c context.Context, req *milo.BuildbotBuil
dRequest) ( |
| 41 *milo.BuildbotBuildJSON, error) { | 42 *milo.BuildbotBuildJSON, error) { |
| 42 | 43 |
| 43 if req.Master == "" { | 44 if req.Master == "" { |
| 44 return nil, grpc.Errorf(codes.InvalidArgument, "No master specif
ied") | 45 return nil, grpc.Errorf(codes.InvalidArgument, "No master specif
ied") |
| 45 } | 46 } |
| 46 if req.Builder == "" { | 47 if req.Builder == "" { |
| 47 return nil, grpc.Errorf(codes.InvalidArgument, "No builder speci
fied") | 48 return nil, grpc.Errorf(codes.InvalidArgument, "No builder speci
fied") |
| 48 } | 49 } |
| 49 | 50 |
| 50 cu := auth.CurrentUser(c) | 51 cu := auth.CurrentUser(c) |
| 51 logging.Debugf(c, "%s is requesting %s/%s/%d", | 52 logging.Debugf(c, "%s is requesting %s/%s/%d", |
| 52 cu.Identity, req.Master, req.Builder, req.BuildNum) | 53 cu.Identity, req.Master, req.Builder, req.BuildNum) |
| 53 | 54 |
| 54 b, err := getBuild(c, req.Master, req.Builder, int(req.BuildNum)) | 55 b, err := getBuild(c, req.Master, req.Builder, int(req.BuildNum)) |
| 55 » switch { | 56 » if err != nil { |
| 56 » case err == errBuildNotFound: | 57 » » switch common.ErrorTag.In(err) { |
| 57 » » return nil, grpc.Errorf(codes.NotFound, "Build not found") | 58 » » case common.CodeNotFound: |
| 58 » case err == errNotAuth: | 59 » » » return nil, grpc.Errorf(codes.NotFound, "Build not found
") |
| 59 » » return nil, grpc.Errorf(codes.Unauthenticated, "Unauthenticated
request") | 60 » » case common.CodeUnauthorized: |
| 60 » case err != nil: | 61 » » » return nil, grpc.Errorf(codes.Unauthenticated, "Unauthen
ticated request") |
| 61 » » return nil, err | 62 » » default: |
| 63 » » » return nil, err |
| 64 » » } |
| 62 } | 65 } |
| 63 | 66 |
| 64 updatePostProcessBuild(b) | 67 updatePostProcessBuild(b) |
| 65 bs, err := json.Marshal(b) | 68 bs, err := json.Marshal(b) |
| 66 if err != nil { | 69 if err != nil { |
| 67 return nil, err | 70 return nil, err |
| 68 } | 71 } |
| 69 | 72 |
| 70 // Marshal the build back into JSON format. | 73 // Marshal the build back into JSON format. |
| 71 return &milo.BuildbotBuildJSON{Data: bs}, nil | 74 return &milo.BuildbotBuildJSON{Data: bs}, nil |
| (...skipping 12 matching lines...) Expand all Loading... |
| 84 | 87 |
| 85 limit := req.Limit | 88 limit := req.Limit |
| 86 if limit == 0 { | 89 if limit == 0 { |
| 87 limit = 20 | 90 limit = 20 |
| 88 } | 91 } |
| 89 | 92 |
| 90 cu := auth.CurrentUser(c) | 93 cu := auth.CurrentUser(c) |
| 91 logging.Debugf(c, "%s is requesting %s/%s (limit %d, cursor %s)", | 94 logging.Debugf(c, "%s is requesting %s/%s (limit %d, cursor %s)", |
| 92 cu.Identity, req.Master, req.Builder, limit, req.Cursor) | 95 cu.Identity, req.Master, req.Builder, limit, req.Cursor) |
| 93 | 96 |
| 94 » // Perform an ACL check by fetching the master. | 97 » if err := canAccessMaster(c, req.Master); err != nil { |
| 95 » _, err := getMasterEntry(c, req.Master) | 98 » » switch common.ErrorTag.In(err) { |
| 96 » switch { | 99 » » case common.CodeNotFound: |
| 97 » case err == errMasterNotFound: | 100 » » » return nil, grpc.Errorf(codes.NotFound, "Master not foun
d") |
| 98 » » return nil, grpc.Errorf(codes.NotFound, "Master not found") | 101 » » case common.CodeUnauthorized: |
| 99 » case err == errNotAuth: | 102 » » » return nil, grpc.Errorf(codes.Unauthenticated, "Unauthen
ticated request") |
| 100 » » return nil, grpc.Errorf(codes.Unauthenticated, "Unauthenticated
request") | 103 » » default: |
| 101 » case err != nil: | 104 » » » return nil, err |
| 102 » » return nil, err | 105 » » } |
| 103 } | 106 } |
| 104 | 107 |
| 105 q := datastore.NewQuery("buildbotBuild") | 108 q := datastore.NewQuery("buildbotBuild") |
| 106 q = q.Eq("master", req.Master). | 109 q = q.Eq("master", req.Master). |
| 107 Eq("builder", req.Builder). | 110 Eq("builder", req.Builder). |
| 108 Limit(limit). | 111 Limit(limit). |
| 109 Order("-number") | 112 Order("-number") |
| 110 if req.IncludeCurrent == false { | 113 if req.IncludeCurrent == false { |
| 111 q = q.Eq("finished", true) | 114 q = q.Eq("finished", true) |
| 112 } | 115 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 132 bs, err := json.Marshal(b) | 135 bs, err := json.Marshal(b) |
| 133 if err != nil { | 136 if err != nil { |
| 134 return nil, err | 137 return nil, err |
| 135 } | 138 } |
| 136 results[i] = &milo.BuildbotBuildJSON{Data: bs} | 139 results[i] = &milo.BuildbotBuildJSON{Data: bs} |
| 137 } | 140 } |
| 138 buildsJSON := &milo.BuildbotBuildsJSON{ | 141 buildsJSON := &milo.BuildbotBuildsJSON{ |
| 139 Builds: results, | 142 Builds: results, |
| 140 } | 143 } |
| 141 if nextCursor != nil { | 144 if nextCursor != nil { |
| 142 » » buildsJSON.Cursor = (*nextCursor).String() | 145 » » buildsJSON.Cursor = nextCursor.String() |
| 143 } | 146 } |
| 144 return buildsJSON, nil | 147 return buildsJSON, nil |
| 145 } | 148 } |
| 146 | 149 |
| 147 // GetCompressedMasterJSON assembles a CompressedMasterJSON object from the | 150 // GetCompressedMasterJSON assembles a CompressedMasterJSON object from the |
| 148 // provided MasterRequest. | 151 // provided MasterRequest. |
| 149 func (s *Service) GetCompressedMasterJSON(c context.Context, req *milo.MasterReq
uest) ( | 152 func (s *Service) GetCompressedMasterJSON(c context.Context, req *milo.MasterReq
uest) ( |
| 150 *milo.CompressedMasterJSON, error) { | 153 *milo.CompressedMasterJSON, error) { |
| 151 | 154 |
| 152 if req.Name == "" { | 155 if req.Name == "" { |
| 153 return nil, grpc.Errorf(codes.InvalidArgument, "No master specif
ied") | 156 return nil, grpc.Errorf(codes.InvalidArgument, "No master specif
ied") |
| 154 } | 157 } |
| 155 | 158 |
| 156 cu := auth.CurrentUser(c) | 159 cu := auth.CurrentUser(c) |
| 157 logging.Debugf(c, "%s is making a master request for %s", cu.Identity, r
eq.Name) | 160 logging.Debugf(c, "%s is making a master request for %s", cu.Identity, r
eq.Name) |
| 158 | 161 |
| 159 entry, err := getMasterEntry(c, req.Name) | 162 entry, err := getMasterEntry(c, req.Name) |
| 160 » switch { | 163 » if err != nil { |
| 161 » case err == errMasterNotFound: | 164 » » switch common.ErrorTag.In(err) { |
| 162 » » return nil, errNotFoundGRPC | 165 » » case common.CodeNotFound: |
| 163 » case err == errNotAuth: | 166 » » » return nil, errNotFoundGRPC |
| 164 » » return nil, grpc.Errorf(codes.Unauthenticated, "Unauthenticated
request") | 167 » » case common.CodeUnauthorized: |
| 165 » case err != nil: | 168 » » » return nil, grpc.Errorf(codes.Unauthenticated, "Unauthen
ticated request") |
| 166 » » return nil, err | 169 » » default: |
| 170 » » » return nil, err |
| 171 » » } |
| 167 } | 172 } |
| 173 |
| 168 // Decompress it so we can inject current build information. | 174 // Decompress it so we can inject current build information. |
| 169 master := &buildbotMaster{} | 175 master := &buildbotMaster{} |
| 170 if err = decodeMasterEntry(c, entry, master); err != nil { | 176 if err = decodeMasterEntry(c, entry, master); err != nil { |
| 171 return nil, err | 177 return nil, err |
| 172 } | 178 } |
| 173 for _, slave := range master.Slaves { | 179 for _, slave := range master.Slaves { |
| 174 numBuilds := 0 | 180 numBuilds := 0 |
| 175 for _, builds := range slave.RunningbuildsMap { | 181 for _, builds := range slave.RunningbuildsMap { |
| 176 numBuilds += len(builds) | 182 numBuilds += len(builds) |
| 177 } | 183 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 | 240 |
| 235 return &milo.CompressedMasterJSON{ | 241 return &milo.CompressedMasterJSON{ |
| 236 Internal: entry.Internal, | 242 Internal: entry.Internal, |
| 237 Modified: ×tamp.Timestamp{ | 243 Modified: ×tamp.Timestamp{ |
| 238 Seconds: entry.Modified.Unix(), | 244 Seconds: entry.Modified.Unix(), |
| 239 Nanos: int32(entry.Modified.Nanosecond()), | 245 Nanos: int32(entry.Modified.Nanosecond()), |
| 240 }, | 246 }, |
| 241 Data: gzbs.Bytes(), | 247 Data: gzbs.Bytes(), |
| 242 }, nil | 248 }, nil |
| 243 } | 249 } |
| OLD | NEW |