Chromium Code Reviews| 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 git | |
| 6 | |
| 7 import ( | |
| 8 "encoding/json" | |
| 9 "fmt" | |
| 10 "net/url" | |
| 11 "strings" | |
| 12 | |
| 13 "github.com/luci/luci-go/appengine/cmd/milo/resp" | |
| 14 "github.com/luci/luci-go/common/transport" | |
| 15 "golang.org/x/net/context" | |
| 16 ) | |
| 17 | |
| 18 type Repo struct { | |
|
estaab
2016/08/04 23:21:25
docs. doesn't lint warn about this?
hinoka
2016/08/05 00:10:42
They're types not methods, so apparently not.
| |
| 19 Server string | |
| 20 Branch string | |
| 21 } | |
| 22 | |
| 23 type Author struct { | |
| 24 Name string `json:"name"` | |
| 25 Email string `json:"email"` | |
| 26 Time string `json:"time"` | |
| 27 } | |
| 28 | |
| 29 type Commiter struct { | |
| 30 Name string `json:"name"` | |
| 31 Email string `json:"email"` | |
| 32 Time string `json:"time"` | |
| 33 } | |
| 34 | |
| 35 type Log struct { | |
| 36 Commit string `json:"commit"` | |
| 37 Tree string `json:"tree"` | |
| 38 Parents []string `json:"parents"` | |
| 39 Author Author `json:"author"` | |
| 40 Committer Commiter `json:"committer"` | |
| 41 Message string `json:"message"` | |
| 42 } | |
| 43 | |
| 44 type Commit struct { | |
| 45 Log []Log `json:"log"` | |
| 46 Next string `json:"next"` | |
| 47 } | |
| 48 | |
| 49 func fixURL(repoURL, treeish string) (string, error) { | |
| 50 u, err := url.Parse(repoURL) | |
| 51 if err != nil { | |
| 52 return "", err | |
| 53 } | |
| 54 if u.Scheme != "https" { | |
| 55 return "", fmt.Errorf("%s should start with https://", repoURL) | |
| 56 } | |
| 57 if !strings.HasSuffix(u.Host, "googlesource.com") { | |
| 58 return "", fmt.Errorf("Only googlesource.com repos supported") | |
| 59 } | |
| 60 URL := fmt.Sprintf("%s/+log/%s?format=JSON", repoURL, treeish) | |
| 61 // Use the authenticated URL | |
| 62 u.Path = "a/" + u.Path | |
|
estaab
2016/08/04 23:21:25
u isn't used after this line?
hinoka
2016/08/05 00:10:42
Good catch. Fixed.
| |
| 63 return URL, nil | |
| 64 } | |
| 65 | |
| 66 func GetCommits(c context.Context, repoURL, treeish string) ([]resp.Commit, erro r) { | |
|
estaab
2016/08/04 23:21:25
docs
hinoka
2016/08/05 00:10:42
Done.
| |
| 67 URL, err := fixURL(repoURL, treeish) | |
| 68 if err != nil { | |
| 69 return nil, err | |
| 70 } | |
| 71 client := transport.GetClient(c) | |
| 72 r, err := client.Get(URL) | |
| 73 if err != nil { | |
| 74 return nil, err | |
| 75 } | |
| 76 if r.StatusCode != 200 { | |
| 77 return nil, fmt.Errorf("Failed to fetch %s, status code %d", URL , r.StatusCode) | |
| 78 } | |
| 79 defer r.Body.Close() | |
| 80 // Strip out the jsonp header, which is ")]}'" | |
| 81 trash := make([]byte, 4) | |
| 82 r.Body.Read(trash) // Read the jsonp header | |
| 83 commits := Commit{} | |
| 84 if err := json.NewDecoder(r.Body).Decode(&commits); err != nil { | |
| 85 return nil, err | |
| 86 } | |
| 87 // TODO(hinoka): Follow paging | |
|
estaab
2016/08/04 23:21:25
add more details
hinoka
2016/08/05 00:10:42
Done.
| |
| 88 | |
| 89 // Move things into our own datastructure. | |
| 90 result := make([]resp.Commit, len(commits.Log)) | |
| 91 for i, log := range commits.Log { | |
| 92 result[i] = resp.Commit{ | |
| 93 AuthorName: log.Author.Name, | |
| 94 AuthorEmail: log.Author.Email, | |
| 95 Repo: repoURL, | |
| 96 Revision: log.Commit, | |
| 97 Description: log.Message, | |
| 98 Title: strings.SplitN(log.Message, "\n", 2)[0], | |
| 99 // TODO(hinoka): The other stuff | |
|
estaab
2016/08/04 23:21:25
add more details
hinoka
2016/08/05 00:10:42
Done.
| |
| 100 } | |
| 101 } | |
| 102 return result, nil | |
| 103 } | |
| OLD | NEW |