Skip to content

Commit 43de75a

Browse files
fix: handle first release GetCommits correctly
1 parent 3b61301 commit 43de75a

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

pkg/provider/github.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ func (repo *GitHubRepository) GetInfo() (*provider.RepositoryInfo, error) {
8383
}, nil
8484
}
8585

86-
func (repo *GitHubRepository) getCommitsFromGithub(fromSha, toSha string, opts *github.ListOptions) ([]*github.RepositoryCommit, *github.Response, error) {
87-
if !repo.compareCommits {
86+
func (repo *GitHubRepository) getCommitsFromGithub(compareCommits bool, fromSha, toSha string, opts *github.ListOptions) ([]*github.RepositoryCommit, *github.Response, error) {
87+
if !compareCommits {
8888
return repo.client.Repositories.ListCommits(context.Background(), repo.owner, repo.repo, &github.CommitsListOptions{
8989
SHA: toSha,
9090
ListOptions: *opts,
@@ -98,17 +98,23 @@ func (repo *GitHubRepository) getCommitsFromGithub(fromSha, toSha string, opts *
9898
}
9999

100100
func (repo *GitHubRepository) GetCommits(fromSha, toSha string) ([]*semrel.RawCommit, error) {
101+
compareCommits := repo.compareCommits
102+
if compareCommits && fromSha == "" {
103+
// we want all commits for the first release, hence disable compareCommits
104+
compareCommits = false
105+
}
101106
allCommits := make([]*semrel.RawCommit, 0)
102107
opts := &github.ListOptions{PerPage: 100}
103108
done := false
104109
for {
105-
commits, resp, err := repo.getCommitsFromGithub(fromSha, toSha, opts)
110+
commits, resp, err := repo.getCommitsFromGithub(compareCommits, fromSha, toSha, opts)
106111
if err != nil {
107112
return nil, err
108113
}
109114
for _, commit := range commits {
110115
sha := commit.GetSHA()
111-
if sha == fromSha {
116+
// compare commits already returns the relevant commits and no extra filtering is needed
117+
if !compareCommits && sha == fromSha {
112118
done = true
113119
break
114120
}
@@ -122,7 +128,6 @@ func (repo *GitHubRepository) GetCommits(fromSha, toSha string) ([]*semrel.RawCo
122128
}
123129
opts.Page = resp.NextPage
124130
}
125-
126131
return allCommits, nil
127132
}
128133

pkg/provider/github_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,18 @@ func githubHandler(w http.ResponseWriter, r *http.Request) {
104104
if r.Method == "GET" && strings.HasPrefix(r.URL.Path, "/repos/owner/test-repo/compare/") {
105105
li := strings.LastIndex(r.URL.Path, "/")
106106
shaRange := strings.Split(r.URL.Path[li+1:], "...")
107+
fromSha := shaRange[0]
107108
toSha := shaRange[1]
108-
skip := 0
109+
start := 0
110+
end := 0
109111
for i, commit := range GITHUB_COMMITS {
110112
if commit.GetSHA() == toSha {
111-
skip = i
112-
break
113+
start = i
114+
} else if commit.GetSHA() == fromSha {
115+
end = i
113116
}
114117
}
115-
json.NewEncoder(w).Encode(github.CommitsComparison{Commits: GITHUB_COMMITS[skip:]})
118+
json.NewEncoder(w).Encode(github.CommitsComparison{Commits: GITHUB_COMMITS[start:end]})
116119
return
117120
}
118121
if r.Method == "GET" && r.URL.Path == "/repos/owner/test-repo/commits" {

0 commit comments

Comments
 (0)