# get the diff info for the commit
class Gitea::Commit::DiffService < Gitea::ClientService
  attr_reader :owner, :repo, :sha, :token

  # GET /repos/{owner}/{repo}/commits/{sha}/diff
  # owner: 用户
  # repo: 仓库名称/标识
  # sha: commit唯一标识
  # eg:
  # Gitea::Commit::DiffService.call('jasder', 'repo_identifier', 'sha value')
  def initialize(owner, repo, sha, token=nil)
    @owner   = owner
    @repo   = repo
    @sha  = sha
    @token = token
  end

  def call
    response = get(url, params, true)
    render_result(response)
  end

  private
  def params
    Hash.new.merge(token: token)
  end

  def url
    "/repos/#{owner}/#{repo}/commits/#{sha}/diff".freeze
  end

  def render_result(response)
    case response.status
    when 200
      JSON.parse(response.body)
    else
      nil
    end
  end
end
