github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/modules/scm/repo.go (about) 1 package scm 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/covergates/covergates/config" 8 "github.com/covergates/covergates/core" 9 "github.com/drone/go-scm/scm" 10 "github.com/rs/xid" 11 ) 12 13 // RepoService provides repository operations with SCM 14 type repoService struct { 15 config *config.Config 16 client *scm.Client 17 scm core.SCMProvider 18 } 19 20 // NewReportID for upload report 21 func (service *repoService) NewReportID(repo *core.Repo) string { 22 guid := xid.New() 23 return guid.String() 24 } 25 26 // List repositories from SCM 27 func (service *repoService) List( 28 ctx context.Context, 29 user *core.User, 30 ) ([]*core.Repo, error) { 31 client := service.client 32 ctx = withUser(ctx, service.scm, user) 33 results := make([]*scm.Repository, 0) 34 for i := 1; i < 5; i++ { 35 repos, _, err := client.Repositories.List(ctx, scm.ListOptions{Size: 50, Page: i}) 36 if err != nil { 37 return nil, err 38 } 39 if len(repos) <= 0 { 40 break 41 } 42 results = append(results, repos...) 43 } 44 repositories := make([]*core.Repo, len(results)) 45 for i, r := range results { 46 repositories[i] = &core.Repo{ 47 NameSpace: r.Namespace, 48 Name: r.Name, 49 URL: r.Link, 50 SCM: service.scm, 51 Branch: r.Branch, 52 } 53 } 54 return repositories, nil 55 } 56 57 // Find repository by it's name (namespace/name) 58 func (service *repoService) Find( 59 ctx context.Context, 60 user *core.User, 61 name string, 62 ) (*core.Repo, error) { 63 client := service.client 64 ctx = withUser(ctx, service.scm, user) 65 repo, _, err := client.Repositories.Find(ctx, name) 66 if err != nil { 67 return nil, err 68 } 69 return &core.Repo{ 70 Name: repo.Name, 71 NameSpace: repo.Namespace, 72 SCM: core.SCMProvider(service.scm), 73 URL: repo.Link, 74 Branch: repo.Branch, 75 Private: repo.Private, 76 }, nil 77 } 78 79 func (service *repoService) CloneURL( 80 ctx context.Context, 81 user *core.User, 82 name string, 83 ) (string, error) { 84 client := service.client 85 ctx = withUser(ctx, service.scm, user) 86 repo, _, err := client.Repositories.Find(ctx, name) 87 if err != nil { 88 return "", err 89 } 90 return repo.Clone, nil 91 } 92 93 func (service *repoService) CreateHook(ctx context.Context, user *core.User, name string) (*core.Hook, error) { 94 target := fmt.Sprintf( 95 "%s/api/v1/repos/%s/%s/hook", 96 service.config.Server.URL(), 97 string(service.scm), 98 name, 99 ) 100 input := &scm.HookInput{ 101 Name: "covergates", 102 Target: target, 103 Secret: service.config.Server.Secret, 104 SkipVerify: service.config.Server.SkipVerity, 105 Events: scm.HookEvents{ 106 Push: true, 107 PullRequest: true, 108 }, 109 } 110 ctx = withUser(ctx, service.scm, user) 111 hook, _, err := service.client.Repositories.CreateHook(ctx, name, input) 112 if err != nil { 113 return nil, err 114 } 115 return &core.Hook{ 116 ID: hook.ID, 117 }, nil 118 } 119 120 func (service *repoService) RemoveHook(ctx context.Context, user *core.User, name string, hook *core.Hook) error { 121 ctx = withUser(ctx, service.scm, user) 122 _, err := service.client.Repositories.DeleteHook(ctx, name, hook.ID) 123 return err 124 } 125 126 func (service *repoService) IsAdmin(ctx context.Context, user *core.User, name string) bool { 127 ctx = withUser(ctx, service.scm, user) 128 perm, _, err := service.client.Repositories.FindPerms(ctx, name) 129 if err != nil { 130 return false 131 } 132 return perm.Admin 133 }