Welcome to mirror list, hosted at ThFree Co, Russian Federation.

commit_statuses.rb « api « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 954b572c9b1fe267714abe2f442758da7cf9ae1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# frozen_string_literal: true

require 'mime/types'

module API
  class CommitStatuses < ::API::Base
    feature_category :continuous_integration
    urgency :low

    params do
      requires :id, types: [String, Integer], desc: 'The ID or URL-encoded path of the project'
    end
    resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
      include PaginationParams

      before { authenticate! }

      desc "Get a commit's statuses" do
        success code: 200, model: Entities::CommitStatus
        failure [
          { code: 401, message: 'Unauthorized' },
          { code: 403, message: 'Forbidden' },
          { code: 404, message: 'Not found' }
        ]
        is_array true
      end
      params do
        requires :sha,   type: String, desc: 'The commit hash', documentation: { example: '18f3e63d05582537db6d183d9d557be09e1f90c8' }
        optional :ref,   type: String, desc: 'The ref', documentation: { example: 'develop' }
        optional :stage, type: String, desc: 'The stage', documentation: { example: 'test' }
        optional :name,  type: String, desc: 'The name', documentation: { example: 'bundler:audit' }
        optional :all,   type: Boolean, desc: 'Show all statuses', documentation: { default: false }
        use :pagination
      end
      # rubocop: disable CodeReuse/ActiveRecord
      get ':id/repository/commits/:sha/statuses' do
        authorize!(:read_commit_status, user_project)

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

        pipelines = user_project.ci_pipelines.where(sha: params[:sha])
        statuses = ::CommitStatus.where(pipeline: pipelines)
        statuses = statuses.latest unless to_boolean(params[:all])
        statuses = statuses.where(ref: params[:ref]) if params[:ref].present?
        statuses = statuses.where(stage: params[:stage]) if params[:stage].present?
        statuses = statuses.where(name: params[:name]) if params[:name].present?
        present paginate(statuses), with: Entities::CommitStatus
      end
      # rubocop: enable CodeReuse/ActiveRecord

      desc 'Post status to a commit' do
        success code: 200, model: Entities::CommitStatus
        failure [
          { code: 400, message: 'Bad request' },
          { code: 401, message: 'Unauthorized' },
          { code: 403, message: 'Forbidden' },
          { code: 404, message: 'Not found' }
        ]
      end
      params do
        requires :sha,          type: String, desc: 'The commit hash',
                                documentation: { example: '18f3e63d05582537db6d183d9d557be09e1f90c8' }
        requires :state,        type: String, desc: 'The state of the status',
                                values: %w(pending running success failed canceled),
                                documentation: { example: 'pending' }
        optional :ref,          type: String, desc: 'The ref',
                                documentation: { example: 'develop' }
        optional :target_url,   type: String, desc: 'The target URL to associate with this status',
                                documentation: { example: 'https://gitlab.example.com/janedoe/gitlab-foss/builds/91' }
        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',
                                documentation: { example: 'coverage', default: 'default' }
        optional :context,      type: String, desc: 'A string label to differentiate this status from the status of other systems',
                                documentation: { example: 'coverage', default: 'default' }
        optional :coverage,     type: Float, desc: 'The total code coverage',
                                documentation: { example: 100.0 }
        optional :pipeline_id,  type: Integer, desc: 'An existing pipeline ID, when multiple pipelines on the same commit SHA have been triggered'
      end
      # rubocop: disable CodeReuse/ActiveRecord
      post ':id/statuses/:sha' do
        authorize! :create_commit_status, user_project

        not_found! 'Commit' unless commit

        # Since the CommitStatus is attached to ::Ci::Pipeline (in the future Pipeline)
        # 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

        pipeline = all_matching_pipelines.first

        ref = params[:ref]
        ref ||= pipeline&.ref
        ref ||= user_project.repository.branch_names_contains(commit.sha).first
        not_found! 'References for commit' unless ref

        name = params[:name] || params[:context] || 'default'

        pipeline ||= user_project.ci_pipelines.build(
          source: :external,
          sha: commit.sha,
          ref: ref,
          user: current_user,
          protected: user_project.protected_for?(ref))

        pipeline.ensure_project_iid!
        pipeline.save!

        authorize! :update_pipeline, pipeline

        status = GenericCommitStatus.running_or_pending.find_or_initialize_by(
          project: user_project,
          pipeline: pipeline,
          name: name,
          ref: ref,
          user: current_user,
          protected: user_project.protected_for?(ref)
        )

        updatable_optional_attributes = %w[target_url description coverage]
        status.assign_attributes(attributes_for_keys(updatable_optional_attributes))

        render_validation_error!(status) unless status.valid?

        response = ::Ci::Pipelines::AddJobService.new(pipeline).execute!(status) do |job|
          apply_job_state!(job)
        rescue ::StateMachines::InvalidTransition => e
          render_api_error!(e.message, 400)
        end

        render_validation_error!(response.payload[:job]) unless response.success?

        if pipeline.latest?
          MergeRequest
            .where(source_project: user_project, source_branch: ref)
            .update_all(head_pipeline_id: pipeline.id)
        end

        present response.payload[:job], with: Entities::CommitStatus
      end
      # rubocop: enable CodeReuse/ActiveRecord

      helpers do
        def commit
          strong_memoize(:commit) do
            user_project.commit(params[:sha])
          end
        end

        def all_matching_pipelines
          pipelines = user_project.ci_pipelines.newest_first(sha: commit.sha)
          pipelines = pipelines.for_ref(params[:ref]) if params[:ref]
          pipelines = pipelines.for_id(params[:pipeline_id]) if params[:pipeline_id]
          pipelines
        end

        def apply_job_state!(job)
          case params[:state]
          when 'pending'
            job.enqueue!
          when 'running'
            job.enqueue
            job.run!
          when 'success'
            job.success!
          when 'failed'
            job.drop!(:api_failure)
          when 'canceled'
            job.cancel!
          else
            render_api_error!('invalid state', 400)
          end
        end
      end
    end
  end
end