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

🐛 handle gitlab repos with no commits #3731

Merged
merged 6 commits into from
Jan 4, 2024
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/checkruns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Test_CheckRuns(t *testing.T) {
},
{
name: "valid checkruns with zero results",
responsePath: "./testdata/valid-checkruns-1",
responsePath: "./testdata/empty-response",
ref: "eb94b618fb5865b26e80fdd8ae531b7a63ad851a",
wantErr: false,
},
Expand Down
4 changes: 4 additions & 0 deletions clients/gitlabrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ func (client *Client) ListCommits() ([]clients.Commit, error) {
return []clients.Commit{}, err
}

if len(commitsRaw) < 1 {
return []clients.Commit{}, nil
}

before := commitsRaw[0].CommittedDate
// Get merge request details from GraphQL
// GitLab REST API doesn't provide a way to link Merge Requests and Commits that
Expand Down
73 changes: 73 additions & 0 deletions clients/gitlabrepo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
package gitlabrepo

import (
"context"
"errors"
"net/http"
"testing"

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

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

func TestCheckRepoInaccessible(t *testing.T) {
Expand Down Expand Up @@ -69,3 +74,71 @@ func TestCheckRepoInaccessible(t *testing.T) {
})
}
}

func TestListCommits(t *testing.T) {
t.Parallel()

tests := []struct {
name string
responsePath string
commits []clients.Commit
wantErr bool
}{
{
name: "Error in ListRawCommits",
responsePath: "./testdata/invalid-commits",
commits: []clients.Commit{},
wantErr: true,
},
{
name: "No commits in repo",
responsePath: "./testdata/empty-response",
commits: []clients.Commit{},
wantErr: false,
},
}
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.responsePath, // corresponds to projects/<id>/repository/commits
},
},
}
glclient, err := gitlab.NewClient("", gitlab.WithHTTPClient(httpClient))
if err != nil {
t.Fatalf("gitlab.NewClient error: %v", err)
}
commitshandler := &commitsHandler{
glClient: glclient,
}

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

commitshandler.init(&repoURL, 30)

gqlhandler := graphqlHandler{
client: httpClient,
}
gqlhandler.init(context.Background(), &repoURL)

client := &Client{glClient: glclient, commits: commitshandler, graphql: &gqlhandler}

got, Err := client.ListCommits()

if (Err != nil) != tt.wantErr {
t.Fatalf("ListCommits, wanted Error: %v, got Error: %v", tt.wantErr, Err)
}
if !cmp.Equal(got, tt.commits) {
t.Errorf("ListCommits() got %v, want %v", got, tt.commits)
}
})
}
}
Loading