Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add commit depth support for GitLab #3672

Merged
merged 4 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clients/gitlabrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (client *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitD
client.contributors.init(client.repourl)

// Init commitsHandler
client.commits.init(client.repourl)
client.commits.init(client.repourl, client.commitDepth)

// Init branchesHandler
client.branches.init(client.repourl)
Expand Down
55 changes: 40 additions & 15 deletions clients/gitlabrepo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,56 @@
)

type commitsHandler struct {
glClient *gitlab.Client
once *sync.Once
errSetup error
repourl *repoURL
commitsRaw []*gitlab.Commit
glClient *gitlab.Client
once *sync.Once
errSetup error
repourl *repoURL
commitsRaw []*gitlab.Commit
commitDepth int
}

func (handler *commitsHandler) init(repourl *repoURL) {
func (handler *commitsHandler) init(repourl *repoURL, commitDepth int) {
handler.repourl = repourl
handler.errSetup = nil
handler.once = new(sync.Once)
handler.commitDepth = commitDepth
}

func (handler *commitsHandler) setup() error {
handler.once.Do(func() {
commits, _, err := handler.glClient.Commits.ListCommits(
handler.repourl.projectID,
&gitlab.ListCommitsOptions{
RefName: &handler.repourl.commitSHA,
spencerschrock marked this conversation as resolved.
Show resolved Hide resolved
},
)
if err != nil {
handler.errSetup = fmt.Errorf("request for commits failed with %w", err)
return
var commits []*gitlab.Commit
opt := gitlab.ListOptions{
Page: 1,
PerPage: handler.commitDepth,
}

for {
c, resp, err := handler.glClient.Commits.ListCommits(handler.repourl.projectID,
&gitlab.ListCommitsOptions{
RefName: &handler.repourl.commitSHA,
ListOptions: opt,
})
if err != nil {
handler.errSetup = fmt.Errorf("request for commits failed with %w", err)
return
}

commits = append(commits, c...)

if len(commits) >= handler.commitDepth {
commits = commits[:handler.commitDepth]
break
}

// Exit the loop when we've seen all pages.
if resp.NextPage == 0 {
break
spencerschrock marked this conversation as resolved.
Show resolved Hide resolved
}

// Update the page number to get the next page.
opt.Page = resp.NextPage

Check warning on line 77 in clients/gitlabrepo/commits.go

View check run for this annotation

Codecov / codecov/patch

clients/gitlabrepo/commits.go#L77

Added line #L77 was not covered by tests
}

handler.commitsRaw = commits
if handler.repourl.commitSHA != clients.HeadSHA {
//nolint:lll
Expand Down
81 changes: 81 additions & 0 deletions clients/gitlabrepo/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
package gitlabrepo

import (
"net/http"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/xanzy/go-gitlab"

"github.com/ossf/scorecard/v4/clients"
)

func TestParsingEmail(t *testing.T) {
Expand Down Expand Up @@ -56,3 +62,78 @@ func TestParsingEmail(t *testing.T) {
})
}
}

func TestListRawCommits(t *testing.T) {
t.Parallel()
tests := []struct {
name string
commitsPath string
commitDepth int
want int
wantErr bool
}{
{
name: "commits to non-default depth",
commitsPath: "./testdata/valid-commits",
commitDepth: 17,
want: 17,
wantErr: false,
},
{
name: "commits to default depth",
commitsPath: "./testdata/valid-commits",
commitDepth: 30,
want: 30,
wantErr: false,
},
{
name: "request more commits than exist in the repo",
commitsPath: "./testdata/valid-commits",
commitDepth: 60,
want: 32,
wantErr: false,
},
{
name: "failure fetching commits",
commitsPath: "./testdata/invalid-commits",
commitDepth: 30,
want: 0,
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

httpClient := &http.Client{
Transport: suffixStubTripper{
responsePaths: map[string]string{
"commits": tt.commitsPath, // corresponds to projects/<id>/repository/commits
},
},
}
client, err := gitlab.NewClient("", gitlab.WithHTTPClient(httpClient))
if err != nil {
t.Fatalf("gitlab.NewClient error: %v", err)
}
handler := &commitsHandler{
glClient: client,
}

repoURL := repoURL{
owner: "ossf-tests",
commitSHA: clients.HeadSHA,
}

handler.init(&repoURL, tt.commitDepth)
commits, err := handler.listRawCommits()
if (err != nil) != tt.wantErr {
t.Fatalf("listIssues error: %v, wantedErr: %t", err, tt.wantErr)
}
if !cmp.Equal(len(commits), tt.want) {
t.Errorf("listCommits() = %v, want %v", len(commits), cmp.Diff(len(commits), tt.want))
}
})
}
}
Loading