Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(319)

Unified Diff: milo/buildsource/swarming/build.go

Issue 2977863002: [milo] Refactor all html knowledge out of backends. (Closed)
Patch Set: seems to work :) Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: milo/buildsource/swarming/build.go
diff --git a/milo/buildsource/swarming/build.go b/milo/buildsource/swarming/build.go
index b622497861bdd060ca80b798064932263a2a5ada..b663ca132afb115bd91c3005c226e7025dd8801b 100644
--- a/milo/buildsource/swarming/build.go
+++ b/milo/buildsource/swarming/build.go
@@ -42,7 +42,7 @@ import (
// errNotMiloJob is returned if a Swarming task is fetched that does not self-
// identify as a Milo job.
-var errNotMiloJob = errors.New("Not a Milo Job or access denied")
+var errNotMiloJob = errors.New("Not a Milo Job or access denied", common.CodeNoAccess)
// SwarmingTimeLayout is time layout used by swarming.
const SwarmingTimeLayout = "2006-01-02T15:04:05.999999999"
@@ -778,3 +778,84 @@ func swarmingTags(v []string) map[string]string {
}
return res
}
+
+// BuildID is swarming's notion of a Build. See buildsource.ID.
+type BuildID struct {
+ // (Required) The Swarming TaskID.
+ TaskID string
+
+ // (Optional) The Swarming host. If empty, will use the
+ // milo-instance-configured swarming host.
+ Host string
+}
+
+// getSwarmingHost parses the swarming hostname out of the context. If
+// none is specified, get the default swarming host out of the global
+// configs.
+func (b *BuildID) getSwarmingHost(c context.Context) (server string, err error) {
+ server = b.Host
+ settings := common.GetSettings(c)
+ if settings.Swarming == nil {
+ err := errors.New("swarming not in settings")
+ logging.WithError(err).Errorf(c, "Go configure swarming in the settings page.")
+ return "", err
+ }
+ if server == "" || server == settings.Swarming.DefaultHost {
+ return settings.Swarming.DefaultHost, nil
+ }
+ // If it is specified, validate the hostname.
+ for _, hostname := range settings.Swarming.AllowedHosts {
+ if server == hostname {
+ return server, nil
+ }
+ }
+ return "", errors.New("unknown swarming host", common.CodeParameterError)
+}
+
+func (b *BuildID) validate() error {
+ if b.TaskID == "" {
+ return errors.New("no swarming task id", common.CodeParameterError)
+ }
+ return nil
+}
+
+// Get implements buildsource.ID
+func (b *BuildID) Get(ctx context.Context) (*resp.MiloBuild, error) {
Ryan Tseng 2017/07/13 22:00:46 nit: conventionally Milo uses "ctx" for router.Con
iannucci 2017/07/14 19:00:22 this is all goofy everywhere for reasons, but I'll
+ if err := b.validate(); err != nil {
+ return nil, err
+ }
+
+ hostname, err := b.getSwarmingHost(ctx)
+ if err != nil {
+ return nil, err
+ }
+ sf, err := NewProdService(ctx, hostname)
+ if err != nil {
+ return nil, err
+ }
+
+ return (&BuildLoader{}).SwarmingBuildImpl(ctx, sf, b.TaskID)
+}
+
+// GetLog implements buildsource.ID
+func (b *BuildID) GetLog(ctx context.Context, logname string) (text string, closed bool, err error) {
+ if err = b.validate(); err != nil {
+ return
+ }
+ if logname == "" {
+ err = errors.New("no log name", common.CodeParameterError)
+ return
+ }
+
+ hostname, err := b.getSwarmingHost(ctx)
+ if err != nil {
+ return
+ }
+
+ sf, err := NewProdService(ctx, hostname)
+ if err != nil {
+ return
+ }
+
+ return swarmingBuildLogImpl(ctx, sf, b.TaskID, logname)
+}

Powered by Google App Engine
This is Rietveld 408576698