diff --git a/app/models/event.rb b/app/models/event.rb index 4b75087dc2a..793ed445947 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -75,7 +75,9 @@ class Event < ActiveRecord::Base end def target_title - target.try :title + if target && target.respond_to?(:title) + target.title + end end def push? diff --git a/doc/api/projects.md b/doc/api/projects.md index f5f0afb2bc0..323c0be63a4 100644 --- a/doc/api/projects.md +++ b/doc/api/projects.md @@ -77,6 +77,7 @@ Parameters: { "id": 5, "name": "gitlab", + "name_with_namespace": "GitLab / gitlabhq", "description": null, "default_branch": "api", "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 diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 8e815f2b25c..0d8cac5c8fd 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -120,5 +120,11 @@ module API expose :note expose :author, using: Entities::UserBasic 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 diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 7fcde3794f4..6dc051e4ba2 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -41,6 +41,20 @@ module API present user_project, with: Entities::Project 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 # # Parameters: diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index 031b1412b0c..31075149647 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -173,6 +173,29 @@ describe API::API do 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 it "should return project team members" do get api("/projects/#{project.id}/members", user)