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
expose :name
end
class Commit < Grape::Entity
class BranchCommit < Grape::Entity
expose :id, as: :sha
expose :type do |model|
'commit'
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
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)
end
end
......
......@@ -31,6 +31,12 @@ module API
project = params[: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))
present paginate(branches),
......@@ -38,49 +44,69 @@ module API
project: user_project
end
get ':namespace/:repo/commits/:sha' do
hash =
{
"sha" => "6367e27cc0928789a860676f560ceda6b41b6215",
"commit" => {
"author" => {
"name" => "oswaksd",
"email" => "oswluizf@gmail.com",
"date" => "2011-04-14T16:00:49Z"
},
"committer" => {
"name" => "oswaksd",
"email" => "oswluizf@gmail.com",
"date" => "2011-04-14T16:00:49Z"
},
"message" => "Fix all the bugs GL-1 [1]",
},
"author" => {
"login" => "oswaldo",
"gravatar_id" => "",
},
"committer" => {
"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
params do
requires :namespace, type: String
requires :project, type: String
end
get ':namespace/:project/commits/:sha' do
Rails.logger.info("FETCHING COMMITS FOR #{params[:namespace]}/#{params[:project]} [hardcoded]")
namespace = params[:namespace]
project = params[:project]
user_project = find_project!("#{namespace}/#{project}")
# sent :sha HAS to match with the returned sha on commit in order to succeed
commit = user_project.commit(params[:sha])
not_found! 'Commit' unless commit
json_commit = ::API::Entities::Github::RepoCommit.represent(commit).as_json
Rails.logger.info("JSON COMMIT: #{json_commit}")
present commit, with: ::API::Entities::Github::RepoCommit
# hash =
# {
# "sha" => "357fb168fc667ef07a3303e4bb528fbcb2147430",
# "commit" => {
# "author" => {
# "name" => "oswaksd",
# "email" => "oswluizf@gmail.com",
# "date" => "2011-04-14T16:00:49Z"
# },
# "committer" => {
# "name" => "oswaksd",
# "email" => "oswluizf@gmail.com",
# "date" => "2011-04-14T16:00:49Z"
# },
# "message" => "hardcoded GL-2",
# },
# "author" => {
# "login" => "oswaldo",
# "gravatar_id" => "",
# },
# "committer" => {
# "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
......
......@@ -19,14 +19,24 @@ describe API::V3::GithubRepos do
end
describe 'GET /users/:id/repos' do
it 'returns an array of projects with github format' do
get v3_api("/users/whatever/repos", user)
context 'authenticated' do
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(json_response).to be_an(Array)
expect(json_response.size).to eq(1)
expect(json_response.first.keys).to contain_exactly('id', 'owner', 'name')
expect(json_response.first['owner'].keys).to contain_exactly('login')
expect(response).to have_http_status(200)
expect(json_response).to be_an(Array)
expect(json_response.size).to eq(1)
expect(json_response.first.keys).to contain_exactly('id', 'owner', 'name')
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
......@@ -47,11 +57,46 @@ describe API::V3::GithubRepos do
end
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
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(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
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