From 7e6c25510c28113b553688d0e80f8bad7aed31a5 Mon Sep 17 00:00:00 2001 From: AlvoBen <144705560+AlvoBen@users.noreply.github.com> Date: Thu, 4 Apr 2024 12:18:50 +0300 Subject: [PATCH] CLI | Handling disabled repos in ADO (#695) * Add Disabled Repo check * Add unit test * Change test title --------- Co-authored-by: AlvoBen Co-authored-by: Or Shamir Checkmarx <93518641+OrShamirCM@users.noreply.github.com> --- internal/commands/util/usercount/azure.go | 1 + .../util/usercount/user-count_test.go | 21 +++++++++++++++++++ internal/params/flags.go | 1 + internal/wrappers/azure-http.go | 2 +- internal/wrappers/azure.go | 13 +++++++++++- internal/wrappers/mock/azure-mock.go | 3 ++- 6 files changed, 38 insertions(+), 3 deletions(-) diff --git a/internal/commands/util/usercount/azure.go b/internal/commands/util/usercount/azure.go index 93545ef59..443c729ce 100644 --- a/internal/commands/util/usercount/azure.go +++ b/internal/commands/util/usercount/azure.go @@ -107,6 +107,7 @@ func createRunAzureUserCountFunc(azureWrapper wrappers.AzureWrapper) func(cmd *c } log.Println(params.BotCount) + log.Println(params.DisabledReposCount) return err } diff --git a/internal/commands/util/usercount/user-count_test.go b/internal/commands/util/usercount/user-count_test.go index 61625a10a..b673ba13a 100644 --- a/internal/commands/util/usercount/user-count_test.go +++ b/internal/commands/util/usercount/user-count_test.go @@ -3,6 +3,7 @@ package usercount import ( "testing" + "github.com/checkmarx/ast-cli/internal/wrappers" "github.com/spf13/cobra" "gotest.tools/assert" ) @@ -20,3 +21,23 @@ func TestNewUserCountCommand(t *testing.T) { mockParentCmd.SetArgs([]string{UcCommand, "-h"}) assert.NilError(t, cmd.Execute()) } + +func TestGetEnabledRepositories_2Enable1DisableRepo_Success(t *testing.T) { + repos := []wrappers.AzureRepo{ + { + Name: "MOCK REPO", + IsDisabled: false, + }, + { + Name: "MOCK REPO 2", + IsDisabled: true, + }, + { + Name: "MOCK REPO 3", + IsDisabled: false, + }, + } + rootRepo := wrappers.AzureRootRepo{Repos: repos} + enabledRepos := rootRepo.GetEnabledRepos() + assert.Equal(t, len(enabledRepos.Repos), 2) +} diff --git a/internal/params/flags.go b/internal/params/flags.go index ddf5fc9f5..4d3c6b707 100644 --- a/internal/params/flags.go +++ b/internal/params/flags.go @@ -119,6 +119,7 @@ const ( GithubTokenUsage = "GitHub OAuth token. Requires “Repo” scope and organization SSO authorization, if enforced by the organization" GitLabTokenUsage = "GitLab OAuth token" BotCount = "Note: dependabot is not counted but other bots might be considered as contributors." + DisabledReposCount = "Note: Disabled repositories are not counted." URLFlag = "url" GitLabURLFlag = "url-gitlab" URLFlagUsage = "API base URL" diff --git a/internal/wrappers/azure-http.go b/internal/wrappers/azure-http.go index df8b98b52..91409ba17 100644 --- a/internal/wrappers/azure-http.go +++ b/internal/wrappers/azure-http.go @@ -78,7 +78,7 @@ func (g *AzureHTTPWrapper) GetRepositories(url, organizationName, projectName, t if err != nil { return rootRepo, err } - return rootRepo, err + return rootRepo.GetEnabledRepos(), err } func (g *AzureHTTPWrapper) GetProjects(url, organizationName, token string) (AzureRootProject, error) { diff --git a/internal/wrappers/azure.go b/internal/wrappers/azure.go index a2043e3af..5c7af98f9 100644 --- a/internal/wrappers/azure.go +++ b/internal/wrappers/azure.go @@ -17,8 +17,19 @@ type AzureRootRepo struct { Repos []AzureRepo `json:"value,omitempty"` } +func (a *AzureRootRepo) GetEnabledRepos() AzureRootRepo { + var enabledRepos []AzureRepo + for _, repo := range a.Repos { + if !repo.IsDisabled { + enabledRepos = append(enabledRepos, repo) + } + } + return AzureRootRepo{Repos: enabledRepos} +} + type AzureRepo struct { - Name string `json:"name"` + Name string `json:"name"` + IsDisabled bool `json:"isDisabled"` } type AzureRootProject struct { diff --git a/internal/wrappers/mock/azure-mock.go b/internal/wrappers/mock/azure-mock.go index b7ecc8d63..a2081ba8e 100644 --- a/internal/wrappers/mock/azure-mock.go +++ b/internal/wrappers/mock/azure-mock.go @@ -34,7 +34,8 @@ func (g AzureMockWrapper) GetRepositories(url, organizationName, projectName, to if len(projectName) > 0 { var repos = make([]wrappers.AzureRepo, 1) repos[0] = wrappers.AzureRepo{ - Name: "MOCK REPO", + Name: "MOCK REPO", + IsDisabled: false, } return wrappers.AzureRootRepo{Repos: repos}, nil }