| // Copyright 2020 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package main |
| |
| import ( |
| "context" |
| "fmt" |
| |
| "go.chromium.org/luci/auth" |
| "go.chromium.org/luci/common/api/gitiles" |
| gitilespb "go.chromium.org/luci/common/proto/gitiles" |
| ) |
| |
| // gitilesClientWrapper provides utilities for interacting with a Gitiles project. |
| type gitilesClientWrapper struct { |
| client gitilespb.GitilesClient |
| project string |
| } |
| |
| // newGitilesClient returns an authenticated gitilesClientWrapper. |
| func newGitilesClient(ctx context.Context, host, project string, authOpts auth.Options) (*gitilesClientWrapper, error) { |
| authClient, err := newAuthClient(ctx, authOpts) |
| if err != nil { |
| return nil, err |
| } |
| client, err := gitiles.NewRESTClient(authClient, host, true) |
| if err != nil { |
| return nil, err |
| } |
| return &gitilesClientWrapper{client: client, project: project}, nil |
| } |
| |
| // getLatestCommit returns the revision at refs/heads/master HEAD. |
| func (c *gitilesClientWrapper) getLatestCommit(ctx context.Context) (string, error) { |
| resp, err := c.client.Log(ctx, &gitilespb.LogRequest{ |
| Project: c.project, |
| Committish: "refs/heads/master", |
| PageSize: 1, |
| }) |
| if err != nil { |
| return "", err |
| } |
| if len(resp.Log) == 0 { |
| return "", fmt.Errorf("empty log for project %s", c.project) |
| } |
| return resp.Log[0].GetId(), nil |
| } |