class Api::V1::Projects::Commits::ListService < ApplicationService 

  attr_reader :project, :sha, :page, :limit, :owner, :repo, :token
  attr_accessor :gitea_data 

  def initialize(project, params, token=nil) 
    @project = project 
    @sha = params[:sha]
    @page = params[:page] || 1
    @limit = params[:limit] || 15
    @owner = project&.owner.login
    @repo = project&.identifier
    @token = token
  end

  def call 
    load_gitea_data 

    gitea_data
  end

  private 
  def request_params 
    param = {
      access_token: token,
      page: page, 
      limit: limit
    }
    param.merge!(sha: sha) if sha.present?

    param
  end

  def load_gitea_data 
    @gitea_data = $gitea_client.get_repos_commits_by_owner_repo(owner, repo, {query: request_params}) rescue nil
    raise Error, '获取提交列表失败！' unless @gitea_data.is_a?(Hash)
  end
end