summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/api/entities.rb7
-rw-r--r--lib/api/helpers.rb6
-rw-r--r--lib/api/repositories.rb30
-rw-r--r--lib/api/users.rb14
-rw-r--r--lib/tasks/gitlab/check.rake3
-rw-r--r--lib/tasks/gitlab/test.rake18
-rw-r--r--lib/tasks/gitlab/web_hook.rake65
-rw-r--r--lib/tasks/travis.rake5
8 files changed, 112 insertions, 36 deletions
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index 1f35e9ec5fc..ab949f530ab 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -3,6 +3,9 @@ module API
class User < Grape::Entity
expose :id, :username, :email, :name, :bio, :skype, :linkedin, :twitter,
:theme_id, :color_scheme_id, :state, :created_at, :extern_uid, :provider
+ expose :is_admin?, as: :is_admin
+ expose :can_create_group?, as: :can_create_group
+ expose :can_create_project?, as: :can_create_project
end
class UserSafe < Grape::Entity
@@ -15,10 +18,6 @@ module API
class UserLogin < User
expose :private_token
- expose :is_admin?, as: :is_admin
- expose :can_create_group?, as: :can_create_group
- expose :can_create_project?, as: :can_create_project
- expose :can_create_team?, as: :can_create_team
end
class Hook < Grape::Entity
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb
index 4f189f35196..2b0c672c7fa 100644
--- a/lib/api/helpers.rb
+++ b/lib/api/helpers.rb
@@ -64,6 +64,10 @@ module API
end
end
+ def authorize_admin_project
+ authorize! :admin_project, user_project
+ end
+
def can?(object, action, subject)
abilities.allowed?(object, action, subject)
end
@@ -82,7 +86,7 @@ module API
def attributes_for_keys(keys)
attrs = {}
keys.each do |key|
- attrs[key] = params[key] if params[key].present?
+ attrs[key] = params[key] if params[key].present? or (params.has_key?(key) and params[key] == false)
end
attrs
end
diff --git a/lib/api/repositories.rb b/lib/api/repositories.rb
index fef32d3a2fe..c2b229b0172 100644
--- a/lib/api/repositories.rb
+++ b/lib/api/repositories.rb
@@ -2,6 +2,7 @@ module API
# Projects API
class Repositories < Grape::API
before { authenticate! }
+ before { authorize! :download_code, user_project }
resource :projects do
helpers do
@@ -44,13 +45,12 @@ module API
# Example Request:
# PUT /projects/:id/repository/branches/:branch/protect
put ":id/repository/branches/:branch/protect" do
- @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
- not_found! unless @branch
- protected = user_project.protected_branches.find_by_name(@branch.name)
+ authorize_admin_project
- unless protected
- user_project.protected_branches.create(name: @branch.name)
- end
+ @branch = user_project.repository.find_branch(params[:branch])
+ not_found! unless @branch
+ protected_branch = user_project.protected_branches.find_by_name(@branch.name)
+ user_project.protected_branches.create(name: @branch.name) unless protected_branch
present @branch, with: Entities::RepoObject, project: user_project
end
@@ -63,13 +63,12 @@ module API
# Example Request:
# PUT /projects/:id/repository/branches/:branch/unprotect
put ":id/repository/branches/:branch/unprotect" do
- @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
- not_found! unless @branch
- protected = user_project.protected_branches.find_by_name(@branch.name)
+ authorize_admin_project
- if protected
- protected.destroy
- end
+ @branch = user_project.repository.find_branch(params[:branch])
+ not_found! unless @branch
+ protected_branch = user_project.protected_branches.find_by_name(@branch.name)
+ protected_branch.destroy if protected_branch
present @branch, with: Entities::RepoObject, project: user_project
end
@@ -92,8 +91,6 @@ module API
# Example Request:
# GET /projects/:id/repository/commits
get ":id/repository/commits" do
- authorize! :download_code, user_project
-
page = (params[:page] || 0).to_i
per_page = (params[:per_page] || 20).to_i
ref = params[:ref_name] || user_project.try(:default_branch) || 'master'
@@ -110,7 +107,6 @@ module API
# Example Request:
# GET /projects/:id/repository/commits/:sha
get ":id/repository/commits/:sha" do
- authorize! :download_code, user_project
sha = params[:sha]
commit = user_project.repository.commit(sha)
not_found! "Commit" unless commit
@@ -125,7 +121,6 @@ module API
# Example Request:
# GET /projects/:id/repository/commits/:sha/diff
get ":id/repository/commits/:sha/diff" do
- authorize! :download_code, user_project
sha = params[:sha]
result = CommitLoadContext.new(user_project, current_user, {id: sha}).execute
not_found! "Commit" unless result[:commit]
@@ -140,8 +135,6 @@ module API
# Example Request:
# GET /projects/:id/repository/tree
get ":id/repository/tree" do
- authorize! :download_code, user_project
-
ref = params[:ref_name] || user_project.try(:default_branch) || 'master'
path = params[:path] || nil
@@ -166,7 +159,6 @@ module API
# Example Request:
# GET /projects/:id/repository/blobs/:sha
get [ ":id/repository/blobs/:sha", ":id/repository/commits/:sha/blob" ] do
- authorize! :download_code, user_project
required_attributes! [:filepath]
ref = params[:sha]
diff --git a/lib/api/users.rb b/lib/api/users.rb
index 00dc2311ffd..54d3aeecb70 100644
--- a/lib/api/users.rb
+++ b/lib/api/users.rb
@@ -40,13 +40,17 @@ module API
# extern_uid - External authentication provider UID
# provider - External provider
# bio - Bio
+ # admin - User is admin - true or false (default)
+ # can_create_group - User can create groups - true or false
# Example Request:
# POST /users
post do
authenticated_as_admin!
required_attributes! [:email, :password, :name, :username]
- attrs = attributes_for_keys [:email, :name, :password, :skype, :linkedin, :twitter, :projects_limit, :username, :extern_uid, :provider, :bio]
+ attrs = attributes_for_keys [:email, :name, :password, :skype, :linkedin, :twitter, :projects_limit, :username, :extern_uid, :provider, :bio, :can_create_group, :admin]
user = User.build_user(attrs, as: :admin)
+ admin = attrs.delete(:admin)
+ user.admin = admin unless admin.nil?
if user.save
present user, with: Entities::User
else
@@ -67,16 +71,20 @@ module API
# extern_uid - External authentication provider UID
# provider - External provider
# bio - Bio
+ # admin - User is admin - true or false (default)
+ # can_create_group - User can create groups - true or false
# Example Request:
# PUT /users/:id
put ":id" do
authenticated_as_admin!
- attrs = attributes_for_keys [:email, :name, :password, :skype, :linkedin, :twitter, :projects_limit, :username, :extern_uid, :provider, :bio]
+ attrs = attributes_for_keys [:email, :name, :password, :skype, :linkedin, :twitter, :projects_limit, :username, :extern_uid, :provider, :bio, :can_create_group, :admin]
user = User.find(params[:id])
not_found!("User not found") unless user
- if user.update_attributes(attrs)
+ admin = attrs.delete(:admin)
+ user.admin = admin unless admin.nil?
+ if user.update_attributes(attrs, as: :admin)
present user, with: Entities::User
else
not_found!
diff --git a/lib/tasks/gitlab/check.rake b/lib/tasks/gitlab/check.rake
index 88eb088c911..6e2a59f62ac 100644
--- a/lib/tasks/gitlab/check.rake
+++ b/lib/tasks/gitlab/check.rake
@@ -663,7 +663,6 @@ namespace :gitlab do
else
puts "#{sidekiq_match.length}".red
try_fixing_it(
- 'Unless you are running another Rails application on this server there should only be one Sidekiq process.',
'sudo service gitlab stop',
'sudo pkill -f sidekiq',
'sleep 10 && sudo pkill -9 -f sidekiq',
@@ -674,7 +673,7 @@ namespace :gitlab do
end
def sidekiq_process_match
- run_and_match("ps aux | grep -i sidekiq", /(sidekiq \d+\.\d+\.\d+.+$)/)
+ run_and_match("ps ux | grep -i sidekiq", /(sidekiq \d+\.\d+\.\d+.+$)/)
end
end
diff --git a/lib/tasks/gitlab/test.rake b/lib/tasks/gitlab/test.rake
index 03b3fc5ea20..011748c9711 100644
--- a/lib/tasks/gitlab/test.rake
+++ b/lib/tasks/gitlab/test.rake
@@ -1,4 +1,18 @@
namespace :gitlab do
- desc "GITLAB | Run both spinach and rspec"
- task test: ['spinach', 'spec']
+ desc "GITLAB | Run all tests"
+ task :test do
+ cmds = [
+ "rake db:setup",
+ "rake db:seed_fu",
+ "rake spinach",
+ "rake spec",
+ "rake jasmine:ci"
+ ]
+
+ cmds.each do |cmd|
+ system(cmd + " RAILS_ENV=test")
+
+ raise "#{cmd} failed!" unless $?.exitstatus.zero?
+ end
+ end
end
diff --git a/lib/tasks/gitlab/web_hook.rake b/lib/tasks/gitlab/web_hook.rake
new file mode 100644
index 00000000000..f9f586db93c
--- /dev/null
+++ b/lib/tasks/gitlab/web_hook.rake
@@ -0,0 +1,65 @@
+namespace :gitlab do
+ namespace :web_hook do
+ desc "GITLAB | Adds a web hook to the projects"
+ task :add => :environment do
+ web_hook_url = ENV['URL']
+ namespace_path = ENV['NAMESPACE']
+
+ projects = find_projects(namespace_path)
+
+ puts "Adding web hook '#{web_hook_url}' to:"
+ projects.find_each(batch_size: 1000) do |project|
+ print "- #{project.name} ... "
+ web_hook = project.hooks.new(url: web_hook_url)
+ if web_hook.save
+ puts "added".green
+ else
+ print "failed".red
+ puts " [#{web_hook.errors.full_messages.to_sentence}]"
+ end
+ end
+ end
+
+ desc "GITLAB | Remove a web hook from the projects"
+ task :rm => :environment do
+ web_hook_url = ENV['URL']
+ namespace_path = ENV['NAMESPACE']
+
+ projects = find_projects(namespace_path)
+ projects_ids = projects.pluck(:id)
+
+ puts "Removing web hooks with the url '#{web_hook_url}' ... "
+ count = WebHook.where(url: web_hook_url, project_id: projects_ids, type: 'ProjectHook').delete_all
+ puts "#{count} web hooks were removed."
+ end
+
+ desc "GITLAB | List web hooks"
+ task :list => :environment do
+ namespace_path = ENV['NAMESPACE']
+
+ projects = find_projects(namespace_path)
+ web_hooks = projects.all.map(&:hooks).flatten
+ web_hooks.each do |hook|
+ puts "#{hook.project.name.truncate(20).ljust(20)} -> #{hook.url}"
+ end
+
+ puts "\n#{web_hooks.size} web hooks found."
+ end
+ end
+
+ def find_projects(namespace_path)
+ if namespace_path.blank?
+ Project
+ elsif namespace_path == '/'
+ Project.where(namespace_id: nil)
+ else
+ namespace = Namespace.where(path: namespace_path).first
+ if namespace
+ Project.where(namespace_id: namespace.id)
+ else
+ puts "Namespace not found: #{namespace_path}".red
+ exit 2
+ end
+ end
+ end
+end
diff --git a/lib/tasks/travis.rake b/lib/tasks/travis.rake
deleted file mode 100644
index bc1b8aadbc5..00000000000
--- a/lib/tasks/travis.rake
+++ /dev/null
@@ -1,5 +0,0 @@
-desc "Travis run tests"
-task travis: [
- :spinach,
- :spec
-]