Commit c8e3d3192532839b7648301eb0849fd76da4f04b
Exists in
master
and in
4 other branches
Merge pull request #4892 from Aigeruth/feature/api-add-diff
Feature: get the diff of a commit via API.
Showing
3 changed files
with
64 additions
and
0 deletions
Show diff stats
doc/api/repositories.md
| @@ -239,6 +239,34 @@ Parameters: | @@ -239,6 +239,34 @@ Parameters: | ||
| 239 | ] | 239 | ] |
| 240 | ``` | 240 | ``` |
| 241 | 241 | ||
| 242 | +## Get the diff of a commit | ||
| 243 | + | ||
| 244 | +Get the diff of a commit in a project. | ||
| 245 | + | ||
| 246 | +``` | ||
| 247 | +GET /projects/:id/repository/commit/:sha | ||
| 248 | +``` | ||
| 249 | + | ||
| 250 | +Parameters: | ||
| 251 | + | ||
| 252 | ++ `id` (required) - The ID of a project | ||
| 253 | ++ `sha` (required) - The name of a repository branch or tag or if not given the default branch | ||
| 254 | + | ||
| 255 | +```json | ||
| 256 | +[ | ||
| 257 | + { | ||
| 258 | + "diff": "--- a/doc/update/5.4-to-6.0.md\n+++ b/doc/update/5.4-to-6.0.md\n@@ -71,6 +71,8 @@\n sudo -u git -H bundle exec rake migrate_keys RAILS_ENV=production\n sudo -u git -H bundle exec rake migrate_inline_notes RAILS_ENV=production\n \n+sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production\n+\n ```\n \n ### 6. Update config files", | ||
| 259 | + "new_path": "doc/update/5.4-to-6.0.md", | ||
| 260 | + "old_path": "doc/update/5.4-to-6.0.md", | ||
| 261 | + "a_mode": null, | ||
| 262 | + "b_mode": "100644", | ||
| 263 | + "new_file": false, | ||
| 264 | + "renamed_file": false, | ||
| 265 | + "deleted_file": false | ||
| 266 | + } | ||
| 267 | +] | ||
| 268 | +``` | ||
| 269 | + | ||
| 242 | ## List repository tree | 270 | ## List repository tree |
| 243 | 271 | ||
| 244 | Get a list of repository files and directories in a project. | 272 | Get a list of repository files and directories in a project. |
lib/api/repositories.rb
| @@ -102,6 +102,20 @@ module API | @@ -102,6 +102,20 @@ module API | ||
| 102 | present commits, with: Entities::RepoCommit | 102 | present commits, with: Entities::RepoCommit |
| 103 | end | 103 | end |
| 104 | 104 | ||
| 105 | + # Get a specific commit of a project | ||
| 106 | + # | ||
| 107 | + # Parameters: | ||
| 108 | + # id (required) - The ID of a project | ||
| 109 | + # sha (required) - The commit or branch name | ||
| 110 | + # Example Request: | ||
| 111 | + # GET /projects/:id/repository/commit/:sha | ||
| 112 | + get ":id/repository/commit/:sha" do | ||
| 113 | + authorize! :download_code, user_project | ||
| 114 | + sha = params[:sha] | ||
| 115 | + result = CommitLoadContext.new(user_project, current_user, {id: sha}).execute | ||
| 116 | + result[:commit].diffs | ||
| 117 | + end | ||
| 118 | + | ||
| 105 | # Get a project repository tree | 119 | # Get a project repository tree |
| 106 | # | 120 | # |
| 107 | # Parameters: | 121 | # Parameters: |
spec/requests/api/repositories_spec.rb
| @@ -112,6 +112,28 @@ describe API::API do | @@ -112,6 +112,28 @@ describe API::API do | ||
| 112 | end | 112 | end |
| 113 | end | 113 | end |
| 114 | 114 | ||
| 115 | + describe "GET /projects:id/repository/commit/:sha" do | ||
| 116 | + context "authorized user" do | ||
| 117 | + before { project.team << [user2, :reporter] } | ||
| 118 | + | ||
| 119 | + it "should return the diff of the selected commit" do | ||
| 120 | + get api("/projects/#{project.id}/repository/commit/#{project.repository.commit.id}", user) | ||
| 121 | + response.status.should == 200 | ||
| 122 | + | ||
| 123 | + json_response.should be_an Array | ||
| 124 | + json_response.length.should >= 1 | ||
| 125 | + json_response.first.keys.should include "diff" | ||
| 126 | + end | ||
| 127 | + end | ||
| 128 | + | ||
| 129 | + context "unauthorized user" do | ||
| 130 | + it "should not return the diff of the selected commit" do | ||
| 131 | + get api("/projects/#{project.id}/repository/commit/#{project.repository.commit.id}") | ||
| 132 | + response.status.should == 401 | ||
| 133 | + end | ||
| 134 | + end | ||
| 135 | + end | ||
| 136 | + | ||
| 115 | describe "GET /projects/:id/repository/tree" do | 137 | describe "GET /projects/:id/repository/tree" do |
| 116 | context "authorized user" do | 138 | context "authorized user" do |
| 117 | before { project.team << [user2, :reporter] } | 139 | before { project.team << [user2, :reporter] } |