Commit a6b1f018 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Refactor and remove hardcoded remainings

parent 2b9d0ef7
......@@ -1092,13 +1092,13 @@ module API
end
module Github
class Namespace < Grape::Entity
expose :path, as: :login
class User < Grape::Entity
expose :username
end
class Repository < Grape::Entity
expose :id
expose :namespace, as: :owner, using: Namespace
expose :owner, using: User
expose :name
end
......@@ -1109,20 +1109,18 @@ module API
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",
login: commit.author.username,
email: commit.author_email
}
end
expose :committer do |commit|
{
"login" => "oswaldo",
login: commit.author.username,
email: commit.committer_email
}
end
expose :commit do |commit|
......@@ -1131,12 +1129,14 @@ module API
author: {
name: commit.author_name,
email: commit.author_email,
date: commit.authored_date.iso8601(3)
date: commit.authored_date.iso8601,
type: 'User'
},
committer: {
name: commit.committer_name,
email: commit.committer_email,
date: commit.committed_date.iso8601(3)
date: commit.committed_date.iso8601,
type: 'User'
},
message: commit.safe_message
}
......@@ -1145,6 +1145,9 @@ module API
# TODO: export to entity
commit.parent_ids.map { |id| { sha: id } }
end
expose :files do |commit|
[]
end
end
class Branch < Grape::Entity
......
......@@ -5,43 +5,6 @@ module API
allow_access_with_scope :read_user, if: -> (request) { request.get? }
resource :users do
get ':id' do
present {
"login" => "oswaldo",
"id" => 1,
"avatar_url" => "https://github.com/images/error/octocat_happy.gif",
"gravatar_id" => "",
"url" => "https://api.github.com/users/octocat",
"html_url" => "https://github.com/octocat",
"followers_url" => "https://api.github.com/users/octocat/followers",
"following_url" => "https://api.github.com/users/octocat/following{/other_user}",
"gists_url" => "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url" => "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url" => "https://api.github.com/users/octocat/subscriptions",
"organizations_url" => "https://api.github.com/users/octocat/orgs",
"repos_url" => "https://api.github.com/users/octocat/repos",
"events_url" => "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url" => "https://api.github.com/users/octocat/received_events",
"type" => "User",
"site_admin" => false,
"name" => "monalisa octocat",
"company" => "GitHub",
"blog" => "https://github.com/blog",
"location" => "San Francisco",
"email" => "octocat@github.com",
"hireable" => false,
"bio" => "There once was...",
"public_repos" => 2,
"public_gists" => 1,
"followers" => 20,
"following" => 0,
"created_at" => "2008-01-14T04:33:35Z",
"updated_at" => "2008-01-14T04:33:35Z"
}
end
end
resource :users, requirements: { uid: /[0-9]*/, id: /[0-9]*/ } do
before do
authenticate_non_get!
......
......@@ -3,6 +3,13 @@ module API
class GithubRepos < Grape::API
before { authenticate! }
helpers do
params :project_full_path do
requires :namespace, type: String
requires :project, type: String
end
end
resource :orgs do
get ':namespace/repos' do
present []
......@@ -17,20 +24,19 @@ module API
resource :users do
get ':namespace/repos' do
projs = ::API::Entities::Github::Repository.represent(current_user.authorized_projects).as_json
Rails.logger.info("PROJS JSON: #{projs}")
present paginate(current_user.authorized_projects), with: ::API::Entities::Github::Repository
present paginate(current_user.authorized_projects),
with: ::API::Entities::Github::Repository
end
end
params do
requires :namespace, type: String
requires :project, type: String
end
resource :repos do
get '/-/jira/pulls' do
present []
end
params do
use :project_full_path
end
get ':namespace/:project/branches' do
namespace = params[:namespace]
project = params[:project]
......@@ -40,8 +46,6 @@ module API
.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),
......@@ -50,8 +54,7 @@ module API
end
params do
requires :namespace, type: String
requires :project, type: String
use :project_full_path
end
get ':namespace/:project/commits/:sha' do
namespace = params[:namespace]
......@@ -66,49 +69,6 @@ module API
Rails.logger.info("JSON COMMIT: #{json_commit}")
present commit, with: ::API::Entities::Github::RepoCommit
# hash =
# {
# "sha" => "b0ee36f0a5356c092a5d4913f3523db93a1db565",
# "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-2",
# },
# "author" => {
# "login" => "oswaldo",
# "gravatar_id" => "",
# },
# "committer" => {
# "login" => "oswaldo",
# "gravatar_id" => "",
# },
# "parents" => [
# {
# "sha" => "e7d7eb49018d7bffa6be52586c518974778965c5"
# }
# ],
# "files" => [
# {
# "filename" => "file1.txt",
# "additions" => 10,
# "deletions" => 2,
# "changes" => 12,
# "status" => "modified",
# "patch" => "@@ -29,7 +29,7 @@\n....."
# }
# ]
# }
# present hash
end
end
end
......
......@@ -8,13 +8,32 @@ describe API::V3::GithubRepos do
project.add_master(user)
end
describe 'GET /orgs/:id/repos' do
it 'returns an array of projects' do
describe 'GET /orgs/:namespace/repos' do
it 'returns an empty array' do
group = create(:group)
get v3_api("/orgs/#{group.path}/repos", user)
expect(response).to have_http_status(200)
expect(json_response).to eq([])
end
end
describe 'GET /user/repos' do
it 'returns an empty array' do
get v3_api("/user/repos", user)
expect(response).to have_http_status(200)
expect(json_response).to eq([])
end
end
describe 'GET /-/jira/pulls' do
it 'returns an empty array' do
get v3_api("/repos/-/jira/pulls", user)
expect(response).to have_http_status(200)
expect(json_response).to eq([])
end
end
......@@ -32,7 +51,7 @@ describe API::V3::GithubRepos do
end
context 'unauthenticated' do
it 'returns an array of projects with github format' do
it 'returns 401' do
get v3_api("/users/whatever/repos", nil)
expect(response).to have_http_status(401)
......@@ -40,63 +59,66 @@ describe API::V3::GithubRepos do
end
end
describe 'GET /repos/:namespace/:repo/branches' do
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
xcontext 'when group path' do
end
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/#{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['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
describe 'GET /repos/:namespace/:repo/branches' do
context 'authenticated' do
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
end
context 'unauthenticated' do
it 'returns 401' do
get v3_api("/repos/#{project.namespace.path}/#{project.path}/branches", nil)
expect(response).to have_http_status(401)
end
end
end
describe 'GET /repos/:namespace/:repo/commits/:sha' do
let(:commit) { project.repository.commit }
let(:commit_id) { commit.id }
context 'authenticated' do
it 'returns commit with expected format' do
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['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)
end
end
context 'unauthenticated' do
it 'returns 401' do
get v3_api("/repos/#{project.namespace.path}/#{project.path}/commits/#{commit_id}", nil)
expect(response).to have_http_status(401)
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