Commit d005242d authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge pull request #1803 from gitlabhq/api_merge_request

Api for merge request
parents fe6d122d 4281de36
## List merge requests
Get all MR for this project.
```
GET /projects/:id/merge_requests
```
Parameters:
+ `id` (required) - The ID or code name of a project
```json
[
{
"id":1,
"target_branch":"master",
"source_branch":"test1",
"project_id":3,
"title":"test1",
"closed":true,
"merged":false,
"author":{
"id":1,
"email":"admin@local.host",
"name":"Administrator",
"blocked":false,
"created_at":"2012-04-29T08:46:00Z"
},
"assignee":{
"id":1,
"email":"admin@local.host",
"name":"Administrator",
"blocked":false,
"created_at":"2012-04-29T08:46:00Z"
}
}
]
```
## Show MR
Show information about MR.
```
GET /projects/:id/merge_request/:merge_request_id
```
Parameters:
+ `id` (required) - The ID or code name of a project
+ `merge_request_id` (required) - The ID of MR
```json
{
"id":1,
"target_branch":"master",
"source_branch":"test1",
"project_id":3,
"title":"test1",
"closed":true,
"merged":false,
"author":{
"id":1,
"email":"admin@local.host",
"name":"Administrator",
"blocked":false,
"created_at":"2012-04-29T08:46:00Z"
},
"assignee":{
"id":1,
"email":"admin@local.host",
"name":"Administrator",
"blocked":false,
"created_at":"2012-04-29T08:46:00Z"
}
}
```
## Create MR
Create MR.
```
POST /projects/:id/merge_requests
```
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
```json
{
"id":1,
"target_branch":"master",
"source_branch":"test1",
"project_id":3,
"title":"test1",
"closed":true,
"merged":false,
"author":{
"id":1,
"email":"admin@local.host",
"name":"Administrator",
"blocked":false,
"created_at":"2012-04-29T08:46:00Z"
},
"assignee":{
"id":1,
"email":"admin@local.host",
"name":"Administrator",
"blocked":false,
"created_at":"2012-04-29T08:46:00Z"
}
}
```
## Update MR
Update MR. You can change branches, title, or even close the MR.
```
PUT /projects/:id/merge_request/:merge_request_id
```
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
```json
{
"id":1,
"target_branch":"master",
"source_branch":"test1",
"project_id":3,
"title":"test1",
"closed":true,
"merged":false,
"author":{
"id":1,
"email":"admin@local.host",
"name":"Administrator",
"blocked":false,
"created_at":"2012-04-29T08:46:00Z"
},
"assignee":{
"id":1,
"email":"admin@local.host",
"name":"Administrator",
"blocked":false,
"created_at":"2012-04-29T08:46:00Z"
}
}
```
## Post comment to MR
Post comment to MR
```
POST /projects/:id/merge_request/:merge_request_id/comments
```
Parameters:
+ `id` (required) - The ID or code name of a project
+ `merge_request_id` (required) - ID of MR
+ `note` (required) - Text of comment
Will return created note with status `201 Created` on success, or `404 Not found` on fail.
```json
{
"author":{
"id":1,
"email":"admin@local.host",
"name":"Administrator",
"blocked":false,
"created_at":"2012-04-29T08:46:00Z"
},
"note":"text1"
}
```
......@@ -18,5 +18,6 @@ module Gitlab
mount Issues
mount Milestones
mount Session
mount MergeRequests
end
end
......@@ -63,5 +63,15 @@ module Gitlab
class SSHKey < Grape::Entity
expose :id, :title, :key
end
class MergeRequest < Grape::Entity
expose :id, :target_branch, :source_branch, :project_id, :title, :closed, :merged
expose :author, :assignee, using: Entities::UserBasic
end
class Note < Grape::Entity
expose :author, using: Entities::UserBasic
expose :note
end
end
end
module Gitlab
# MergeRequest API
class MergeRequests < Grape::API
before { authenticate! }
resource :projects do
# List merge requests
#
# Parameters:
# id (required) - The ID or code name of a project
#
# Example:
# GET /projects/:id/merge_requests
#
get ":id/merge_requests" do
authorize! :read_merge_request, user_project
present paginate(user_project.merge_requests), with: Entities::MergeRequest
end
# Show MR
#
# Parameters:
# id (required) - The ID or code name of a project
# merge_request_id (required) - The ID of MR
#
# Example:
# GET /projects/:id/merge_request/:merge_request_id
#
get ":id/merge_request/:merge_request_id" do
merge_request = user_project.merge_requests.find(params[:merge_request_id])
authorize! :read_merge_request, merge_request
present merge_request, with: Entities::MergeRequest
end
# 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:
# POST /projects/:id/merge_requests
#
post ":id/merge_requests" do
authorize! :write_merge_request, user_project
attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title]
merge_request = user_project.merge_requests.new(attrs)
merge_request.author = current_user
if merge_request.save
merge_request.reload_code
present merge_request, with: Entities::MergeRequest
else
not_found!
end
end
# 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:
# PUT /projects/:id/merge_request/:merge_request_id
#
put ":id/merge_request/:merge_request_id" do
attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title, :closed]
merge_request = user_project.merge_requests.find(params[:merge_request_id])
authorize! :modify_merge_request, merge_request
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
end
# 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:
# POST /projects/:id/merge_request/:merge_request_id/comments
#
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
end
end
end
require "spec_helper"
describe Gitlab::API do
include ApiHelpers
let(:user) { Factory :user }
let!(:project) { Factory :project, owner: user }
let!(:merge_request) { Factory :merge_request, author: user, assignee: user, project: project, title: "Test" }
before { project.add_access(user, :read) }
describe "GET /projects/:id/merge_requests" do
context "when unauthenticated" do
it "should return authentication error" do
get api("/projects/#{project.code}/merge_requests")
response.status.should == 401
end
end
context "when authenticated" do
it "should return an array of merge_requests" do
get api("/projects/#{project.code}/merge_requests", user)
response.status.should == 200
json_response.should be_an Array
json_response.first['title'].should == merge_request.title
end
end
end
describe "GET /projects/:id/merge_request/:merge_request_id" do
it "should return merge_request" do
get api("/projects/#{project.code}/merge_request/#{merge_request.id}", user)
response.status.should == 200
json_response['title'].should == merge_request.title
end
end
describe "POST /projects/:id/merge_requests" do
it "should return merge_request" do
post api("/projects/#{project.code}/merge_requests", user),
title: 'Test merge_request', source_branch: "stable", target_branch: "master", author: user
response.status.should == 201
json_response['title'].should == 'Test merge_request'
end
end
describe "PUT /projects/:id/merge_request/:merge_request_id" do
it "should return merge_request" do
put api("/projects/#{project.code}/merge_request/#{merge_request.id}", user), title: "New title"
response.status.should == 200
json_response['title'].should == 'New title'
end
end
describe "POST /projects/:id/merge_request/:merge_request_id/comments" do
it "should return comment" do
post api("/projects/#{project.code}/merge_request/#{merge_request.id}/comments", user), note: "My comment"
response.status.should == 201
json_response['note'].should == 'My comment'
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment