BigW Consortium Gitlab

merge_requests.rb 9.62 KB
Newer Older
1
module API
Valeriy Sizov committed
2
  # MergeRequest API
3 4 5
  class MergeRequests < Grape::API
    before { authenticate! }

Hiroyuki Sato committed
6
    resource :projects do
7
      helpers do
8 9 10
        def handle_merge_request_errors!(errors)
          if errors[:project_access].any?
            error!(errors[:project_access], 422)
11 12
          elsif errors[:branch_conflict].any?
            error!(errors[:branch_conflict], 422)
13 14 15 16
          elsif errors[:validate_fork].any?
            error!(errors[:validate_fork], 422)
          elsif errors[:validate_branches].any?
            conflict!(errors[:validate_branches])
17
          end
18 19

          render_api_error!(errors, 400)
20 21 22
        end
      end

23
      # List merge requests
24
      #
25
      # Parameters:
26
      #   id (required) - The ID of a project
27
      #   iid (optional) - Return the project MR having the given `iid`
28
      #   state (optional) - Return requests "merged", "opened" or "closed"
29 30
      #   order_by (optional) - Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at`
      #   sort (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc`
31 32
      #
      # Example:
33
      #   GET /projects/:id/merge_requests
34 35
      #   GET /projects/:id/merge_requests?state=opened
      #   GET /projects/:id/merge_requests?state=closed
36
      #   GET /projects/:id/merge_requests?order_by=created_at
Marin Jankovski committed
37
      #   GET /projects/:id/merge_requests?order_by=updated_at
38 39
      #   GET /projects/:id/merge_requests?sort=desc
      #   GET /projects/:id/merge_requests?sort=asc
40
      #   GET /projects/:id/merge_requests?iid=42
41
      #
42
      get ":id/merge_requests" do
43
        authorize! :read_merge_request, user_project
44
        merge_requests = user_project.merge_requests
45

46 47 48 49
        unless params[:iid].nil?
          merge_requests = filter_by_iid(merge_requests, params[:iid])
        end

50 51 52 53 54 55 56
        merge_requests =
          case params["state"]
          when "opened" then merge_requests.opened
          when "closed" then merge_requests.closed
          when "merged" then merge_requests.merged
          else merge_requests
          end
57

58
        merge_requests = merge_requests.reorder(issuable_order_by => issuable_sort)
59
        present paginate(merge_requests), with: Entities::MergeRequest
60
      end
61

62
      # Show MR
63
      #
64
      # Parameters:
65
      #   id (required)               - The ID of a project
66
      #   merge_request_id (required) - The ID of MR
67
      #
68
      # Example:
69
      #   GET /projects/:id/merge_request/:merge_request_id
70
      #
71
      get ":id/merge_request/:merge_request_id" do
72
        merge_request = user_project.merge_requests.find(params[:merge_request_id])
73

74
        authorize! :read_merge_request, merge_request
75

76
        present merge_request, with: Entities::MergeRequest
77 78
      end

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      # Show MR changes
      #
      # Parameters:
      #   id (required)               - The ID of a project
      #   merge_request_id (required) - The ID of MR
      #
      # Example:
      #   GET /projects/:id/merge_request/:merge_request_id/changes
      #
      get ':id/merge_request/:merge_request_id/changes' do
        merge_request = user_project.merge_requests.
          find(params[:merge_request_id])
        authorize! :read_merge_request, merge_request
        present merge_request, with: Entities::MergeRequestChanges
      end

95 96 97 98
      # Create MR
      #
      # Parameters:
      #
99
      #   id (required)            - The ID of a project - this will be the source of the merge request
100 101
      #   source_branch (required) - The source branch
      #   target_branch (required) - The target branch
sue445 committed
102
      #   target_project_id        - The target project of the merge request defaults to the :id of the project
103 104
      #   assignee_id              - Assignee user ID
      #   title (required)         - Title of MR
105
      #   description              - Description of MR
106
      #   labels (optional)        - Labels for MR as a comma-separated list
107
      #
108
      # Example:
109
      #   POST /projects/:id/merge_requests
110
      #
111
      post ":id/merge_requests" do
112
        authorize! :create_merge_request, user_project
113 114
        required_attributes! [:source_branch, :target_branch, :title]
        attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title, :target_project_id, :description]
115 116

        # Validate label names in advance
Robert Schilling committed
117 118
        if (errors = validate_label_params(params)).any?
          render_api_error!({ labels: errors }, 400)
119 120
        end

121
        merge_request = ::MergeRequests::CreateService.new(user_project, current_user, attrs).execute
Izaak Alpert committed
122

123
        if merge_request.valid?
124 125 126 127 128
          # Find or create labels and attach to issue
          if params[:labels].present?
            merge_request.add_labels_by_names(params[:labels].split(","))
          end

129 130 131
          present merge_request, with: Entities::MergeRequest
        else
          handle_merge_request_errors! merge_request.errors
132
        end
133 134
      end

135 136 137
      # Update MR
      #
      # Parameters:
138
      #   id (required)               - The ID of a project
139 140 141 142
      #   merge_request_id (required) - ID of MR
      #   target_branch               - The target branch
      #   assignee_id                 - Assignee user ID
      #   title                       - Title of MR
143
      #   state_event                 - Status of MR. (close|reopen|merge)
144
      #   description                 - Description of MR
145
      #   labels (optional)           - Labels for a MR as a comma-separated list
146
      # Example:
147
      #   PUT /projects/:id/merge_request/:merge_request_id
148
      #
149
      put ":id/merge_request/:merge_request_id" do
150
        attrs = attributes_for_keys [:target_branch, :assignee_id, :title, :state_event, :description]
151
        merge_request = user_project.merge_requests.find(params[:merge_request_id])
152
        authorize! :update_merge_request, merge_request
153

154 155 156 157 158
        # Ensure source_branch is not specified
        if params[:source_branch].present?
          render_api_error!('Source branch cannot be changed', 400)
        end

159
        # Validate label names in advance
Robert Schilling committed
160
        if (errors = validate_label_params(params)).any?
161
          render_api_error!({ labels: errors }, 400)
162 163
        end

164
        merge_request = ::MergeRequests::UpdateService.new(user_project, current_user, attrs).execute(merge_request)
165

166
        if merge_request.valid?
167
          # Find or create labels and attach to issue
Robert Schilling committed
168 169
          unless params[:labels].nil?
            merge_request.remove_labels
170 171 172
            merge_request.add_labels_by_names(params[:labels].split(","))
          end

173 174 175
          present merge_request, with: Entities::MergeRequest
        else
          handle_merge_request_errors! merge_request.errors
176
        end
177 178
      end

179 180 181
      # Merge MR
      #
      # Parameters:
182 183
      #   id (required)                   - The ID of a project
      #   merge_request_id (required)     - ID of MR
184 185 186 187 188 189 190
      #   merge_commit_message (optional) - Custom merge commit message
      # Example:
      #   PUT /projects/:id/merge_request/:merge_request_id/merge
      #
      put ":id/merge_request/:merge_request_id/merge" do
        merge_request = user_project.merge_requests.find(params[:merge_request_id])

191 192
        allowed = ::Gitlab::GitAccess.new(current_user, user_project).
          can_push_to_branch?(merge_request.target_branch)
193

194
        if allowed
195 196 197 198
          if merge_request.unchecked?
            merge_request.check_if_can_be_merged
          end

199
          if merge_request.open? && !merge_request.work_in_progress?
200
            if merge_request.can_be_merged?
201 202 203 204 205
              commit_message = params[:merge_commit_message] || merge_request.merge_commit_message

              ::MergeRequests::MergeService.new(merge_request.target_project, current_user).
                execute(merge_request, commit_message)

Dmitriy Zaporozhets committed
206
              present merge_request, with: Entities::MergeRequest
207 208 209
            else
              render_api_error!('Branch cannot be merged', 405)
            end
210
          else
211
            # Merge request can not be merged
212
            # because it is already closed/merged or marked as WIP
213
            not_allowed!
214 215
          end
        else
216 217 218
          # Merge request can not be merged
          # because user dont have permissions to push into target branch
          unauthorized!
219 220 221 222
        end
      end


223 224 225
      # Get a merge request's comments
      #
      # Parameters:
226
      #   id (required)               - The ID of a project
227 228 229 230 231 232 233 234 235
      #   merge_request_id (required) - ID of MR
      # Examples:
      #   GET /projects/:id/merge_request/:merge_request_id/comments
      #
      get ":id/merge_request/:merge_request_id/comments" do
        merge_request = user_project.merge_requests.find(params[:merge_request_id])

        authorize! :read_merge_request, merge_request

236
        present paginate(merge_request.notes.fresh), with: Entities::MRNote
237 238
      end

239 240 241
      # Post comment to merge request
      #
      # Parameters:
242
      #   id (required)               - The ID of a project
243
      #   merge_request_id (required) - ID of MR
244
      #   note (required)             - Text of comment
245
      # Examples:
246
      #   POST /projects/:id/merge_request/:merge_request_id/comments
247
      #
248
      post ":id/merge_request/:merge_request_id/comments" do
249
        required_attributes! [:note]
250

251
        merge_request = user_project.merge_requests.find(params[:merge_request_id])
252 253 254 255 256 257 258 259 260 261

        authorize! :create_note, merge_request

        opts = {
          note: params[:note],
          noteable_type: 'MergeRequest',
          noteable_id: merge_request.id
        }

        note = ::Notes::CreateService.new(user_project, current_user, opts).execute
Valeriy Sizov committed
262

263 264 265
        if note.save
          present note, with: Entities::MRNote
        else
266
          render_api_error!("Failed to save note #{note.errors.messages}", 400)
267 268
        end
      end
269 270 271
    end
  end
end