add functions for OrgMembership endpoints (#275)

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/275
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
6543 2020-02-09 18:01:13 +00:00 committed by techknowlogick
parent ffaf5e0d1e
commit b6504ee1db
2 changed files with 141 additions and 11 deletions

View file

@ -1,26 +1,98 @@
// Copyright 2016 The Gogs Authors. All rights reserved. // Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package gitea package gitea
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"net/http"
"net/url"
) )
// AddOrgMembershipOption add user to organization options // DeleteOrgMembership remove a member from an organization
type AddOrgMembershipOption struct { func (c *Client) DeleteOrgMembership(org, user string) error {
Role string `json:"role"` _, err := c.getResponse("DELETE", fmt.Sprintf("/orgs/%s/members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
return err
} }
// AddOrgMembership add some one to an organization's member // ListOrgMembershipOption list OrgMembership options
func (c *Client) AddOrgMembership(org, user string, opt AddOrgMembershipOption) error { type ListOrgMembershipOption struct {
body, err := json.Marshal(&opt) ListOptions
}
// ListOrgMembership list an organization's members
func (c *Client) ListOrgMembership(org string, opt ListOrgMembershipOption) ([]*User, error) {
opt.setDefaults()
users := make([]*User, 0, opt.PageSize)
link, _ := url.Parse(fmt.Sprintf("/orgs/%s/members", url.PathEscape(org)))
link.RawQuery = opt.getURLQuery().Encode()
return users, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &users)
}
// ListPublicOrgMembership list an organization's members
func (c *Client) ListPublicOrgMembership(org string, opt ListOrgMembershipOption) ([]*User, error) {
opt.setDefaults()
users := make([]*User, 0, opt.PageSize)
link, _ := url.Parse(fmt.Sprintf("/orgs/%s/public_members", url.PathEscape(org)))
link.RawQuery = opt.getURLQuery().Encode()
return users, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &users)
}
// CheckOrgMembership Check if a user is a member of an organization
func (c *Client) CheckOrgMembership(org, user string) (bool, error) {
status, err := c.getStatusCode("GET", fmt.Sprintf("/orgs/%s/members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
if err != nil {
return false, err
}
switch status {
case http.StatusNoContent:
return true, nil
case http.StatusNotFound:
return false, nil
default:
return false, fmt.Errorf("unexpected Status: %d", status)
}
}
// CheckPublicOrgMembership Check if a user is a member of an organization
func (c *Client) CheckPublicOrgMembership(org, user string) (bool, error) {
status, err := c.getStatusCode("GET", fmt.Sprintf("/orgs/%s/public_members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
if err != nil {
return false, err
}
switch status {
case http.StatusNoContent:
return true, nil
case http.StatusNotFound:
return false, nil
default:
return false, fmt.Errorf("unexpected Status: %d", status)
}
}
// SetPublicOrgMembership publicize/conceal a user's membership
func (c *Client) SetPublicOrgMembership(org, user string, visible bool) error {
var (
status int
err error
)
if visible {
status, err = c.getStatusCode("PUT", fmt.Sprintf("/orgs/%s/public_members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
} else {
status, err = c.getStatusCode("DELETE", fmt.Sprintf("/orgs/%s/public_members/%s", url.PathEscape(org), url.PathEscape(user)), nil, nil)
}
if err != nil { if err != nil {
return err return err
} }
_, err = c.getResponse("PUT", fmt.Sprintf("/orgs/%s/membership/%s", org, user), jsonHeader, bytes.NewReader(body)) switch status {
return err case http.StatusNoContent:
return nil
case http.StatusNotFound:
return fmt.Errorf("forbidden")
default:
return fmt.Errorf("unexpected Status: %d", status)
}
} }

58
gitea/org_member_test.go Normal file
View file

@ -0,0 +1,58 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
/**
// DeleteOrgMembership remove a member from an organization
func (c *Client) DeleteOrgMembership(org, user string) error {}
*/
func TestOrgMembership(t *testing.T) {
log.Println("== TestOrgMembership ==")
c := newTestClient()
user := createTestUser(t, "org_mem_user", c)
c.SetSudo(user.UserName)
newOrg, err := c.CreateOrg(CreateOrgOption{UserName: "MemberOrg"})
assert.NoError(t, err)
assert.NotNil(t, newOrg)
// Check func
check, err := c.CheckPublicOrgMembership(newOrg.UserName, user.UserName)
assert.NoError(t, err)
assert.False(t, check)
check, err = c.CheckOrgMembership(newOrg.UserName, user.UserName)
assert.NoError(t, err)
assert.True(t, check)
err = c.SetPublicOrgMembership(newOrg.UserName, user.UserName, true)
check, err = c.CheckPublicOrgMembership(newOrg.UserName, user.UserName)
assert.NoError(t, err)
assert.True(t, check)
u, err := c.ListOrgMembership(newOrg.UserName, ListOrgMembershipOption{})
assert.NoError(t, err)
assert.Len(t, u, 1)
assert.EqualValues(t, user.UserName, u[0].UserName)
u, err = c.ListPublicOrgMembership(newOrg.UserName, ListOrgMembershipOption{})
assert.NoError(t, err)
assert.Len(t, u, 1)
assert.EqualValues(t, user.UserName, u[0].UserName)
assert.Error(t, c.DeleteOrgMembership(newOrg.UserName, user.UserName))
c.sudo = ""
assert.Error(t, c.AdminDeleteUser(user.UserName))
assert.NoError(t, c.DeleteOrg(newOrg.UserName))
assert.NoError(t, c.AdminDeleteUser(user.UserName))
}