Commit 3bdf0e2921c4cac46084834899302b25858e6bde

Authored by Dmitriy Zaporozhets
2 parents 3553e36d c7e00aca

Merge branch 'compare-api' into 'master'

Compare api

Fixes #1165
doc/api/repositories.md
... ... @@ -131,3 +131,56 @@ GET /projects/:id/repository/archive
131 131 Parameters:
132 132 + `id` (required) - The ID of a project
133 133 + `sha` (optional) - The commit sha to download defaults to the tip of the default branch
  134 +
  135 +
  136 +## Compare branches, tags or commits
  137 +
  138 +```
  139 +GET /projects/:id/repository/compare
  140 +```
  141 +
  142 +Parameters:
  143 ++ `id` (required) - The ID of a project
  144 ++ `from` (required) - the commit sha or branch name
  145 ++ `to` (required) - the commit sha or branch name
  146 +
  147 +
  148 +```
  149 +GET /projects/:id/repository/compare?from=master&to=feature
  150 +```
  151 +
  152 +Response:
  153 +
  154 +```json
  155 +
  156 +{
  157 + "commit": {
  158 + "id": "12d65c8dd2b2676fa3ac47d955accc085a37a9c1",
  159 + "short_id": "12d65c8dd2b",
  160 + "title": "JS fix",
  161 + "author_name": "Dmitriy Zaporozhets",
  162 + "author_email": "dmitriy.zaporozhets@gmail.com",
  163 + "created_at": "2014-02-27T10:27:00+02:00"
  164 + },
  165 + "commits": [{
  166 + "id": "12d65c8dd2b2676fa3ac47d955accc085a37a9c1",
  167 + "short_id": "12d65c8dd2b",
  168 + "title": "JS fix",
  169 + "author_name": "Dmitriy Zaporozhets",
  170 + "author_email": "dmitriy.zaporozhets@gmail.com",
  171 + "created_at": "2014-02-27T10:27:00+02:00"
  172 + }],
  173 + "diffs": [{
  174 + "old_path": "files/js/application.js",
  175 + "new_path": "files/js/application.js",
  176 + "a_mode": null,
  177 + "b_mode": "100644",
  178 + "diff": "--- a/files/js/application.js\n+++ b/files/js/application.js\n@@ -24,8 +24,10 @@\n //= require g.raphael-min\n //= require g.bar-min\n //= require branch-graph\n-//= require highlightjs.min\n-//= require ace/ace\n //= require_tree .\n //= require d3\n //= require underscore\n+\n+function fix() { \n+ alert(\"Fixed\")\n+}",
  179 + "new_file": false,
  180 + "renamed_file": false,
  181 + "deleted_file": false
  182 + }],
  183 + "compare_timeout": false,
  184 + "compare_same_ref": false
  185 +}
  186 +```
... ...
lib/api/entities.rb
... ... @@ -194,5 +194,30 @@ module API
194 194 class Label < Grape::Entity
195 195 expose :name
196 196 end
  197 +
  198 + class RepoDiff < Grape::Entity
  199 + expose :old_path, :new_path, :a_mode, :b_mode, :diff
  200 + expose :new_file, :renamed_file, :deleted_file
  201 + end
  202 +
  203 + class Compare < Grape::Entity
  204 + expose :commit, using: Entities::RepoCommit do |compare, options|
  205 + if compare.commit
  206 + Commit.new compare.commit
  207 + end
  208 + end
  209 + expose :commits, using: Entities::RepoCommit do |compare, options|
  210 + Commit.decorate compare.commits
  211 + end
  212 + expose :diffs, using: Entities::RepoDiff do |compare, options|
  213 + compare.diffs
  214 + end
  215 +
  216 + expose :compare_timeout do |compare, options|
  217 + compare.timeout
  218 + end
  219 +
  220 + expose :same, as: :compare_same_ref
  221 + end
197 222 end
198 223 end
... ...
lib/api/repositories.rb
... ... @@ -15,6 +15,7 @@ module API
15 15 not_found!
16 16 end
17 17 end
  18 +
18 19 # Get a project repository tags
19 20 #
20 21 # Parameters:
... ... @@ -118,6 +119,21 @@ module API
118 119 not_found!
119 120 end
120 121 end
  122 +
  123 + # Compare two branches, tags or commits
  124 + #
  125 + # Parameters:
  126 + # id (required) - The ID of a project
  127 + # from (required) - the commit sha or branch name
  128 + # to (required) - the commit sha or branch name
  129 + # Example Request:
  130 + # GET /projects/:id/repository/compare?from=master&to=feature
  131 + get ':id/repository/compare' do
  132 + authorize! :download_code, user_project
  133 + required_attributes! [:from, :to]
  134 + compare = Gitlab::Git::Compare.new(user_project.repository.raw_repository, params[:from], params[:to], MergeRequestDiff::COMMITS_SAFE_SIZE)
  135 + present compare, with: Entities::Compare
  136 + end
121 137 end
122 138 end
123 139 end
... ...
spec/requests/api/repositories_spec.rb
... ... @@ -112,4 +112,43 @@ describe API::API, api: true do
112 112 response.status.should == 404
113 113 end
114 114 end
  115 +
  116 + describe 'GET /GET /projects/:id/repository/compare' do
  117 + it "should compare branches" do
  118 + get api("/projects/#{project.id}/repository/compare", user), from: 'master', to: 'simple_merge_request'
  119 + response.status.should == 200
  120 + json_response['commits'].should be_present
  121 + json_response['diffs'].should be_present
  122 + end
  123 +
  124 + it "should compare tags" do
  125 + get api("/projects/#{project.id}/repository/compare", user), from: 'v1.0.1', to: 'v1.0.2'
  126 + response.status.should == 200
  127 + json_response['commits'].should be_present
  128 + json_response['diffs'].should be_present
  129 + end
  130 +
  131 + it "should compare commits" do
  132 + get api("/projects/#{project.id}/repository/compare", user), from: 'b1e6a9dbf1c85', to: '1e689bfba395'
  133 + response.status.should == 200
  134 + json_response['commits'].should be_empty
  135 + json_response['diffs'].should be_empty
  136 + json_response['compare_same_ref'].should be_false
  137 + end
  138 +
  139 + it "should compare commits in reverse order" do
  140 + get api("/projects/#{project.id}/repository/compare", user), from: '1e689bfba395', to: 'b1e6a9dbf1c85'
  141 + response.status.should == 200
  142 + json_response['commits'].should be_present
  143 + json_response['diffs'].should be_present
  144 + end
  145 +
  146 + it "should compare same refs" do
  147 + get api("/projects/#{project.id}/repository/compare", user), from: 'master', to: 'master'
  148 + response.status.should == 200
  149 + json_response['commits'].should be_empty
  150 + json_response['diffs'].should be_empty
  151 + json_response['compare_same_ref'].should be_true
  152 + end
  153 + end
115 154 end
... ...