diff --git a/internal/services/gateway/action/org.go b/internal/services/gateway/action/org.go index 7c540ca..ef1a7cc 100644 --- a/internal/services/gateway/action/org.go +++ b/internal/services/gateway/action/org.go @@ -45,6 +45,40 @@ func (h *ActionHandler) GetOrgs(ctx context.Context, req *GetOrgsRequest) ([]*ty return orgs, nil } +type OrgMembersResponse struct { + Organization *types.Organization + Members []*OrgMemberResponse +} + +type OrgMemberResponse struct { + User *types.User + Role types.MemberRole +} + +func (h *ActionHandler) GetOrgMembers(ctx context.Context, orgRef string) (*OrgMembersResponse, error) { + org, resp, err := h.configstoreClient.GetOrg(ctx, orgRef) + if err != nil { + return nil, ErrFromRemote(resp, err) + } + + orgMembers, resp, err := h.configstoreClient.GetOrgMembers(ctx, orgRef) + if err != nil { + return nil, ErrFromRemote(resp, err) + } + + res := &OrgMembersResponse{ + Organization: org, + Members: make([]*OrgMemberResponse, len(orgMembers)), + } + for i, orgMember := range orgMembers { + res.Members[i] = &OrgMemberResponse{ + User: orgMember.User, + Role: orgMember.Role, + } + } + return res, nil +} + type CreateOrgRequest struct { Name string Visibility types.Visibility diff --git a/internal/services/gateway/api/client.go b/internal/services/gateway/api/client.go index 7ca49f3..d05b0f1 100644 --- a/internal/services/gateway/api/client.go +++ b/internal/services/gateway/api/client.go @@ -416,3 +416,9 @@ func (c *Client) AddOrgMember(ctx context.Context, orgRef, userRef string, role func (c *Client) RemoveOrgMember(ctx context.Context, orgRef, userRef string) (*http.Response, error) { return c.getResponse(ctx, "DELETE", fmt.Sprintf("/orgs/%s/members/%s", orgRef, userRef), nil, jsonContent, nil) } + +func (c *Client) GetOrgMembers(ctx context.Context, orgRef string) (*OrgMembersResponse, *http.Response, error) { + res := &OrgMembersResponse{} + resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/orgs/%s/members", orgRef), nil, jsonContent, nil, &res) + return res, resp, err +} diff --git a/internal/services/gateway/api/org.go b/internal/services/gateway/api/org.go index 757df45..7494e2c 100644 --- a/internal/services/gateway/api/org.go +++ b/internal/services/gateway/api/org.go @@ -217,6 +217,39 @@ func createOrgMemberResponse(user *types.User, role types.MemberRole) *OrgMember } } +type OrgMembersHandler struct { + log *zap.SugaredLogger + ah *action.ActionHandler +} + +func NewOrgMembersHandler(logger *zap.Logger, ah *action.ActionHandler) *OrgMembersHandler { + return &OrgMembersHandler{log: logger.Sugar(), ah: ah} +} + +func (h *OrgMembersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + vars := mux.Vars(r) + orgRef := vars["orgref"] + + ares, err := h.ah.GetOrgMembers(ctx, orgRef) + if httpError(w, err) { + h.log.Errorf("err: %+v", err) + return + } + + res := &OrgMembersResponse{ + Organization: createOrgResponse(ares.Organization), + Members: make([]*OrgMemberResponse, len(ares.Members)), + } + for i, m := range ares.Members { + res.Members[i] = createOrgMemberResponse(m.User, m.Role) + } + if err := httpResponse(w, http.StatusOK, res); err != nil { + h.log.Errorf("err: %+v", err) + } +} + type AddOrgMemberResponse struct { Organization *OrgResponse `json:"organization"` OrgMemberResponse diff --git a/internal/services/gateway/api/user.go b/internal/services/gateway/api/user.go index 4b12a13..e9c7c3b 100644 --- a/internal/services/gateway/api/user.go +++ b/internal/services/gateway/api/user.go @@ -21,14 +21,14 @@ import ( "sort" "strconv" - "github.com/pkg/errors" gitsource "github.com/sorintlab/agola/internal/gitsources" "github.com/sorintlab/agola/internal/services/gateway/action" "github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/util" - "go.uber.org/zap" "github.com/gorilla/mux" + "github.com/pkg/errors" + "go.uber.org/zap" ) type CreateUserRequest struct { diff --git a/internal/services/gateway/gateway.go b/internal/services/gateway/gateway.go index eccaa3a..40c5154 100644 --- a/internal/services/gateway/gateway.go +++ b/internal/services/gateway/gateway.go @@ -191,6 +191,7 @@ func (g *Gateway) Run(ctx context.Context) error { createOrgHandler := api.NewCreateOrgHandler(logger, g.ah) deleteOrgHandler := api.NewDeleteOrgHandler(logger, g.ah) + orgMembersHandler := api.NewOrgMembersHandler(logger, g.ah) addOrgMemberHandler := api.NewAddOrgMemberHandler(logger, g.ah) removeOrgMemberHandler := api.NewRemoveOrgMemberHandler(logger, g.ah) @@ -272,6 +273,7 @@ func (g *Gateway) Run(ctx context.Context) error { apirouter.Handle("/orgs", authForcedHandler(orgsHandler)).Methods("GET") apirouter.Handle("/orgs", authForcedHandler(createOrgHandler)).Methods("POST") apirouter.Handle("/orgs/{orgref}", authForcedHandler(deleteOrgHandler)).Methods("DELETE") + apirouter.Handle("/orgs/{orgref}/members", authForcedHandler(orgMembersHandler)).Methods("GET") apirouter.Handle("/orgs/{orgref}/members/{userref}", authForcedHandler(addOrgMemberHandler)).Methods("PUT") apirouter.Handle("/orgs/{orgref}/members/{userref}", authForcedHandler(removeOrgMemberHandler)).Methods("DELETE")