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
end
module Github
class GithubRepoOwner < Grape::Entity
class User < Grape::Entity
expose :id
expose :login do |project|
project.namespace.name
......@@ -1101,9 +1101,24 @@ module API
class Repository < Grape::Entity
expose :id
expose :owner, using: GithubRepoOwner, unless: -> (project, options) { project.group }
expose :owner, using: User, unless: -> (project, options) { project.group }
expose :name
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
......@@ -19,21 +19,25 @@ module API
get ':username/repos' do
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
params do
requires :namespace, type: String
requires :project, type: String
end
resource :repos do
get ':namespace/:repo/branches' do
present [
{
"name" => "feature",
"commit" => {
"type" => 'commit',
"sha" => "6367e27cc0928789a860676f560ceda6b41b6215"
}
}
]
get ':namespace/:project/branches' do
namespace = params[:namespace]
project = params[:project]
user_project = find_project!("#{namespace}/#{project}")
branches = ::Kaminari.paginate_array(user_project.repository.branches.sort_by(&:name))
present paginate(branches),
with: ::API::Entities::Github::Branch,
project: user_project
end
get ':namespace/:repo/commits/:sha' do
......
......@@ -2,15 +2,17 @@ require 'spec_helper'
describe API::V3::GithubRepos do
let(:user) { create(:user) }
let!(:project) { create(:project, namespace: user.namespace) }
let!(:project) { create(:project, :repository, creator: user) }
describe 'GET /orgs/:id/repos' do
let(:current_user) { user }
before do
project.add_master(user)
end
describe 'GET /orgs/:id/repos' do
it 'returns an array of projects' do
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)
end
......@@ -29,12 +31,18 @@ describe API::V3::GithubRepos do
end
describe 'GET /repos/:namespace/:repo/branches' do
it 'returns branches with expected format' do
get v3_api("/repos/#{user.namespace.path}/foo/branches", user)
context 'when user namespace path' do
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)
expect(json_response).to be_an(Array)
expect(json_response).to eq('')
xcontext 'when group path' do
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