# encoding: utf-8
module BitBucket
class Issues < API
extend AutoloadHelper
autoload_all 'bitbucket_rest_api/issues',
:Comments => 'comments',
:Components => 'components',
:Milestones => 'milestones'
VALID_ISSUE_PARAM_NAMES = %w[
title
content
component
milestone
version
responsible
priority
status
kind
limit
start
search
sort
reported_by
].freeze
VALID_ISSUE_PARAM_VALUES = {
'priority' => %w[ trivial minor major critical blocker ],
'status' => ['new', 'open', 'resolved', 'on hold', 'invalid', 'duplicate', 'wontfix'],
'kind' => %w[ bug enhancement proposal task ]
}
# Creates new Issues API
def initialize(options = { })
super(options)
end
# Access to Issues::Comments API
def comments
@comments ||= ApiFactory.new 'Issues::Comments'
end
# Access to Issues::Components API
def components
@components ||= ApiFactory.new 'Issues::Components'
end
# Access to Issues::Milestones API
def milestones
@milestones ||= ApiFactory.new 'Issues::Milestones'
end
# List issues for a repository
#
# = Inputs
# :limit - Optional - Number of issues to retrieve, default 15
# :start - Optional - Issue offset, default 0
# :search - Optional - A string to search for
# :sort - Optional - Sorts the output by any of the metadata fields
# :title - Optional - Contains a filter operation to restrict the list of issues by the issue title
# :content - Optional - Contains a filter operation to restrict the list of issues by the issue content
# :version - Optional - Contains an is or ! ( is not) filter to restrict the list of issues by the version
# :milestone - Optional - Contains an is or ! ( is not) filter to restrict the list of issues by the milestone
# :component - Optional - Contains an is or ! ( is not) filter to restrict the list of issues by the component
# :kind - Optional - Contains an is or ! ( is not) filter to restrict the list of issues by the issue kind
# :status - Optional - Contains an is or ! ( is not) filter to restrict the list of issues by the issue status
# :responsible - Optional - Contains an is or ! ( is not) filter to restrict the list of issues by the user responsible
# :reported_by - Optional - Contains a filter operation to restrict the list of issues by the user that reported the issue
#
# = Examples
# bitbucket = BitBucket.new :user => 'user-name', :repo => 'repo-name'
# bitbucket.issues.list_repo :filter => 'kind=bug&kind=enhancement'
#
def list_repo(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_ISSUE_PARAM_NAMES, params
# _merge_mime_type(:issue, params)
assert_valid_values(VALID_ISSUE_PARAM_VALUES, params)
response = get_request("/1.0/repositories/#{user}/#{repo.downcase}/issues", params)
return response.issues unless block_given?
response.issues.each { |el| yield el }
end
alias :list_repository :list_repo
# Get a single issue
#
# = Examples
# bitbucket = BitBucket.new
# bitbucket.issues.get 'user-name', 'repo-name', 'issue-id'
#
def get(user_name, repo_name, issue_id, params={ })
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
_validate_presence_of issue_id
normalize! params
# _merge_mime_type(:issue, params)
get_request("/1.0/repositories/#{user}/#{repo.downcase}/issues/#{issue_id}", params)
end
alias :find :get
# Delete a single issue
#
# = Examples
# bitbucket = BitBucket.new
# bitbucket.issues.delete 'user-name', 'repo-name', 'issue-id'
#
def delete(user_name, repo_name, issue_id, params={ })
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
_validate_presence_of issue_id
normalize! params
# _merge_mime_type(:issue, params)
delete_request("/1.0/repositories/#{user}/#{repo}/issues/#{issue_id}", params)
end
# Create an issue
#
# = Inputs
# :title - Required string
# :content - Optional string
# :responsible - Optional string - Login for the user that this issue should be assigned to.
# :milestone - Optional number - Milestone to associate this issue with
# :version - Optional number - Version to associate this issue with
# :component - Optional number - Component to associate this issue with
# :priority - Optional string - The priority of this issue
# * trivial
# * minor
# * major
# * critical
# * blocker
# :status - Optional string - The status of this issue
# * new
# * open
# * resolved
# * on hold
# * invalid
# * duplicate
# * wontfix
# :kind - Optional string - The kind of issue
# * bug
# * enhancement
# * proposal
# * task
#
# = Examples
# bitbucket = BitBucket.new :user => 'user-name', :repo => 'repo-name'
# bitbucket.issues.create
# "title" => "Found a bug",
# "content" => "I'm having a problem with this.",
# "responsible" => "octocat",
# "milestone" => 1,
# "priority" => "blocker"
#
def create(user_name, repo_name, params={ })
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
normalize! params
_merge_user_into_params!(params) unless params.has_key?('user')
# _merge_mime_type(:issue, params)
filter! VALID_ISSUE_PARAM_NAMES, params
assert_required_keys(%w[ title ], params)
post_request("/1.0/repositories/#{user}/#{repo.downcase}/issues/", params)
end
# Edit an issue
#
# = Inputs
# :title - Required string
# :content - Optional string
# :responsible - Optional string - Login for the user that this issue should be assigned to.
# :milestone - Optional number - Milestone to associate this issue with
# :version - Optional number - Version to associate this issue with
# :component - Optional number - Component to associate this issue with
# :priority - Optional string - The priority of this issue
# * trivial
# * minor
# * major
# * critical
# * blocker
# :status - Optional string - The status of this issue
# * new
# * open
# * resolved
# * on hold
# * invalid
# * duplicate
# * wontfix
# :kind - Optional string - The kind of issue
# * bug
# * enhancement
# * proposal
# * task
#
# = Examples
# bitbucket = BitBucket.new :user => 'user-name', :repo => 'repo-name'
# bitbucket.issues.create
# "title" => "Found a bug",
# "content" => "I'm having a problem with this.",
# "responsible" => "octocat",
# "milestone" => 1,
# "priority" => "blocker"
#
def edit(user_name, repo_name, issue_id, params={ })
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
_validate_presence_of issue_id
normalize! params
# _merge_mime_type(:issue, params)
filter! VALID_ISSUE_PARAM_NAMES, params
put_request("/1.0/repositories/#{user}/#{repo.downcase}/issues/#{issue_id}/", params)
end
end # Issues
end # BitBucket