OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 gitiles | |
6 | |
7 import ( | |
8 "encoding/json" | |
9 "fmt" | |
10 "net/http" | |
11 "net/url" | |
12 "strings" | |
13 | |
14 "github.com/luci/luci-go/milo/api/resp" | |
15 "github.com/luci/luci-go/server/auth" | |
16 "golang.org/x/net/context" | |
17 ) | |
18 | |
19 // Repo defines a git repository. | |
20 type Repo struct { | |
21 // Server is the full path to a git repository. Server must start with
https:// | |
22 // and should not end with .git. | |
23 Server string | |
24 // Branch specifies a treeish of a git repository. This is generally a
branch. | |
25 Branch string | |
26 } | |
27 | |
28 // Author is the author returned from a gitiles log request. | |
29 type Author struct { | |
30 Name string `json:"name"` | |
31 Email string `json:"email"` | |
32 Time string `json:"time"` | |
33 } | |
34 | |
35 // Committer is the committer information returned from a gitiles log request. | |
36 type Committer struct { | |
37 Name string `json:"name"` | |
38 Email string `json:"email"` | |
39 Time string `json:"time"` | |
40 } | |
41 | |
42 // Log is the Log information of a commit returned from a gitiles log request. | |
43 type Log struct { | |
44 Commit string `json:"commit"` | |
45 Tree string `json:"tree"` | |
46 Parents []string `json:"parents"` | |
47 Author Author `json:"author"` | |
48 Committer Committer `json:"committer"` | |
49 Message string `json:"message"` | |
50 } | |
51 | |
52 // Commit is the JSON response from querying gitiles for a log request. | |
53 type Commit struct { | |
54 Log []Log `json:"log"` | |
55 Next string `json:"next"` | |
56 } | |
57 | |
58 // fixURL validates and normalizes a repoURL and treeish, and returns the | |
59 // log JSON gitiles URL. | |
60 func fixURL(repoURL, treeish string) (string, error) { | |
61 u, err := url.Parse(repoURL) | |
62 if err != nil { | |
63 return "", err | |
64 } | |
65 if u.Scheme != "https" { | |
66 return "", fmt.Errorf("%s should start with https://", repoURL) | |
67 } | |
68 if !strings.HasSuffix(u.Host, ".googlesource.com") { | |
69 return "", fmt.Errorf("Only .googlesource.com repos supported") | |
70 } | |
71 // Use the authenticated URL | |
72 u.Path = "a/" + u.Path | |
73 URL := fmt.Sprintf("%s/+log/%s?format=JSON", u.String(), treeish) | |
74 return URL, nil | |
75 } | |
76 | |
77 // GetCommits returns a list of commits based on a repo and treeish (usually | |
78 // a branch). This should be equivilent of a "git log <treeish>" call in | |
79 // that repository. | |
80 func GetCommits(c context.Context, repoURL, treeish string, limit int) ([]resp.C
ommit, error) { | |
81 // TODO(hinoka): Respect the limit. | |
82 URL, err := fixURL(repoURL, treeish) | |
83 if err != nil { | |
84 return nil, err | |
85 } | |
86 t, err := auth.GetRPCTransport(c, auth.NoAuth) | |
87 if err != nil { | |
88 return nil, err | |
89 } | |
90 client := http.Client{Transport: t} | |
91 r, err := client.Get(URL) | |
92 if err != nil { | |
93 return nil, err | |
94 } | |
95 if r.StatusCode != 200 { | |
96 return nil, fmt.Errorf("Failed to fetch %s, status code %d", URL
, r.StatusCode) | |
97 } | |
98 defer r.Body.Close() | |
99 // Strip out the jsonp header, which is ")]}'" | |
100 trash := make([]byte, 4) | |
101 r.Body.Read(trash) // Read the jsonp header | |
102 commits := Commit{} | |
103 if err := json.NewDecoder(r.Body).Decode(&commits); err != nil { | |
104 return nil, err | |
105 } | |
106 // TODO(hinoka): If there is a page and we have gotten less than the lim
it, | |
107 // keep making requests for the next page until we have enough commits. | |
108 | |
109 // Move things into our own datastructure. | |
110 result := make([]resp.Commit, len(commits.Log)) | |
111 for i, log := range commits.Log { | |
112 result[i] = resp.Commit{ | |
113 AuthorName: log.Author.Name, | |
114 AuthorEmail: log.Author.Email, | |
115 Repo: repoURL, | |
116 Revision: resp.NewLink(log.Commit, repoURL+"/+/"+log.
Commit), | |
117 Description: log.Message, | |
118 Title: strings.SplitN(log.Message, "\n", 2)[0], | |
119 // TODO(hinoka): Fill in the rest of resp.Commit and add
those details | |
120 // in the html. | |
121 } | |
122 } | |
123 return result, nil | |
124 } | |
OLD | NEW |