Skip to content

Commit e46c4e1

Browse files
authored
Merge pull request #61 from cltweedie/teams
Teams
2 parents 650c0a4 + d51cdd7 commit e46c4e1

File tree

5 files changed

+227
-3
lines changed

5 files changed

+227
-3
lines changed

lib/bitbucket_rest_api.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ def lookup_constant(const_name)
7676
:Issues => 'issues',
7777
:User => 'user',
7878
:Users => 'users',
79-
:Invitations => 'invitations'
79+
:Invitations => 'invitations',
80+
:Teams => 'teams'
8081

8182
#:Teams => 'teams',
8283
#:PullRequests => 'pull_requests',

lib/bitbucket_rest_api/client.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ def oauth(options = {})
2121
alias :authorizations :oauth
2222

2323
def teams(options = {})
24-
raise "Unimplemented"
25-
#@teams ||= ApiFactory.new 'teams', options
24+
@teams ||= ApiFactory.new 'Teams', options
2625
end
2726

2827
def pull_requests(options = {})

lib/bitbucket_rest_api/teams.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# encoding: utf-8
2+
3+
module BitBucket
4+
class Teams < API
5+
extend AutoloadHelper
6+
7+
def initialize(options = { })
8+
super(options)
9+
end
10+
11+
# List teams for the authenticated user where the user has the provided role
12+
# Roles are :admin, :contributor, :member
13+
#
14+
# = Examples
15+
# bitbucket = BitBucket.new :oauth_token => '...', :oauth_secret => '...'
16+
# bitbucket.teams.list(:admin)
17+
# bitbucket.teams.list('member')
18+
# bitbucket.teams.list(:contributor) { |team| ... }
19+
def list(user_role)
20+
response = get_request("/2.0/teams/?role=#{user_role.to_s}")
21+
return response["values"] unless block_given?
22+
response["values"].each { |el| yield el }
23+
end
24+
25+
alias :all :list
26+
27+
# Return the profile for the provided team
28+
#
29+
# = Example
30+
# bitbucket = BitBucket.new :oauth_token => '...', :oauth_secret => '...'
31+
# bitbucket.teams.profile(:team_name_here)
32+
def profile(team_name)
33+
get_request("/2.0/teams/#{team_name.to_s}")
34+
end
35+
36+
# List members of the provided team
37+
#
38+
# = Examples
39+
# bitbucket = BitBucket.new :oauth_token => '...', :oauth_secret => '...'
40+
# bitbucket.teams.members(:team_name_here)
41+
# bitbucket.teams.members(:team_name_here) { |member| ... }
42+
def members(team_name)
43+
response = get_request("/2.0/teams/#{team_name.to_s}/members")
44+
return response["values"] unless block_given?
45+
response["values"].each { |el| yield el }
46+
end
47+
48+
# List followers of the provided team
49+
#
50+
# = Examples
51+
# bitbucket = BitBucket.new :oauth_token => '...', :oauth_secret => '...'
52+
# bitbucket.teams.followers(:team_name_here)
53+
# bitbucket.teams.followers(:team_name_here) { |follower| ... }
54+
def followers(team_name)
55+
response = get_request("/2.0/teams/#{team_name.to_s}/followers")
56+
return response["values"] unless block_given?
57+
response["values"].each { |el| yield el }
58+
end
59+
60+
# List accounts following the provided team
61+
#
62+
# = Examples
63+
# bitbucket = BitBucket.new :oauth_token => '...', :oauth_secret => '...'
64+
# bitbucket.teams.following(:team_name_here)
65+
# bitbucket.teams.following(:team_name_here) { |followee| ... }
66+
def following(team_name)
67+
response = get_request("/2.0/teams/#{team_name.to_s}/following")
68+
return response["values"] unless block_given?
69+
response["values"].each { |el| yield el }
70+
end
71+
72+
# List repos for provided team
73+
# Private repos will only be returned if the user is authorized to view them
74+
#
75+
# = Examples
76+
# bitbucket = BitBucket.new :oauth_token => '...', :oauth_secret => '...'
77+
# bitbucket.teams.repos(:team_name_here)
78+
# bitbucket.teams.repos(:team_name_here) { |repo| ... }
79+
def repos(team_name)
80+
response = get_request("/2.0/repositories/#{team_name.to_s}")
81+
return response["values"] unless block_given?
82+
response["values"].each { |el| yield el }
83+
end
84+
85+
alias :repositories :repos
86+
87+
end # Team
88+
end # BitBucket

spec/bitbucket_rest_api/client_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
expect(client.users).to be_a BitBucket::Users
1010
expect(client.user_api).to be_a BitBucket::User
1111
expect(client.invitations).to be_a BitBucket::Invitations
12+
expect(client.teams).to be_a BitBucket::Teams
1213
expect(client.pull_requests).to be_a BitBucket::Repos::PullRequest
1314
expect(client.oauth).to be_a BitBucket::Request::OAuth
1415
end
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
require 'spec_helper'
2+
3+
describe BitBucket::Teams do
4+
let(:team) { described_class.new }
5+
6+
describe '.list' do
7+
before do
8+
expect(team).to receive(:request).with(
9+
:get,
10+
'/2.0/teams/?role=member',
11+
{},
12+
{}
13+
).and_return({"values" => ['team1', 'team2', 'team3']})
14+
end
15+
16+
context 'without a block' do
17+
it 'sends a GET request for the teams of which the user is a member' do
18+
team.list(:member)
19+
end
20+
end
21+
22+
context 'with a block' do
23+
it 'sends a GET request for the teams of which the user is a member' do
24+
team.list(:member) { |team| team }
25+
end
26+
end
27+
end
28+
29+
describe '.profile' do
30+
before do
31+
expect(team).to receive(:request).with(
32+
:get,
33+
'/2.0/teams/team_name',
34+
{},
35+
{}
36+
)
37+
end
38+
39+
it 'sends a GET request for the profile for the team' do
40+
team.profile('team_name')
41+
end
42+
end
43+
44+
describe '.members' do
45+
before do
46+
expect(team).to receive(:request).with(
47+
:get,
48+
'/2.0/teams/team_name/members',
49+
{},
50+
{}
51+
).and_return({"values" => ['member1', 'member2', 'member3']})
52+
end
53+
54+
context "without a block" do
55+
it 'sends a GET request for the members of the team' do
56+
team.members('team_name')
57+
end
58+
end
59+
60+
context "with a block" do
61+
it 'sends a GET request for the members of the team' do
62+
team.members('team_name') { |member| member }
63+
end
64+
end
65+
end
66+
67+
describe '.followers' do
68+
before do
69+
expect(team).to receive(:request).with(
70+
:get,
71+
'/2.0/teams/team_name/followers',
72+
{},
73+
{}
74+
).and_return({"values" => ['follower1', 'follower2', 'follower3']})
75+
end
76+
77+
context "without a block" do
78+
it 'sends a GET request for the followers of the team' do
79+
team.followers('team_name')
80+
end
81+
end
82+
83+
context "with a block" do
84+
it 'sends a GET request for the followers of the team' do
85+
team.followers('team_name') { |follower| follower }
86+
end
87+
end
88+
end
89+
90+
describe '.following' do
91+
before do
92+
expect(team).to receive(:request).with(
93+
:get,
94+
'/2.0/teams/team_name/following',
95+
{},
96+
{}
97+
).and_return({"values" => ['following1', 'following2', 'following3']})
98+
end
99+
100+
context "without a block" do
101+
it 'sends a GET request for accounts the team is following' do
102+
team.following('team_name')
103+
end
104+
end
105+
106+
context "with a block" do
107+
it 'sends a GET request for accounts the team is following' do
108+
team.following('team_name') { |followee| followee }
109+
end
110+
end
111+
end
112+
113+
describe '.repos' do
114+
before do
115+
expect(team).to receive(:request).with(
116+
:get,
117+
'/2.0/repositories/team_name',
118+
{},
119+
{}
120+
).and_return({"values" => ['repo1', 'repo2', 'repo3']})
121+
end
122+
123+
context "without a block" do
124+
it 'sends a GET request for the repos for the team' do
125+
team.repos('team_name')
126+
end
127+
end
128+
129+
context "with a block" do
130+
it 'sends a GET request for the repos for the team' do
131+
team.repos('team_name') { |repo| repo }
132+
end
133+
end
134+
end
135+
end

0 commit comments

Comments
 (0)