Repository: xanzy/go-gitlab
Pull request #953 (2020-10-05 20:28:09)

User memberships
Resolves https://github.com/xanzy/go-gitlab/issues/944

- Added user memberships method
- Added a unit test with some test data

Tried to test this locally but I don't have access to an admin account. It looks like you might need to create a self-hosted gitlab instance, is that right?


Patch:
diff --git a/users.go b/users.go
index 9a220b8..2dbed92 100644
--- a/users.go
+++ b/users.go
@@ -931,3 +931,44 @@ func (s *UsersService) SetUserStatus(opt *UserStatusOptions, options ...RequestO
 
 	return status, resp, err
 }
+
+// UserMembership represents a membership of the user in a namespace or project.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/users.html#user-memberships-admin-only
+type UserMembership struct {
+	SourceID    int    `json:"source_id"`
+	SourceName  string `json:"source_name"`
+	SourceType  string `json:"source_type"`
+	AccessLevel string `json:"access_level"`
+}
+
+// GetUserMembershipOptions represents the options available to query user memberships.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/users.html#user-memberships-admin-only
+type GetUserMembershipOptions struct {
+	ListOptions
+	Type *string `url:"type,omitempty" json:"type,omitempty"`
+}
+
+// GetUserMemberships retrieves a list of the user's memberships.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/users.html#user-memberships-admin-only
+func (s *UsersService) GetUserMemberships(user int, opt *GetUserMembershipOptions, options ...RequestOptionFunc) ([]*UserMembership, *Response, error) {
+	u := fmt.Sprintf("users/%d/memberships", user)
+
+	req, err := s.client.NewRequest("GET", u, opt, options)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	var m []*UserMembership
+	resp, err := s.client.Do(req, &m)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return m, resp, err
+}


Test patch:
diff --git a/testdata/get_user_memberships.json b/testdata/get_user_memberships.json
new file mode 100644
index 0000000..80dfd62
--- /dev/null
+++ b/testdata/get_user_memberships.json
@@ -0,0 +1,14 @@
+[
+  {
+    "source_id": 1,
+    "source_name": "Project one",
+    "source_type": "Project",
+    "access_level": "20"
+  },
+  {
+    "source_id": 3,
+    "source_name": "Group three",
+    "source_type": "Namespace",
+    "access_level": "20"
+  }
+]
diff --git a/users_test.go b/users_test.go
index 25efc44..1472c6d 100644
--- a/users_test.go
+++ b/users_test.go
@@ -4,6 +4,8 @@ import (
 	"fmt"
 	"net/http"
 	"testing"
+
+	"github.com/stretchr/testify/assert"
 )
 
 func TestBlockUser(t *testing.T) {
@@ -234,3 +236,23 @@ func TestActivateUser_UserNotFound(t *testing.T) {
 		t.Errorf("Users.ActivateUser error.\nExpected: %+v\n\tGot: %+v", ErrUserNotFound, err)
 	}
 }
+
+func TestGetMemberships(t *testing.T) {
+	mux, server, client := setup(t)
+	defer teardown(server)
+
+	path := fmt.Sprintf("/%susers/1/memberships", apiVersionPath)
+	mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "GET")
+		mustWriteHTTPResponse(t, w, "testdata/get_user_memberships.json")
+	})
+
+	opt := new(GetUserMembershipOptions)
+
+	memberships, _, err := client.Users.GetUserMemberships(1, opt)
+
+	assert.NoError(t, err)
+
+	want := []*UserMembership{{SourceID: 1, SourceName: "Project one", SourceType: "Project", AccessLevel: "20"}, {SourceID: 3, SourceName: "Group three", SourceType: "Namespace", AccessLevel: "20"}}
+	assert.Equal(t, want, memberships)
+}