OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 buildsource | |
6 | |
7 import ( | |
8 "strconv" | |
9 "strings" | |
10 | |
11 "golang.org/x/net/context" | |
12 | |
13 "github.com/luci/gae/service/datastore" | |
14 | |
15 "github.com/luci/luci-go/common/errors" | |
16 "github.com/luci/luci-go/common/logging" | |
17 "github.com/luci/luci-go/milo/api/resp" | |
18 "github.com/luci/luci-go/milo/buildsource/buildbot" | |
19 "github.com/luci/luci-go/milo/buildsource/buildbucket" | |
20 "github.com/luci/luci-go/milo/common" | |
21 ) | |
22 | |
23 // BuilderID is the universal ID of a builder, and has the form: | |
24 // buildbucket/bucket/builder | |
25 // buildbot/master/builder | |
26 type BuilderID string | |
27 | |
28 func parseTwo(path, nameType, nameA, nameB string) (a, b string, err error) { | |
Ryan Tseng
2017/07/13 22:00:46
docstring - what is path? what is nameType? nameA?
iannucci
2017/07/14 19:00:22
Refactored
| |
29 toks := strings.SplitN(path, "/", 2) | |
30 if len(toks) != 2 { | |
31 err = errors.Reason("bad BuilderID: cannot parse %s token %q", n ameType, path). | |
32 Tag(common.CodeParameterError).Err() | |
33 return | |
34 } | |
35 a, b = toks[0], toks[1] | |
36 if a == "" { | |
37 err = errors.Reason("bad BuilderID: empty %s", nameA).Tag(common .CodeParameterError).Err() | |
38 return | |
39 } | |
40 if b == "" { | |
41 err = errors.Reason("bad BuilderID: empty %s", nameB).Tag(common .CodeParameterError).Err() | |
42 return | |
43 } | |
44 return | |
45 } | |
46 | |
47 // Get allows you to obtain the resp.Builder that corresponds with this | |
48 // BuilderID. | |
49 func (b BuilderID) Get(ctx context.Context, limitStr string, cursorStr string) ( *resp.Builder, error) { | |
50 // TODO(iannucci): replace these implementations with a BuildSummary que ry. | |
51 | |
52 limit := 0 | |
53 if limitStr != "" { | |
54 switch limitVal, err := strconv.ParseInt(limitStr, 10, 0); err { | |
55 case nil: | |
56 limit = int(limitVal) | |
57 default: | |
58 logging.WithError(err).Warningf(ctx, "ignoring bad limit %q", limitStr) | |
59 } | |
60 } | |
61 if limit <= 0 { | |
62 limit = 25 | |
63 } | |
64 | |
65 source, path, err := parseTwo(string(b), "BuilderID", "source", "path") | |
66 if err != nil { | |
67 return nil, err | |
68 } | |
69 | |
70 switch source { | |
71 case "buildbot": | |
72 master, builder, err := parseTwo(path, "buildbot", "master", "bu ilder") | |
73 if err != nil { | |
74 return nil, err | |
75 } | |
76 | |
77 cursor, err := datastore.DecodeCursor(ctx, cursorStr) | |
78 if err != nil { | |
79 return nil, errors.Annotate(err, "bad cursor").Err() | |
80 } | |
81 | |
82 return buildbot.GetBuilder(ctx, master, builder, limit, cursor) | |
83 | |
84 case "buildbucket": | |
85 bucket, builder, err := parseTwo(path, "buildbucket", "bucket", "builder") | |
86 if err != nil { | |
87 return nil, err | |
88 } | |
89 return buildbucket.GetBuilder(ctx, bucket, builder, limit) | |
90 } | |
91 | |
92 return nil, errors.Reason("bad BuilderID: unknown backend %q", source). | |
93 Tag(common.CodeParameterError).Err() | |
94 } | |
OLD | NEW |