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

Side by Side Diff: common/api/gitiles/gitiles.go

Issue 2983513002: gitiles.Log: implement paging. (Closed)
Patch Set: Better doc. 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 unified diff | Download patch
« no previous file with comments | « no previous file | common/api/gitiles/gitiles_test.go » ('j') | common/api/gitiles/gitiles_test.go » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The LUCI Authors. 1 // Copyright 2016 The LUCI Authors.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 package gitiles 15 package gitiles
16 16
17 // TODO(tandrii): add tests. 17 // TODO(tandrii): add tests.
18 18
19 import ( 19 import (
20 "encoding/json" 20 "encoding/json"
21 "fmt" 21 "fmt"
22 "net/http" 22 "net/http"
23 "net/url" 23 "net/url"
24 "reflect"
24 "strings" 25 "strings"
25 "time" 26 "time"
26 27
28 "github.com/luci/luci-go/common/errors"
27 "github.com/luci/luci-go/server/auth" 29 "github.com/luci/luci-go/server/auth"
28 "golang.org/x/net/context" 30 "golang.org/x/net/context"
29 ) 31 )
30 32
31 // User is the author or the committer returned from gitiles. 33 // User is the author or the committer returned from gitiles.
32 type User struct { 34 type User struct {
33 Name string `json:"name"` 35 Name string `json:"name"`
34 Email string `json:"email"` 36 Email string `json:"email"`
35 Time string `json:"time"` 37 Time string `json:"time"`
36 } 38 }
37 39
38 // GetTime returns the Time field as real data! 40 // GetTime returns the Time field as real data!
39 func (u *User) GetTime() (time.Time, error) { 41 func (u *User) GetTime() (time.Time, error) {
40 return time.Parse(time.ANSIC, u.Time) 42 return time.Parse(time.ANSIC, u.Time)
41 } 43 }
42 44
43 // Commit is the information of a commit returned from gitiles. 45 // Commit is the information of a commit returned from gitiles.
44 type Commit struct { 46 type Commit struct {
45 Commit string `json:"commit"` 47 Commit string `json:"commit"`
46 Tree string `json:"tree"` 48 Tree string `json:"tree"`
47 Parents []string `json:"parents"` 49 Parents []string `json:"parents"`
48 Author User `json:"author"` 50 Author User `json:"author"`
49 Committer User `json:"committer"` 51 Committer User `json:"committer"`
50 Message string `json:"message"` 52 Message string `json:"message"`
51 } 53 }
52 54
53 // LogResponse is the JSON response from querying gitiles for a log request. 55 // ValidateRepoURL validates gitiles repository URL for use in this package.
54 type LogResponse struct { 56 func ValidateRepoURL(repoURL string) error {
55 » Log []Commit `json:"log"` 57 » _, err := NormalizeRepoURL(repoURL)
56 » Next string `json:"next"` 58 » return err
57 } 59 }
58 60
59 // fixURL validates and normalizes a repoURL and treeish, and returns the 61 // NormalizeRepoURL returns canonical for gitiles URL of the repo including "a/" path prefix.
60 // log JSON gitiles URL. 62 // error is returned is validation fails.
Vadim Sh. 2017/07/19 17:09:24 if validation fails
tandrii(chromium) 2017/07/19 20:12:30 Done.
61 func fixURL(repoURL, treeish string) (string, error) { 63 func NormalizeRepoURL(repoURL string) (string, error) {
62 u, err := url.Parse(repoURL) 64 u, err := url.Parse(repoURL)
63 if err != nil { 65 if err != nil {
64 return "", err 66 return "", err
65 } 67 }
66 if u.Scheme != "https" { 68 if u.Scheme != "https" {
67 return "", fmt.Errorf("%s should start with https://", repoURL) 69 return "", fmt.Errorf("%s should start with https://", repoURL)
68 } 70 }
69 if !strings.HasSuffix(u.Host, ".googlesource.com") { 71 if !strings.HasSuffix(u.Host, ".googlesource.com") {
Vadim Sh. 2017/07/19 17:09:24 this reminds me of general uselessness of luci as
tandrii(chromium) 2017/07/19 20:12:31 yeah, i had the same thought :(
70 » » return "", fmt.Errorf("Only .googlesource.com repos supported") 72 » » return "", fmt.Errorf("only .googlesource.com repos supported")
71 } 73 }
72 » // Use the authenticated URL 74 » if u.Path == "" || u.Path == "/" {
73 » u.Path = "a/" + u.Path 75 » » return "", fmt.Errorf("path to repo is empty")
74 » URL := fmt.Sprintf("%s/+log/%s?format=JSON", u.String(), treeish) 76 » }
75 » return URL, nil 77 » if !strings.HasPrefix(u.Path, "/") {
78 » » u.Path = "/" + u.Path
79 » }
80 » if !strings.HasPrefix(u.Path, "/a/") {
81 » » // Use the authenticated URL
82 » » u.Path = "/a" + u.Path
83 » }
84
85 » u.Path = strings.TrimRight(u.Path, "/")
86 » u.Path = strings.TrimSuffix(u.Path, ".git")
87 » return u.String(), nil
76 } 88 }
77 89
78 // Log returns a list of commits based on a repo and treeish (usually 90 // Log returns a list of commits based on a repo and treeish.
79 // a branch). This should be equivilent of a "git log <treeish>" call in 91 // This should be equivalent of a "git log <treeish>" call in that repository.
80 // that repository. 92 //
93 // treeish can be either:
94 // (1) a git revision as 40-char string or its prefix so long as its unique in repo.
95 // (2) a ref such as "refs/heads/branch" or just "branch"
96 // (3) a ref defined as n-th parent of R in the form "R~n".
97 // For example, "master~2" or "deadbeef~1".
98 // (4) a range between two revisions in the form "CHILD..PREDECESSOR", where
99 // CHILD and PREDECESSOR are each specified in either (1), (2) or (3)
100 // formats listed above.
101 // For example, "foo..ba1", "master..refs/branch-heads/1.2.3",
102 // or even "master~5..master~9".
103 //
104 //
105 // If the returned log has a commit with 2+ parents, the order of commits after
106 // that is whatever Gitiles returns, which currently means ordered
107 // by topological sort first, and then by commit timestamps.
108 //
109 // This means that if Log(C) contains commit A, Log(A) will not necessarily retu rn
110 // a subsequence of Log(C) (though definitely a subset). For example,
111 //
112 //» » common... -> base ------> A ----> C
113 // » » » » » » » » » » \ /
114 // » » » » » » » » » --> B ------
115 //
116 //» » ----commit timestamp increases--->
117 //
118 // Log(A) = [A, base, common...]
119 // Log(B) = [B, base, common...]
120 // Log(C) = [C, A, B, base, common...]
121 //
81 func Log(c context.Context, repoURL, treeish string, limit int) ([]Commit, error ) { 122 func Log(c context.Context, repoURL, treeish string, limit int) ([]Commit, error ) {
82 » // TODO(hinoka): Respect the limit. 123 » logURL, err := NormalizeRepoURL(repoURL)
83 » URL, err := fixURL(repoURL, treeish)
84 if err != nil { 124 if err != nil {
85 return nil, err 125 return nil, err
86 } 126 }
127 logURL = fmt.Sprintf("%s/+log/%s?format=JSON", logURL, treeish)
Vadim Sh. 2017/07/19 17:09:24 url-encode treeish to be safe (assuming gitiles is
tandrii(chromium) 2017/07/19 20:12:31 tested gitiles, they seem OK.
87 t, err := auth.GetRPCTransport(c, auth.NoAuth) 128 t, err := auth.GetRPCTransport(c, auth.NoAuth)
Vadim Sh. 2017/07/19 17:09:24 strictly speaking, this library will be much more
tandrii(chromium) 2017/07/19 20:12:31 Yay, I thought of this too bcz it'd have made test
Vadim Sh. 2017/07/19 23:02:30 ok
tandrii(chromium) 2017/07/20 16:19:14 Actually, Done here.
88 if err != nil { 129 if err != nil {
89 return nil, err 130 return nil, err
90 } 131 }
91 client := http.Client{Transport: t} 132 client := http.Client{Transport: t}
133 resp := &logResponse{}
134 if err = get(client, logURL, resp); err != nil {
135 return nil, err
136 }
137 result := resp.Log
138 for {
139 if resp.Next == "" || len(result) >= limit {
140 return result, nil
Vadim Sh. 2017/07/19 17:09:24 do we want to trim this to the limit? It may be su
tandrii(chromium) 2017/07/19 20:12:31 trimmed.
141 }
142 nextURL := logURL + "&s=" + resp.Next
143 resp = &logResponse{}
144 if err = get(client, nextURL, resp); err != nil {
145 return nil, err
146 }
147 result = append(result, resp.Log...)
148 }
149 }
150
151 ////////////////////////////////////////////////////////////////////////////////
152
153 // logResponse is the JSON response from querying gitiles for a log request.
154 type logResponse struct {
155 Log []Commit `json:"log"`
156 Next string `json:"next"`
157 }
158
159 func get(client http.Client, URL string, result interface{}) error {
92 r, err := client.Get(URL) 160 r, err := client.Get(URL)
Vadim Sh. 2017/07/19 17:09:24 please use https://godoc.org/golang.org/x/net/cont
tandrii(chromium) 2017/07/19 20:12:31 Good point. done.
93 if err != nil { 161 if err != nil {
94 » » return nil, err 162 » » return err
Vadim Sh. 2017/07/19 17:09:24 we should probably retry transient errors (like HT
tandrii(chromium) 2017/07/19 20:12:30 annotated + todo.
95 » }
96 » if r.StatusCode != 200 {
97 » » return nil, fmt.Errorf("Failed to fetch %s, status code %d", URL , r.StatusCode)
98 } 163 }
99 defer r.Body.Close() 164 defer r.Body.Close()
165 if r.StatusCode != 200 {
166 return fmt.Errorf("Failed to fetch %s, status code %d", URL, r.S tatusCode)
167 }
100 // Strip out the jsonp header, which is ")]}'" 168 // Strip out the jsonp header, which is ")]}'"
101 trash := make([]byte, 4) 169 trash := make([]byte, 4)
102 » r.Body.Read(trash) // Read the jsonp header 170 » if c, err := r.Body.Read(trash); err != nil || c != 4 {
Vadim Sh. 2017/07/19 17:09:24 please assert that 'trash' is indeed ")]}'"
tandrii(chromium) 2017/07/19 20:12:31 Done.
103 » commits := LogResponse{} 171 » » return errors.Annotate(err, "unexpected response from Gitiles"). Err()
104 » if err := json.NewDecoder(r.Body).Decode(&commits); err != nil {
105 » » return nil, err
106 } 172 }
107 » // TODO(hinoka): If there is a page and we have gotten less than the lim it, 173 » if err := json.NewDecoder(r.Body).Decode(result); err != nil {
108 » // keep making requests for the next page until we have enough commits. 174 » » return errors.Annotate(err, "failed to decode Gitiles response i nto %v", reflect.TypeOf(result)).Err()
109 » return commits.Log, nil 175 » }
176 » return nil
110 } 177 }
OLDNEW
« no previous file with comments | « no previous file | common/api/gitiles/gitiles_test.go » ('j') | common/api/gitiles/gitiles_test.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698