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

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

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

Powered by Google App Engine
This is Rietveld 408576698