Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add projects api #130

Merged
merged 5 commits into from
Jul 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/tinybucket/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Api
:CommitsApi,
:CommentsApi,
:DiffApi,
:ProjectsApi,
:PullRequestsApi,
:ReposApi,
:RepoApi,
Expand Down
1 change: 1 addition & 0 deletions lib/tinybucket/api/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Helper
:ReposHelper,
:RepoHelper,
:PullRequestsHelper,
:ProjectsHelper,
:TeamHelper,
:UserHelper
].each do |klass_name|
Expand Down
29 changes: 29 additions & 0 deletions lib/tinybucket/api/helper/projects_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Tinybucket
module Api
module Helper
module ProjectsHelper
include ::Tinybucket::Api::Helper::ApiHelper

private

def path_to_list(owner)
base_path(owner)
end

def path_to_find(owner, project_key)
build_path(base_path(owner),
[project_key, 'project_key'])
end

def base_path(owner)
build_path('/teams',
[owner, 'owner'],
'projects',
'/')
end
end
end
end
end
26 changes: 26 additions & 0 deletions lib/tinybucket/api/projects_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Tinybucket
module Api
class ProjectsApi < BaseApi
include Tinybucket::Api::Helper::ProjectsHelper
attr_accessor :owner

def list(options = {})
get_path(
path_to_list(owner),
options,
Tinybucket::Parser::ProjectsParser
)
end

def find(project_key, options = {})
get_path(
path_to_find(owner, project_key),
options,
Tinybucket::Parser::ProjectParser
)
end
end
end
end
1 change: 1 addition & 0 deletions lib/tinybucket/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Model
:ErrorResponse,
:Page,
:Profile,
:Project,
:PullRequest,
:Repository,
:Team
Expand Down
26 changes: 26 additions & 0 deletions lib/tinybucket/model/project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Tinybucket
module Model
class Project < Base
acceptable_attributes \
:type, :description, :links, :uuid, :created_on,
:key, :updated_on, :is_private, :name, :owner

# Update this project
#
# @param _params [Hash]
# @raise [NotImplementedError] to be implemented
def update(_params)
raise NotImplementedError
end

# Destroy this project
#
# @raise [NotImplementedError] to be implemented.
def destroy
raise NotImplementedError
end
end
end
end
20 changes: 20 additions & 0 deletions lib/tinybucket/model/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ def following(options = {})
Tinybucket::Resource::Team::Following.new(username, options)
end

# Get projects
#
# @param options [Hash]
# @return [Tinybucket::Resource::Team::Project]
def projects(options = {})
projects_resource(options)
end

# Get the project
#
# @param project_key [String]
# @return [Tinybucket::Model::Project]
def project(project_key, options = {})
projects_resource().find(project_key, options)
end

# Get this team's repositories.
#
# @param options [Hash]
Expand All @@ -64,6 +80,10 @@ def repos(options = {})

private

def projects_resource(options = {})
Tinybucket::Resource::Projects.new(username, options)
end

def team_api
create_api('Team')
end
Expand Down
2 changes: 2 additions & 0 deletions lib/tinybucket/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module Parser
:CommentsParser,
:ProfileParser,
:ProfilesParser,
:ProjectParser,
:ProjectsParser,
:PullRequestParser,
:PullRequestsParser,
:RepoParser,
Expand Down
11 changes: 11 additions & 0 deletions lib/tinybucket/parser/project_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Tinybucket
module Parser
class ProjectParser < BaseParser
def convert(json)
Tinybucket::Model::Project.new(json)
end
end
end
end
11 changes: 11 additions & 0 deletions lib/tinybucket/parser/projects_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Tinybucket
module Parser
class ProjectsParser < BaseParser
def convert(json)
Tinybucket::Model::Page.new(json, Tinybucket::Model::Project)
end
end
end
end
1 change: 1 addition & 0 deletions lib/tinybucket/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Resource
:Commits,
:Forks,
:OwnersRepos,
:Projects,
:PublicRepos,
:PullRequests,
:Repos,
Expand Down
49 changes: 49 additions & 0 deletions lib/tinybucket/resource/projects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

module Tinybucket
module Resource
class Projects < Base
attr_accessor :owner

# Initialize
#
# @param owner_name [String]
# @param options [Hash]
def initialize(owner_name, options = {})
@owner = owner_name
@args = [options]
end

# Find the project
#
# @param project_key [String]
# @param options [Hash]
# @return [Tinybucket::Model::Project]
def find(project_key, options = {})
projects_api.find(project_key, options)
end

# Create a new project
#
# NOTE: Not Implemented yet.
#
# @param _params [Hash]
# @raise [NotImplementedError] to be implemented
def create(_params)
raise NotImplementedError
end

private

def projects_api
create_api('Projects').tap do |api|
api.owner = @owner
end
end

def enumerator
create_enumerator(projects_api, :list, *@args)
end
end
end
end
89 changes: 89 additions & 0 deletions spec/fixtures/teams/test_team/projects/get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"pagelen": 10,
"values": [
{
"uuid": "{45d24c3d-efa0-4c2a-be4e-78c1b30a30d0}",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/teams/test_team/projects/TEST"
},
"html": {
"href": "https://bitbucket.org/account/user/test_team/projects/TEST"
},
"repositories": {
"href": "https://api.bitbucket.org/2.0/repositories/test_team?q='project.key=\"TEST\"'"
},
"avatar": {
"href": "https://bitbucket.org/account/user/test_team/projects/TEST/avatar/32"
}
},
"description": null,
"created_on": "2016-03-19T03:34:27.792230+00:00",
"key": "TEST",
"owner": {
"username": "test_team",
"display_name": "test team",
"type": "team",
"uuid": "{e0f17982-effb-43eb-8589-7bacabb37cab}",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/teams/test_team"
},
"html": {
"href": "https://bitbucket.org/test_team/"
},
"avatar": {
"href": "https://bitbucket.org/account/test_team/avatar/32/"
}
}
},
"updated_on": "2016-03-19T03:34:27.792256+00:00",
"type": "project",
"is_private": false,
"name": "test"
},
{
"uuid": "{59278b96-1238-4418-a2b9-8f438234ae45}",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/teams/test_team/projects/PROJ"
},
"html": {
"href": "https://bitbucket.org/account/user/test_team/projects/PROJ"
},
"repositories": {
"href": "https://api.bitbucket.org/2.0/repositories/test_team?q='project.key=\"PROJ\"'"
},
"avatar": {
"href": "https://bitbucket.org/account/user/test_team/projects/PROJ/avatar/32"
}
},
"description": "Project created by Bitbucket for test",
"created_on": "2015-12-04T02:27:25.909083+00:00",
"key": "PROJ",
"owner": {
"username": "test_team",
"display_name": "test team",
"type": "team",
"uuid": "{e0f17982-effb-41cb-8589-7bacabb37cab}",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/teams/test_team"
},
"html": {
"href": "https://bitbucket.org/test_team/"
},
"avatar": {
"href": "https://bitbucket.org/account/test_team/avatar/32/"
}
}
},
"updated_on": "2015-12-04T02:27:25.909123+00:00",
"type": "project",
"is_private": false,
"name": "Untitled project"
}
],
"page": 1,
"size": 2
}
41 changes: 41 additions & 0 deletions spec/fixtures/teams/test_team/projects/myprj/get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"uuid": "{45234c3d-efa0-4c2a-be4e-78c1b30a30d0}",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/teams/test_team/projects/myprj"
},
"html": {
"href": "https://bitbucket.org/account/user/test_team/projects/myprj"
},
"repositories": {
"href": "https://api.bitbucket.org/2.0/repositories/test_team?q='project.key=\"myprj\"'"
},
"avatar": {
"href": "https://bitbucket.org/account/user/test_team/projects/myprj/avatar/32"
}
},
"description": null,
"created_on": "2016-03-19T03:34:27.792230+00:00",
"key": "myprj",
"owner": {
"username": "test_team",
"display_name": "test team",
"type": "team",
"uuid": "{e0f17982-efab-43cb-8589-7bacabb37cab}",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/teams/test_team"
},
"html": {
"href": "https://bitbucket.org/test_team/"
},
"avatar": {
"href": "https://bitbucket.org/account/test_team/avatar/32/"
}
}
},
"updated_on": "2016-03-19T03:34:27.792256+00:00",
"type": "project",
"is_private": false,
"name": "myprj"
}
29 changes: 29 additions & 0 deletions spec/lib/tinybucket/api/projects_api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'spec_helper'

RSpec.describe Tinybucket::Api::ProjectsApi do
include ApiResponseMacros

let(:api) { Tinybucket::Api::ProjectsApi.new }
let(:owner) { 'test_team' }
let(:request_path) { nil }
before do
api.owner = owner
stub_apiresponse(:get, request_path) if request_path
end

it { expect(api).to be_a_kind_of(Tinybucket::Api::BaseApi) }

describe 'list' do
subject { api.list() }
let(:request_path) { '/teams/test_team/projects/' }
it { expect(subject).to be_an_instance_of(Tinybucket::Model::Page) }
end

describe 'find' do
let(:project_key) { 'myprj' }
subject { api.find(project_key) }

let(:request_path) { "/teams/test_team/projects/#{project_key}" }
it { expect(subject).to be_an_instance_of(Tinybucket::Model::Project) }
end
end
Loading