~vtuson/scopecreator/twitter-template

« back to all changes in this revision

Viewing changes to src/go/src/code.google.com/p/go.tools/dashboard/builder/http.go

  • Committer: Victor Palau
  • Date: 2015-03-11 14:24:42 UTC
  • Revision ID: vtuson@gmail.com-20150311142442-f2pxp111c8ynv232
public release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2011 The Go Authors. All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
package main
 
6
 
 
7
import (
 
8
        "bytes"
 
9
        "encoding/json"
 
10
        "errors"
 
11
        "fmt"
 
12
        "io"
 
13
        "log"
 
14
        "net/http"
 
15
        "net/url"
 
16
        "time"
 
17
)
 
18
 
 
19
type obj map[string]interface{}
 
20
 
 
21
// dash runs the given method and command on the dashboard.
 
22
// If args is non-nil it is encoded as the URL query string.
 
23
// If req is non-nil it is JSON-encoded and passed as the body of the HTTP POST.
 
24
// If resp is non-nil the server's response is decoded into the value pointed
 
25
// to by resp (resp must be a pointer).
 
26
func dash(meth, cmd string, args url.Values, req, resp interface{}) error {
 
27
        var r *http.Response
 
28
        var err error
 
29
        if *verbose {
 
30
                log.Println("dash <-", meth, cmd, args, req)
 
31
        }
 
32
        cmd = "http://" + *dashboard + "/" + cmd
 
33
        if len(args) > 0 {
 
34
                cmd += "?" + args.Encode()
 
35
        }
 
36
        switch meth {
 
37
        case "GET":
 
38
                if req != nil {
 
39
                        log.Panicf("%s to %s with req", meth, cmd)
 
40
                }
 
41
                r, err = http.Get(cmd)
 
42
        case "POST":
 
43
                var body io.Reader
 
44
                if req != nil {
 
45
                        b, err := json.Marshal(req)
 
46
                        if err != nil {
 
47
                                return err
 
48
                        }
 
49
                        body = bytes.NewBuffer(b)
 
50
                }
 
51
                r, err = http.Post(cmd, "text/json", body)
 
52
        default:
 
53
                log.Panicf("%s: invalid method %q", cmd, meth)
 
54
                panic("invalid method: " + meth)
 
55
        }
 
56
        if err != nil {
 
57
                return err
 
58
        }
 
59
        defer r.Body.Close()
 
60
        if r.StatusCode != http.StatusOK {
 
61
                return fmt.Errorf("bad http response: %v", r.Status)
 
62
        }
 
63
        body := new(bytes.Buffer)
 
64
        if _, err := body.ReadFrom(r.Body); err != nil {
 
65
                return err
 
66
        }
 
67
 
 
68
        // Read JSON-encoded Response into provided resp
 
69
        // and return an error if present.
 
70
        var result = struct {
 
71
                Response interface{}
 
72
                Error    string
 
73
        }{
 
74
                // Put the provided resp in here as it can be a pointer to
 
75
                // some value we should unmarshal into.
 
76
                Response: resp,
 
77
        }
 
78
        if err = json.Unmarshal(body.Bytes(), &result); err != nil {
 
79
                log.Printf("json unmarshal %#q: %s\n", body.Bytes(), err)
 
80
                return err
 
81
        }
 
82
        if *verbose {
 
83
                log.Println("dash ->", result)
 
84
        }
 
85
        if result.Error != "" {
 
86
                return errors.New(result.Error)
 
87
        }
 
88
 
 
89
        return nil
 
90
}
 
91
 
 
92
// todo returns the next hash to build.
 
93
func (b *Builder) todo(kind, pkg, goHash string) (rev string, err error) {
 
94
        args := url.Values{
 
95
                "kind":        {kind},
 
96
                "builder":     {b.name},
 
97
                "packagePath": {pkg},
 
98
                "goHash":      {goHash},
 
99
        }
 
100
        var resp *struct {
 
101
                Kind string
 
102
                Data struct {
 
103
                        Hash string
 
104
                }
 
105
        }
 
106
        if err = dash("GET", "todo", args, nil, &resp); err != nil {
 
107
                return "", err
 
108
        }
 
109
        if resp == nil {
 
110
                return "", nil
 
111
        }
 
112
        if kind != resp.Kind {
 
113
                return "", fmt.Errorf("expecting Kind %q, got %q", kind, resp.Kind)
 
114
        }
 
115
        return resp.Data.Hash, nil
 
116
}
 
117
 
 
118
// recordResult sends build results to the dashboard
 
119
func (b *Builder) recordResult(ok bool, pkg, hash, goHash, buildLog string, runTime time.Duration) error {
 
120
        req := obj{
 
121
                "Builder":     b.name,
 
122
                "PackagePath": pkg,
 
123
                "Hash":        hash,
 
124
                "GoHash":      goHash,
 
125
                "OK":          ok,
 
126
                "Log":         buildLog,
 
127
                "RunTime":     runTime,
 
128
        }
 
129
        args := url.Values{"key": {b.key}, "builder": {b.name}}
 
130
        return dash("POST", "result", args, req, nil)
 
131
}
 
132
 
 
133
func postCommit(key, pkg string, l *HgLog) error {
 
134
        t, err := time.Parse(time.RFC3339, l.Date)
 
135
        if err != nil {
 
136
                return fmt.Errorf("parsing %q: %v", l.Date, t)
 
137
        }
 
138
        return dash("POST", "commit", url.Values{"key": {key}}, obj{
 
139
                "PackagePath": pkg,
 
140
                "Hash":        l.Hash,
 
141
                "ParentHash":  l.Parent,
 
142
                "Time":        t.Format(time.RFC3339),
 
143
                "User":        l.Author,
 
144
                "Desc":        l.Desc,
 
145
        }, nil)
 
146
}
 
147
 
 
148
func dashboardCommit(pkg, hash string) bool {
 
149
        err := dash("GET", "commit", url.Values{
 
150
                "packagePath": {pkg},
 
151
                "hash":        {hash},
 
152
        }, nil, nil)
 
153
        return err == nil
 
154
}
 
155
 
 
156
func dashboardPackages(kind string) []string {
 
157
        args := url.Values{"kind": []string{kind}}
 
158
        var resp []struct {
 
159
                Path string
 
160
        }
 
161
        if err := dash("GET", "packages", args, nil, &resp); err != nil {
 
162
                log.Println("dashboardPackages:", err)
 
163
                return nil
 
164
        }
 
165
        var pkgs []string
 
166
        for _, r := range resp {
 
167
                pkgs = append(pkgs, r.Path)
 
168
        }
 
169
        return pkgs
 
170
}