-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ implement
ListContributors
for Azure DevOps (#4437)
* ✨ implement `ListContributors` for Azure DevOps Signed-off-by: Jamie Magee <[email protected]> * Fix tests Signed-off-by: Jamie Magee <[email protected]> --------- Signed-off-by: Jamie Magee <[email protected]>
- Loading branch information
1 parent
687b739
commit b0dfb70
Showing
4 changed files
with
250 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2024 OpenSSF Scorecard Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package azuredevopsrepo | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/git" | ||
|
||
"github.com/ossf/scorecard/v5/clients" | ||
) | ||
|
||
type contributorsHandler struct { | ||
ctx context.Context | ||
once *sync.Once | ||
repourl *Repo | ||
gitClient git.Client | ||
errSetup error | ||
getCommits fnGetCommits | ||
contributors []clients.User | ||
} | ||
|
||
func (c *contributorsHandler) init(ctx context.Context, repourl *Repo) { | ||
c.ctx = ctx | ||
c.once = new(sync.Once) | ||
c.repourl = repourl | ||
c.errSetup = nil | ||
c.getCommits = c.gitClient.GetCommits | ||
c.contributors = nil | ||
} | ||
|
||
func (c *contributorsHandler) setup() error { | ||
c.once.Do(func() { | ||
contributors := make(map[string]clients.User) | ||
commitsPageSize := 1000 | ||
skip := 0 | ||
for { | ||
args := git.GetCommitsArgs{ | ||
RepositoryId: &c.repourl.id, | ||
SearchCriteria: &git.GitQueryCommitsCriteria{ | ||
Top: &commitsPageSize, | ||
Skip: &skip, | ||
}, | ||
} | ||
commits, err := c.getCommits(c.ctx, args) | ||
if err != nil { | ||
c.errSetup = err | ||
return | ||
} | ||
|
||
if commits == nil || len(*commits) == 0 { | ||
break | ||
} | ||
|
||
for i := range *commits { | ||
commit := (*commits)[i] | ||
email := *commit.Author.Email | ||
if _, ok := contributors[email]; ok { | ||
user := contributors[email] | ||
user.NumContributions++ | ||
contributors[email] = user | ||
} else { | ||
contributors[email] = clients.User{ | ||
Login: email, | ||
NumContributions: 1, | ||
Companies: []string{c.repourl.organization}, | ||
} | ||
} | ||
} | ||
|
||
skip += commitsPageSize | ||
} | ||
|
||
for _, contributor := range contributors { | ||
c.contributors = append(c.contributors, contributor) | ||
} | ||
}) | ||
return c.errSetup | ||
} | ||
|
||
func (c *contributorsHandler) listContributors() ([]clients.User, error) { | ||
if err := c.setup(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return c.contributors, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright 2024 OpenSSF Scorecard Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package azuredevopsrepo | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/git" | ||
|
||
"github.com/ossf/scorecard/v5/clients" | ||
) | ||
|
||
func Test_listContributors(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
getCommits fnGetCommits | ||
wantContribs []clients.User | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "no commits", | ||
getCommits: func(ctx context.Context, args git.GetCommitsArgs) (*[]git.GitCommitRef, error) { | ||
return &[]git.GitCommitRef{}, nil | ||
}, | ||
wantContribs: nil, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "single contributor", | ||
getCommits: func(ctx context.Context, args git.GetCommitsArgs) (*[]git.GitCommitRef, error) { | ||
if *args.SearchCriteria.Skip == 0 { | ||
return &[]git.GitCommitRef{ | ||
{ | ||
Author: &git.GitUserDate{ | ||
Email: toPtr("[email protected]"), | ||
}, | ||
}, | ||
}, nil | ||
} else { | ||
return &[]git.GitCommitRef{}, nil | ||
} | ||
}, | ||
wantContribs: []clients.User{ | ||
{ | ||
Login: "[email protected]", | ||
Companies: []string{"testOrg"}, | ||
NumContributions: 1, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "multiple contributors", | ||
getCommits: func(ctx context.Context, args git.GetCommitsArgs) (*[]git.GitCommitRef, error) { | ||
if *args.SearchCriteria.Skip == 0 { | ||
return &[]git.GitCommitRef{ | ||
{ | ||
Author: &git.GitUserDate{ | ||
Email: toPtr("[email protected]"), | ||
}, | ||
}, | ||
{ | ||
Author: &git.GitUserDate{ | ||
Email: toPtr("[email protected]"), | ||
}, | ||
}, | ||
{ | ||
Author: &git.GitUserDate{ | ||
Email: toPtr("[email protected]"), | ||
}, | ||
}, | ||
}, nil | ||
} else { | ||
return &[]git.GitCommitRef{}, nil | ||
} | ||
}, | ||
wantContribs: []clients.User{ | ||
{ | ||
Login: "[email protected]", | ||
Companies: []string{"testOrg"}, | ||
NumContributions: 1, | ||
}, | ||
{ | ||
Login: "[email protected]", | ||
Companies: []string{"testOrg"}, | ||
NumContributions: 2, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
c := contributorsHandler{ | ||
ctx: context.Background(), | ||
once: new(sync.Once), | ||
repourl: &Repo{ | ||
organization: "testOrg", | ||
}, | ||
getCommits: tt.getCommits, | ||
} | ||
err := c.setup() | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("contributorsHandler.setup() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if diff := cmp.Diff(tt.wantContribs, c.contributors, cmpopts.SortSlices(func(a, b string) bool { return a < b })); diff != "" { | ||
t.Errorf("contributorsHandler.setup() mismatch (-want +got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters