|
8 | 8 | "context" |
9 | 9 | "fmt" |
10 | 10 | "net/url" |
| 11 | + "strings" |
11 | 12 | "time" |
12 | 13 |
|
13 | 14 | "github.com/drone/go-scm/scm" |
@@ -87,34 +88,108 @@ func (s *repositoryService) FindHook(ctx context.Context, repo string, id string |
87 | 88 |
|
88 | 89 | // FindPerms returns the repository permissions. |
89 | 90 | func (s *repositoryService) FindPerms(ctx context.Context, repo string) (*scm.Perm, *scm.Response, error) { |
90 | | - path := fmt.Sprintf("2.0/user/permissions/repositories?q=repository.full_name=%q", repo) |
91 | | - out := new(perms) |
92 | | - res, err := s.client.do(ctx, "GET", path, nil, out) |
93 | | - return convertPerms(out), res, err |
| 91 | + // First, try to fetch using the repo identifier provided |
| 92 | + perm, res, err := s.findPermsWithIdentifier(ctx, repo) |
| 93 | + if err == nil { |
| 94 | + return perm, res, nil |
| 95 | + } |
| 96 | + |
| 97 | + // If that fails and repo isn't in "workspace/repo" format, |
| 98 | + // search all workspaces as a fallback |
| 99 | + if !strings.Contains(repo, "/") { |
| 100 | + return s.findPermsAcrossWorkspaces(ctx, repo) |
| 101 | + } |
| 102 | + |
| 103 | + // Repo is in "workspace/repo" format and fetch failed |
| 104 | + return nil, res, err |
94 | 105 | } |
95 | 106 |
|
96 | | -// List returns the user repository list. |
| 107 | +// findPermsWithIdentifier attempts to fetch permissions using either the |
| 108 | +// extracted workspace from the client URL or the workspace in "workspace/repo" format. |
| 109 | +func (s *repositoryService) findPermsWithIdentifier(ctx context.Context, repo string) (*scm.Perm, *scm.Response, error) { |
| 110 | + workspace := s.client.extractWorkspaceFromURL() |
| 111 | + repoSlug := repo |
| 112 | + |
| 113 | + // If workspace is available from URL, use it and only extract repo slug |
| 114 | + if workspace != "" { |
| 115 | + if strings.Contains(repo, "/") { |
| 116 | + _, repoSlug = scm.Split(repo) |
| 117 | + } |
| 118 | + return s.fetchRepoPerms(ctx, workspace, repoSlug) |
| 119 | + } |
| 120 | + |
| 121 | + // If repo is in "workspace/repo" format, use the workspace from repo identifier |
| 122 | + if strings.Contains(repo, "/") { |
| 123 | + workspace, repoSlug = scm.Split(repo) |
| 124 | + return s.fetchRepoPerms(ctx, workspace, repoSlug) |
| 125 | + } |
| 126 | + |
| 127 | + // No workspace available |
| 128 | + return nil, nil, fmt.Errorf("unable to determine workspace for repository %s", repo) |
| 129 | +} |
| 130 | + |
| 131 | +// findPermsAcrossWorkspaces searches all user workspaces for the repository |
| 132 | +// and returns permissions if the user has access. |
| 133 | +func (s *repositoryService) findPermsAcrossWorkspaces(ctx context.Context, repoSlug string) (*scm.Perm, *scm.Response, error) { |
| 134 | + workspaces, err := s.client.fetchAllWorkspaces(ctx) |
| 135 | + if err != nil { |
| 136 | + return nil, nil, err |
| 137 | + } |
| 138 | + |
| 139 | + for _, workspace := range workspaces { |
| 140 | + perm, res, err := s.fetchRepoPerms(ctx, workspace, repoSlug) |
| 141 | + if err != nil { |
| 142 | + // If it's a 404, the repo doesn't exist in this workspace |
| 143 | + if res != nil && res.Status == 404 { |
| 144 | + continue |
| 145 | + } |
| 146 | + // For other errors (network, auth, etc.), return immediately |
| 147 | + return nil, res, err |
| 148 | + } |
| 149 | + |
| 150 | + // Return permissions if user has any access to the repository |
| 151 | + if perm.Pull || perm.Push || perm.Admin { |
| 152 | + return perm, res, nil |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return nil, nil, fmt.Errorf("repository %s not found in any workspace", repoSlug) |
| 157 | +} |
| 158 | + |
| 159 | +// List returns the user repository list using workspace-aware pagination. |
97 | 160 | func (s *repositoryService) List(ctx context.Context, opts scm.ListOptions) ([]*scm.Repository, *scm.Response, error) { |
98 | | - path := fmt.Sprintf("2.0/repositories?%s", encodeListRoleOptions(opts)) |
99 | 161 | if opts.URL != "" { |
100 | | - path = opts.URL |
| 162 | + out := new(repositories) |
| 163 | + res, err := s.client.do(ctx, "GET", opts.URL, nil, &out) |
| 164 | + if err != nil { |
| 165 | + return nil, res, err |
| 166 | + } |
| 167 | + copyPagination(out.pagination, res) |
| 168 | + return convertRepositoryList(out), res, err |
101 | 169 | } |
102 | | - out := new(repositories) |
103 | | - res, err := s.client.do(ctx, "GET", path, nil, &out) |
104 | | - copyPagination(out.pagination, res) |
105 | | - return convertRepositoryList(out), res, err |
| 170 | + |
| 171 | + if opts.Page == 0 { |
| 172 | + opts.Page = 1 |
| 173 | + } |
| 174 | + return s.client.fetchReposWithPagination(ctx, encodeListRoleOptions(opts), opts.Page, opts.Size) |
106 | 175 | } |
107 | 176 |
|
108 | 177 | // ListV2 returns the user repository list based on the searchTerm passed. |
109 | 178 | func (s *repositoryService) ListV2(ctx context.Context, opts scm.RepoListOptions) ([]*scm.Repository, *scm.Response, error) { |
110 | | - path := fmt.Sprintf("2.0/repositories?%s", encodeRepoListOptions(opts)) |
111 | 179 | if opts.ListOptions.URL != "" { |
112 | | - path = opts.ListOptions.URL |
| 180 | + out := new(repositories) |
| 181 | + res, err := s.client.do(ctx, "GET", opts.ListOptions.URL, nil, &out) |
| 182 | + if err != nil { |
| 183 | + return nil, res, err |
| 184 | + } |
| 185 | + copyPagination(out.pagination, res) |
| 186 | + return convertRepositoryList(out), res, err |
113 | 187 | } |
114 | | - out := new(repositories) |
115 | | - res, err := s.client.do(ctx, "GET", path, nil, &out) |
116 | | - copyPagination(out.pagination, res) |
117 | | - return convertRepositoryList(out), res, err |
| 188 | + |
| 189 | + if opts.ListOptions.Page == 0 { |
| 190 | + opts.ListOptions.Page = 1 |
| 191 | + } |
| 192 | + return s.client.fetchReposWithPagination(ctx, encodeRepoListOptions(opts), opts.ListOptions.Page, opts.ListOptions.Size) |
118 | 193 | } |
119 | 194 |
|
120 | 195 | func (s *repositoryService) ListNamespace(ctx context.Context, namespace string, opts scm.ListOptions) ([]*scm.Repository, *scm.Response, error) { |
@@ -391,3 +466,43 @@ func convertFromState(from scm.State) string { |
391 | 466 | return "FAILED" |
392 | 467 | } |
393 | 468 | } |
| 469 | + |
| 470 | +// workspaceRepoPerms represents the response from |
| 471 | +// GET /2.0/workspaces/{workspace}/permissions/repositories/{repo_slug} |
| 472 | +type workspaceRepoPerms struct { |
| 473 | + Values []*workspaceRepoPerm `json:"values"` |
| 474 | +} |
| 475 | + |
| 476 | +type workspaceRepoPerm struct { |
| 477 | + Permission string `json:"permission"` // "admin", "write", "read" |
| 478 | +} |
| 479 | + |
| 480 | +func convertWorkspaceRepoPerms(from *workspaceRepoPerms) *scm.Perm { |
| 481 | + to := new(scm.Perm) |
| 482 | + if len(from.Values) == 0 { |
| 483 | + return to |
| 484 | + } |
| 485 | + switch from.Values[0].Permission { |
| 486 | + case "admin": |
| 487 | + to.Pull = true |
| 488 | + to.Push = true |
| 489 | + to.Admin = true |
| 490 | + case "write": |
| 491 | + to.Pull = true |
| 492 | + to.Push = true |
| 493 | + default: |
| 494 | + to.Pull = true |
| 495 | + } |
| 496 | + return to |
| 497 | +} |
| 498 | + |
| 499 | +// fetchRepoPerms fetches repository permissions for a given workspace and repo slug. |
| 500 | +func (s *repositoryService) fetchRepoPerms(ctx context.Context, workspace, repoSlug string) (*scm.Perm, *scm.Response, error) { |
| 501 | + path := fmt.Sprintf("2.0/workspaces/%s/permissions/repositories/%s", workspace, repoSlug) |
| 502 | + out := new(workspaceRepoPerms) |
| 503 | + res, err := s.client.do(ctx, "GET", path, nil, out) |
| 504 | + if err != nil { |
| 505 | + return nil, res, err |
| 506 | + } |
| 507 | + return convertWorkspaceRepoPerms(out), res, nil |
| 508 | +} |
0 commit comments