OLD | NEW |
---|---|
(Empty) | |
1 package frontend | |
2 | |
3 import ( | |
4 "encoding/json" | |
5 "fmt" | |
6 "net/http" | |
7 "sync" | |
8 | |
9 "golang.org/x/net/context" | |
10 | |
11 "github.com/luci/gae/service/urlfetch" | |
12 "github.com/luci/luci-go/server/router" | |
13 ) | |
14 | |
15 // TODO(estaab): Comment copied from python implementation: | |
16 // Get rid of this one crrev.com supports this directly. | |
17 // See http://crbug.com/407198. | |
18 | |
19 func revisionHandler(c *router.Context) { | |
20 type result struct { | |
21 hash string | |
22 err error | |
23 } | |
24 results := make([]result, 2) | |
25 wg := sync.WaitGroup{} | |
26 wg.Add(2) | |
27 | |
28 go func() { | |
29 defer wg.Done() | |
30 hash, err := commitPositionToHash(c.Context, c.Request.FormValue ("start")) | |
31 results[0] = result{hash, err} | |
32 }() | |
33 go func() { | |
34 defer wg.Done() | |
35 hash, err := commitPositionToHash(c.Context, c.Request.FormValue ("end")) | |
36 results[1] = result{hash, err} | |
37 }() | |
38 | |
39 wg.Wait() | |
40 for _, r := range results { | |
41 if r.err != nil { | |
42 http.Error(c.Writer, r.err.Error(), http.StatusInternalS erverError) | |
43 return | |
44 } | |
45 } | |
46 | |
47 http.Redirect(c.Writer, c.Request, fmt.Sprintf( | |
48 "https://chromium.googlesource.com/chromium/src/+log/%s^..%s?pre tty=fuller", | |
49 results[0].hash, | |
50 results[1].hash, | |
51 ), http.StatusMovedPermanently) | |
52 } | |
53 | |
54 var ( | |
55 // crRevURL is the URL used by commitPositionToHash. | |
56 // It is global to allow switching in tests. | |
57 crRevURL = "https://cr-rev.appspot.com/_ah/api/crrev/v1/redirect" | |
58 | |
59 // makeCrRevClient returns a HTTP client for use by commitPositionToHash . | |
60 // It is global to allow switching in tests. | |
61 makeCrRevClient = func(c context.Context) *http.Client { | |
62 return &http.Client{Transport: urlfetch.Get(c)} | |
63 } | |
64 ) | |
65 | |
66 func commitPositionToHash(c context.Context, pos string) (string, error) { | |
estaab
2016/08/17 18:34:50
How about having a separate function that takes a
nishanths
2016/08/17 18:44:08
Much better!
Should I do the same for the crRevURL
estaab
2016/08/17 18:47:42
Is the test actually using it? I see it being set
| |
67 client := makeCrRevClient(c) | |
68 resp, err := client.Get(crRevURL + "/" + pos) | |
69 if err != nil { | |
70 return "", err | |
71 } | |
72 defer resp.Body.Close() | |
73 | |
74 if resp.StatusCode != http.StatusOK { | |
75 return "", fmt.Errorf("HTTP status code %d from %s", resp.Status Code, resp.Request.URL) | |
76 } | |
77 | |
78 hash := struct { | |
79 Hash string `json:"git_sha"` | |
80 }{} | |
81 if err := json.NewDecoder(resp.Body).Decode(&hash); err != nil { | |
82 return "", err | |
83 } | |
84 return hash.Hash, nil | |
85 } | |
OLD | NEW |