Commit ec2f5263 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Remove hardcoded code from /api/v3/repos/:ns/:prj/commits/:sha [wip]

parent 2fc55b2e
...@@ -1102,17 +1102,55 @@ module API ...@@ -1102,17 +1102,55 @@ module API
expose :name expose :name
end end
class Commit < Grape::Entity class BranchCommit < Grape::Entity
expose :id, as: :sha expose :id, as: :sha
expose :type do |model| expose :type do |model|
'commit' 'commit'
end end
end end
class CommitUser < Grape::Entity
expose :path, as: :login
end
class RepoCommit < Grape::Entity
expose :id, as: :sha
expose :author do |commit|
{
"login" => "oswaldo",
}
end
expose :committer do |commit|
{
"login" => "oswaldo",
}
end
expose :commit do |commit|
# TODO: export to entity
{
author: {
name: commit.author_name,
email: commit.author_email,
date: "2011-04-14T16:00:49Z"
},
committer: {
name: commit.committer_name,
email: commit.committer_email,
date: "2011-04-14T16:00:49Z"#commit.committed_date.iso8601(3)
},
message: commit.safe_message
}
end
expose :parents do |commit|
# TODO: export to entity
commit.parent_ids.map { |id| { sha: id } }
end
end
class Branch < Grape::Entity class Branch < Grape::Entity
expose :name expose :name
expose :commit, using: Commit do |repo_branch, options| expose :commit, using: BranchCommit do |repo_branch, options|
options[:project].repository.commit(repo_branch.dereferenced_target) options[:project].repository.commit(repo_branch.dereferenced_target)
end end
end end
......
...@@ -31,6 +31,12 @@ module API ...@@ -31,6 +31,12 @@ module API
project = params[:project] project = params[:project]
user_project = find_project!("#{namespace}/#{project}") user_project = find_project!("#{namespace}/#{project}")
branches = ::API::Entities::Github::Branch
.represent(user_project.repository.branches.sort_by(&:name), project: user_project)
.as_json
Rails.logger.info("BRANCHES: #{branches}")
branches = ::Kaminari.paginate_array(user_project.repository.branches.sort_by(&:name)) branches = ::Kaminari.paginate_array(user_project.repository.branches.sort_by(&:name))
present paginate(branches), present paginate(branches),
...@@ -38,49 +44,69 @@ module API ...@@ -38,49 +44,69 @@ module API
project: user_project project: user_project
end end
get ':namespace/:repo/commits/:sha' do params do
hash = requires :namespace, type: String
{ requires :project, type: String
"sha" => "6367e27cc0928789a860676f560ceda6b41b6215", end
"commit" => { get ':namespace/:project/commits/:sha' do
"author" => { Rails.logger.info("FETCHING COMMITS FOR #{params[:namespace]}/#{params[:project]} [hardcoded]")
"name" => "oswaksd", namespace = params[:namespace]
"email" => "oswluizf@gmail.com", project = params[:project]
"date" => "2011-04-14T16:00:49Z" user_project = find_project!("#{namespace}/#{project}")
},
"committer" => { # sent :sha HAS to match with the returned sha on commit in order to succeed
"name" => "oswaksd",
"email" => "oswluizf@gmail.com", commit = user_project.commit(params[:sha])
"date" => "2011-04-14T16:00:49Z"
}, not_found! 'Commit' unless commit
"message" => "Fix all the bugs GL-1 [1]",
}, json_commit = ::API::Entities::Github::RepoCommit.represent(commit).as_json
"author" => { Rails.logger.info("JSON COMMIT: #{json_commit}")
"login" => "oswaldo",
"gravatar_id" => "", present commit, with: ::API::Entities::Github::RepoCommit
},
"committer" => { # hash =
"login" => "oswaldo", # {
"gravatar_id" => "", # "sha" => "357fb168fc667ef07a3303e4bb528fbcb2147430",
}, # "commit" => {
"parents" => [ # "author" => {
{ # "name" => "oswaksd",
"sha" => "357fb168fc667ef07a3303e4bb528fbcb2147430" # "email" => "oswluizf@gmail.com",
} # "date" => "2011-04-14T16:00:49Z"
], # },
"files" => [ # "committer" => {
{ # "name" => "oswaksd",
"filename" => "file1.txt", # "email" => "oswluizf@gmail.com",
"additions" => 10, # "date" => "2011-04-14T16:00:49Z"
"deletions" => 2, # },
"changes" => 12, # "message" => "hardcoded GL-2",
"status" => "modified", # },
"patch" => "@@ -29,7 +29,7 @@\n....." # "author" => {
} # "login" => "oswaldo",
] # "gravatar_id" => "",
} # },
# "committer" => {
present hash # "login" => "oswaldo",
# "gravatar_id" => "",
# },
# "parents" => [
# {
# "sha" => "357fb168fc667ef07a3303e4bb528fbcb2147430"
# }
# ],
# "files" => [
# {
# "filename" => "file1.txt",
# "additions" => 10,
# "deletions" => 2,
# "changes" => 12,
# "status" => "modified",
# "patch" => "@@ -29,7 +29,7 @@\n....."
# }
# ]
# }
#present hash
end end
end end
end end
......
...@@ -19,14 +19,24 @@ describe API::V3::GithubRepos do ...@@ -19,14 +19,24 @@ describe API::V3::GithubRepos do
end end
describe 'GET /users/:id/repos' do describe 'GET /users/:id/repos' do
it 'returns an array of projects with github format' do context 'authenticated' do
get v3_api("/users/whatever/repos", user) it 'returns an array of projects with github format' do
get v3_api("/users/whatever/repos", user)
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response).to be_an(Array) expect(json_response).to be_an(Array)
expect(json_response.size).to eq(1) expect(json_response.size).to eq(1)
expect(json_response.first.keys).to contain_exactly('id', 'owner', 'name') expect(json_response.first.keys).to contain_exactly('id', 'owner', 'name')
expect(json_response.first['owner'].keys).to contain_exactly('login') expect(json_response.first['owner'].keys).to contain_exactly('login')
end
end
context 'unauthenticated' do
it 'returns an array of projects with github format' do
get v3_api("/users/whatever/repos", nil)
expect(response).to have_http_status(401)
end
end end
end end
...@@ -47,11 +57,46 @@ describe API::V3::GithubRepos do ...@@ -47,11 +57,46 @@ describe API::V3::GithubRepos do
end end
describe 'GET /repos/:namespace/:repo/commits/:sha' do describe 'GET /repos/:namespace/:repo/commits/:sha' do
let(:commit) { project.repository.commit }
let(:commit_id) { commit.id }
it 'returns commit with expected format' do it 'returns commit with expected format' do
get v3_api("/repos/#{user.namespace.path}/foo/commits/sha123", user) get v3_api("/repos/#{project.namespace.path}/#{project.path}/commits/#{commit_id}", user)
commit_author = {
'name' => commit.author_name,
'email' => commit.author_email,
'date' => commit.authored_date.iso8601(3)
}
commit_committer = {
'name' => commit.committer_name,
'email' => commit.committer_email,
'date' => commit.committed_date.iso8601(3)
}
parent_commits = commit.parent_ids.map { |id| { 'sha' => id } }
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response).to be_a(Hash) expect(json_response['sha']).to eq(commit.id)
expect(json_response['parents']).to eq(parent_commits)
expect(json_response.dig('commit', 'author')).to eq(commit_author)
expect(json_response.dig('commit', 'committer')).to eq(commit_committer)
expect(json_response.dig('commit', 'message')).to eq(commit.safe_message)
# expect(json_response['short_id']).to eq(commit.short_id)
# expect(json_response['title']).to eq(commit.title)
# expect(json_response['author_name']).to eq(commit.author_name)
# expect(json_response['author_email']).to eq(commit.author_email)
# expect(json_response['authored_date']).to eq(commit.authored_date.iso8601(3))
# expect(json_response['committer_name']).to eq(commit.committer_name)
# expect(json_response['committer_email']).to eq(commit.committer_email)
# expect(json_response['committed_date']).to eq(commit.committed_date.iso8601(3))
# expect(json_response['parent_ids']).to eq(commit.parent_ids)
# expect(json_response['stats']['additions']).to eq(commit.stats.additions)
# expect(json_response['stats']['deletions']).to eq(commit.stats.deletions)
# expect(json_response['stats']['total']).to eq(commit.stats.total)
# expect(json_response['status']).to be_nil
end end
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