diff --git a/orgs.go b/orgs.go index a128e4c..aaf1e74 100644 --- a/orgs.go +++ b/orgs.go @@ -65,6 +65,19 @@ func (s *OrgService) RemoveMember(ctx context.Context, org, username string) err return s.client.Delete(ctx, path) } +// IsMember reports whether a user is a member of an organisation. +func (s *OrgService) IsMember(ctx context.Context, org, username string) (bool, error) { + path := ResolvePath("/api/v1/orgs/{org}/members/{username}", pathParams("org", org, "username", username)) + resp, err := s.client.doJSON(ctx, http.MethodGet, path, nil, nil) + if err != nil { + if IsNotFound(err) { + return false, nil + } + return false, err + } + return resp.StatusCode == http.StatusNoContent, nil +} + // ListBlockedUsers returns all users blocked by an organisation. func (s *OrgService) ListBlockedUsers(ctx context.Context, org string) ([]types.User, error) { path := ResolvePath("/api/v1/orgs/{org}/blocks", pathParams("org", org)) diff --git a/orgs_test.go b/orgs_test.go index 08a36ca..94bb985 100644 --- a/orgs_test.go +++ b/orgs_test.go @@ -88,6 +88,28 @@ func TestOrgService_ListMembers_Good(t *testing.T) { } } +func TestOrgService_IsMember_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/api/v1/orgs/core/members/alice" { + t.Errorf("wrong path: %s", r.URL.Path) + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + member, err := f.Orgs.IsMember(context.Background(), "core", "alice") + if err != nil { + t.Fatal(err) + } + if !member { + t.Fatal("got member=false, want true") + } +} + func TestOrgService_ListPublicMembers_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet {