# encoding: utf-8
module Github
class Issues
module Milestones
VALID_MILSTONE_OPTIONS = {
'state' => %w[ open closed ],
'sort' => %w[ due_date completeness ],
'direction' => %w[ desc asc ]
}
VALID_MILESTONE_INPUTS = %w[ title state description due_on ]
# List milestones for a repository
#
# = Parameters
# :state - open, closed, default: open
# :sort - due_date, completeness, default: due_date
# :direction - asc, desc, default: desc
#
# = Examples
# @github = Github.new :user => 'user-name', :repo => 'repo-name'
# @github.issues.milestones
#
def milestones(user_name=nil, repo_name=nil, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
_normalize_params_keys(params)
_filter_params_keys(VALID_MILSTONE_OPTIONS.keys, params)
_validate_params_values(VALID_MILSTONE_OPTIONS, params)
response = get("/repos/#{user}/#{repo}/milestones", params)
return response unless block_given?
response.each { |el| yield el }
end
# Get a single milestone
#
# = Examples
# @github = Github.new
# @github.issues.milestone 'user-name', 'repo-name', 'milestone-id'
#
def milestone(user_name, repo_name, milestone_id, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
_normalize_params_keys(params)
get("/repos/#{user}/#{repo}/milestones/#{milestone_id}", params)
end
# Create a milestone
#
# = Inputs
# :title - Required string
# :state - Optional string - open or closed
# :description - Optional string
# :due_on - Optional string - ISO 8601 time
#
# = Examples
# @github = Github.new :user => 'user-name', :repo => 'repo-name'
# @github.issues.create_milestone :title => 'hello-world'
#
def create_milestone(user_name=nil, repo_name=nil, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
_normalize_params_keys(params)
_filter_params_keys(VALID_MILESTONE_INPUTS, params)
raise ArgumentError, "Required params are: :title" unless _validate_inputs(%w[ title ], params)
post("/repos/#{user}/#{repo}/milestones", params)
end
# Update a milestone
#
# = Inputs
# :title - Required string
# :state - Optional string - open or closed
# :description - Optional string
# :due_on - Optional string - ISO 8601 time
#
# = Examples
# @github = Github.new
# @github.issues.update_milestone 'user-name', 'repo-name', 'milestone-id', :title => 'hello-world'
#
def update_milestone(user_name, repo_name, milestone_id, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
_validate_presence_of milestone_id
_normalize_params_keys(params)
_filter_params_keys(VALID_MILESTONE_INPUTS, params)
raise ArgumentError, "Required params are: :title" unless _validate_inputs(%w[ title ], params)
patch("/repos/#{user}/#{repo}/milestones/#{milestone_id}", params)
end
# Delete a milestone
#
# = Examples
# @github = Github.new
# @github.issues.delete_milestone 'user-name', 'repo-name', 'milestone-id'
#
def delete_milestone(user_name, repo_name, milestone_id, params={})
_update_user_repo_params(user_name, repo_name)
_validate_user_repo_params(user, repo) unless user? && repo?
_validate_presence_of milestone_id
_normalize_params_keys(params)
delete("/repos/#{user}/#{repo}/milestones/#{milestone_id}", params)
end
end # Milestones
end # Issues
end # Github