Skip to content

Commit

Permalink
simplify IsArchived call
Browse files Browse the repository at this point in the history
Signed-off-by: Jamie Magee <[email protected]>
  • Loading branch information
JamieMagee committed Nov 12, 2024
1 parent dc61385 commit ecf43e0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 46 deletions.
27 changes: 5 additions & 22 deletions clients/azuredevopsrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"io"
"os"
"strings"
"sync"
"time"

"github.com/microsoft/azure-devops-go-api/azuredevops/v7"
Expand All @@ -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 {
Expand All @@ -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)
}

Check warning on line 60 in clients/azuredevopsrepo/client.go

View check run for this annotation

Codecov / codecov/patch

clients/azuredevopsrepo/client.go#L54-L60

Added lines #L54 - L60 were not covered by tests

c.repo = repo

if commitDepth <= 0 {
c.commitDepth = 30 // default
} else {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -137,7 +120,7 @@ func (c *Client) GetDefaultBranchName() (string, error) {
return c.repourl.defaultBranch, nil
}

Check warning on line 121 in clients/azuredevopsrepo/client.go

View check run for this annotation

Codecov / codecov/patch

clients/azuredevopsrepo/client.go#L118-L121

Added lines #L118 - L121 were not covered by tests

return "", fmt.Errorf("%w", errDefaultBranchNotFound)
return "", errDefaultBranchNotFound

Check warning on line 123 in clients/azuredevopsrepo/client.go

View check run for this annotation

Codecov / codecov/patch

clients/azuredevopsrepo/client.go#L123

Added line #L123 was not covered by tests
}

func (c *Client) GetDefaultBranch() (*clients.BranchRef, error) {
Expand Down
26 changes: 2 additions & 24 deletions clients/azuredevopsrepo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"},
}
Expand Down

0 comments on commit ecf43e0

Please sign in to comment.