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

    resource :projects do
7

Valeriy Sizov's avatar
Valeriy Sizov committed
8
      # List merge requests
9
      #
Valeriy Sizov's avatar
Valeriy Sizov committed
10
      # Parameters:
11
      #   id (required) - The ID of a project
Valeriy Sizov's avatar
Valeriy Sizov committed
12 13
      #
      # Example:
14
      #   GET /projects/:id/merge_requests
Valeriy Sizov's avatar
Valeriy Sizov committed
15
      #
16
      get ":id/merge_requests" do
17
        authorize! :read_merge_request, user_project
18

Valeriy Sizov's avatar
Valeriy Sizov committed
19
        present paginate(user_project.merge_requests), with: Entities::MergeRequest
20
      end
21

Valeriy Sizov's avatar
Valeriy Sizov committed
22
      # Show MR
23
      #
Valeriy Sizov's avatar
Valeriy Sizov committed
24
      # Parameters:
25
      #   id (required)               - The ID of a project
Valeriy Sizov's avatar
Valeriy Sizov committed
26
      #   merge_request_id (required) - The ID of MR
27
      #
Valeriy Sizov's avatar
Valeriy Sizov committed
28
      # Example:
29
      #   GET /projects/:id/merge_request/:merge_request_id
Valeriy Sizov's avatar
Valeriy Sizov committed
30
      #
31
      get ":id/merge_request/:merge_request_id" do
32
        merge_request = user_project.merge_requests.find(params[:merge_request_id])
33

34
        authorize! :read_merge_request, merge_request
35

36
        present merge_request, with: Entities::MergeRequest
37 38
      end

Valeriy Sizov's avatar
Valeriy Sizov committed
39 40 41 42
      # Create MR
      #
      # Parameters:
      #
43
      #   id (required)            - The ID of a project
Valeriy Sizov's avatar
Valeriy Sizov committed
44 45 46 47
      #   source_branch (required) - The source branch
      #   target_branch (required) - The target branch
      #   assignee_id              - Assignee user ID
      #   title (required)         - Title of MR
48
      #
Valeriy Sizov's avatar
Valeriy Sizov committed
49
      # Example:
50
      #   POST /projects/:id/merge_requests
Valeriy Sizov's avatar
Valeriy Sizov committed
51
      #
52
      post ":id/merge_requests" do
Valeriy Sizov's avatar
Valeriy Sizov committed
53
        authorize! :write_merge_request, user_project
54

Valeriy Sizov's avatar
Valeriy Sizov committed
55
        attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title]
56
        merge_request = user_project.merge_requests.new(attrs)
Valeriy Sizov's avatar
Valeriy Sizov committed
57
        merge_request.author = current_user
58

Valeriy Sizov's avatar
Valeriy Sizov committed
59 60 61 62 63 64
        if merge_request.save
          merge_request.reload_code
          present merge_request, with: Entities::MergeRequest
        else
          not_found!
        end
65 66
      end

Valeriy Sizov's avatar
Valeriy Sizov committed
67 68 69
      # Update MR
      #
      # Parameters:
70
      #   id (required)               - The ID of a project
Valeriy Sizov's avatar
Valeriy Sizov committed
71 72 73 74 75 76 77
      #   merge_request_id (required) - ID of MR
      #   source_branch               - The source branch
      #   target_branch               - The target branch
      #   assignee_id                 - Assignee user ID
      #   title                       - Title of MR
      #   closed                      - Status of MR. true - closed
      # Example:
78
      #   PUT /projects/:id/merge_request/:merge_request_id
Valeriy Sizov's avatar
Valeriy Sizov committed
79
      #
80
      put ":id/merge_request/:merge_request_id" do
81
        attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title, :closed]
82
        merge_request = user_project.merge_requests.find(params[:merge_request_id])
83

84
        authorize! :modify_merge_request, merge_request
85

86 87 88 89 90 91 92
        if merge_request.update_attributes attrs
          merge_request.reload_code
          merge_request.mark_as_unchecked
          present merge_request, with: Entities::MergeRequest
        else
          not_found!
        end
93 94
      end

Valeriy Sizov's avatar
Valeriy Sizov committed
95 96 97
      # Post comment to merge request
      #
      # Parameters:
98
      #   id (required) - The ID of a project
Valeriy Sizov's avatar
Valeriy Sizov committed
99 100
      #   merge_request_id (required) - ID of MR
      #   note (required) - Text of comment
101
      # Examples:
102
      #   POST /projects/:id/merge_request/:merge_request_id/comments
Valeriy Sizov's avatar
Valeriy Sizov committed
103
      #
104 105 106 107
      post ":id/merge_request/:merge_request_id/comments" do
        merge_request = user_project.merge_requests.find(params[:merge_request_id])
        note = merge_request.notes.new(note: params[:note], project_id: user_project.id)
        note.author = current_user
Valeriy Sizov's avatar
Valeriy Sizov committed
108

109
        if note.save
110
          present note, with: Entities::MRNote
111 112 113 114 115
        else
          not_found!
        end
      end

116 117 118
    end
  end
end