Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(gitlab): support pagination for `FetchRepositoryActiveMemberList` (#1423)

Joe Chen3221d9babb48
diff --git a/plugin/vcs/gitlab/gitlab.go b/plugin/vcs/gitlab/gitlab.go
index 1f4b96fdfe..2f0cacf71e 100644
--- a/plugin/vcs/gitlab/gitlab.go
+++ b/plugin/vcs/gitlab/gitlab.go
@@ -145,8 +145,8 @@ const (
ProjectRoleNoAccess ProjectRole = "NoAccess"
)
-// gitLabRepositoryMember is the API message for repository member
-type gitLabRepositoryMember struct {
+// RepositoryMember represents a GitLab API response for a repository member.
+type RepositoryMember struct {
ID int `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
@@ -447,10 +447,79 @@ func getRoleAndMappedRole(accessLevel int32) (gitLabRole ProjectRole, bytebaseRo
return "", ""
}
-// FetchRepositoryActiveMemberList fetch all active members of a repository
+// FetchRepositoryActiveMemberList fetches all active members of a repository.
+//
+// Docs: https://docs.gitlab.com/ee/api/members.html#list-all-members-of-a-group-or-project
func (p *Provider) FetchRepositoryActiveMemberList(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID string) ([]*vcs.RepositoryMember, error) {
- // Official API doc: https://docs.gitlab.com/14.6/ee/api/members.html
- url := fmt.Sprintf("%s/projects/%s/members/all", p.APIURL(instanceURL), repositoryID)
+ var allMembers []RepositoryMember
+ page := 1
+ for {
+ members, hasNextPage, err := p.fetchPaginatedRepositoryActiveMemberList(ctx, oauthCtx, instanceURL, repositoryID, page)
+ if err != nil {
+ return nil, errors.Wrap(err, "fetch paginated list")
+ }
+ allMembers = append(allMembers, members...)
+
+ if !hasNextPage {
+ break
+ }
+ page++
+ }
+
+ var emptyEmailUserIDList []string
+ var activeMembers []*vcs.RepositoryMember
+ for _, m := range allMembers {
+ // We only want active member (both state and membership_state is active)
+ if m.State != vcs.StateActive {
+ continue
+ }
+
+ // The email field is only returned if the caller credential is associated with
+ // a GitLab admin account. We'll try getting the email by fetching the info of
+ // individual users.
+ if m.Email == "" {
+ // TODO: need to work around this if the user does not set public email. For now, we just return an error listing users not having public emails.
+ // TODO: if the number of the member is too large, fetching sequentially may cause performance issue
+ userInfo, err := p.FetchUserInfo(ctx, oauthCtx, instanceURL, strconv.Itoa(m.ID))
+ if err != nil {
+ return nil, errors.Wrapf(err, "fetch user info, id: %d", m.ID)
+ }
+ m.Email = userInfo.PublicEmail
+ }
+
+ if m.Email == "" {
+ emptyEmailUserIDList = append(emptyEmailUserIDList, m.Name)
+ continue
+ }
+
+ gitlabRole, bytebaseRole := getRoleAndMappedRole(m.AccessLevel)
+ activeMembers = append(
+ activeMembers,
+ &vcs.RepositoryMember{
+ Name: m.Name,
+ Email: m.Email,
+ Role: bytebaseRole,
+ VCSRole: string(gitlabRole),
+ State: vcs.StateActive,
+ RoleProvider: vcs.GitLabSelfHost,
+ },
+ )
+ }
+
+ 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, ", "))
+ }
+
+ return activeMembers, nil
+}
+
+// fetchPaginatedRepositoryActiveMemberList fetches active members of a
+// repository in given page. It return the paginated results along with a
+// boolean indicating whether the next page exists.
+func (p *Provider) fetchPaginatedRepositoryActiveMemberList(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID string, page int) (members []RepositoryMember, hasNextPage bool, err error) {
+ // The "state" filter only available in GitLab Premium self-managed, GitLab
+ // Premium SaaS, and higher tiers, but worth a try for less abandoned results.
+ url := fmt.Sprintf("%s/projects/%s/members/all?state=active&page=%d&per_page=100", p.APIURL(instanceURL), repositoryID, page)
code, body, err := oauth.Get(
ctx,
p.client,
@@ -467,58 +536,29 @@ func (p *Provider) FetchRepositoryActiveMemberList(ctx context.Context, oauthCtx
),
)
if err != nil {
- return nil, errors.Wrap(err, "GET")
+ return nil, false, errors.Wrapf(err, "GET %s", url)
}
if code == http.StatusNotFound {
- return nil, common.Errorf(common.NotFound, fmt.Errorf("failed to fetch repository members from GitLab instance %s", instanceURL))
+ return nil, false, common.Errorf(common.NotFound, fmt.Errorf("failed to fetch repository members from URL %s", url))
} else if code >= 300 {
- return nil, fmt.Errorf("failed to read repository members from GitLab instance %s, status code: %d",
- instanceURL,
- code,
- )
- }
-
- var gitLabrepositoryMember []gitLabRepositoryMember
- if err := json.Unmarshal([]byte(body), &gitLabrepositoryMember); err != nil {
- return nil, err
- }
-
- // we only return active member (both state and membership_state is active)
- var emptyEmailUserIDList []string
- var activeRepositoryMemberList []*vcs.RepositoryMember
- for _, gitLabMember := range gitLabrepositoryMember {
- if gitLabMember.State == vcs.StateActive {
- // The email field will only be returned if the caller credential is associated with a GitLab admin account.
- // And since most callers are not GitLab admins, thus we fetch public email
- // TODO: need to work around this if the user does not set public email. For now, we just return an error listing users not having public emails.
- // TODO: if the number of the member is too large, fetching sequentially may cause performance issue
- userInfo, err := p.FetchUserInfo(ctx, oauthCtx, instanceURL, strconv.Itoa(gitLabMember.ID))
- if err != nil {
- return nil, err
- }
- if userInfo.PublicEmail == "" {
- emptyEmailUserIDList = append(emptyEmailUserIDList, gitLabMember.Name)
- }
-
- gitLabRole, bytebaseRole := getRoleAndMappedRole(gitLabMember.AccessLevel)
- repositoryMember := &vcs.RepositoryMember{
- Name: gitLabMember.Name,
- Email: userInfo.PublicEmail,
- Role: bytebaseRole,
- VCSRole: string(gitLabRole),
- State: vcs.StateActive,
- RoleProvider: vcs.GitLabSelfHost,
- }
- activeRepositoryMemberList = append(activeRepositoryMemberList, repositoryMember)
- }
+ return nil, false,
+ fmt.Errorf("failed to read repository members from URL %s, status code: %d, body: %s",
+ url,
+ code,
+ body,
+ )
}
- 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 err := json.Unmarshal([]byte(body), &members); err != nil {
+ return nil, false, errors.Wrap(err, "unmarshal body")
}
- return activeRepositoryMemberList, nil
+ // NOTE: We deliberately choose to not use the Link header for checking the next
+ // page to avoid introducing a new dependency, see
+ // https://github.com/bytebase/bytebase/pull/1423#discussion_r884278534 for the
+ // discussion.
+ return members, len(members) >= 100, nil
}
// FetchRepositoryFileList fetch the files from repository tree
diff --git a/plugin/vcs/gitlab/gitlab_test.go b/plugin/vcs/gitlab/gitlab_test.go
index d97cd7e330..edc870f561 100644
--- a/plugin/vcs/gitlab/gitlab_test.go
+++ b/plugin/vcs/gitlab/gitlab_test.go
@@ -7,6 +7,7 @@ import (
"strings"
"testing"
+ "github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -67,6 +68,123 @@ func TestProvider_FetchUserInfo(t *testing.T) {
assert.Equal(t, want, got)
}
+func TestProvider_FetchRepositoryActiveMemberList(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"
+ },
+ {
+ "id": 2,
+ "username": "john_doe",
+ "name": "John Doe",
+ "state": "archived",
+ "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": 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"
+ },
+ "expires_at": "2012-10-22T14:13:35Z",
+ "access_level": 30,
+ "email": "[email protected]",
+ "group_saml_identity": {
+ "extern_uid":"ABC-1234567890",
+ "provider": "group_saml",
+ "saml_provider_id": 10
+ },
+ "membership_state": "active"
+ }
+]
+`)),
+ }, nil
+ case "/api/v4/users/1":
+ return &http.Response{
+ StatusCode: http.StatusOK,
+ // Example response taken 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": "[email protected]",
+ "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, err := p.FetchRepositoryActiveMemberList(ctx, common.OauthContext{}, "", "1")
+ require.NoError(t, err)
+
+ // Non-active member should be excluded
+ want := []*vcs.RepositoryMember{
+ {
+ Email: "[email protected]",
+ Name: "Raymond Smith",
+ State: vcs.StateActive,
+ Role: common.ProjectDeveloper,
+ VCSRole: string(ProjectRoleDeveloper),
+ RoleProvider: vcs.GitLabSelfHost,
+ },
+ }
+ assert.Equal(t, want, got)
+}
+
func TestProvider_FetchCommitByID(t *testing.T) {
p := newProvider(
vcs.ProviderConfig{