forked from fossabot/releasekit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit.go
62 lines (47 loc) · 1.58 KB
/
git.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package releasekit
import (
"context"
"github.com/google/go-github/v18/github"
)
// GetCommitForTag gets the commit a tag is a reference to.
func GetCommitForTag(c *github.Client, owner, repo, tag string) (*github.RepositoryCommit, error) {
ref, _, err := c.Git.GetRef(context.Background(), owner, repo, "refs/tags/"+tag)
if err != nil {
return nil, err
}
sha := *ref.Object.SHA
if *ref.Object.Type == "tag" {
tag, _, err := c.Git.GetTag(context.Background(), owner, repo, *ref.Object.SHA)
if err != nil {
return nil, err
}
sha = *tag.Object.SHA
}
commit, _, err := c.Repositories.GetCommit(context.Background(), owner, repo, sha)
if err != nil {
return nil, err
}
return commit, nil
}
// GetFirstCommit gets the first commit to the repository.
func GetFirstCommit(c *github.Client, owner, repo string) (*github.RepositoryCommit, error) {
opts := &github.CommitsListOptions{}
commits, resp, err := c.Repositories.ListCommits(context.Background(), owner, repo, opts)
if err != nil {
return nil, err
}
if resp.NextPage == 0 {
return commits[len(commits)-1], nil
}
opts.Page = resp.LastPage
commits, _, err = c.Repositories.ListCommits(context.Background(), owner, repo, opts)
if err != nil {
return nil, err
}
return commits[len(commits)-1], nil
}
// GetComparison gets the commit comparison for the given base and head range.
func GetComparison(c *github.Client, owner, repo, base, head string) (*github.CommitsComparison, error) {
comparison, _, err := c.Repositories.CompareCommits(context.Background(), owner, repo, base, head)
return comparison, err
}