-
Notifications
You must be signed in to change notification settings - Fork 0
/
view_build_legacy.go
157 lines (140 loc) · 5.21 KB
/
view_build_legacy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2018 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package frontend
import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/golang/protobuf/proto"
"github.com/julienschmidt/httprouter"
buildbucketpb "go.chromium.org/luci/buildbucket/proto"
"go.chromium.org/luci/common/errors"
"go.chromium.org/luci/common/logging"
"go.chromium.org/luci/grpc/grpcutil"
"go.chromium.org/luci/logdog/common/types"
"go.chromium.org/luci/server/router"
"go.chromium.org/luci/server/templates"
buildbotapi "go.chromium.org/luci/milo/api/buildbot"
"go.chromium.org/luci/milo/api/config"
"go.chromium.org/luci/milo/buildsource/buildbot"
"go.chromium.org/luci/milo/buildsource/buildbot/buildstore"
"go.chromium.org/luci/milo/buildsource/buildbucket"
"go.chromium.org/luci/milo/buildsource/rawpresentation"
"go.chromium.org/luci/milo/buildsource/swarming"
"go.chromium.org/luci/milo/common"
"go.chromium.org/luci/milo/frontend/ui"
)
// handleBuildbotBuild renders a buildbot build.
// Requires emulationMiddleware.
func handleBuildbotBuild(c *router.Context) error {
buildNum, err := strconv.Atoi(c.Params.ByName("number"))
if err != nil {
return errors.Annotate(err, "build number is not a number").
Tag(grpcutil.InvalidArgumentTag).
Err()
}
id := buildbotapi.BuildID{
Master: c.Params.ByName("master"),
Builder: c.Params.ByName("builder"),
Number: buildNum,
}
if err := id.Validate(); err != nil {
return err
}
// If this build is emulated, redirect to LUCI.
b, err := buildstore.EmulationOf(c.Context, id)
switch {
case err != nil:
return err
case b != nil && b.Number != nil:
u := *c.Request.URL
u.Path = fmt.Sprintf("/p/%s/builders/%s/%s/%d", b.Project, b.Bucket, b.Builder, *b.Number)
http.Redirect(c.Writer, c.Request, u.String(), http.StatusFound)
return nil
default:
build, err := buildbot.GetBuild(c.Context, id)
return renderBuildLegacy(c, build, false, err)
}
}
func handleSwarmingBuild(c *router.Context) error {
build, err := swarming.GetBuild(
c.Context,
c.Request.FormValue("server"),
c.Params.ByName("id"))
return renderBuildLegacy(c, build, false, err)
}
func handleRawPresentationBuild(c *router.Context) error {
build, err := rawpresentation.GetBuild(
c.Context,
c.Params.ByName("logdog_host"),
types.ProjectName(c.Params.ByName("project")),
types.StreamPath(strings.Trim(c.Params.ByName("path"), "/")))
return renderBuildLegacy(c, build, false, err)
}
// renderBuildLegacy is a shortcut for rendering build or returning err if it is not
// nil. Also calls build.Fix().
func renderBuildLegacy(c *router.Context, build *ui.MiloBuildLegacy, renderTimeline bool, err error) error {
if err != nil {
return err
}
build.StepDisplayPref = getStepDisplayPrefCookie(c)
build.ShowDebugLogsPref = getShowDebugLogsPrefCookie(c)
build.Fix(c.Context)
templates.MustRender(c.Context, c.Writer, "pages/build_legacy.html", templates.Args{
"Build": build,
"BuildFeedbackLink": makeFeedbackLink(c, build),
})
return nil
}
// makeFeedbackLink attempts to create the feedback link for the build page. If the
// project is not configured for a custom feedback link or an interpolation placeholder
// cannot be satisfied an empty string is returned.
func makeFeedbackLink(c *router.Context, build *ui.MiloBuildLegacy) string {
project, err := common.GetProject(c.Context, c.Params.ByName("project"))
if err != nil || proto.Equal(&project.BuildBugTemplate, &config.BugTemplate{}) {
return ""
}
buildURL := c.Request.URL
var builderURL *url.URL
if build.Summary.ParentLabel != nil && build.Summary.ParentLabel.URL != "" {
builderURL, err = buildURL.Parse(build.Summary.ParentLabel.URL)
if err != nil {
logging.WithError(err).Errorf(c.Context, "Unable to parse build.Summary.ParentLabel.URL for custom feedback link")
return ""
}
}
link, err := buildbucket.MakeBuildBugLink(&project.BuildBugTemplate, map[string]interface{}{
"Build": makeBuild(c.Params, build),
"MiloBuildUrl": buildURL,
"MiloBuilderUrl": builderURL,
})
if err != nil {
logging.WithError(err).Errorf(c.Context, "Unable to make custom feedback link")
return ""
}
return link
}
// makeBuild partially populates a buildbucketpb.Build. Currently it attempts to
// make available .Builder.Project, .Builder.Bucket, and .Builder.Builder.
func makeBuild(params httprouter.Params, build *ui.MiloBuildLegacy) *buildbucketpb.Build {
return &buildbucketpb.Build{
Builder: &buildbucketpb.BuilderID{
Project: build.Trigger.Project, // equivalent params.ByName("project")
Bucket: params.ByName("bucket"), // way to get from ui.MiloBuildLegacy so don't need params here?
Builder: build.Summary.ParentLabel.Label, // params.ByName("builder")
},
}
}