BigW Consortium Gitlab

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

    resource :projects do
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
15
      #
16
      get ":id/merge_requests" do
17 18
        authorize! :read_merge_request, user_project
        
19
        present paginate(user_project.merge_requests), with: Entities::MergeRequest
20 21
      end
      
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
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

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
51
      #
52
      post ":id/merge_requests" do
Valeriy Sizov committed
53 54
        authorize! :write_merge_request, user_project
        
55
        attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title]
56
        merge_request = user_project.merge_requests.new(attrs)
57 58 59 60 61 62 63 64
        merge_request.author = current_user
        
        if merge_request.save
          merge_request.reload_code
          present merge_request, with: Entities::MergeRequest
        else
          not_found!
        end
65 66
      end

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
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

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
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 committed
108

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

116 117 118
    end
  end
end