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