Commit a6b1f018 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Refactor and remove hardcoded remainings

parent 2b9d0ef7
...@@ -1092,13 +1092,13 @@ module API ...@@ -1092,13 +1092,13 @@ module API
end end
module Github module Github
class Namespace < Grape::Entity class User < Grape::Entity
expose :path, as: :login expose :username
end end
class Repository < Grape::Entity class Repository < Grape::Entity
expose :id expose :id
expose :namespace, as: :owner, using: Namespace expose :owner, using: User
expose :name expose :name
end end
...@@ -1109,20 +1109,18 @@ module API ...@@ -1109,20 +1109,18 @@ module API
end end
end end
class CommitUser < Grape::Entity
expose :path, as: :login
end
class RepoCommit < Grape::Entity class RepoCommit < Grape::Entity
expose :id, as: :sha expose :id, as: :sha
expose :author do |commit| expose :author do |commit|
{ {
"login" => "oswaldo", login: commit.author.username,
email: commit.author_email
} }
end end
expose :committer do |commit| expose :committer do |commit|
{ {
"login" => "oswaldo", login: commit.author.username,
email: commit.committer_email
} }
end end
expose :commit do |commit| expose :commit do |commit|
...@@ -1131,12 +1129,14 @@ module API ...@@ -1131,12 +1129,14 @@ module API
author: { author: {
name: commit.author_name, name: commit.author_name,
email: commit.author_email, email: commit.author_email,
date: commit.authored_date.iso8601(3) date: commit.authored_date.iso8601,
type: 'User'
}, },
committer: { committer: {
name: commit.committer_name, name: commit.committer_name,
email: commit.committer_email, email: commit.committer_email,
date: commit.committed_date.iso8601(3) date: commit.committed_date.iso8601,
type: 'User'
}, },
message: commit.safe_message message: commit.safe_message
} }
...@@ -1145,6 +1145,9 @@ module API ...@@ -1145,6 +1145,9 @@ module API
# TODO: export to entity # TODO: export to entity
commit.parent_ids.map { |id| { sha: id } } commit.parent_ids.map { |id| { sha: id } }
end end
expose :files do |commit|
[]
end
end end
class Branch < Grape::Entity class Branch < Grape::Entity
......
...@@ -5,43 +5,6 @@ module API ...@@ -5,43 +5,6 @@ module API
allow_access_with_scope :read_user, if: -> (request) { request.get? } 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 resource :users, requirements: { uid: /[0-9]*/, id: /[0-9]*/ } do
before do before do
authenticate_non_get! authenticate_non_get!
......
...@@ -3,6 +3,13 @@ module API ...@@ -3,6 +3,13 @@ module API
class GithubRepos < Grape::API class GithubRepos < Grape::API
before { authenticate! } before { authenticate! }
helpers do
params :project_full_path do
requires :namespace, type: String
requires :project, type: String
end
end
resource :orgs do resource :orgs do
get ':namespace/repos' do get ':namespace/repos' do
present [] present []
...@@ -17,20 +24,19 @@ module API ...@@ -17,20 +24,19 @@ module API
resource :users do resource :users do
get ':namespace/repos' do get ':namespace/repos' do
projs = ::API::Entities::Github::Repository.represent(current_user.authorized_projects).as_json present paginate(current_user.authorized_projects),
with: ::API::Entities::Github::Repository
Rails.logger.info("PROJS JSON: #{projs}")
present paginate(current_user.authorized_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 '/-/jira/pulls' do
present []
end
params do
use :project_full_path
end
get ':namespace/:project/branches' do get ':namespace/:project/branches' do
namespace = params[:namespace] namespace = params[:namespace]
project = params[:project] project = params[:project]
...@@ -40,8 +46,6 @@ module API ...@@ -40,8 +46,6 @@ module API
.represent(user_project.repository.branches.sort_by(&:name), project: user_project) .represent(user_project.repository.branches.sort_by(&:name), project: user_project)
.as_json .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),
...@@ -50,8 +54,7 @@ module API ...@@ -50,8 +54,7 @@ module API
end end
params do params do
requires :namespace, type: String use :project_full_path
requires :project, type: String
end end
get ':namespace/:project/commits/:sha' do get ':namespace/:project/commits/:sha' do
namespace = params[:namespace] namespace = params[:namespace]
...@@ -66,49 +69,6 @@ module API ...@@ -66,49 +69,6 @@ module API
Rails.logger.info("JSON COMMIT: #{json_commit}") Rails.logger.info("JSON COMMIT: #{json_commit}")
present commit, with: ::API::Entities::Github::RepoCommit 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 end
end end
......
...@@ -8,13 +8,32 @@ describe API::V3::GithubRepos do ...@@ -8,13 +8,32 @@ describe API::V3::GithubRepos do
project.add_master(user) project.add_master(user)
end end
describe 'GET /orgs/:id/repos' do describe 'GET /orgs/:namespace/repos' do
it 'returns an array of projects' do it 'returns an empty array' do
group = create(:group) group = create(:group)
get v3_api("/orgs/#{group.path}/repos", user) get v3_api("/orgs/#{group.path}/repos", user)
expect(response).to have_http_status(200) 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
end end
...@@ -32,7 +51,7 @@ describe API::V3::GithubRepos do ...@@ -32,7 +51,7 @@ describe API::V3::GithubRepos do
end end
context 'unauthenticated' do context 'unauthenticated' do
it 'returns an array of projects with github format' do it 'returns 401' do
get v3_api("/users/whatever/repos", nil) get v3_api("/users/whatever/repos", nil)
expect(response).to have_http_status(401) expect(response).to have_http_status(401)
...@@ -40,63 +59,66 @@ describe API::V3::GithubRepos do ...@@ -40,63 +59,66 @@ describe API::V3::GithubRepos do
end end
end end
describe 'GET /repos/:namespace/:repo/branches' do describe 'GET /repos/:namespace/:repo/branches' do
context 'when user namespace path' do context 'authenticated' do
it 'returns an array of project branches with github format' do context 'when user namespace path' do
get v3_api("/repos/#{project.namespace.path}/#{project.path}/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(response).to have_http_status(200)
expect(json_response.first.keys).to contain_exactly('name', 'commit') expect(json_response).to be_an(Array)
expect(json_response.first['commit'].keys).to contain_exactly('sha', 'type') expect(json_response.first.keys).to contain_exactly('name', 'commit')
end expect(json_response.first['commit'].keys).to contain_exactly('sha', 'type')
end end
end
xcontext 'when group path' do end
end
end context 'unauthenticated' do
it 'returns 401' do
describe 'GET /repos/:namespace/:repo/commits/:sha' do get v3_api("/repos/#{project.namespace.path}/#{project.path}/branches", nil)
let(:commit) { project.repository.commit }
let(:commit_id) { commit.id } expect(response).to have_http_status(401)
end
it 'returns commit with expected format' do end
get v3_api("/repos/#{project.namespace.path}/#{project.path}/commits/#{commit_id}", user) end
commit_author = { describe 'GET /repos/:namespace/:repo/commits/:sha' do
'name' => commit.author_name, let(:commit) { project.repository.commit }
'email' => commit.author_email, let(:commit_id) { commit.id }
'date' => commit.authored_date.iso8601(3)
} context 'authenticated' do
it 'returns commit with expected format' do
commit_committer = { get v3_api("/repos/#{project.namespace.path}/#{project.path}/commits/#{commit_id}", user)
'name' => commit.committer_name,
'email' => commit.committer_email, commit_author = {
'date' => commit.committed_date.iso8601(3) 'name' => commit.author_name,
} 'email' => commit.author_email,
'date' => commit.authored_date.iso8601(3)
parent_commits = commit.parent_ids.map { |id| { 'sha' => id } } }
expect(response).to have_http_status(200) commit_committer = {
expect(json_response['sha']).to eq(commit.id) 'name' => commit.committer_name,
expect(json_response['parents']).to eq(parent_commits) 'email' => commit.committer_email,
expect(json_response.dig('commit', 'author')).to eq(commit_author) 'date' => commit.committed_date.iso8601(3)
expect(json_response.dig('commit', 'committer')).to eq(commit_committer) }
expect(json_response.dig('commit', 'message')).to eq(commit.safe_message)
parent_commits = commit.parent_ids.map { |id| { 'sha' => id } }
# expect(json_response['short_id']).to eq(commit.short_id)
# expect(json_response['title']).to eq(commit.title) expect(response).to have_http_status(200)
# expect(json_response['author_name']).to eq(commit.author_name) expect(json_response['sha']).to eq(commit.id)
# expect(json_response['author_email']).to eq(commit.author_email) expect(json_response['parents']).to eq(parent_commits)
# expect(json_response['authored_date']).to eq(commit.authored_date.iso8601(3)) expect(json_response.dig('commit', 'author')).to eq(commit_author)
# expect(json_response['committer_name']).to eq(commit.committer_name) expect(json_response.dig('commit', 'committer')).to eq(commit_committer)
# expect(json_response['committer_email']).to eq(commit.committer_email) expect(json_response.dig('commit', 'message')).to eq(commit.safe_message)
# expect(json_response['committed_date']).to eq(commit.committed_date.iso8601(3)) end
# expect(json_response['parent_ids']).to eq(commit.parent_ids) end
# expect(json_response['stats']['additions']).to eq(commit.stats.additions)
# expect(json_response['stats']['deletions']).to eq(commit.stats.deletions) context 'unauthenticated' do
# expect(json_response['stats']['total']).to eq(commit.stats.total) it 'returns 401' do
# expect(json_response['status']).to be_nil get v3_api("/repos/#{project.namespace.path}/#{project.path}/commits/#{commit_id}", nil)
end
end expect(response).to have_http_status(401)
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