class Api::V2::CommitsController < Api::V2::BaseController
  before_filter :ensure_permission, only: :update

  expose(:commit)
  expose(:project)
  expose(:ticket)
  expose(:commits) { find_commits }
  expose(:paginated_commits) { commits.order('expires_at DESC').page params[:page] }

  def index
    respond_with paginated_commits, meta: { total_pages: commit_pages_count }
  end

  def update
    commit.attempt_transition_to commit_params[:state]
    commit.save
    respond_with(commit)
  end

  private

  def ensure_permission
    render json: { message: "You are not authorized to do it." }, status: 401 if commit.author == current_user.person
  end

  def commit_params
    params.require(:commit).permit(:state)
  end

  def project_id
    params[:project_id].presence
  end

  def ticket_id
    params[:ticket_id].presence
  end

  def find_commits
    if project_id.present?
      project.commits
    elsif ticket_id.present?
      ticket.commits
    else
      Commit.all
    end
  end

  def commit_pages_count
    (commits.count / paginated_commits.default_per_page.to_f).ceil
  end
end

class Api::V2::CommitsController < Api::V2::BaseController
  before_filter :ensure_permission, only: :update

  expose(:commit)
  expose(:project)
  expose(:ticket)
  expose(:commits) { find_commits }
  expose(:paginated_commits) { commits.order('expires_at DESC').page params[:page] }

  def index
    respond_with paginated_commits, meta: { total_pages: commit_pages_count }
  end

  def update
    commit.attempt_transition_to commit_params[:state]
    commit.reviewer_id = params[:commit][:reviewer_id]
    commit.save
    respond_with(commit)
  end

  private

  def ensure_permission
    render json: { message: "You are not authorized to do it." }, status: 401 if commit.author == current_user.person
  end

  def commit_params
    params.require(:commit).permit(:state)
  end

  def project_id
    params[:project_id].presence
  end

  def ticket_id
    params[:ticket_id].presence
  end

  def find_commits
    if project_id.present?
      project.commits
    elsif ticket_id.present?
      ticket.commits
    else
      Commit.all
    end
  end

  def commit_pages_count
    (commits.count / paginated_commits.default_per_page.to_f).ceil
  end
end

Set reviewer when changing commit state
Set reviewer when changing commit state

[#97001584]
