Commit f441436e53377f207657ac0e0e518e5ee2b33a6c

Authored by Dmitriy Zaporozhets
1 parent ef933ae6

Add compare branches endpoint to API

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
doc/api/repositories.md
... ... @@ -131,3 +131,69 @@ 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 + "commit": {
  157 + "id": "72e10ef47e770a95439255b2c49de722e8782106",
  158 + "short_id": "72e10ef47e7",
  159 + "title": "Add NEWFILE",
  160 + "author_name": "Dmitriy Zaporozhets",
  161 + "author_email": "dmitriy.zaporozhets@gmail.com",
  162 + "created_at": "2014-05-26T16:03:54+03:00"
  163 + },
  164 + "commits": [{
  165 + "id": "0b4bc9a49b562e85de7cc9e834518ea6828729b9",
  166 + "short_id": "0b4bc9a49b5",
  167 + "title": "Feature added",
  168 + "author_name": "Dmitriy Zaporozhets",
  169 + "author_email": "dmitriy.zaporozhets@gmail.com",
  170 + "created_at": "2014-02-27T10:26:01+02:00"
  171 + }, {
  172 + "id": "72e10ef47e770a95439255b2c49de722e8782106",
  173 + "short_id": "72e10ef47e7",
  174 + "title": "Add NEWFILE",
  175 + "author_name": "Dmitriy Zaporozhets",
  176 + "author_email": "dmitriy.zaporozhets@gmail.com",
  177 + "created_at": "2014-05-26T16:03:54+03:00"
  178 + }],
  179 + "diffs": [{
  180 + "old_path": "NEWFILE",
  181 + "new_path": "NEWFILE",
  182 + "a_mode": null,
  183 + "b_mode": null,
  184 + "diff": "--- /dev/null\n+++ b/NEWFILE\n@@ -0,0 +1 @@\n+This is NEWFILE content\n\\ No newline at end of file",
  185 + "new_file": true,
  186 + "renamed_file": false,
  187 + "deleted_file": false
  188 + }, {
  189 + "old_path": "files/ruby/feature.rb",
  190 + "new_path": "files/ruby/feature.rb",
  191 + "a_mode": null,
  192 + "b_mode": null,
  193 + "diff": "--- /dev/null\n+++ b/files/ruby/feature.rb\n@@ -0,0 +1,5 @@\n+class Feature\n+ def foo\n+ puts 'bar'\n+ end\n+end",
  194 + "new_file": true,
  195 + "renamed_file": false,
  196 + "deleted_file": false
  197 + }]
  198 +}
  199 +```
... ...
lib/api/entities.rb
... ... @@ -194,5 +194,22 @@ 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 + Commit.new compare.commit
  206 + end
  207 + expose :commits, using: Entities::RepoCommit do |compare, options|
  208 + Commit.decorate compare.commits
  209 + end
  210 + expose :diffs, using: Entities::RepoDiff do |compare, options|
  211 + compare.diffs
  212 + end
  213 + end
197 214 end
198 215 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 + compare = Gitlab::Git::Compare.new(user_project.repository.raw_repository, params[:from], params[:to], MergeRequestDiff::COMMITS_SAFE_SIZE)
  134 +
  135 + present compare, with: Entities::Compare
  136 + end
121 137 end
122 138 end
123 139 end
... ...