Commit 59f428dca20228984e9f50c33b12f54eb15638e5

Authored by Alex Van't Hof
1 parent 79f0858a

Standardize commit diff api url, change blob api url, add get single commit

Use "/commits/:sha/diff" as opposed to "/commit/:sha", keeping in line
with existing api urls (e.g. "/projects/:id", etc.)

Fix 500 error resulting from a diff api call with an invalid commit hash

Move "/commits/:sha/blob" to "/blobs/:sha", leaving the old path for
backwards compatibility.

Add ability to get a single commit via "/commits/:sha"
doc/api/repositories.md
... ... @@ -239,12 +239,37 @@ Parameters:
239 239 ]
240 240 ```
241 241  
  242 +## Get a single commit
  243 +
  244 +Get a specific commit identified by the commit hash or name of a branch or tag.
  245 +
  246 +```
  247 +GET /projects/:id/repository/commits/:sha
  248 +```
  249 +
  250 +Parameters:
  251 +
  252 ++ `id` (required) - The ID of a project
  253 ++ `sha` (required) - The commit hash or name of a repository branch or tag
  254 +
  255 +```json
  256 +{
  257 + "id": "6104942438c14ec7bd21c6cd5bd995272b3faff6",
  258 + "short_id": "6104942438c",
  259 + "title": "Sanitize for network graph",
  260 + "author_name": "randx",
  261 + "author_email": "dmitriy.zaporozhets@gmail.com",
  262 + "created_at": "2012-09-20T09:06:12+03:00"
  263 +}
  264 +```
  265 +
  266 +
242 267 ## Get the diff of a commit
243 268  
244 269 Get the diff of a commit in a project.
245 270  
246 271 ```
247   -GET /projects/:id/repository/commit/:sha
  272 +GET /projects/:id/repository/commits/:sha/diff
248 273 ```
249 274  
250 275 Parameters:
... ... @@ -323,7 +348,7 @@ Parameters:
323 348 Get the raw file contents for a file.
324 349  
325 350 ```
326   -GET /projects/:id/repository/commits/:sha/blob
  351 +GET /projects/:id/repository/blobs/:sha
327 352 ```
328 353  
329 354 Parameters:
... ...
lib/api/repositories.rb
... ... @@ -106,13 +106,29 @@ module API
106 106 #
107 107 # Parameters:
108 108 # id (required) - The ID of a project
  109 + # sha (required) - The commit hash or name of a repository branch or tag
  110 + # Example Request:
  111 + # GET /projects/:id/repository/commits/:sha
  112 + get ":id/repository/commits/:sha" do
  113 + authorize! :download_code, user_project
  114 + sha = params[:sha]
  115 + commit = user_project.repository.commit(sha)
  116 + not_found! "Commit" unless commit
  117 + present commit, with: Entities::RepoCommit
  118 + end
  119 +
  120 + # Get the diff for a specific commit of a project
  121 + #
  122 + # Parameters:
  123 + # id (required) - The ID of a project
109 124 # sha (required) - The commit or branch name
110 125 # Example Request:
111   - # GET /projects/:id/repository/commit/:sha
112   - get ":id/repository/commit/:sha" do
  126 + # GET /projects/:id/repository/commits/:sha/diff
  127 + get ":id/repository/commits/:sha/diff" do
113 128 authorize! :download_code, user_project
114 129 sha = params[:sha]
115 130 result = CommitLoadContext.new(user_project, current_user, {id: sha}).execute
  131 + not_found! "Commit" unless result[:commit]
116 132 result[:commit].diffs
117 133 end
118 134  
... ... @@ -148,8 +164,8 @@ module API
148 164 # sha (required) - The commit or branch name
149 165 # filepath (required) - The path to the file to display
150 166 # Example Request:
151   - # GET /projects/:id/repository/commits/:sha/blob
152   - get ":id/repository/commits/:sha/blob" do
  167 + # GET /projects/:id/repository/blobs/:sha
  168 + get [ ":id/repository/blobs/:sha", ":id/repository/commits/:sha/blob" ] do
153 169 authorize! :download_code, user_project
154 170 required_attributes! [:filepath]
155 171  
... ...
spec/requests/api/repositories_spec.rb
... ... @@ -112,23 +112,51 @@ describe API::API do
112 112 end
113 113 end
114 114  
115   - describe "GET /projects:id/repository/commit/:sha" do
  115 + describe "GET /projects:id/repository/commits/:sha" do
  116 + context "authorized user" do
  117 + it "should return a commit by sha" do
  118 + get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}", user)
  119 + response.status.should == 200
  120 + json_response['id'].should == project.repository.commit.id
  121 + json_response['title'].should == project.repository.commit.title
  122 + end
  123 +
  124 + it "should return a 404 error if not found" do
  125 + get api("/projects/#{project.id}/repository/commits/invalid_sha", user)
  126 + response.status.should == 404
  127 + end
  128 + end
  129 +
  130 + context "unauthorized user" do
  131 + it "should not return the selected commit" do
  132 + get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}")
  133 + response.status.should == 401
  134 + end
  135 + end
  136 + end
  137 +
  138 + describe "GET /projects:id/repository/commits/:sha/diff" do
116 139 context "authorized user" do
117 140 before { project.team << [user2, :reporter] }
118 141  
119 142 it "should return the diff of the selected commit" do
120   - get api("/projects/#{project.id}/repository/commit/#{project.repository.commit.id}", user)
  143 + get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/diff", user)
121 144 response.status.should == 200
122 145  
123 146 json_response.should be_an Array
124 147 json_response.length.should >= 1
125 148 json_response.first.keys.should include "diff"
126 149 end
  150 +
  151 + it "should return a 404 error if invalid commit" do
  152 + get api("/projects/#{project.id}/repository/commits/invalid_sha/diff", user)
  153 + response.status.should == 404
  154 + end
127 155 end
128 156  
129 157 context "unauthorized user" do
130 158 it "should not return the diff of the selected commit" do
131   - get api("/projects/#{project.id}/repository/commit/#{project.repository.commit.id}")
  159 + get api("/projects/#{project.id}/repository/commits/#{project.repository.commit.id}/diff")
132 160 response.status.should == 401
133 161 end
134 162 end
... ... @@ -157,25 +185,33 @@ describe API::API do
157 185 end
158 186 end
159 187  
160   - describe "GET /projects/:id/repository/commits/:sha/blob" do
  188 + describe "GET /projects/:id/repository/blobs/:sha" do
161 189 it "should get the raw file contents" do
162   - get api("/projects/#{project.id}/repository/commits/master/blob?filepath=README.md", user)
  190 + get api("/projects/#{project.id}/repository/blobs/master?filepath=README.md", user)
163 191 response.status.should == 200
164 192 end
165 193  
166 194 it "should return 404 for invalid branch_name" do
167   - get api("/projects/#{project.id}/repository/commits/invalid_branch_name/blob?filepath=README.md", user)
  195 + get api("/projects/#{project.id}/repository/blobs/invalid_branch_name?filepath=README.md", user)
168 196 response.status.should == 404
169 197 end
170 198  
171 199 it "should return 404 for invalid file" do
172   - get api("/projects/#{project.id}/repository/commits/master/blob?filepath=README.invalid", user)
  200 + get api("/projects/#{project.id}/repository/blobs/master?filepath=README.invalid", user)
173 201 response.status.should == 404
174 202 end
175 203  
176 204 it "should return a 400 error if filepath is missing" do
177   - get api("/projects/#{project.id}/repository/commits/master/blob", user)
  205 + get api("/projects/#{project.id}/repository/blobs/master", user)
178 206 response.status.should == 400
179 207 end
180 208 end
  209 +
  210 + describe "GET /projects/:id/repository/commits/:sha/blob" do
  211 + it "should get the raw file contents" do
  212 + get api("/projects/#{project.id}/repository/commits/master/blob?filepath=README.md", user)
  213 + response.status.should == 200
  214 + end
  215 + end
  216 +
181 217 end
... ...