BigW Consortium Gitlab

commit_statuses.rb 2.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
require 'mime/types'

module API
  # Project commit statuses API
  class CommitStatus < Grape::API
    resource :projects do
      before { authenticate! }

      # Get a commit's statuses
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   sha (required) - The commit hash
      #   ref (optional) - The ref
      #   stage (optional) - The stage
      #   name (optional) - The name
      #   all (optional) - Show all statuses, default: false
      # Examples:
      #   GET /projects/:id/repository/commits/:sha/statuses
      get ':id/repository/commits/:sha/statuses' do
21 22 23
        authorize!(:read_commit_status, user_project)

        not_found!('Commit') unless user_project.commit(params[:sha])
24
        ci_commit = user_project.ci_commit(params[:sha])
25 26
        return [] unless ci_commit

27 28 29
        statuses = ci_commit.statuses
        statuses = statuses.latest unless parse_boolean(params[:all])
        statuses = statuses.where(ref: params[:ref]) if params[:ref].present?
Kamil Trzcinski committed
30
        statuses = statuses.where(stage: params[:stage]) if params[:stage].present?
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
        statuses = statuses.where(name: params[:name]) if params[:name].present?
        present paginate(statuses), with: Entities::CommitStatus
      end

      # Post status to commit
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   sha (required) - The commit hash
      #   ref (optional) - The ref
      #   state (required) - The state of the status. Can be: pending, running, success, error or failure
      #   target_url (optional) - The target URL to associate with this status
      #   description (optional) - A short description of the status
      #   name or context (optional) - A string label to differentiate this status from the status of other systems. Default: "default"
      # Examples:
46
      #   POST /projects/:id/statuses/:sha
47
      post ':id/statuses/:sha' do
Kamil Trzcinski committed
48
        authorize! :create_commit_status, user_project
49 50 51 52 53 54 55 56 57
        required_attributes! [:state]
        attrs = attributes_for_keys [:ref, :target_url, :description, :context, :name]
        commit = @project.commit(params[:sha])
        not_found! 'Commit' unless commit

        ci_commit = @project.ensure_ci_commit(commit.sha)

        name = params[:name] || params[:context]
        status = GenericCommitStatus.running_or_pending.find_by(commit: ci_commit, name: name, ref: params[:ref])
58
        status ||= GenericCommitStatus.new(project: @project, commit: ci_commit, user: current_user)
59 60 61
        status.update(attrs)

        case params[:state].to_s
62 63 64 65 66 67 68 69 70 71
        when 'running'
          status.run
        when 'success'
          status.success
        when 'failed'
          status.drop
        when 'canceled'
          status.cancel
        else
          status.status = params[:state].to_s
72 73 74 75 76 77 78 79 80 81 82
        end

        if status.save
          present status, with: Entities::CommitStatus
        else
          render_validation_error!(status)
        end
      end
    end
  end
end