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 1 commit
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
51 changes: 36 additions & 15 deletions clients/gitlabrepo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,52 @@ import (
)

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
i := 1

for {
c, _, err := handler.glClient.Commits.ListCommits(handler.repourl.projectID,
&gitlab.ListCommitsOptions{
ListOptions: gitlab.ListOptions{
Page: i,
PerPage: handler.commitDepth,
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
}

if len(c) == 0 {
ashearin marked this conversation as resolved.
Show resolved Hide resolved
break
}
i++

commits = append(commits, c...)

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

handler.commitsRaw = commits
if handler.repourl.commitSHA != clients.HeadSHA {
//nolint:lll
Expand Down
74 changes: 74 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,71 @@ 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: "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
Loading