Skip to content

Commit 3b61301

Browse files
feat: add github compare api support
1 parent 095c86a commit 3b61301

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

pkg/provider/github.go

+27-8
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ import (
1818
var PVERSION = "dev"
1919

2020
type GitHubRepository struct {
21-
owner string
22-
repo string
23-
client *github.Client
21+
owner string
22+
repo string
23+
client *github.Client
24+
compareCommits bool
2425
}
2526

2627
func (repo *GitHubRepository) Init(config map[string]string) error {
@@ -42,12 +43,14 @@ func (repo *GitHubRepository) Init(config map[string]string) error {
4243
if token == "" {
4344
return errors.New("github token missing")
4445
}
46+
4547
if !strings.Contains(slug, "/") {
4648
return errors.New("invalid slug")
4749
}
4850
split := strings.Split(slug, "/")
4951
repo.owner = split[0]
5052
repo.repo = split[1]
53+
5154
oauthClient := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}))
5255
if gheHost != "" {
5356
gheUrl := fmt.Sprintf("https://%s/api/v3/", gheHost)
@@ -59,6 +62,11 @@ func (repo *GitHubRepository) Init(config map[string]string) error {
5962
} else {
6063
repo.client = github.NewClient(oauthClient)
6164
}
65+
66+
if config["github_use_compare_commits"] == "true" {
67+
repo.compareCommits = true
68+
}
69+
6270
return nil
6371
}
6472

@@ -75,15 +83,26 @@ func (repo *GitHubRepository) GetInfo() (*provider.RepositoryInfo, error) {
7583
}, nil
7684
}
7785

86+
func (repo *GitHubRepository) getCommitsFromGithub(fromSha, toSha string, opts *github.ListOptions) ([]*github.RepositoryCommit, *github.Response, error) {
87+
if !repo.compareCommits {
88+
return repo.client.Repositories.ListCommits(context.Background(), repo.owner, repo.repo, &github.CommitsListOptions{
89+
SHA: toSha,
90+
ListOptions: *opts,
91+
})
92+
}
93+
compCommits, resp, err := repo.client.Repositories.CompareCommits(context.Background(), repo.owner, repo.repo, fromSha, toSha, opts)
94+
if err != nil {
95+
return nil, nil, err
96+
}
97+
return compCommits.Commits, resp, nil
98+
}
99+
78100
func (repo *GitHubRepository) GetCommits(fromSha, toSha string) ([]*semrel.RawCommit, error) {
79101
allCommits := make([]*semrel.RawCommit, 0)
80-
opts := &github.CommitsListOptions{
81-
SHA: toSha,
82-
ListOptions: github.ListOptions{PerPage: 100},
83-
}
102+
opts := &github.ListOptions{PerPage: 100}
84103
done := false
85104
for {
86-
commits, resp, err := repo.client.Repositories.ListCommits(context.Background(), repo.owner, repo.repo, opts)
105+
commits, resp, err := repo.getCommitsFromGithub(fromSha, toSha, opts)
87106
if err != nil {
88107
return nil, err
89108
}

pkg/provider/github_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"net/http/httptest"
88
"net/url"
9+
"strings"
910
"testing"
1011

1112
"github.com/go-semantic-release/semantic-release/v2/pkg/provider"
@@ -100,6 +101,20 @@ func githubHandler(w http.ResponseWriter, r *http.Request) {
100101
json.NewEncoder(w).Encode(GITHUB_REPO)
101102
return
102103
}
104+
if r.Method == "GET" && strings.HasPrefix(r.URL.Path, "/repos/owner/test-repo/compare/") {
105+
li := strings.LastIndex(r.URL.Path, "/")
106+
shaRange := strings.Split(r.URL.Path[li+1:], "...")
107+
toSha := shaRange[1]
108+
skip := 0
109+
for i, commit := range GITHUB_COMMITS {
110+
if commit.GetSHA() == toSha {
111+
skip = i
112+
break
113+
}
114+
}
115+
json.NewEncoder(w).Encode(github.CommitsComparison{Commits: GITHUB_COMMITS[skip:]})
116+
return
117+
}
103118
if r.Method == "GET" && r.URL.Path == "/repos/owner/test-repo/commits" {
104119
toSha := r.URL.Query().Get("sha")
105120
skip := 0
@@ -185,6 +200,21 @@ func TestGithubGetCommits(t *testing.T) {
185200
}
186201
}
187202

203+
func TestGithubGetCommitsWithCompare(t *testing.T) {
204+
repo, ts := getNewGithubTestRepo(t)
205+
defer ts.Close()
206+
repo.compareCommits = true
207+
commits, err := repo.GetCommits("2222", "1111")
208+
require.NoError(t, err)
209+
require.Len(t, commits, 5)
210+
211+
for i, c := range commits {
212+
idxOff := i + 1
213+
require.Equal(t, c.SHA, GITHUB_COMMITS[idxOff].GetSHA())
214+
require.Equal(t, c.RawMessage, GITHUB_COMMITS[idxOff].Commit.GetMessage())
215+
}
216+
}
217+
188218
func TestGithubGetReleases(t *testing.T) {
189219
repo, ts := getNewGithubTestRepo(t)
190220
defer ts.Close()

0 commit comments

Comments
 (0)