Skip to content

Commit

Permalink
Merge branch 'main' into labels_comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dmjb authored Apr 19, 2024
2 parents 45f9d60 + 9a1a4ab commit 40416ee
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmd/cli/app/repo/repo_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ func registerSelectedRepos(
}

func printRepoRegistrationStatus(cmd *cobra.Command, results []*minderv1.RegisterRepoResult) {
// If there were no results, print a message and return
if len(results) == 0 {
cmd.Println("No repositories registered")
return
}

t := table.New(table.Simple, layouts.Default, []string{"Repository", "Status", "Message"})
for _, result := range results {
// in the case of a malformed response, skip over it to avoid segfaulting
Expand Down
2 changes: 1 addition & 1 deletion internal/controlplane/handlers_repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *Server) RegisterRepository(

newRepo, err := s.repos.CreateRepository(ctx, client, provider, projectID, githubRepo.GetOwner(), githubRepo.GetName())
if err != nil {
if errors.Is(err, ghrepo.ErrPrivateRepoForbidden) {
if errors.Is(err, ghrepo.ErrPrivateRepoForbidden) || errors.Is(err, ghrepo.ErrArchivedRepoForbidden) {
return nil, util.UserVisibleError(codes.InvalidArgument, err.Error())
}
return nil, util.UserVisibleError(codes.Internal, "unable to register repository: %v", err)
Expand Down
7 changes: 7 additions & 0 deletions internal/controlplane/handlers_repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ func TestServer_RegisterRepository(t *testing.T) {
RepoServiceSetup: newRepoService(withFailedCreate(ghrepo.ErrPrivateRepoForbidden)),
ExpectedError: "private repos cannot be registered in this project",
},
{
Name: "Repo creation fails repo is archived, and archived repos are not allowed",
RepoOwner: repoOwner,
RepoName: repoName,
RepoServiceSetup: newRepoService(withFailedCreate(ghrepo.ErrArchivedRepoForbidden)),
ExpectedError: "archived repos cannot be registered in this project",
},
{
Name: "Repo creation on unexpected error",
RepoOwner: repoOwner,
Expand Down
4 changes: 4 additions & 0 deletions internal/providers/github/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
func ConvertRepositories(repos []*gogithub.Repository) []*minderv1.Repository {
var converted []*minderv1.Repository
for _, repo := range repos {
// Skip archived repositories
if repo.Archived != nil && *repo.Archived {
continue
}
converted = append(converted, ConvertRepository(repo))
}
return converted
Expand Down
8 changes: 8 additions & 0 deletions internal/repositories/github/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ var (
// attempt to register a private repo in a project which does not allow
// private repos
ErrPrivateRepoForbidden = errors.New("private repos cannot be registered in this project")
// ErrArchivedRepoForbidden is returned when creation fails due to an
// attempt to register an archived repo
ErrArchivedRepoForbidden = errors.New("archived repos cannot be registered in this project")
)

type repositoryService struct {
Expand Down Expand Up @@ -114,6 +117,11 @@ func (r *repositoryService) CreateRepository(
return nil, fmt.Errorf("error retrieving repo from github: %w", err)
}

// skip if this is an archived repo
if githubRepo.GetArchived() {
return nil, ErrArchivedRepoForbidden
}

// skip if this is a private repo, and private repos are not enabled
if githubRepo.GetPrivate() && !features.ProjectAllowsPrivateRepos(ctx, r.store, projectID) {
return nil, ErrPrivateRepoForbidden
Expand Down

0 comments on commit 40416ee

Please sign in to comment.