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): return an error when member is missing public email (#1825)
Joe Chenff1e12b856b6
diff --git a/plugin/vcs/github/github.go b/plugin/vcs/github/github.goindex a2ea1f49be..d2a9cac238 100644--- a/plugin/vcs/github/github.go+++ b/plugin/vcs/github/github.go@@ -209,6 +209,7 @@ func (p *Provider) FetchRepositoryActiveMemberList(ctx context.Context, oauthCtxpage++}+ var emptyEmailUserList []stringvar allMembers []*vcs.RepositoryMemberfor _, c := range allCollaborators {userInfo, err := p.FetchUserInfo(ctx, oauthCtx, "", c.Login)@@ -216,6 +217,11 @@ func (p *Provider) FetchRepositoryActiveMemberList(ctx context.Context, oauthCtxreturn nil, errors.Wrapf(err, "fetch user info, login: %s", c.Login)}+ if userInfo.PublicEmail == "" {+ emptyEmailUserList = append(emptyEmailUserList, userInfo.Name)+ continue+ }+githubRole, bytebaseRole := getRoleAndMappedRole(c.RoleName)allMembers = append(allMembers,&vcs.RepositoryMember{@@ -228,6 +234,11 @@ func (p *Provider) FetchRepositoryActiveMemberList(ctx context.Context, oauthCtx},)}++ if len(emptyEmailUserList) != 0 {+ return nil, fmt.Errorf("[ %v ] did not configure their public email in GitHub, please make sure every members' public email is configured before syncing, see https://docs.github.com/en/account-and-profile", strings.Join(emptyEmailUserList, ", "))+ }+return allMembers, nil}diff --git a/plugin/vcs/github/github_test.go b/plugin/vcs/github/github_test.goindex 7b2b024f43..2f6ffd3aef 100644--- a/plugin/vcs/github/github_test.go+++ b/plugin/vcs/github/github_test.go@@ -93,6 +93,117 @@ func TestProvider_FetchUserInfo(t *testing.T) {}func TestProvider_FetchRepositoryActiveMemberList(t *testing.T) {+ t.Run("missing public email", func(t *testing.T) {+ p := newProvider(+ vcs.ProviderConfig{+ Client: &http.Client{+ Transport: &common.MockRoundTripper{+ MockRoundTrip: func(r *http.Request) (*http.Response, error) {+ switch r.URL.Path {+ case "/repos/octocat/Hello-World/collaborators":+ return &http.Response{+ StatusCode: http.StatusOK,+ // Example response taken from https://docs.github.com/en/rest/collaborators/collaborators#list-repository-collaborators+ Body: io.NopCloser(strings.NewReader(`+[+ {+ "login": "octocat",+ "id": 1,+ "node_id": "MDQ6VXNlcjE=",+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",+ "gravatar_id": "",+ "url": "https://api.github.com/users/octocat",+ "html_url": "https://github.com/octocat",+ "followers_url": "https://api.github.com/users/octocat/followers",+ "following_url": "https://api.github.com/users/octocat/following{/other_user}",+ "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",+ "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",+ "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",+ "organizations_url": "https://api.github.com/users/octocat/orgs",+ "repos_url": "https://api.github.com/users/octocat/repos",+ "events_url": "https://api.github.com/users/octocat/events{/privacy}",+ "received_events_url": "https://api.github.com/users/octocat/received_events",+ "type": "User",+ "site_admin": false,+ "permissions": {+ "pull": true,+ "triage": true,+ "push": true,+ "maintain": false,+ "admin": false+ },+ "role_name": "write"+ }+]+`)),+ }, nil+ case "/users/octocat":+ return &http.Response{+ StatusCode: http.StatusOK,+ // Example response derived from https://docs.github.com/en/rest/reference/users#get-a-user+ Body: io.NopCloser(strings.NewReader(`+{+ "login": "octocat",+ "id": 1,+ "node_id": "MDQ6VXNlcjE=",+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",+ "gravatar_id": "",+ "url": "https://api.github.com/users/octocat",+ "html_url": "https://github.com/octocat",+ "followers_url": "https://api.github.com/users/octocat/followers",+ "following_url": "https://api.github.com/users/octocat/following{/other_user}",+ "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",+ "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",+ "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",+ "organizations_url": "https://api.github.com/users/octocat/orgs",+ "repos_url": "https://api.github.com/users/octocat/repos",+ "events_url": "https://api.github.com/users/octocat/events{/privacy}",+ "received_events_url": "https://api.github.com/users/octocat/received_events",+ "type": "User",+ "site_admin": false,+ "name": "monalisa octocat",+ "company": "GitHub",+ "blog": "https://github.com/blog",+ "location": "San Francisco",+ "email": "",+ "hireable": false,+ "bio": "There once was...",+ "twitter_username": "monatheoctocat",+ "public_repos": 2,+ "public_gists": 1,+ "followers": 20,+ "following": 0,+ "created_at": "2008-01-14T04:33:35Z",+ "updated_at": "2008-01-14T04:33:35Z",+ "private_gists": 81,+ "total_private_repos": 100,+ "owned_private_repos": 100,+ "disk_usage": 10000,+ "collaborators": 8,+ "two_factor_authentication": true,+ "plan": {+ "name": "Medium",+ "space": 400,+ "private_repos": 20,+ "collaborators": 0+ }+}+`)),+ }, nil+ }+ return nil, errors.Errorf("unexpected request path: %s", r.URL.Path)+ },+ },+ },+ },+ )++ ctx := context.Background()+ _, got := p.FetchRepositoryActiveMemberList(ctx, common.OauthContext{}, "", "octocat/Hello-World")+ want := "[ monalisa octocat ] did not configure their public email in GitHub, please make sure every members' public email is configured before syncing, see https://docs.github.com/en/account-and-profile"+ assert.EqualError(t, got, want)+ })+p := newProvider(vcs.ProviderConfig{Client: &http.Client{diff --git a/plugin/vcs/gitlab/gitlab.go b/plugin/vcs/gitlab/gitlab.goindex f8b46addc0..1dd16f758e 100644--- a/plugin/vcs/gitlab/gitlab.go+++ b/plugin/vcs/gitlab/gitlab.go@@ -466,7 +466,7 @@ func (p *Provider) FetchRepositoryActiveMemberList(ctx context.Context, oauthCtxpage++}- var emptyEmailUserIDList []string+ var emptyEmailUserList []stringvar activeMembers []*vcs.RepositoryMemberfor _, m := range allMembers {// We only want active member (both state and membership_state is active)@@ -488,7 +488,7 @@ func (p *Provider) FetchRepositoryActiveMemberList(ctx context.Context, oauthCtx}if m.Email == "" {- emptyEmailUserIDList = append(emptyEmailUserIDList, m.Name)+ emptyEmailUserList = append(emptyEmailUserList, m.Name)continue}@@ -506,8 +506,8 @@ func (p *Provider) FetchRepositoryActiveMemberList(ctx context.Context, oauthCtx)}- if len(emptyEmailUserIDList) != 0 {- return nil, fmt.Errorf("[ %v ] did not configure their public email in GitLab, please make sure every members' public email is configured before syncing, see https://docs.gitlab.com/ee/user/profile", strings.Join(emptyEmailUserIDList, ", "))+ if len(emptyEmailUserList) != 0 {+ return nil, fmt.Errorf("[ %v ] did not configure their public email in GitLab, please make sure every members' public email is configured before syncing, see https://docs.gitlab.com/ee/user/profile", strings.Join(emptyEmailUserList, ", "))}return activeMembers, nildiff --git a/plugin/vcs/gitlab/gitlab_test.go b/plugin/vcs/gitlab/gitlab_test.goindex edc870f561..d410e53d8f 100644--- a/plugin/vcs/gitlab/gitlab_test.go+++ b/plugin/vcs/gitlab/gitlab_test.go@@ -69,6 +69,85 @@ func TestProvider_FetchUserInfo(t *testing.T) {}func TestProvider_FetchRepositoryActiveMemberList(t *testing.T) {+ t.Run("missing public email", func(t *testing.T) {+ p := newProvider(+ vcs.ProviderConfig{+ Client: &http.Client{+ Transport: &common.MockRoundTripper{+ MockRoundTrip: func(r *http.Request) (*http.Response, error) {+ switch r.URL.Path {+ case "/api/v4/projects/1/members/all":+ return &http.Response{+ StatusCode: http.StatusOK,+ // Example response derived from https://docs.gitlab.com/ee/api/members.html#list-all-members-of-a-group-or-project-including-inherited-and-invited-members+ Body: io.NopCloser(strings.NewReader(`+[+ {+ "id": 1,+ "username": "raymond_smith",+ "name": "Raymond Smith",+ "state": "active",+ "avatar_url": "https://www.gravatar.com/avatar/c2525a7f58ae3776070e44c106c48e15?s=80&d=identicon",+ "web_url": "http://192.168.1.8:3000/root",+ "created_at": "2012-09-22T14:13:35Z",+ "created_by": {+ "id": 2,+ "username": "john_doe",+ "name": "John Doe",+ "state": "active",+ "avatar_url": "https://www.gravatar.com/avatar/c2525a7f58ae3776070e44c106c48e15?s=80&d=identicon",+ "web_url": "http://192.168.1.8:3000/root"+ },+ "expires_at": "2012-10-22T14:13:35Z",+ "access_level": 30,+ "group_saml_identity": null,+ "membership_state": "active"+ }+]+`)),+ }, nil+ case "/api/v4/users/1":+ return &http.Response{+ StatusCode: http.StatusOK,+ // Example response derived from https://docs.gitlab.com/ee/api/users.html#single-user+ Body: io.NopCloser(strings.NewReader(`+{+ "id": 1,+ "username": "john_smith",+ "name": "John Smith",+ "state": "active",+ "avatar_url": "http://localhost:3000/uploads/user/avatar/1/cd8.jpeg",+ "web_url": "http://localhost:3000/john_smith",+ "created_at": "2012-05-23T08:00:58Z",+ "bio": "",+ "bot": false,+ "location": null,+ "public_email": "",+ "skype": "",+ "linkedin": "",+ "twitter": "",+ "website_url": "",+ "organization": "",+ "job_title": "Operations Specialist",+ "followers": 1,+ "following": 1+}+`)),+ }, nil+ }+ return nil, errors.Errorf("unexpected request path: %s", r.URL.Path)+ },+ },+ },+ },+ )++ ctx := context.Background()+ _, got := p.FetchRepositoryActiveMemberList(ctx, common.OauthContext{}, "", "1")+ want := "[ Raymond Smith ] did not configure their public email in GitLab, please make sure every members' public email is configured before syncing, see https://docs.gitlab.com/ee/user/profile"+ assert.EqualError(t, got, want)+ })+p := newProvider(vcs.ProviderConfig{Client: &http.Client{