| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. | 1 // Copyright 2017 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 git | 15 package git |
| 16 | 16 |
| 17 import ( | 17 import ( |
| 18 "bytes" | 18 "bytes" |
| 19 "compress/gzip" | 19 "compress/gzip" |
| 20 "encoding/hex" | 20 "encoding/hex" |
| 21 "fmt" | 21 "fmt" |
| 22 "io/ioutil" | 22 "io/ioutil" |
| 23 "net/http" |
| 23 "regexp" | 24 "regexp" |
| 24 | 25 |
| 25 "golang.org/x/net/context" | 26 "golang.org/x/net/context" |
| 26 | 27 |
| 27 "github.com/golang/protobuf/proto" | 28 "github.com/golang/protobuf/proto" |
| 28 | 29 |
| 29 "github.com/luci/gae/service/memcache" | 30 "github.com/luci/gae/service/memcache" |
| 30 | 31 |
| 31 "github.com/luci/luci-go/common/api/gitiles" | 32 "github.com/luci/luci-go/common/api/gitiles" |
| 32 "github.com/luci/luci-go/common/errors" | 33 "github.com/luci/luci-go/common/errors" |
| 33 "github.com/luci/luci-go/common/logging" | 34 "github.com/luci/luci-go/common/logging" |
| 34 "github.com/luci/luci-go/common/proto/google" | 35 "github.com/luci/luci-go/common/proto/google" |
| 36 "github.com/luci/luci-go/server/auth" |
| 35 | 37 |
| 36 milo "github.com/luci/luci-go/milo/api/proto" | 38 milo "github.com/luci/luci-go/milo/api/proto" |
| 37 ) | 39 ) |
| 38 | 40 |
| 39 var gitHash = regexp.MustCompile("[0-9a-fA-F]{40}") | 41 var gitHash = regexp.MustCompile("[0-9a-fA-F]{40}") |
| 40 | 42 |
| 41 // Resolve resolves a commitish to a git commit hash. | 43 // Resolve resolves a commitish to a git commit hash. |
| 42 // | 44 // |
| 43 // This operation will assumed to be either fully local (in the case that | 45 // This operation will assumed to be either fully local (in the case that |
| 44 // commitish is already a git-hash-looking-thing), or local to the datastore (in | 46 // commitish is already a git-hash-looking-thing), or local to the datastore (in |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 // the given url, commitish and limit. | 146 // the given url, commitish and limit. |
| 145 func GetHistory(c context.Context, url, commitish string, limit int) (*milo.Cons
oleGitInfo, error) { | 147 func GetHistory(c context.Context, url, commitish string, limit int) (*milo.Cons
oleGitInfo, error) { |
| 146 commitish, useCache, err := Resolve(c, url, commitish) | 148 commitish, useCache, err := Resolve(c, url, commitish) |
| 147 if err != nil { | 149 if err != nil { |
| 148 return nil, errors.Annotate(err, "resolving %q", commitish).Err(
) | 150 return nil, errors.Annotate(err, "resolving %q", commitish).Err(
) |
| 149 } | 151 } |
| 150 | 152 |
| 151 ret := &milo.ConsoleGitInfo{} | 153 ret := &milo.ConsoleGitInfo{} |
| 152 cacheKey := fmt.Sprintf("GetHistory|%s|%s|%d", url, commitish, limit) | 154 cacheKey := fmt.Sprintf("GetHistory|%s|%s|%d", url, commitish, limit) |
| 153 err = protoCache(c, useCache, cacheKey, ret, func() error { | 155 err = protoCache(c, useCache, cacheKey, ret, func() error { |
| 154 » » rawEntries, err := gitiles.Log(c, url, commitish, limit) | 156 » » t, err := auth.GetRPCTransport(c, auth.AsSelf, auth.WithScopes( |
| 157 » » » "https://www.googleapis.com/auth/gerritcodereview", |
| 158 » » )) |
| 159 » » if err != nil { |
| 160 » » » return errors.Annotate(err, "getting RPC Transport").Err
() |
| 161 » » } |
| 162 » » g := &gitiles.Client{Client: &http.Client{Transport: t}} |
| 163 » » rawEntries, err := g.Log(c, url, commitish, limit) |
| 155 if err != nil { | 164 if err != nil { |
| 156 return errors.Annotate(err, "GetHistory").Err() | 165 return errors.Annotate(err, "GetHistory").Err() |
| 157 } | 166 } |
| 158 | 167 |
| 159 ret.Commits = make([]*milo.ConsoleGitInfo_Commit, len(rawEntries
)) | 168 ret.Commits = make([]*milo.ConsoleGitInfo_Commit, len(rawEntries
)) |
| 160 | 169 |
| 161 for i, e := range rawEntries { | 170 for i, e := range rawEntries { |
| 162 commit := &milo.ConsoleGitInfo_Commit{} | 171 commit := &milo.ConsoleGitInfo_Commit{} |
| 163 if commit.Hash, err = hex.DecodeString(e.Commit); err !=
nil { | 172 if commit.Hash, err = hex.DecodeString(e.Commit); err !=
nil { |
| 164 return errors.Annotate(err, "commit is not hex (
%q)", e.Commit).Err() | 173 return errors.Annotate(err, "commit is not hex (
%q)", e.Commit).Err() |
| 165 } | 174 } |
| 166 | 175 |
| 167 commit.AuthorName = e.Author.Name | 176 commit.AuthorName = e.Author.Name |
| 168 commit.AuthorEmail = e.Author.Email | 177 commit.AuthorEmail = e.Author.Email |
| 169 | 178 |
| 170 ts, err := e.Committer.GetTime() | 179 ts, err := e.Committer.GetTime() |
| 171 if err != nil { | 180 if err != nil { |
| 172 return errors.Annotate(err, "commit time unparsi
ble (%q)", e.Committer.Time).Err() | 181 return errors.Annotate(err, "commit time unparsi
ble (%q)", e.Committer.Time).Err() |
| 173 } | 182 } |
| 174 | 183 |
| 175 commit.CommitTime = google.NewTimestamp(ts) | 184 commit.CommitTime = google.NewTimestamp(ts) |
| 176 commit.Msg = e.Message | 185 commit.Msg = e.Message |
| 177 | 186 |
| 178 ret.Commits[i] = commit | 187 ret.Commits[i] = commit |
| 179 } | 188 } |
| 180 return nil | 189 return nil |
| 181 }) | 190 }) |
| 182 return ret, err | 191 return ret, err |
| 183 } | 192 } |
| OLD | NEW |