Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(github): list all repositories the user has admin permissions (#2153)
Joe Chende9523279f8b
diff --git a/frontend/src/components/RepositorySelectionPanel.vue b/frontend/src/components/RepositorySelectionPanel.vueindex 7471e4a640..10f964b127 100644--- a/frontend/src/components/RepositorySelectionPanel.vue+++ b/frontend/src/components/RepositorySelectionPanel.vue@@ -115,6 +115,8 @@ const repositoryList = computed(() => {const attentionText = computed((): string => {if (props.config.vcs.type == "GITLAB_SELF_HOST") {return "repository.select-repository-attention-gitlab";+ } else if (props.config.vcs.type == "GITHUB_COM") {+ return "repository.select-repository-attention-github";}return "";});diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex 54d3951684..2d4f2876b8 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -892,6 +892,7 @@"choose-git-provider-visit-workspace": "Visit {workspace} setting to add more Git providers.","choose-git-provider-contact-workspace-owner": "Contact your Bytebase owner if you want other Git providers to appear here. Bytebase currently supports self-host GitLab EE/CE, and plan to add GitLab.com, and GitHub Enterprise later.","select-repository-attention-gitlab": "Bytebase only lists GitLab projects granting you at least the 'Maintainer' role, which allows to configure the project webhook to observe the code push event.",+ "select-repository-attention-github": "Bytebase only lists GitHub repositories you have admin permissions, which allows to configure the repository webhook to observe the code push event.","select-repository-search": "Search repository","linked": "Linked repositories","role-provider": "Project role provider",diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex e7a483b294..a54a078d43 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -893,6 +893,7 @@"choose-git-provider-visit-workspace": "访问 {workspace} 设置以添加更多 Git 提供方.","choose-git-provider-contact-workspace-owner": "如果您希望其他 Git 提供方出现在列表中,请联系 Bytebase 实例的所有者。当前 Bytebase 支持自己托管的 GitLab EE/CE,我们之后也会支持 GitLab.com 和 GitHub Enterprise","select-repository-attention-gitlab": "Bytebase 仅列出您至少拥有 'Maintainer' 权限的 GitLab 项目。因为只有至少拥有该权限,才能够配置项目的 webhook 用来监听代码推送事件。",+ "select-repository-attention-github": "Bytebase 仅列出您拥有管理员权限的 GitHub 仓库。因为只有拥有该权限,才能够配置仓库的 webhook 用来监听代码推送事件。","select-repository-search": "搜索仓库","linked": "关联的仓库","role-provider": "项目角色权限提供方",diff --git a/plugin/vcs/github/github.go b/plugin/vcs/github/github.goindex 9ab2813046..b5802d2599 100644--- a/plugin/vcs/github/github.go+++ b/plugin/vcs/github/github.go@@ -85,10 +85,13 @@ type User struct {// Repository represents a GitHub API response for a repository.type Repository struct {- ID int64 `json:"id"`- Name string `json:"name"`- FullName string `json:"full_name"`- HTMLURL string `json:"html_url"`+ ID int64 `json:"id"`+ Name string `json:"name"`+ FullName string `json:"full_name"`+ HTMLURL string `json:"html_url"`+ Permissions struct {+ Admin bool `json:"admin"`+ } `json:"permissions"`}// RepositoryTree represents a GitHub API response for a repository tree.@@ -481,7 +484,11 @@ func (p *Provider) ExchangeOAuthToken(ctx context.Context, instanceURL string, o}// FetchAllRepositoryList fetches all repositories where the authenticated user-// has a owner role, which is required to create webhook in the repository.+// has admin permissions, which is required to create webhook in the repository.+//+// NOTE: GitHub API does not provide a native filter for admin permissions, thus+// we need to first fetch all repositories and then filter down the list using+// the `permissions.admin` field.//// Docs: https://docs.github.com/en/rest/repos/repos#list-repositories-for-the-authenticated-userfunc (p *Provider) FetchAllRepositoryList(ctx context.Context, oauthCtx common.OauthContext, instanceURL string) ([]*vcs.Repository, error) {@@ -502,6 +509,9 @@ func (p *Provider) FetchAllRepositoryList(ctx context.Context, oauthCtx common.Ovar allRepos []*vcs.Repositoryfor _, r := range githubRepos {+ if !r.Permissions.Admin {+ continue+ }allRepos = append(allRepos,&vcs.Repository{ID: r.ID,@@ -515,12 +525,10 @@ func (p *Provider) FetchAllRepositoryList(ctx context.Context, oauthCtx common.O}// fetchPaginatedRepositoryList fetches repositories where the authenticated-// user has a owner role in given page. It return the paginated results along+// user has access to in given page. It returns the paginated results along// with a boolean indicating whether the next page exists.func (p *Provider) fetchPaginatedRepositoryList(ctx context.Context, oauthCtx common.OauthContext, instanceURL string, page int) (repos []Repository, hasNextPage bool, err error) {- // We will use user's token to create webhook in the project, which requires the- // token owner to be at least the project maintainer(40).- url := fmt.Sprintf("%s/user/repos?affiliation=owner&page=%d&per_page=%d", p.APIURL(instanceURL), page, apiPageSize)+ url := fmt.Sprintf("%s/user/repos?page=%d&per_page=%d", p.APIURL(instanceURL), page, apiPageSize)code, body, err := oauth.Get(ctx,p.client,diff --git a/plugin/vcs/github/github_test.go b/plugin/vcs/github/github_test.goindex ecc143f356..b944d2fcfa 100644--- a/plugin/vcs/github/github_test.go+++ b/plugin/vcs/github/github_test.go@@ -445,7 +445,7 @@ func TestProvider_FetchAllRepositoryList(t *testing.T) {assert.Equal(t, "/user/repos", r.URL.Path)return &http.Response{StatusCode: http.StatusOK,- // Example response taken from https://docs.github.com/en/rest/repos/repos#list-repositories-for-the-authenticated-user+ // Example response derived from https://docs.github.com/en/rest/repos/repos#list-repositories-for-the-authenticated-userBody: io.NopCloser(strings.NewReader(`[{@@ -546,8 +546,8 @@ func TestProvider_FetchAllRepositoryList(t *testing.T) {"created_at": "2011-01-26T19:01:12Z","updated_at": "2011-01-26T19:14:43Z","permissions": {- "admin": false,- "push": false,+ "admin": true,+ "push": true,"pull": true},"allow_rebase_merge": true,@@ -570,6 +570,17 @@ func TestProvider_FetchAllRepositoryList(t *testing.T) {"forks": 1,"open_issues": 1,"watchers": 1+ },+ {+ "id": 1296270,+ "name": "Hello-World2",+ "full_name": "octocat/Hello-World2",+ "html_url": "https://github.com/octocat/Hello-World2",+ "permissions": {+ "admin": false,+ "push": false,+ "pull": true+ }}]`)),@@ -584,6 +595,7 @@ func TestProvider_FetchAllRepositoryList(t *testing.T) {got, err := p.FetchAllRepositoryList(ctx, common.OauthContext{}, githubComURL)require.NoError(t, err)+ // Repositories without admin permissions should be excludedwant := []*vcs.Repository{{ID: 1296269,@@ -680,7 +692,7 @@ func TestProvider_FetchRepositoryFileList(t *testing.T) {got, err := p.FetchRepositoryFileList(ctx, common.OauthContext{}, githubComURL, "octocat/Hello-World", "main", "subdir")require.NoError(t, err)- // Non-blob type should excluded+ // Non-blob type should be excludedwant := []*vcs.RepositoryTreeNode{{Path: "subdir/exec_file",