-
Notifications
You must be signed in to change notification settings - Fork 24
/
github.go
40 lines (33 loc) · 883 Bytes
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package conveyor
import (
"context"
"fmt"
"strings"
"github.com/google/go-github/github"
)
// GitHubAPI represents an interface for performing Git operations.
type GitHubAPI interface {
ResolveBranch(ctx context.Context, owner, repo, branch string) (sha string, err error)
}
func NewGitHub(c *github.Client) *GitHub {
return &GitHub{
Git: c.Git,
}
}
// GitHub is an implementation of the Git interface
// backed by the GitHub API.
type GitHub struct {
Git *github.GitService
}
func (g *GitHub) ResolveBranch(ctx context.Context, owner, repo, branch string) (string, error) {
ref, _, err := g.Git.GetRef(ctx, owner, repo, fmt.Sprintf("refs/heads/%s", branch))
if err != nil {
return "", err
}
return *ref.Object.SHA, nil
}
func splitRepo(fullRepo string) (owner, repo string) {
parts := strings.Split(fullRepo, "/")
owner, repo = parts[0], parts[1]
return
}