Commit c6d13e36 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Github API commits layer

parent bfc57099
# This controller's role is to mimic the Gitlab OAuth flow routes for Jira DVCS
# integration.
# This controller's role is to mimic and rewire the Gitlab OAuth
# flow routes for Jira DVCS integration.
# See https://gitlab.com/gitlab-org/gitlab-ee/issues/2381
#
class Oauth::Jira::AuthorizationsController < ActionController::Base
......@@ -24,8 +24,7 @@ class Oauth::Jira::AuthorizationsController < ActionController::Base
.slice(:code, :client_id, :client_secret)
.merge(grant_type: 'authorization_code', redirect_uri: oauth_jira_callback_url)
auth_response =
HTTParty.post(oauth_token_url, body: auth_params)
auth_response = HTTParty.post(oauth_token_url, body: auth_params)
# TODO: join url params in a better way
token = "access_token=" +
......
......@@ -2,3 +2,4 @@
# in the middleware stack, because it tracks events with
# GitLab Performance Monitoring
Rails.application.config.middleware.use(Gitlab::EtagCaching::Middleware)
Rails.application.config.middleware.use(Gitlab::Jira::Middleware)
......@@ -5,6 +5,9 @@ module API
allow_access_with_scope :api
prefix :api
NO_SLASH_URL_PART_REGEX = %r{[^/]+}
PROJECT_ENDPOINT_REQUIREMENTS = { id: NO_SLASH_URL_PART_REGEX }.freeze
version %w(v3 v4), using: :path
version 'v3', using: :path do
......@@ -12,6 +15,8 @@ module API
helpers ::API::Helpers::CommonHelpers
mount ::API::V3::AwardEmoji
mount ::API::V3::GithubRepos
mount ::API::V3::Boards
mount ::API::V3::Branches
mount ::API::V3::BroadcastMessages
......@@ -90,9 +95,6 @@ module API
helpers ::API::Helpers
helpers ::API::Helpers::CommonHelpers
NO_SLASH_URL_PART_REGEX = %r{[^/]+}
PROJECT_ENDPOINT_REQUIREMENTS = { id: NO_SLASH_URL_PART_REGEX }.freeze
# Keep in alphabetical order
mount ::API::AccessRequests
mount ::API::AwardEmoji
......
......@@ -1090,5 +1090,18 @@ module API
expose :failing_on_hosts
expose :total_failures
end
class GithubRepoOwner < Grape::Entity
expose :id
expose :login do |project|
project.namespace&.name
end
end
class GithubUserRepo < Grape::Entity
expose :id
expose :owner, using: GithubRepoOwner, unless: -> (project, options) { project.group }
expose :name
end
end
end
module API
module V3
class GithubRepos < Grape::API
before { authenticate! }
desc 'Get a list of repos of a group'
resource :orgs do
get ':namespace/repos' do
present []
end
end
resource :users do
get ':namespace/repos' do
present [{
"id" => 11,
"owner" => {
"login" => "oswaldo",
"id" => 1,
},
"name" => "test",
}]
end
end
resource :repos do
get ':namespace/:repo/branches' do
present [
{
"name" => "feature",
"commit" => {
"type" => 'commit',
"sha" => "6367e27cc0928789a860676f560ceda6b41b6215"
}
}
]
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
end
end
resource :user do
get :repos do
present []
end
end
end
end
end
......@@ -64,6 +64,11 @@ module API
end
end
# desc ':org/repos' do
# Rails.logger.info("Requesting /users/#{params[:org]}/repos with params #{params}")
# Rails.logger.info("User: #{current_user.name}")
# end
desc 'Get the SSH keys of a specified user. Available only for admins.' do
success ::API::Entities::SSHKey
end
......
module Gitlab
module Jira
class Middleware
def initialize(app)
@app = app
end
def call(env)
return @app.call(env) unless /JIRA DVCS Connector/.match(env['HTTP_USER_AGENT'])
env['HTTP_AUTHORIZATION'] = env['HTTP_AUTHORIZATION'].sub('token', 'Bearer')
@app.call(env)
end
end
end
end
require 'spec_helper'
describe API::V3::GithubRepos do
let(:user) { create(:user) }
describe 'GET /orgs/:id/repos' do
let(:current_user) { user }
it 'returns repos with expected format' do
group = create(:group)
get v3_api("/orgs/#{group.path}/repos", current_user)
expect(response).to have_http_status(200)
expect(json_response).to be_empty
end
end
describe 'GET /users/:id/repos' do
let(:current_user) { user }
it 'returns repos with expected format' do
get v3_api("/users/#{user.namespace.path}/repos", current_user)
expect(response).to have_http_status(200)
expect(json_response).to be_an(Array)
expect(json_response).to eq('')
end
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)
expect(response).to have_http_status(200)
expect(json_response).to be_an(Array)
expect(json_response).to eq('')
end
end
describe 'GET /repos/:namespace/:repo/commits/:sha' do
it 'returns commit with expected format' do
get v3_api("/repos/#{user.namespace.path}/foo/commits/sha123", user)
expect(response).to have_http_status(200)
expect(json_response).to be_a(Hash)
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