Commit da6f4f06c7b8ad32ae014fd232037ec05e8e8a80

Authored by Dmitriy Zaporozhets
1 parent 470f9064

API: implement retrieve of repository tree

doc/api/repositories.md
... ... @@ -239,6 +239,56 @@ Parameters:
239 239 ]
240 240 ```
241 241  
  242 +## List repository tree
  243 +
  244 +Get a list of repository files and directories in a project.
  245 +
  246 +```
  247 +GET /projects/:id/repository/tree
  248 +```
  249 +
  250 +Parameters:
  251 +
  252 ++ `id` (required) - The ID of a project
  253 ++ `path` (optional) - The path inside repository. Used to get contend of subdirectories
  254 ++ `ref_name` (optional) - The name of a repository branch or tag or if not given the default branch
  255 +
  256 +```json
  257 +
  258 +[{
  259 + "name": "assets",
  260 + "type": "tree",
  261 + "mode": "040000",
  262 + "id": "6229c43a7e16fcc7e95f923f8ddadb8281d9c6c6"
  263 +}, {
  264 + "name": "contexts",
  265 + "type": "tree",
  266 + "mode": "040000",
  267 + "id": "faf1cdf33feadc7973118ca42d35f1e62977e91f"
  268 +}, {
  269 + "name": "controllers",
  270 + "type": "tree",
  271 + "mode": "040000",
  272 + "id": "95633e8d258bf3dfba3a5268fb8440d263218d74"
  273 +}, {
  274 + "name": "Rakefile",
  275 + "type": "blob",
  276 + "mode": "100644",
  277 + "id": "35b2f05cbb4566b71b34554cf184a9d0bd9d46d6"
  278 +}, {
  279 + "name": "VERSION",
  280 + "type": "blob",
  281 + "mode": "100644",
  282 + "id": "803e4a4f3727286c3093c63870c2b6524d30ec4f"
  283 +}, {
  284 + "name": "config.ru",
  285 + "type": "blob",
  286 + "mode": "100644",
  287 + "id": "dfd2d862237323aa599be31b473d70a8a817943b"
  288 +}]
  289 +
  290 +```
  291 +
242 292  
243 293 ## Raw blob content
244 294  
... ...
lib/api/repositories.rb
... ... @@ -102,6 +102,31 @@ module API
102 102 present commits, with: Entities::RepoCommit
103 103 end
104 104  
  105 + # Get a project repository tree
  106 + #
  107 + # Parameters:
  108 + # id (required) - The ID of a project
  109 + # ref_name (optional) - The name of a repository branch or tag, if not given the default branch is used
  110 + # Example Request:
  111 + # GET /projects/:id/repository/tree
  112 + get ":id/repository/tree" do
  113 + authorize! :download_code, user_project
  114 +
  115 + ref = params[:ref_name] || user_project.try(:default_branch) || 'master'
  116 + path = params[:path] || nil
  117 +
  118 + commit = user_project.repository.commit(ref)
  119 + tree = Tree.new(user_project.repository, commit.id, ref, path)
  120 +
  121 + trees = []
  122 +
  123 + %w(trees blobs submodules).each do |type|
  124 + trees += tree.send(type).map { |t| { name: t.name, type: type.singularize, mode: t.mode, id: t.id } }
  125 + end
  126 +
  127 + trees
  128 + end
  129 +
105 130 # Get a raw file contents
106 131 #
107 132 # Parameters:
... ...
spec/requests/api/repositories_spec.rb
... ... @@ -111,6 +111,29 @@ describe API::API do
111 111 end
112 112 end
113 113  
  114 + describe "GET /projects/:id/repository/tree" do
  115 + context "authorized user" do
  116 + before { project.team << [user2, :reporter] }
  117 +
  118 + it "should return project commits" do
  119 + get api("/projects/#{project.id}/repository/tree", user)
  120 + response.status.should == 200
  121 +
  122 + json_response.should be_an Array
  123 + json_response.first['name'].should == 'app'
  124 + json_response.first['type'].should == 'tree'
  125 + json_response.first['mode'].should == '040000'
  126 + end
  127 + end
  128 +
  129 + context "unauthorized user" do
  130 + it "should not return project commits" do
  131 + get api("/projects/#{project.id}/repository/tree")
  132 + response.status.should == 401
  133 + end
  134 + end
  135 + end
  136 +
114 137 describe "GET /projects/:id/repository/commits/:sha/blob" do
115 138 it "should get the raw file contents" do
116 139 get api("/projects/#{project.id}/repository/commits/master/blob?filepath=README.md", user)
... ...