-
Notifications
You must be signed in to change notification settings - Fork 39
/
member.go
76 lines (62 loc) · 2.28 KB
/
member.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package sentry
import (
"fmt"
"net/url"
"time"
)
// Member is a sentry member
type Member struct {
Email string `json:"email,omitempty"`
Expired *bool `json:"expired,omitempty"`
Name *string `json:"name,omitempty"`
IsPending *bool `json:"isPending,omitempty"`
DateCreated *time.Time `json:"dateCreated,omitempty"`
Role string `json:"role,omitempty"`
ID *string `json:"id,omitempty"`
RoleName *string `json:"roleName,omitempty"`
Teams []string `json:"teams,omitempty"`
}
type memberQuery struct {
query string `json:"query,omitempty"`
}
func (o *memberQuery) ToQueryString() string {
query := url.Values{}
query.Add("query", string(o.query))
return query.Encode()
}
// CreateMember takes an email and creates a new member
func (c *Client) CreateMember(o Organization, email string) (Member, error) {
var member Member
memberRole := "member"
memberreq := Member{
Email: email,
Role: memberRole,
}
err := c.do("POST", fmt.Sprintf("organizations/%s/members", *o.Slug), &member, &memberreq)
return member, err
}
// GetMemberByEmail takes a user email and returns back the user
func (c *Client) GetMemberByEmail(o Organization, memberEmail string) (Member, error) {
var members []Member
err := c.doWithQuery("GET", fmt.Sprintf("organizations/%s/members", *o.Slug), &members, nil, &memberQuery{memberEmail})
if err != nil {
return Member{}, fmt.Errorf("failed to get member with error: %s", err.Error())
}
if len(members) == 0 {
return Member{}, fmt.Errorf("no member with that email found")
}
return members[0], err
}
// AddExistingMemberToTeam takes a member and adds them to a team
func (c *Client) AddExistingMemberToTeam(o Organization, t Team, m Member) error {
return c.do("POST", fmt.Sprintf("organizations/%s/members/%s/teams/%s", *o.Slug, *m.ID, *t.Slug), nil, nil)
}
// DeleteMember takes a member and deletes from the org
func (c *Client) DeleteMember(o Organization, m Member) error {
return c.do("DELETE", fmt.Sprintf("organizations/%s/members/%s", *o.Slug, *m.ID), nil, nil)
}
// MakeAdmin takes a member and makes them admin
func (c *Client) MakeAdmin(o Organization, a Member) error {
a.Role = "admin"
return c.do("PUT", fmt.Sprintf("organizations/%s/members/%s", *o.Slug, *a.ID), nil, &a)
}