API: project events

This commit is contained in:
Dmitriy Zaporozhets 2013-06-06 19:19:17 +03:00
parent 543506f3c5
commit 8df699a336
5 changed files with 115 additions and 1 deletions

View file

@ -75,7 +75,9 @@ class Event < ActiveRecord::Base
end end
def target_title def target_title
target.try :title if target && target.respond_to?(:title)
target.title
end
end end
def push? def push?

View file

@ -77,6 +77,7 @@ Parameters:
{ {
"id": 5, "id": 5,
"name": "gitlab", "name": "gitlab",
"name_with_namespace": "GitLab / gitlabhq",
"description": null, "description": null,
"default_branch": "api", "default_branch": "api",
"owner": { "owner": {
@ -99,6 +100,74 @@ Parameters:
} }
``` ```
### Get project events
Get a project events for specific project.
Sorted from newest to latest
```
GET /projects/:id/events
```
Parameters:
+ `id` (required) - The ID or NAME of a project
```json
[{
"title": null,
"project_id": 15,
"action_name": "closed",
"target_id": 830,
"target_type": "Issue",
"author_id": 1,
"data": null,
"target_title": "Public project search field"
}, {
"title": null,
"project_id": 15,
"action_name": "opened",
"target_id": null,
"target_type": null,
"author_id": 1,
"data": {
"before": "50d4420237a9de7be1304607147aec22e4a14af7",
"after": "c5feabde2d8cd023215af4d2ceeb7a64839fc428",
"ref": "refs/heads/master",
"user_id": 1,
"user_name": "Dmitriy Zaporozhets",
"repository": {
"name": "gitlabhq",
"url": "git@dev.gitlab.org:gitlab/gitlabhq.git",
"description": "GitLab: self hosted Git management software. \r\nDistributed under the MIT License.",
"homepage": "https://dev.gitlab.org/gitlab/gitlabhq"
},
"commits": [{
"id": "c5feabde2d8cd023215af4d2ceeb7a64839fc428",
"message": "Add simple search to projects in public area",
"timestamp": "2013-05-13T18:18:08+00:00",
"url": "https://dev.gitlab.org/gitlab/gitlabhq/commit/c5feabde2d8cd023215af4d2ceeb7a64839fc428",
"author": {
"name": "Dmitriy Zaporozhets",
"email": "dmitriy.zaporozhets@gmail.com"
}
}],
"total_commits_count": 1
},
"target_title": null
}, {
"title": null,
"project_id": 15,
"action_name": "closed",
"target_id": 840,
"target_type": "Issue",
"author_id": 1,
"data": null,
"target_title": "Finish & merge Code search PR"
}]
```
### Create project ### Create project

View file

@ -120,5 +120,11 @@ module API
expose :note expose :note
expose :author, using: Entities::UserBasic expose :author, using: Entities::UserBasic
end end
class Event < Grape::Entity
expose :title, :project_id, :action_name
expose :target_id, :target_type, :author_id
expose :data, :target_title
end
end end
end end

View file

@ -41,6 +41,20 @@ module API
present user_project, with: Entities::Project present user_project, with: Entities::Project
end end
# Get a single project events
#
# Parameters:
# id (required) - The ID of a project
# Example Request:
# GET /projects/:id
get ":id/events" do
limit = (params[:per_page] || 20).to_i
offset = (params[:page] || 0).to_i * limit
events = user_project.events.recent.limit(limit).offset(offset)
present events, with: Entities::Event
end
# Create new project # Create new project
# #
# Parameters: # Parameters:

View file

@ -173,6 +173,29 @@ describe API::API do
end end
end end
describe "GET /projects/:id/events" do
it "should return a project events" do
get api("/projects/#{project.id}/events", user)
response.status.should == 200
json_event = json_response.first
json_event['action_name'].should == 'joined'
json_event['project_id'].to_i.should == project.id
end
it "should return a 404 error if not found" do
get api("/projects/42/events", user)
response.status.should == 404
json_response['message'].should == '404 Not Found'
end
it "should return a 404 error if user is not a member" do
other_user = create(:user)
get api("/projects/#{project.id}/events", other_user)
response.status.should == 404
end
end
describe "GET /projects/:id/members" do describe "GET /projects/:id/members" do
it "should return project team members" do it "should return project team members" do
get api("/projects/#{project.id}/members", user) get api("/projects/#{project.id}/members", user)