# encoding: utf-8
module BitBucket
class Client::Repos < API
require_all 'bitbucket_rest_api/client/repos',
'changesets',
'keys',
'services',
'following',
'sources',
'pull_requests'
@version = '1.0'
DEFAULT_REPO_OPTIONS = {
"website" => "",
"is_private" => false,
"has_issues" => false,
"has_wiki" => false,
"scm" => "git",
"no_public_forks" => false
}.freeze
VALID_REPO_OPTIONS = %w[
owner
name
description
website
is_private
has_issues
has_wiki
no_public_forks
language
scm
].freeze
# Access to Repos::Commits API
namespace :changesets
# Access to Repos::Keys API
namespace :keys
# Access to Repos::Watchin API
namespace :following
# Access to Repos::Commits API
namespace :sources
# Access to Repos::Services API
namespace :services
# Access to Repos::PullRequests API
namespace :pull_requests
# List branches
#
# = Examples
#
# bitbucket = BitBucket.new
# bibucket.repos.branches 'user-name', 'repo-name'
#
# repos = BitBucket::Repos.new
# repos.branches 'user-name', 'repo-name'
#
def branches(user_name, repo_name, params={ })
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless (user? && repo?)
normalize! params
response = get_request("/repositories/#{user}/#{repo.downcase}/branches/", params)
return response unless block_given?
response.each { |el| yield el }
end
alias :list_branches :branches
# Create a new repository for the authenticated user.
#
# = Parameters
# :name - Required string
# :description - Optional string
# :website - Optional string
# :is_private - Optional boolean - true to create a private repository, false to create a public one.
# :has_issues - Optional boolean - true to enable issues for this repository, false to disable them
# :has_wiki - Optional boolean - true to enable the wiki for this repository, false to disable it. Default is true
# :owner Optional string - The team in which this repository will be created
#
# = Examples
# bitbucket = BitBucket.new
# bitbucket.repos.create "name" => 'repo-name'
# "description": "This is your first repo",
# "website": "https://bitbucket.com",
# "is_private": false,
# "has_issues": true,
# "has_wiki": true
#
# Create a new repository in this team. The authenticated user
# must be a member of this team
#
# Examples:
# bitbucket = BitBucket.new :oauth_token => '...', :oauth_secret => '...'
# bitbucket.repos.create :name => 'repo-name', :owner => 'team-name'
#
def create(*args)
params = args.extract_options!
normalize! params
filter! VALID_REPO_OPTIONS + %w[ org ], params
assert_required_keys(%w[ name ], params)
# Requires authenticated user
post_request("/repositories/", DEFAULT_REPO_OPTIONS.merge(params))
end
# Edit a repository
#
# = Parameters
# * :name Required string
# * :description Optional string
# * :website Optional string
# * :private - Optional boolean - false to create public reps, false to create a private one
# * :has_issues Optional boolean - true to enable issues for this repository, false to disable them
# * :has_wiki Optional boolean - true to enable the wiki for this repository, false to disable it. Default is true
# * :has_downloads Optional boolean - true to enable downloads for this repository
#
# = Examples
#
# bitbucket = BitBucket.new
# bitbucket.repos.edit 'user-name', 'repo-name',
# :name => 'hello-world',
# :description => 'This is your first repo',
# :website => "https://bitbucket.com",
# :public => true, :has_issues => true
#
def edit(user_name, repo_name, params={ })
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params
filter! VALID_REPO_OPTIONS, params
put_request("/repositories/#{user}/#{repo.downcase}/", DEFAULT_REPO_OPTIONS.merge(params))
end
# Get a repository
#
# = Examples
# bitbucket = BitBucket.new
# bitbucket.repos.get 'user-name', 'repo-name'
#
def get(user_name, repo_name, params={ })
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params
get_request("/repositories/#{user}/#{repo.downcase}", params)
end
alias :find :get
# Delete a repository
#
# = Examples
# @bitbucket = BitBucket.new
# @bitbucket.repos.delete 'user-name', 'repo-name'
#
def delete(user_name, repo_name)
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
delete_request("/repositories/#{user}/#{repo.downcase}")
end
# List repositories for the authenticated user
#
# = Examples
# bitbucket = BitBucket.new :oauth_token => '...', :oauth_secret => '...'
# bitbucket.repos.list
# bitbucket.repos.list { |repo| ... }
#
# List public repositories for the specified user.
#
# = Examples
# bitbucket = BitBucket.new
# bitbucket.repos.list :user => 'user-name'
# bitbucket.repos.list :user => 'user-name', { |repo| ... }
def list(*args)
#_merge_user_into_params!(params) unless params.has_key?('user')
arguments(args) do
permit %w[ user type ]
end
params = arguments.params
response = get_request("/user/repositories", params)
return response unless block_given?
response.each { |el| yield el }
end
alias :all :list
# List tags
#
# = Examples
# bitbucket = BitBucket.new
# bitbucket.repos.tags 'user-name', 'repo-name'
# bitbucket.repos.tags 'user-name', 'repo-name' { |tag| ... }
#
def tags(user_name, repo_name, params={ })
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params
response = get_request("/repositories/#{user}/#{repo.downcase}/tags/", params)
return response unless block_given?
response.each { |el| yield el }
end
alias :list_tags :tags
alias :repo_tags :tags
alias :repository_tags :tags
end # Repos
end # BitBucket