BigW Consortium Gitlab

commit_statuses.rb 4.21 KB
Newer Older
1 2 3
require 'mime/types'

module API
4
  class CommitStatuses < Grape::API
5 6 7 8
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
    resource :projects, requirements: { id: %r{[^/]+} } do
9 10
      include PaginationParams

11 12
      before { authenticate! }

13 14 15 16 17 18 19 20 21
      desc "Get a commit's statuses" do
        success Entities::CommitStatus
      end
      params do
        requires :sha,   type: String, desc: 'The commit hash'
        optional :ref,   type: String, desc: 'The ref'
        optional :stage, type: String, desc: 'The stage'
        optional :name,  type: String, desc: 'The name'
        optional :all,   type: String, desc: 'Show all statuses, default: false'
22
        use :pagination
23
      end
24
      get ':id/repository/commits/:sha/statuses' do
25 26 27 28
        authorize!(:read_commit_status, user_project)

        not_found!('Commit') unless user_project.commit(params[:sha])

29 30
        pipelines = user_project.pipelines.where(sha: params[:sha])
        statuses = ::CommitStatus.where(pipeline: pipelines)
31
        statuses = statuses.latest unless to_boolean(params[:all])
32
        statuses = statuses.where(ref: params[:ref]) if params[:ref].present?
Kamil Trzcinski committed
33
        statuses = statuses.where(stage: params[:stage]) if params[:stage].present?
34 35 36 37
        statuses = statuses.where(name: params[:name]) if params[:name].present?
        present paginate(statuses), with: Entities::CommitStatus
      end

38 39 40 41 42 43
      desc 'Post status to a commit' do
        success Entities::CommitStatus
      end
      params do
        requires :sha,         type: String,  desc: 'The commit hash'
        requires :state,       type: String,  desc: 'The state of the status',
Douwe Maan committed
44
                               values: %w(pending running success failed canceled)
45 46 47 48 49
        optional :ref,         type: String,  desc: 'The ref'
        optional :target_url,  type: String,  desc: 'The target URL to associate with this status'
        optional :description, type: String,  desc: 'A short description of the status'
        optional :name,        type: String,  desc: 'A string label to differentiate this status from the status of other systems. Default: "default"'
        optional :context,     type: String,  desc: 'A string label to differentiate this status from the status of other systems. Default: "default"'
50
        optional :coverage,    type: Float,   desc: 'The total code coverage'
51
      end
52
      post ':id/statuses/:sha' do
Kamil Trzcinski committed
53
        authorize! :create_commit_status, user_project
54

55 56 57
        commit = @project.commit(params[:sha])
        not_found! 'Commit' unless commit

58
        # Since the CommitStatus is attached to Ci::Pipeline (in the future Pipeline)
59 60 61 62 63 64 65
        # We need to always have the pipeline object
        # To have a valid pipeline object that can be attached to specific MR
        # Other CI service needs to send `ref`
        # If we don't receive it, we will attach the CommitStatus to
        # the first found branch on that commit

        ref = params[:ref]
66 67 68 69
        ref ||= @project.repository.branch_names_contains(commit.sha).first
        not_found! 'References for commit' unless ref

        name = params[:name] || params[:context] || 'default'
Kamil Trzcinski committed
70

71
        pipeline = @project.ensure_pipeline(ref, commit.sha, current_user)
72

73
        status = GenericCommitStatus.running_or_pending.find_or_initialize_by(
74 75 76 77
          project: @project,
          pipeline: pipeline,
          name: name,
          ref: ref,
78
          user: current_user
79
        )
80

81 82 83 84
        optional_attributes =
          attributes_for_keys(%w[target_url description coverage])

        status.update(optional_attributes) if optional_attributes.any?
85 86
        render_validation_error!(status) if status.invalid?

87
        begin
88
          case params[:state]
89 90 91 92 93 94 95 96 97 98 99 100 101 102
          when 'pending'
            status.enqueue!
          when 'running'
            status.enqueue
            status.run!
          when 'success'
            status.success!
          when 'failed'
            status.drop!
          when 'canceled'
            status.cancel!
          else
            render_api_error!('invalid state', 400)
          end
103 104

          present status, with: Entities::CommitStatus
105 106
        rescue StateMachines::InvalidTransition => e
          render_api_error!(e.message, 400)
107 108 109 110 111
        end
      end
    end
  end
end