Commit 76f5865d authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Remove hardcoded code from /api/v3/repos/:ns/:prj/branches

parent 044a1713
...@@ -1092,7 +1092,7 @@ module API ...@@ -1092,7 +1092,7 @@ module API
end end
module Github module Github
class GithubRepoOwner < Grape::Entity class User < Grape::Entity
expose :id expose :id
expose :login do |project| expose :login do |project|
project.namespace.name project.namespace.name
...@@ -1101,9 +1101,24 @@ module API ...@@ -1101,9 +1101,24 @@ module API
class Repository < Grape::Entity class Repository < Grape::Entity
expose :id expose :id
expose :owner, using: GithubRepoOwner, unless: -> (project, options) { project.group } expose :owner, using: User, unless: -> (project, options) { project.group }
expose :name expose :name
end end
class Commit < Grape::Entity
expose :id, as: :sha
expose :type do |model|
'commit'
end
end
class Branch < Grape::Entity
expose :name
expose :commit, using: Commit do |repo_branch, options|
options[:project].repository.commit(repo_branch.dereferenced_target)
end
end
end end
end end
end end
...@@ -19,21 +19,25 @@ module API ...@@ -19,21 +19,25 @@ module API
get ':username/repos' do get ':username/repos' do
projects = ProjectsFinder.new(current_user: current_user, params: project_finder_params).execute projects = ProjectsFinder.new(current_user: current_user, params: project_finder_params).execute
present projects, with: ::API::Entities::Github::Repository present paginate(projects), with: ::API::Entities::Github::Repository
end end
end end
params do
requires :namespace, type: String
requires :project, type: String
end
resource :repos do resource :repos do
get ':namespace/:repo/branches' do get ':namespace/:project/branches' do
present [ namespace = params[:namespace]
{ project = params[:project]
"name" => "feature", user_project = find_project!("#{namespace}/#{project}")
"commit" => {
"type" => 'commit', branches = ::Kaminari.paginate_array(user_project.repository.branches.sort_by(&:name))
"sha" => "6367e27cc0928789a860676f560ceda6b41b6215"
} present paginate(branches),
} with: ::API::Entities::Github::Branch,
] project: user_project
end end
get ':namespace/:repo/commits/:sha' do get ':namespace/:repo/commits/:sha' do
......
...@@ -2,15 +2,17 @@ require 'spec_helper' ...@@ -2,15 +2,17 @@ require 'spec_helper'
describe API::V3::GithubRepos do describe API::V3::GithubRepos do
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:project) { create(:project, namespace: user.namespace) } let!(:project) { create(:project, :repository, creator: user) }
describe 'GET /orgs/:id/repos' do before do
let(:current_user) { user } project.add_master(user)
end
describe 'GET /orgs/:id/repos' do
it 'returns an array of projects' do it 'returns an array of projects' do
group = create(:group) group = create(:group)
get v3_api("/orgs/#{group.path}/repos", current_user) get v3_api("/orgs/#{group.path}/repos", user)
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
...@@ -29,12 +31,18 @@ describe API::V3::GithubRepos do ...@@ -29,12 +31,18 @@ describe API::V3::GithubRepos do
end end
describe 'GET /repos/:namespace/:repo/branches' do describe 'GET /repos/:namespace/:repo/branches' do
it 'returns branches with expected format' do context 'when user namespace path' do
get v3_api("/repos/#{user.namespace.path}/foo/branches", user) it 'returns an array of project branches with github format' do
get v3_api("/repos/#{project.namespace.path}/#{project.path}/branches", user)
expect(response).to have_http_status(200)
expect(json_response).to be_an(Array)
expect(json_response.first.keys).to contain_exactly('name', 'commit')
expect(json_response.first['commit'].keys).to contain_exactly('sha', 'type')
end
end
expect(response).to have_http_status(200) xcontext 'when group path' do
expect(json_response).to be_an(Array)
expect(json_response).to eq('')
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment