endpoint to get github organization membership role

pull/38/head
Brad Rydzewski 5 years ago
parent fda52b1e08
commit dc09550558

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support Head and Base sha for GitHub pull request, from [@bradrydzewski](https://github.com/bradrydzewski).
- Support Before sha for Bitbucket, from [@jkdev81](https://github.com/jkdev81).
- Support for creating GitHub deployment hooks, from [@bradrydzewski](https://github.com/bradrydzewski).
- Endpoint to get organization membership for GitHub, from [@bradrydzewski](https://github.com/bradrydzewski).
### Fixed
- Fix issue getting a GitLab commit by ref, from [@bradrydzewski](https://github.com/bradrydzewski).

@ -1,4 +1,4 @@
BSD 3-Clause License
BSD-2-Clause-Patent
Copyright (c) 2017, Drone.IO Inc.
All rights reserved.
@ -6,24 +6,47 @@ All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
Subject to the terms and conditions of this license, each copyright holder
and contributor hereby grants to those receiving rights under this license
a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except for failure to satisfy the conditions of this license) patent
license to make, have made, use, offer to sell, sell, import, and otherwise
transfer this software, where such license applies only to those patent
claims, already acquired or hereafter acquired, licensable by such copyright
holder or contributor that are necessarily infringed by:
(a) their Contribution(s) (the licensed copyrights of copyright holders and
non-copyrightable additions of contributors, in source or binary form)
alone; or
(b) combination of their Contribution(s) with the work of authorship to
which such Contribution(s) was added by such copyright holder or
contributor, if, at the time the Contribution is added, such addition
causes such combination to be necessarily infringed. The patent license
shall not apply to any other combinations which include the
Contribution.
Except as expressly stated above, no rights or licenses from any copyright
holder or contributor is granted under this license, whether expressly, by
implication, estoppel or otherwise.
DISCLAIMER
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

@ -141,3 +141,25 @@ func (d Driver) String() (s string) {
return "unknown"
}
}
// Role defines membership roles.
type Role int
// Role values.
const (
RoleUndefined Role = iota
RoleMember
RoleAdmin
)
// String returns the string representation of Role.
func (r Role) String() (s string) {
switch r {
case RoleMember:
return "member"
case RoleAdmin:
return "admin"
default:
return "unknown"
}
}

@ -22,6 +22,10 @@ func (s *organizationService) Find(ctx context.Context, name string) (*scm.Organ
return convertOrganization(out), res, err
}
func (s *organizationService) FindMembership(ctx context.Context, name, username string) (*scm.Membership, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *organizationService) List(ctx context.Context, opts scm.ListOptions) ([]*scm.Organization, *scm.Response, error) {
path := fmt.Sprintf("2.0/teams?%s", encodeListRoleOptions(opts))
out := new(organizationList)

@ -22,6 +22,10 @@ func (s *organizationService) Find(ctx context.Context, name string) (*scm.Organ
return convertOrg(out), res, err
}
func (s *organizationService) FindMembership(ctx context.Context, name, username string) (*scm.Membership, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *organizationService) List(ctx context.Context, _ scm.ListOptions) ([]*scm.Organization, *scm.Response, error) {
var out []*org
res, err := s.client.do(ctx, "GET", "api/v1/user/orgs", nil, &out)

@ -41,6 +41,14 @@ func TestOrgFind(t *testing.T) {
}
}
func TestOrganizationFindMembership(t *testing.T) {
client, _ := New("https://try.gitea.io")
_, _, err := client.Organizations.FindMembership(context.Background(), "gogits", "jcitizen")
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func TestOrgList(t *testing.T) {
defer gock.Off()

@ -22,6 +22,13 @@ func (s *organizationService) Find(ctx context.Context, name string) (*scm.Organ
return convertOrganization(out), res, err
}
func (s *organizationService) FindMembership(ctx context.Context, name, username string) (*scm.Membership, *scm.Response, error) {
path := fmt.Sprintf("orgs/%s/memberships/%s", name, username)
out := new(membership)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertMembership(out), res, err
}
func (s *organizationService) List(ctx context.Context, opts scm.ListOptions) ([]*scm.Organization, *scm.Response, error) {
path := fmt.Sprintf("user/orgs?%s", encodeListOptions(opts))
out := []*organization{}
@ -42,9 +49,31 @@ type organization struct {
Avatar string `json:"avatar_url"`
}
type membership struct {
State string `json:"state"`
Role string `json:"role"`
}
func convertOrganization(from *organization) *scm.Organization {
return &scm.Organization{
Name: from.Login,
Avatar: from.Avatar,
}
}
func convertMembership(from *membership) *scm.Membership {
to := new(scm.Membership)
switch from.State {
case "active":
to.Active = true
}
switch from.Role {
case "admin":
to.Role = scm.RoleAdmin
case "member":
to.Role = scm.RoleMember
default:
to.Role = scm.RoleUndefined
}
return to
}

@ -46,6 +46,36 @@ func TestOrganizationFind(t *testing.T) {
t.Run("Rate", testRate(res))
}
func TestOrganizationFindMembership(t *testing.T) {
defer gock.Off()
gock.New("https://api.github.com").
Get("/orgs/github/memberships/octocat").
Reply(200).
Type("application/json").
SetHeaders(mockHeaders).
File("testdata/membership.json")
client := NewDefault()
got, res, err := client.Organizations.FindMembership(context.Background(), "github", "octocat")
if err != nil {
t.Error(err)
return
}
want := new(scm.Membership)
raw, _ := ioutil.ReadFile("testdata/membership.json.golden")
json.Unmarshal(raw, want)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
t.Run("Request", testRequest(res))
t.Run("Rate", testRate(res))
}
func TestOrganizationList(t *testing.T) {
defer gock.Off()

@ -0,0 +1,40 @@
{
"url": "https://api.github.com/orgs/github/memberships/octocat",
"state": "active",
"role": "admin",
"organization_url": "https://api.github.com/orgs/github",
"user": {
"login": "octocat",
"id": 817538,
"node_id": "MDQ6VXNlcjgxNzUzOA==",
"avatar_url": "https://avatars1.githubusercontent.com/u/817538?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
},
"organization": {
"login": "github",
"id": 2181346,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjIxODEzNDY=",
"url": "https://api.github.com/orgs/github",
"repos_url": "https://api.github.com/orgs/github/repos",
"events_url": "https://api.github.com/orgs/github/events",
"hooks_url": "https://api.github.com/orgs/github/hooks",
"issues_url": "https://api.github.com/orgs/github/issues",
"members_url": "https://api.github.com/orgs/github/members{/member}",
"public_members_url": "https://api.github.com/orgs/github/public_members{/member}",
"avatar_url": "https://avatars1.githubusercontent.com/u/2181346?v=4",
"description": ""
}
}

@ -0,0 +1,4 @@
{
"Active": true,
"Role": 2
}

@ -23,6 +23,10 @@ func (s *organizationService) Find(ctx context.Context, name string) (*scm.Organ
return convertOrganization(out), res, err
}
func (s *organizationService) FindMembership(ctx context.Context, name, username string) (*scm.Membership, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *organizationService) List(ctx context.Context, opts scm.ListOptions) ([]*scm.Organization, *scm.Response, error) {
path := fmt.Sprintf("api/v4/groups?%s", encodeListOptions(opts))
out := []*organization{}

@ -22,6 +22,10 @@ func (s *organizationService) Find(ctx context.Context, name string) (*scm.Organ
return convertOrg(out), res, err
}
func (s *organizationService) FindMembership(ctx context.Context, name, username string) (*scm.Membership, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *organizationService) List(ctx context.Context, _ scm.ListOptions) ([]*scm.Organization, *scm.Response, error) {
var out []*org
res, err := s.client.do(ctx, "GET", "api/v1/user/orgs", nil, &out)

@ -41,6 +41,14 @@ func TestOrgFind(t *testing.T) {
}
}
func TestOrgFindMembership(t *testing.T) {
client, _ := New("https://try.gogs.io")
_, _, err := client.Organizations.FindMembership(context.Background(), "gogits", "jcitizen")
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func TestOrgList(t *testing.T) {
defer gock.Off()

@ -18,6 +18,10 @@ func (s *organizationService) Find(ctx context.Context, name string) (*scm.Organ
return nil, nil, scm.ErrNotSupported
}
func (s *organizationService) FindMembership(ctx context.Context, name, username string) (*scm.Membership, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *organizationService) List(ctx context.Context, opts scm.ListOptions) ([]*scm.Organization, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}

@ -19,6 +19,14 @@ func TestOrganizationFind(t *testing.T) {
}
}
func TestOrganizationFindMembership(t *testing.T) {
client, _ := New("https://api.bitbucket.org")
_, _, err := client.Organizations.FindMembership(context.Background(), "atlassian", "janecitizen")
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
}
}
func TestOrganizationList(t *testing.T) {
client, _ := New("https://api.bitbucket.org")
_, _, err := client.Organizations.List(context.Background(), scm.ListOptions{Size: 30, Page: 1})

@ -15,12 +15,22 @@ type (
Avatar string
}
// Membership represents an organization membership.
Membership struct {
Active bool
Role Role
}
// OrganizationService provides access to organization resources.
OrganizationService interface {
// Find returns the organization by name.
Find(context.Context, string) (*Organization, *Response, error)
Find(ctx context.Context, name string) (*Organization, *Response, error)
// FindMembership returns the organization membership
// by a given user account.
FindMembership(ctx context.Context, name, username string) (*Membership, *Response, error)
// List returns the user organization list.
List(context.Context, ListOptions) ([]*Organization, *Response, error)
List(ctx context.Context, opts ListOptions) ([]*Organization, *Response, error)
}
)

Loading…
Cancel
Save