-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨ implement `ListIssues` and `GetCreatedAt` for Azure DevOps Signed-off-by: Jamie Magee <[email protected]> * PR comments Signed-off-by: Jamie Magee <[email protected]> * Reset issues list on initialization Signed-off-by: Jamie Magee <[email protected]> --------- Signed-off-by: Jamie Magee <[email protected]>
- Loading branch information
1 parent
cdfb58b
commit 57850ee
Showing
6 changed files
with
625 additions
and
12 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
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 | ||
return | ||
} | ||
} | ||
|
||
if *auditLog.HasMore { | ||
continuationToken = *auditLog.ContinuationToken | ||
} else { | ||
return | ||
} | ||
} | ||
}) | ||
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
Oops, something went wrong.