commit
b110c6bc86
6 changed files with 178 additions and 54 deletions
57
lib/api.rb
57
lib/api.rb
|
@ -1,61 +1,12 @@
|
||||||
require 'api/entities'
|
Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file}
|
||||||
require 'api/helpers'
|
|
||||||
|
|
||||||
module Gitlab
|
module Gitlab
|
||||||
class API < Grape::API
|
class API < Grape::API
|
||||||
format :json
|
format :json
|
||||||
|
error_format :json
|
||||||
helpers APIHelpers
|
helpers APIHelpers
|
||||||
|
|
||||||
# Users API
|
mount Users
|
||||||
resource :users do
|
mount Projects
|
||||||
before { authenticate! }
|
|
||||||
|
|
||||||
# GET /users
|
|
||||||
get do
|
|
||||||
@users = User.all
|
|
||||||
present @users, :with => Entities::User
|
|
||||||
end
|
|
||||||
|
|
||||||
# GET /users/:id
|
|
||||||
get ":id" do
|
|
||||||
@user = User.find(params[:id])
|
|
||||||
present @user, :with => Entities::User
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# GET /user
|
|
||||||
get "/user" do
|
|
||||||
authenticate!
|
|
||||||
present @current_user, :with => Entities::User
|
|
||||||
end
|
|
||||||
|
|
||||||
# Projects API
|
|
||||||
resource :projects do
|
|
||||||
before { authenticate! }
|
|
||||||
|
|
||||||
# GET /projects
|
|
||||||
get do
|
|
||||||
@projects = current_user.projects
|
|
||||||
present @projects, :with => Entities::Project
|
|
||||||
end
|
|
||||||
|
|
||||||
# GET /projects/:id
|
|
||||||
get ":id" do
|
|
||||||
@project = current_user.projects.find_by_code(params[:id])
|
|
||||||
present @project, :with => Entities::Project
|
|
||||||
end
|
|
||||||
|
|
||||||
# GET /projects/:id/repository/branches
|
|
||||||
get ":id/repository/branches" do
|
|
||||||
@project = current_user.projects.find_by_code(params[:id])
|
|
||||||
present @project.repo.heads.sort_by(&:name), :with => Entities::ProjectRepositoryBranches
|
|
||||||
end
|
|
||||||
|
|
||||||
# GET /projects/:id/repository/tags
|
|
||||||
get ":id/repository/tags" do
|
|
||||||
@project = current_user.projects.find_by_code(params[:id])
|
|
||||||
present @project.repo.tags.sort_by(&:name).reverse, :with => Entities::ProjectRepositoryTags
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -19,5 +19,11 @@ module Gitlab
|
||||||
class ProjectRepositoryTags < Grape::Entity
|
class ProjectRepositoryTags < Grape::Entity
|
||||||
expose :name, :commit
|
expose :name, :commit
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class ProjectSnippet < Grape::Entity
|
||||||
|
expose :id, :title, :file_name
|
||||||
|
expose :author, :using => Entities::User
|
||||||
|
expose :expires_at, :updated_at, :created_at
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,7 @@ module Gitlab
|
||||||
end
|
end
|
||||||
|
|
||||||
def authenticate!
|
def authenticate!
|
||||||
error!('401 Unauthorized', 401) unless current_user
|
error!({'message' => '401 Unauthorized'}, 401) unless current_user
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
103
lib/api/projects.rb
Normal file
103
lib/api/projects.rb
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
module Gitlab
|
||||||
|
# Projects API
|
||||||
|
class Projects < Grape::API
|
||||||
|
before { authenticate! }
|
||||||
|
|
||||||
|
resource :projects do
|
||||||
|
# Get a projects list for authenticated user
|
||||||
|
#
|
||||||
|
# Example Request:
|
||||||
|
# GET /projects
|
||||||
|
get do
|
||||||
|
@projects = current_user.projects
|
||||||
|
present @projects, :with => Entities::Project
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get a single project
|
||||||
|
#
|
||||||
|
# Parameters:
|
||||||
|
# id (required) - The code of a project
|
||||||
|
# Example Request:
|
||||||
|
# GET /projects/:id
|
||||||
|
get ":id" do
|
||||||
|
@project = current_user.projects.find_by_code(params[:id])
|
||||||
|
present @project, :with => Entities::Project
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get a project repository branches
|
||||||
|
#
|
||||||
|
# Parameters:
|
||||||
|
# id (required) - The code of a project
|
||||||
|
# Example Request:
|
||||||
|
# GET /projects/:id/repository/branches
|
||||||
|
get ":id/repository/branches" do
|
||||||
|
@project = current_user.projects.find_by_code(params[:id])
|
||||||
|
present @project.repo.heads.sort_by(&:name), :with => Entities::ProjectRepositoryBranches
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get a project repository tags
|
||||||
|
#
|
||||||
|
# Parameters:
|
||||||
|
# id (required) - The code of a project
|
||||||
|
# Example Request:
|
||||||
|
# GET /projects/:id/repository/tags
|
||||||
|
get ":id/repository/tags" do
|
||||||
|
@project = current_user.projects.find_by_code(params[:id])
|
||||||
|
present @project.repo.tags.sort_by(&:name).reverse, :with => Entities::ProjectRepositoryTags
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get a project snippet
|
||||||
|
#
|
||||||
|
# Parameters:
|
||||||
|
# id (required) - The code of a project
|
||||||
|
# snippet_id (required) - The ID of a project snippet
|
||||||
|
# Example Request:
|
||||||
|
# GET /projects/:id/snippets/:snippet_id
|
||||||
|
get ":id/snippets/:snippet_id" do
|
||||||
|
@project = current_user.projects.find_by_code(params[:id])
|
||||||
|
@snippet = @project.snippets.find(params[:snippet_id])
|
||||||
|
present @snippet, :with => Entities::ProjectSnippet
|
||||||
|
end
|
||||||
|
|
||||||
|
# Create a new project snippet
|
||||||
|
#
|
||||||
|
# Parameters:
|
||||||
|
# id (required) - The code name of a project
|
||||||
|
# title (required) - The title of a snippet
|
||||||
|
# file_name (required) - The name of a snippet file
|
||||||
|
# lifetime (optional) - The expiration date of a snippet
|
||||||
|
# code (required) - The content of a snippet
|
||||||
|
# Example Request:
|
||||||
|
# POST /projects/:id/snippets
|
||||||
|
post ":id/snippets" do
|
||||||
|
@project = current_user.projects.find_by_code(params[:id])
|
||||||
|
@snippet = @project.snippets.new(
|
||||||
|
:title => params[:title],
|
||||||
|
:file_name => params[:file_name],
|
||||||
|
:expires_at => params[:lifetime],
|
||||||
|
:content => params[:code]
|
||||||
|
)
|
||||||
|
@snippet.author = current_user
|
||||||
|
|
||||||
|
if @snippet.save
|
||||||
|
present @snippet, :with => Entities::ProjectSnippet
|
||||||
|
else
|
||||||
|
error!({'message' => '404 Not found'}, 404)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Delete a project snippet
|
||||||
|
#
|
||||||
|
# Parameters:
|
||||||
|
# id (required) - The code of a project
|
||||||
|
# snippet_id (required) - The ID of a project snippet
|
||||||
|
# Example Request:
|
||||||
|
# DELETE /projects/:id/snippets/:snippet_id
|
||||||
|
delete ":id/snippets/:snippet_id" do
|
||||||
|
@project = current_user.projects.find_by_code(params[:id])
|
||||||
|
@snippet = @project.snippets.find(params[:snippet_id])
|
||||||
|
@snippet.destroy
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
36
lib/api/users.rb
Normal file
36
lib/api/users.rb
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
module Gitlab
|
||||||
|
# Users API
|
||||||
|
class Users < Grape::API
|
||||||
|
before { authenticate! }
|
||||||
|
|
||||||
|
resource :users do
|
||||||
|
# Get a users list
|
||||||
|
#
|
||||||
|
# Example Request:
|
||||||
|
# GET /users
|
||||||
|
get do
|
||||||
|
@users = User.all
|
||||||
|
present @users, :with => Entities::User
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get a single user
|
||||||
|
#
|
||||||
|
# Parameters:
|
||||||
|
# id (required) - The ID of a user
|
||||||
|
# Example Request:
|
||||||
|
# GET /users/:id
|
||||||
|
get ":id" do
|
||||||
|
@user = User.find(params[:id])
|
||||||
|
present @user, :with => Entities::User
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get currently authenticated user
|
||||||
|
#
|
||||||
|
# Example Request:
|
||||||
|
# GET /user
|
||||||
|
get "/user" do
|
||||||
|
present @current_user, :with => Entities::User
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -3,6 +3,7 @@ require 'spec_helper'
|
||||||
describe Gitlab::API do
|
describe Gitlab::API do
|
||||||
let(:user) { Factory :user }
|
let(:user) { Factory :user }
|
||||||
let!(:project) { Factory :project, :owner => user }
|
let!(:project) { Factory :project, :owner => user }
|
||||||
|
let!(:snippet) { Factory :snippet, :author => user, :project => project }
|
||||||
before { project.add_access(user, :read) }
|
before { project.add_access(user, :read) }
|
||||||
|
|
||||||
describe "GET /projects" do
|
describe "GET /projects" do
|
||||||
|
@ -52,4 +53,31 @@ describe Gitlab::API do
|
||||||
json.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name
|
json.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "GET /projects/:id/snippets/:snippet_id" do
|
||||||
|
it "should return a project snippet" do
|
||||||
|
get "/api/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}"
|
||||||
|
response.status.should == 200
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
json['title'].should == snippet.title
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "POST /projects/:id/snippets" do
|
||||||
|
it "should create a new project snippet" do
|
||||||
|
post "/api/projects/#{project.code}/snippets?private_token=#{user.private_token}",
|
||||||
|
:title => 'api test', :file_name => 'sample.rb', :code => 'test'
|
||||||
|
response.status.should == 201
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
json['title'].should == 'api test'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "DELETE /projects/:id/snippets/:snippet_id" do
|
||||||
|
it "should create a new project snippet" do
|
||||||
|
expect {
|
||||||
|
delete "/api/projects/#{project.code}/snippets/#{snippet.id}?private_token=#{user.private_token}"
|
||||||
|
}.should change { Snippet.count }.by(-1)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue