merge_requests.rb 3.71 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
Valeriy Sizov's avatar
Valeriy Sizov committed
7 8 9 10 11 12 13
      
      # List merge requests
      # 
      # Parameters:
      #   id (required) - The ID or code name of a project
      #
      # Example:
14
      #   GET /projects/:id/merge_requests
Valeriy Sizov's avatar
Valeriy Sizov committed
15
      #
16
      get ":id/merge_requests" do
17 18
        authorize! :read_merge_request, user_project
        
Valeriy Sizov's avatar
Valeriy Sizov committed
19
        present paginate(user_project.merge_requests), with: Entities::MergeRequest
20 21
      end
      
Valeriy Sizov's avatar
Valeriy Sizov committed
22 23 24 25 26 27 28
      # Show MR
      # 
      # Parameters:
      #   id (required)               - The ID or code name of a project
      #   merge_request_id (required) - The ID of MR
      # 
      # 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 33 34 35 36
        merge_request = user_project.merge_requests.find(params[:merge_request_id])
        
        authorize! :read_merge_request, merge_request
        
        present merge_request, with: Entities::MergeRequest
37 38
      end

Valeriy Sizov's avatar
Valeriy Sizov committed
39 40 41 42 43 44 45 46 47 48 49
      # Create MR
      #
      # Parameters:
      #
      #   id (required)            - The ID or code name of a project
      #   source_branch (required) - The source branch
      #   target_branch (required) - The target branch
      #   assignee_id              - Assignee user ID
      #   title (required)         - Title of MR
      # 
      # 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
        attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title]
54
        merge_request = user_project.merge_requests.new(attrs)
Valeriy Sizov's avatar
Valeriy Sizov committed
55 56
        merge_request.author = current_user
        
Valeriy Sizov's avatar
Valeriy Sizov committed
57
        authorize! :write_merge_request, user_project
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 70 71 72 73 74 75 76 77
      # Update MR
      #
      # Parameters:
      #   id (required)               - The ID or code name of a project
      #   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 85
        authorize! :modify_merge_request, merge_request
        
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 98 99 100 101
      # Post comment to merge request
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
      #   merge_request_id (required) - ID of MR
      #   note (required) - Text of comment
      # Examples: 
102
      #   POST /projects/:id/merge_request/:merge_request_id/comments
Valeriy Sizov's avatar
Valeriy Sizov committed
103
      #
104 105 106 107 108 109 110 111 112 113 114
      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
        if note.save
          present note, with: Entities::Note
        else
          not_found!
        end
      end

115 116 117
    end
  end
end