-
Notifications
You must be signed in to change notification settings - Fork 503
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
✨ implement ListIssues
and GetCreatedAt
for Azure DevOps
#4419
Open
JamieMagee
wants to merge
1
commit into
ossf:main
Choose a base branch
from
JamieMagee:azure-devops-audit-issues
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+600
−12
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// 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" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/audit" | ||
) | ||
|
||
type auditHandler struct { | ||
auditClient audit.Client | ||
once *sync.Once | ||
ctx context.Context | ||
errSetup error | ||
repourl *Repo | ||
createdAt time.Time | ||
queryLog fnQueryLog | ||
} | ||
|
||
func (a *auditHandler) init(ctx context.Context, repourl *Repo) { | ||
a.ctx = ctx | ||
a.errSetup = nil | ||
a.once = new(sync.Once) | ||
a.repourl = repourl | ||
a.queryLog = a.auditClient.QueryLog | ||
} | ||
|
||
type ( | ||
fnQueryLog func(ctx context.Context, args audit.QueryLogArgs) (*audit.AuditLogQueryResult, error) | ||
) | ||
|
||
func (a *auditHandler) setup() error { | ||
a.once.Do(func() { | ||
continuationToken := "" | ||
for { | ||
auditLog, err := a.queryLog(a.ctx, audit.QueryLogArgs{ | ||
ContinuationToken: &continuationToken, | ||
}) | ||
if err != nil { | ||
a.errSetup = fmt.Errorf("error querying audit log: %w", err) | ||
return | ||
} | ||
|
||
// Check if Git.CreateRepo event exists for the repository | ||
for i := range *auditLog.DecoratedAuditLogEntries { | ||
entry := &(*auditLog.DecoratedAuditLogEntries)[i] | ||
if *entry.ActionId == "Git.CreateRepo" && | ||
*entry.ProjectName == a.repourl.project && | ||
(*entry.Data)["RepoName"] == a.repourl.name { | ||
a.createdAt = entry.Timestamp.Time | ||
break | ||
} | ||
} | ||
|
||
if *auditLog.HasMore { | ||
continuationToken = *auditLog.ContinuationToken | ||
} else { | ||
break | ||
} | ||
} | ||
}) | ||
return a.errSetup | ||
} | ||
|
||
func (a *auditHandler) getRepsitoryCreatedAt() (time.Time, error) { | ||
if err := a.setup(); err != nil { | ||
return time.Time{}, fmt.Errorf("error during auditHandler.setup: %w", err) | ||
} | ||
|
||
return a.createdAt, 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,89 @@ | ||
// 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" | ||
"errors" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/microsoft/azure-devops-go-api/azuredevops/v7" | ||
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/audit" | ||
) | ||
|
||
func Test_auditHandler_setup(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
queryLog fnQueryLog | ||
createdAt time.Time | ||
name string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "successful setup", | ||
queryLog: func(ctx context.Context, args audit.QueryLogArgs) (*audit.AuditLogQueryResult, error) { | ||
return &audit.AuditLogQueryResult{ | ||
HasMore: new(bool), | ||
ContinuationToken: new(string), | ||
DecoratedAuditLogEntries: &[]audit.DecoratedAuditLogEntry{ | ||
{ | ||
ActionId: strptr("Git.CreateRepo"), | ||
ProjectName: strptr("test-project"), | ||
Data: &map[string]interface{}{"RepoName": "test-repo"}, | ||
Timestamp: &azuredevops.Time{Time: time.Date(2023, time.January, 1, 0, 0, 0, 0, time.UTC)}, | ||
}, | ||
}, | ||
}, nil | ||
}, | ||
wantErr: false, | ||
createdAt: time.Date(2023, time.January, 1, 0, 0, 0, 0, time.UTC), | ||
}, | ||
{ | ||
name: "query log error", | ||
queryLog: func(ctx context.Context, args audit.QueryLogArgs) (*audit.AuditLogQueryResult, error) { | ||
return nil, errors.New("query log error") | ||
}, | ||
wantErr: true, | ||
createdAt: time.Time{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
handler := &auditHandler{ | ||
once: new(sync.Once), | ||
queryLog: tt.queryLog, | ||
repourl: &Repo{ | ||
project: "test-project", | ||
name: "test-repo", | ||
}, | ||
} | ||
err := handler.setup() | ||
if (err != nil) != tt.wantErr { | ||
t.Fatalf("setup() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
if !tt.wantErr && !handler.createdAt.Equal(tt.createdAt) { | ||
t.Errorf("setup() createdAt = %v, want %v", handler.createdAt, tt.createdAt) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func strptr(s string) *string { | ||
return &s | ||
} |
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
"errors" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/git" | ||
|
||
|
@@ -28,16 +29,18 @@ import ( | |
var errMultiplePullRequests = errors.New("expected 1 pull request for commit, got multiple") | ||
|
||
type commitsHandler struct { | ||
gitClient git.Client | ||
ctx context.Context | ||
errSetup error | ||
once *sync.Once | ||
repourl *Repo | ||
commitsRaw *[]git.GitCommitRef | ||
pullRequestsRaw *git.GitPullRequestQuery | ||
getCommits fnGetCommits | ||
getPullRequestQuery fnGetPullRequestQuery | ||
commitDepth int | ||
gitClient git.Client | ||
ctx context.Context | ||
errSetup error | ||
once *sync.Once | ||
repourl *Repo | ||
commitsRaw *[]git.GitCommitRef | ||
pullRequestsRaw *git.GitPullRequestQuery | ||
firstCommitCreatedAt time.Time | ||
getCommits fnGetCommits | ||
getPullRequestQuery fnGetPullRequestQuery | ||
getFirstCommit fnGetFirstCommit | ||
commitDepth int | ||
} | ||
|
||
func (handler *commitsHandler) init(ctx context.Context, repourl *Repo, commitDepth int) { | ||
|
@@ -48,11 +51,13 @@ func (handler *commitsHandler) init(ctx context.Context, repourl *Repo, commitDe | |
handler.commitDepth = commitDepth | ||
handler.getCommits = handler.gitClient.GetCommits | ||
handler.getPullRequestQuery = handler.gitClient.GetPullRequestQuery | ||
handler.getFirstCommit = handler.gitClient.GetCommits | ||
} | ||
|
||
type ( | ||
fnGetCommits func(ctx context.Context, args git.GetCommitsArgs) (*[]git.GitCommitRef, error) | ||
fnGetPullRequestQuery func(ctx context.Context, args git.GetPullRequestQueryArgs) (*git.GitPullRequestQuery, error) | ||
fnGetFirstCommit func(ctx context.Context, args git.GetCommitsArgs) (*[]git.GitCommitRef, error) | ||
) | ||
|
||
func (handler *commitsHandler) setup() error { | ||
|
@@ -106,6 +111,29 @@ func (handler *commitsHandler) setup() error { | |
return | ||
} | ||
|
||
// If there are fewer commits than requested, the first commit is the createdAt date | ||
if len(*commits) < handler.commitDepth { | ||
handler.firstCommitCreatedAt = (*commits)[len(*commits)-1].Committer.Date.Time | ||
Comment on lines
+114
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can azure have a repository with no commits? These edge cases have been issues for Scorecard on other forges and would cause a panic |
||
} else { | ||
firstCommit, err := handler.getFirstCommit(handler.ctx, git.GetCommitsArgs{ | ||
RepositoryId: &handler.repourl.id, | ||
SearchCriteria: &git.GitQueryCommitsCriteria{ | ||
Top: &[]int{1}[0], | ||
ShowOldestCommitsFirst: &[]bool{true}[0], | ||
ItemVersion: &git.GitVersionDescriptor{ | ||
VersionType: &git.GitVersionTypeValues.Branch, | ||
Version: &handler.repourl.defaultBranch, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
handler.errSetup = fmt.Errorf("request for first commit failed with %w", err) | ||
return | ||
} | ||
|
||
handler.firstCommitCreatedAt = (*firstCommit)[0].Committer.Date.Time | ||
} | ||
|
||
handler.commitsRaw = commits | ||
handler.pullRequestsRaw = pullRequests | ||
|
||
|
@@ -182,3 +210,11 @@ func (handler *commitsHandler) listPullRequests() (map[string]clients.PullReques | |
|
||
return pullRequests, nil | ||
} | ||
|
||
func (handler *commitsHandler) getFirstCommitCreatedAt() (time.Time, error) { | ||
if err := handler.setup(); err != nil { | ||
return time.Time{}, fmt.Errorf("error during commitsHandler.setup: %w", err) | ||
} | ||
|
||
return handler.firstCommitCreatedAt, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these breaks could be returns I think. At least the first one, otherwise it will only break out of the inner loop and keep searching the audit log (if there's more)