feat(repo): allow internal project visibility by default'

This commit is contained in:
JordanSussman 2019-09-10 22:16:19 -05:00 committed by JordanSussman
parent 3efbcb6c0a
commit 4ae206d812
7 changed files with 69 additions and 26 deletions

View File

@ -174,6 +174,7 @@ type (
// Repository provides the repository configuration. // Repository provides the repository configuration.
Repository struct { Repository struct {
Filter []string `envconfig:"DRONE_REPOSITORY_FILTER"` Filter []string `envconfig:"DRONE_REPOSITORY_FILTER"`
Visibility string `envconfig:"DRONE_REPOSITORY_VISIBILITY"`
} }
// Registries provides the registry configuration. // Registries provides the registry configuration.

View File

@ -49,11 +49,11 @@ var serviceSet = wire.NewSet(
orgs.New, orgs.New,
parser.New, parser.New,
pubsub.New, pubsub.New,
repo.New,
token.Renewer, token.Renewer,
trigger.New, trigger.New,
user.New, user.New,
provideRepositoryService,
provideContentService, provideContentService,
provideDatadog, provideDatadog,
provideHookService, provideHookService,
@ -90,6 +90,16 @@ func provideNetrcService(client *scm.Client, renewer core.Renewer, config config
) )
} }
// provideRepo is a Wire provider function that returns
// a repo based on the environment configuration
func provideRepositoryService(client *scm.Client, renewer core.Renewer, config config.Config) core.RepositoryService {
return repo.New(
client,
renewer,
config.Repository.Visibility,
)
}
// provideSession is a Wire provider function that returns a // provideSession is a Wire provider function that returns a
// user session based on the environment configuration. // user session based on the environment configuration.
func provideSession(store core.UserStore, config config.Config) (core.Session, error) { func provideSession(store core.UserStore, config config.Config) (core.Session, error) {

View File

@ -16,7 +16,6 @@ import (
"github.com/drone/drone/service/hook/parser" "github.com/drone/drone/service/hook/parser"
"github.com/drone/drone/service/license" "github.com/drone/drone/service/license"
"github.com/drone/drone/service/org" "github.com/drone/drone/service/org"
"github.com/drone/drone/service/repo"
"github.com/drone/drone/service/token" "github.com/drone/drone/service/token"
"github.com/drone/drone/service/user" "github.com/drone/drone/service/user"
"github.com/drone/drone/store/batch" "github.com/drone/drone/store/batch"
@ -81,7 +80,7 @@ func InitializeApplication(config2 config.Config) (application, error) {
hookService := provideHookService(client, renewer, config2) hookService := provideHookService(client, renewer, config2)
licenseService := license.NewService(userStore, repositoryStore, buildStore, coreLicense) licenseService := license.NewService(userStore, repositoryStore, buildStore, coreLicense)
permStore := perm.New(db) permStore := perm.New(db)
repositoryService := repo.New(client, renewer) repositoryService := provideRepositoryService(client, renewer, config2)
session, err := provideSession(userStore, config2) session, err := provideSession(userStore, config2)
if err != nil { if err != nil {
return application{}, err return application{}, err

View File

@ -24,14 +24,16 @@ import (
type service struct { type service struct {
renew core.Renewer renew core.Renewer
client *scm.Client client *scm.Client
visibility string
} }
// New returns a new Repository service, providing access to the // New returns a new Repository service, providing access to the
// repository information from the source code management system. // repository information from the source code management system.
func New(client *scm.Client, renewer core.Renewer) core.RepositoryService { func New(client *scm.Client, renewer core.Renewer, visibility string) core.RepositoryService {
return &service{ return &service{
renew: renewer, renew: renewer,
client: client, client: client,
visibility: visibility,
} }
} }
@ -53,7 +55,7 @@ func (s *service) List(ctx context.Context, user *core.User) ([]*core.Repository
return nil, err return nil, err
} }
for _, src := range result { for _, src := range result {
repos = append(repos, convertRepository(src)) repos = append(repos, convertRepository(src, s.visibility))
} }
opts.Page = meta.Page.Next opts.Page = meta.Page.Next
opts.URL = meta.Page.NextURL opts.URL = meta.Page.NextURL
@ -79,7 +81,7 @@ func (s *service) Find(ctx context.Context, user *core.User, repo string) (*core
if err != nil { if err != nil {
return nil, err return nil, err
} }
return convertRepository(result), nil return convertRepository(result, s.visibility), nil
} }
func (s *service) FindPerm(ctx context.Context, user *core.User, repo string) (*core.Perm, error) { func (s *service) FindPerm(ctx context.Context, user *core.User, repo string) (*core.Perm, error) {

View File

@ -8,9 +8,9 @@ import (
"context" "context"
"testing" "testing"
"github.com/drone/drone/core"
"github.com/drone/drone/mock" "github.com/drone/drone/mock"
"github.com/drone/drone/mock/mockscm" "github.com/drone/drone/mock/mockscm"
"github.com/drone/drone/core"
"github.com/drone/go-scm/scm" "github.com/drone/go-scm/scm"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
@ -38,7 +38,7 @@ func TestFind(t *testing.T) {
client := new(scm.Client) client := new(scm.Client)
client.Repositories = mockRepoService client.Repositories = mockRepoService
service := New(client, mockRenewer) service := New(client, mockRenewer, "")
want := &core.Repository{ want := &core.Repository{
Namespace: "octocat", Namespace: "octocat",
@ -71,7 +71,7 @@ func TestFind_Err(t *testing.T) {
client := new(scm.Client) client := new(scm.Client)
client.Repositories = mockRepoService client.Repositories = mockRepoService
service := New(client, mockRenewer) service := New(client, mockRenewer, "")
_, err := service.Find(noContext, mockUser, "octocat/hello-world") _, err := service.Find(noContext, mockUser, "octocat/hello-world")
if err != scm.ErrNotFound { if err != scm.ErrNotFound {
t.Errorf("Expect not found error, got %v", err) t.Errorf("Expect not found error, got %v", err)
@ -87,7 +87,7 @@ func TestFind_RefreshErr(t *testing.T) {
mockRenewer := mock.NewMockRenewer(controller) mockRenewer := mock.NewMockRenewer(controller)
mockRenewer.EXPECT().Renew(gomock.Any(), mockUser, false).Return(scm.ErrNotAuthorized) mockRenewer.EXPECT().Renew(gomock.Any(), mockUser, false).Return(scm.ErrNotAuthorized)
service := New(nil, mockRenewer) service := New(nil, mockRenewer, "")
_, err := service.Find(noContext, mockUser, "octocat/hello-world") _, err := service.Find(noContext, mockUser, "octocat/hello-world")
if err == nil { if err == nil {
t.Errorf("Expect error refreshing token") t.Errorf("Expect error refreshing token")
@ -114,7 +114,7 @@ func TestFindPerm(t *testing.T) {
client := new(scm.Client) client := new(scm.Client)
client.Repositories = mockRepoService client.Repositories = mockRepoService
service := New(client, mockRenewer) service := New(client, mockRenewer, "")
want := &core.Perm{ want := &core.Perm{
Read: true, Read: true,
@ -146,7 +146,7 @@ func TestFindPerm_Err(t *testing.T) {
client := new(scm.Client) client := new(scm.Client)
client.Repositories = mockRepoService client.Repositories = mockRepoService
service := New(client, mockRenewer) service := New(client, mockRenewer, "")
_, err := service.FindPerm(noContext, mockUser, "octocat/hello-world") _, err := service.FindPerm(noContext, mockUser, "octocat/hello-world")
if err != scm.ErrNotFound { if err != scm.ErrNotFound {
t.Errorf("Expect not found error, got %v", err) t.Errorf("Expect not found error, got %v", err)
@ -162,7 +162,7 @@ func TestFindPerm_RefreshErr(t *testing.T) {
mockRenewer := mock.NewMockRenewer(controller) mockRenewer := mock.NewMockRenewer(controller)
mockRenewer.EXPECT().Renew(gomock.Any(), mockUser, false).Return(scm.ErrNotAuthorized) mockRenewer.EXPECT().Renew(gomock.Any(), mockUser, false).Return(scm.ErrNotAuthorized)
service := New(nil, mockRenewer) service := New(nil, mockRenewer, "")
_, err := service.FindPerm(noContext, mockUser, "octocat/hello-world") _, err := service.FindPerm(noContext, mockUser, "octocat/hello-world")
if err == nil { if err == nil {
t.Errorf("Expect error refreshing token") t.Errorf("Expect error refreshing token")
@ -199,7 +199,7 @@ func TestList(t *testing.T) {
}, },
} }
service := New(client, mockRenewer) service := New(client, mockRenewer, "")
got, err := service.List(noContext, mockUser) got, err := service.List(noContext, mockUser)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@ -224,7 +224,7 @@ func TestList_Err(t *testing.T) {
client := new(scm.Client) client := new(scm.Client)
client.Repositories = mockRepoService client.Repositories = mockRepoService
service := New(client, mockRenewer) service := New(client, mockRenewer, "")
_, err := service.List(noContext, mockUser) _, err := service.List(noContext, mockUser)
if err != scm.ErrNotAuthorized { if err != scm.ErrNotAuthorized {
t.Errorf("Want not authorized error, got %v", err) t.Errorf("Want not authorized error, got %v", err)
@ -240,7 +240,7 @@ func TestList_RefreshErr(t *testing.T) {
mockRenewer := mock.NewMockRenewer(controller) mockRenewer := mock.NewMockRenewer(controller)
mockRenewer.EXPECT().Renew(gomock.Any(), mockUser, false).Return(scm.ErrNotAuthorized) mockRenewer.EXPECT().Renew(gomock.Any(), mockUser, false).Return(scm.ErrNotAuthorized)
service := New(nil, mockRenewer) service := New(nil, mockRenewer, "")
_, err := service.List(noContext, mockUser) _, err := service.List(noContext, mockUser)
if err == nil { if err == nil {
t.Errorf("Expect error refreshing token") t.Errorf("Expect error refreshing token")

View File

@ -22,7 +22,7 @@ import (
// convertRepository is a helper function that converts a // convertRepository is a helper function that converts a
// repository from the source code management system to the // repository from the source code management system to the
// local datastructure. // local datastructure.
func convertRepository(src *scm.Repository) *core.Repository { func convertRepository(src *scm.Repository, visibility string) *core.Repository {
return &core.Repository{ return &core.Repository{
UID: src.ID, UID: src.ID,
Namespace: src.Namespace, Namespace: src.Namespace,
@ -32,17 +32,19 @@ func convertRepository(src *scm.Repository) *core.Repository {
SSHURL: src.CloneSSH, SSHURL: src.CloneSSH,
Link: src.Link, Link: src.Link,
Private: src.Private, Private: src.Private,
Visibility: convertVisibility(src), Visibility: convertVisibility(src, visibility),
Branch: src.Branch, Branch: src.Branch,
} }
} }
// convertVisibility is a helper function that returns the // convertVisibility is a helper function that returns the
// repository visibility based on the privacy flag. // repository visibility based on the privacy flag.
func convertVisibility(src *scm.Repository) string { func convertVisibility(src *scm.Repository, visibility string) string {
switch { switch {
case src.Private == true: case src.Private == true:
return core.VisibilityPrivate return core.VisibilityPrivate
case visibility == core.VisibilityInternal:
return core.VisibilityInternal
default: default:
return core.VisibilityPublic return core.VisibilityPublic
} }

View File

@ -36,7 +36,7 @@ func TestConvertRepository(t *testing.T) {
Branch: "master", Branch: "master",
Visibility: core.VisibilityPrivate, Visibility: core.VisibilityPrivate,
} }
got := convertRepository(from) got := convertRepository(from, "")
if diff := cmp.Diff(want, got); len(diff) != 0 { if diff := cmp.Diff(want, got); len(diff) != 0 {
t.Errorf(diff) t.Errorf(diff)
} }
@ -58,8 +58,37 @@ func TestConvertVisibility(t *testing.T) {
} }
for i, test := range tests { for i, test := range tests {
if got, want := convertVisibility(test.r), test.v; got != want { if got, want := convertVisibility(test.r, ""), test.v; got != want {
t.Errorf("Want visibility %s, got %s for index %d", got, want, i) t.Errorf("Want visibility %s, got %s for index %d", got, want, i)
} }
} }
} }
func TestDefinedVisibility(t *testing.T) {
from := &scm.Repository{
ID: "42",
Namespace: "octocat",
Name: "hello-world",
Branch: "master",
Private: false,
Clone: "https://github.com/octocat/hello-world.git",
CloneSSH: "git@github.com:octocat/hello-world.git",
Link: "https://github.com/octocat/hello-world",
}
want := &core.Repository{
UID: "42",
Namespace: "octocat",
Name: "hello-world",
Slug: "octocat/hello-world",
HTTPURL: "https://github.com/octocat/hello-world.git",
SSHURL: "git@github.com:octocat/hello-world.git",
Link: "https://github.com/octocat/hello-world",
Private: false,
Branch: "master",
Visibility: core.VisibilityInternal,
}
got := convertRepository(from, "internal")
if diff := cmp.Diff(want, got); len(diff) != 0 {
t.Errorf(diff)
}
}