From ecf43e08dfd487a9d413f1af234cef4998ec7226 Mon Sep 17 00:00:00 2001 From: Jamie Magee Date: Mon, 11 Nov 2024 21:37:28 -0800 Subject: [PATCH] simplify IsArchived call Signed-off-by: Jamie Magee --- clients/azuredevopsrepo/client.go | 27 +++++--------------------- clients/azuredevopsrepo/client_test.go | 26 ++----------------------- 2 files changed, 7 insertions(+), 46 deletions(-) diff --git a/clients/azuredevopsrepo/client.go b/clients/azuredevopsrepo/client.go index 5c79bd09011..0d5e417769c 100644 --- a/clients/azuredevopsrepo/client.go +++ b/clients/azuredevopsrepo/client.go @@ -21,7 +21,6 @@ import ( "io" "os" "strings" - "sync" "time" "github.com/microsoft/azure-devops-go-api/azuredevops/v7" @@ -40,10 +39,10 @@ type Client struct { azdoClient git.Client ctx context.Context repourl *Repo + repo *git.GitRepository branches *branchesHandler commits *commitsHandler commitDepth int - once sync.Once } func (c *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitDepth int) error { @@ -60,6 +59,8 @@ func (c *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitDepth return fmt.Errorf("could not get repository with error: %w", err) } + c.repo = repo + if commitDepth <= 0 { c.commitDepth = 30 // default } else { @@ -91,25 +92,7 @@ func (c *Client) URI() string { } func (c *Client) IsArchived() (bool, error) { - var ( - isArchived bool - isArchivedErr error - ) - - c.once.Do(func() { - repo, err := c.azdoClient.GetRepository(c.ctx, git.GetRepositoryArgs{RepositoryId: &c.repourl.id}) - if err != nil { - isArchivedErr = fmt.Errorf("could not get repository with error: %w", err) - return - } - isArchived = *repo.IsDisabled - }) - - if isArchivedErr != nil { - return false, isArchivedErr - } - - return isArchived, nil + return *c.repo.IsDisabled, nil } func (c *Client) ListFiles(predicate func(string) (bool, error)) ([]string, error) { @@ -137,7 +120,7 @@ func (c *Client) GetDefaultBranchName() (string, error) { return c.repourl.defaultBranch, nil } - return "", fmt.Errorf("%w", errDefaultBranchNotFound) + return "", errDefaultBranchNotFound } func (c *Client) GetDefaultBranch() (*clients.BranchRef, error) { diff --git a/clients/azuredevopsrepo/client_test.go b/clients/azuredevopsrepo/client_test.go index 8f34e01c8f4..dd083b36d25 100644 --- a/clients/azuredevopsrepo/client_test.go +++ b/clients/azuredevopsrepo/client_test.go @@ -15,26 +15,11 @@ package azuredevopsrepo import ( - "context" - "errors" "testing" "github.com/microsoft/azure-devops-go-api/azuredevops/v7/git" ) -type mockGitClient struct { - git.Client - err error - isDisabled bool -} - -func (m *mockGitClient) GetRepository(ctx context.Context, args git.GetRepositoryArgs) (*git.GitRepository, error) { - if m.err != nil { - return nil, m.err - } - return &git.GitRepository{IsDisabled: &m.isDisabled}, nil -} - func TestIsArchived(t *testing.T) { t.Parallel() tests := []struct { @@ -56,21 +41,14 @@ func TestIsArchived(t *testing.T) { want: false, wantErr: false, }, - { - name: "error getting repository", - err: errors.New("some error"), - want: false, - wantErr: true, - }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() client := &Client{ - azdoClient: &mockGitClient{ - isDisabled: tt.isDisabled, - err: tt.err, + repo: &git.GitRepository{ + IsDisabled: &tt.isDisabled, }, repourl: &Repo{id: "test-repo-id"}, }