# Get a pull request
class Gitea::PullRequest::ChangeStatusService < Gitea::ClientService
  attr_reader :owner, :repo, :number, :is_closed, :token

  #eq:
  # Gitea::PullRequest::ChangeStatusService.call(user.login, repository.identifier, pull.gitea_number, true, user.gitea_token)
  def initialize(owner, repo, number, is_closed, token=nil)
    @owner  = owner
    @repo   = repo
    @number = number
    @is_closed = is_closed
    @token  = token
  end

  def call
    response = get(url, request_params, true)
    status, message, body = render_response(response)
    json_format(status, message, body)
  end

  private

  def request_params
    Hash.new.merge(token: token, is_closed: is_closed)
  end

  def url
    "/repos/#{owner}/#{repo}/pulls/#{number}/change_status".freeze
  end

  def json_format(status, message, body)
    case status
    when 204 then success(body)
    else
      error(message, status)
    end
  end
end
