From 239d942606bd35d90c4546eb7b0fd530baf00a05 Mon Sep 17 00:00:00 2001 From: Bastian Krol Date: Wed, 9 Jul 2014 14:49:53 +0200 Subject: print validation errors when import fails --- lib/tasks/gitlab/import.rake | 1 + 1 file changed, 1 insertion(+) (limited to 'lib') diff --git a/lib/tasks/gitlab/import.rake b/lib/tasks/gitlab/import.rake index cbfa736c84c..d38c7a59431 100644 --- a/lib/tasks/gitlab/import.rake +++ b/lib/tasks/gitlab/import.rake @@ -72,6 +72,7 @@ namespace :gitlab do puts " * Created #{project.name} (#{repo_path})".green else puts " * Failed trying to create #{project.name} (#{repo_path})".red + puts " Validation Errors: #{project.errors.messages}".red end end end -- cgit v1.2.1 From 430758653ce7e4d32b40648e6263b79c2709bdad Mon Sep 17 00:00:00 2001 From: Jeroen Jacobs Date: Fri, 27 Jun 2014 16:48:30 +0200 Subject: Adds comments to commits in the API --- lib/api/commits.rb | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/api/entities.rb | 8 +++++++ 2 files changed, 69 insertions(+) (limited to 'lib') diff --git a/lib/api/commits.rb b/lib/api/commits.rb index 4a67313430a..6c5391b98c8 100644 --- a/lib/api/commits.rb +++ b/lib/api/commits.rb @@ -50,6 +50,67 @@ module API not_found! "Commit" unless commit commit.diffs end + + # Get a commit's comments + # + # Parameters: + # id (required) - The ID of a project + # sha (required) - The commit hash + # Examples: + # GET /projects/:id/repository/commits/:sha/comments + get ':id/repository/commits/:sha/comments' do + sha = params[:sha] + commit = user_project.repository.commit(sha) + not_found! 'Commit' unless commit + notes = Note.where(commit_id: commit.id) + present paginate(notes), with: Entities::CommitNote + end + + # Post comment to commit + # + # Parameters: + # id (required) - The ID of a project + # sha (required) - The commit hash + # note (required) - Text of comment + # path (optional) - The file path + # line (optional) - The line number + # line_type (optional) - The type of line (new or old) + # Examples: + # POST /projects/:id/repository/commits/:sha/comments + post ':id/repository/commits/:sha/comments' do + required_attributes! [:note] + + sha = params[:sha] + commit = user_project.repository.commit(sha) + not_found! 'Commit' unless commit + opts = { + note: params[:note], + noteable_type: 'Commit', + commit_id: commit.id + } + + if params[:path] && params[:line] && params[:line_type] + commit.diffs.each do |diff| + next unless diff.new_path == params[:path] + lines = Gitlab::Diff::Parser.new.parse(diff.diff.lines.to_a) + + lines.each do |line| + next unless line.new_pos == params[:line].to_i && line.type == params[:line_type] + break opts[:line_code] = Gitlab::Diff::LineCode.generate(diff.new_path, line.new_pos, line.old_pos) + end + + break if opts[:line_code] + end + end + + note = ::Notes::CreateService.new(user_project, current_user, opts).execute + + if note.save + present note, with: Entities::CommitNote + else + not_found! + end + end end end end diff --git a/lib/api/entities.rb b/lib/api/entities.rb index ffa3e8a149e..c7b86ed3d76 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -158,6 +158,14 @@ module API expose :author, using: Entities::UserBasic end + class CommitNote < Grape::Entity + expose :note + expose(:path) { |note| note.diff_file_name } + expose(:line) { |note| note.diff_new_line } + expose(:line_type) { |note| note.diff_line_type } + expose :author, using: Entities::UserBasic + end + class Event < Grape::Entity expose :title, :project_id, :action_name expose :target_id, :target_type, :author_id -- cgit v1.2.1 From 6bae8c48ef83cf45984930c57282903cbff506ad Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Sat, 16 Aug 2014 11:53:44 +0200 Subject: Update default regex message to match regex. --- lib/gitlab/regex.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/regex.rb b/lib/gitlab/regex.rb index 4b8038843b0..c4d0d85b7f5 100644 --- a/lib/gitlab/regex.rb +++ b/lib/gitlab/regex.rb @@ -67,8 +67,7 @@ module Gitlab def default_regex_message "can contain only letters, digits, '_', '-' and '.'. " \ - "It must start with letter, digit or '_', optionally preceeded by '.'. " \ - "It must not end in '.git'." + "Cannot start with '-' or end in '.git'" \ end def default_regex -- cgit v1.2.1 From a9fadce361163e97eb1de0ec62e4235ff0fa3daa Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Mon, 20 Oct 2014 17:50:53 +0200 Subject: Create dev fixture projects with fixed visibility --- lib/gitlab/seeder.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/seeder.rb b/lib/gitlab/seeder.rb index 31aa3528c4c..e816eedab9e 100644 --- a/lib/gitlab/seeder.rb +++ b/lib/gitlab/seeder.rb @@ -1,9 +1,13 @@ +require 'sidekiq/testing' + module Gitlab class Seeder def self.quiet mute_mailer SeedFu.quiet = true - yield + Sidekiq::Testing.inline! do + yield + end SeedFu.quiet = false puts "\nOK".green end -- cgit v1.2.1 From e00e67db42ea3e4b994160dbdc288a8effa14713 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Tue, 28 Oct 2014 18:52:21 +0100 Subject: Drop all Postgres sequences during backup restore --- lib/backup/database.rb | 1 + lib/tasks/gitlab/db/drop_all_postgres_sequences.rake | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 lib/tasks/gitlab/db/drop_all_postgres_sequences.rake (limited to 'lib') diff --git a/lib/backup/database.rb b/lib/backup/database.rb index d12d30a9110..ea659e3b605 100644 --- a/lib/backup/database.rb +++ b/lib/backup/database.rb @@ -34,6 +34,7 @@ module Backup # Drop all tables because PostgreSQL DB dumps do not contain DROP TABLE # statements like MySQL. Rake::Task["gitlab:db:drop_all_tables"].invoke + Rake::Task["gitlab:db:drop_all_postgres_sequences"].invoke pg_env system('psql', config['database'], '-f', db_file_name) end diff --git a/lib/tasks/gitlab/db/drop_all_postgres_sequences.rake b/lib/tasks/gitlab/db/drop_all_postgres_sequences.rake new file mode 100644 index 00000000000..e9cf0a9b5e8 --- /dev/null +++ b/lib/tasks/gitlab/db/drop_all_postgres_sequences.rake @@ -0,0 +1,10 @@ +namespace :gitlab do + namespace :db do + task drop_all_postgres_sequences: :environment do + connection = ActiveRecord::Base.connection + connection.execute("SELECT c.relname FROM pg_class c WHERE c.relkind = 'S';").each do |sequence| + connection.execute("DROP SEQUENCE #{sequence['relname']}") + end + end + end +end -- cgit v1.2.1 From bafd30f92cfb754fe6864c9cd595df10b52b11f2 Mon Sep 17 00:00:00 2001 From: Andrey Krivko Date: Wed, 22 Oct 2014 22:29:26 +0700 Subject: Session API: Use case-insensitive authentication like in UI --- lib/gitlab/auth.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/auth.rb b/lib/gitlab/auth.rb index ae33c529b93..30509528b8b 100644 --- a/lib/gitlab/auth.rb +++ b/lib/gitlab/auth.rb @@ -1,7 +1,7 @@ module Gitlab class Auth def find(login, password) - user = User.find_by(email: login) || User.find_by(username: login) + user = User.by_login(login) # If no user is found, or it's an LDAP server, try LDAP. # LDAP users are only authenticated via LDAP -- cgit v1.2.1 From 2edf212a8be3bb14b844b542df587b6029897fe6 Mon Sep 17 00:00:00 2001 From: Liam Monahan Date: Sat, 1 Nov 2014 19:14:42 -0400 Subject: Expose projects_limit through users API if UserFull. --- lib/api/entities.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index d19caf5b23a..db2dead487f 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -16,7 +16,8 @@ module API class UserFull < User expose :email - expose :theme_id, :color_scheme_id, :extern_uid, :provider + expose :theme_id, :color_scheme_id, :extern_uid, :provider, \ + :projects_limit expose :can_create_group?, as: :can_create_group expose :can_create_project?, as: :can_create_project end -- cgit v1.2.1 From e3098b69e7a4bc8b08bd85093204305991d8370d Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Mon, 3 Nov 2014 11:25:31 -0300 Subject: Don't enable IPv4 *only* on nginx. The current configuration sample files only enable IPv4 by default, making the server inaccesible for many remote hosts (and an increasing amount every day). Enable IPv4 and IPv6 by default. Older servers with no external IPv6 connectivity will not fail since they'll have a local-link IPv6 address to bind to anyway. --- lib/support/nginx/gitlab | 3 ++- lib/support/nginx/gitlab-ssl | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/support/nginx/gitlab b/lib/support/nginx/gitlab index 49a68c62293..6369c1e02ff 100644 --- a/lib/support/nginx/gitlab +++ b/lib/support/nginx/gitlab @@ -33,7 +33,8 @@ upstream gitlab { ## Normal HTTP host server { - listen *:80 default_server; + listen 0.0.0.0:80 default_server; + listen [::]:80 default_server; server_name YOUR_SERVER_FQDN; ## Replace this with something like gitlab.example.com server_tokens off; ## Don't show the nginx version number, a security best practice root /home/git/gitlab/public; diff --git a/lib/support/nginx/gitlab-ssl b/lib/support/nginx/gitlab-ssl index cbb198086b5..e992ebaf656 100644 --- a/lib/support/nginx/gitlab-ssl +++ b/lib/support/nginx/gitlab-ssl @@ -39,7 +39,8 @@ upstream gitlab { ## Normal HTTP host server { - listen *:80 default_server; + listen 0.0.0.0:80; + listen [::]:80 default_server; server_name YOUR_SERVER_FQDN; ## Replace this with something like gitlab.example.com server_tokens off; ## Don't show the nginx version number, a security best practice @@ -50,7 +51,8 @@ server { ## HTTPS host server { - listen 443 ssl; + listen 0.0.0.0:443 ssl; + listen [::]:443 ssl default_server; server_name YOUR_SERVER_FQDN; ## Replace this with something like gitlab.example.com server_tokens off; root /home/git/gitlab/public; -- cgit v1.2.1 From 71ed0ab06974d0bc72ad737645c35facf2b01c31 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Mon, 3 Nov 2014 20:02:12 +0100 Subject: Fix push not allowed to protected branch if commit starts with 7 zeros. --- lib/gitlab/git.rb | 5 +++++ lib/gitlab/git_access.rb | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 lib/gitlab/git.rb (limited to 'lib') diff --git a/lib/gitlab/git.rb b/lib/gitlab/git.rb new file mode 100644 index 00000000000..67aca5e36e9 --- /dev/null +++ b/lib/gitlab/git.rb @@ -0,0 +1,5 @@ +module Gitlab + module Git + BLANK_SHA = '0' * 40 + end +end diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index b768a99a0e8..129881060d5 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -67,7 +67,7 @@ module Gitlab if forced_push?(project, oldrev, newrev) :force_push_code_to_protected_branches # and we dont allow remove of protected branch - elsif newrev =~ /0000000/ + elsif newrev == Gitlab::Git::BLANK_SHA :remove_protected_branches else :push_code_to_protected_branches @@ -85,7 +85,7 @@ module Gitlab def forced_push?(project, oldrev, newrev) return false if project.empty_repo? - if oldrev !~ /00000000/ && newrev !~ /00000000/ + if oldrev != Gitlab::Git::BLANK_SHA && newrev != Gitlab::Git::BLANK_SHA missed_refs = IO.popen(%W(git --git-dir=#{project.repository.path_to_repo} rev-list #{oldrev} ^#{newrev})).read missed_refs.split("\n").size > 0 else -- cgit v1.2.1 From 0b1084a4538bc46684c8620410988d3b1093e7ab Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Tue, 4 Nov 2014 00:50:07 +0100 Subject: Don't output to stdout from lib non-interactive methods It pollutes the test output too much. --- lib/gitlab/backend/shell.rb | 37 ++++++++++++++++++++++++------------- lib/gitlab/git_ref_validator.rb | 3 ++- lib/gitlab/utils.rb | 14 ++++++++++++++ 3 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 lib/gitlab/utils.rb (limited to 'lib') diff --git a/lib/gitlab/backend/shell.rb b/lib/gitlab/backend/shell.rb index ddb1ac61bf5..cc320da751c 100644 --- a/lib/gitlab/backend/shell.rb +++ b/lib/gitlab/backend/shell.rb @@ -16,7 +16,8 @@ module Gitlab # add_repository("gitlab/gitlab-ci") # def add_repository(name) - system gitlab_shell_projects_path, 'add-project', "#{name}.git" + Gitlab::Utils.system_silent([gitlab_shell_projects_path, + 'add-project', "#{name}.git"]) end # Import repository @@ -27,7 +28,8 @@ module Gitlab # import_repository("gitlab/gitlab-ci", "https://github.com/randx/six.git") # def import_repository(name, url) - system gitlab_shell_projects_path, 'import-project', "#{name}.git", url, '240' + Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'import-project', + "#{name}.git", url, '240']) end # Move repository @@ -39,7 +41,8 @@ module Gitlab # mv_repository("gitlab/gitlab-ci", "randx/gitlab-ci-new.git") # def mv_repository(path, new_path) - system gitlab_shell_projects_path, 'mv-project', "#{path}.git", "#{new_path}.git" + Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'mv-project', + "#{path}.git", "#{new_path}.git"]) end # Update HEAD for repository @@ -51,7 +54,8 @@ module Gitlab # update_repository_head("gitlab/gitlab-ci", "3-1-stable") # def update_repository_head(path, branch) - system gitlab_shell_projects_path, 'update-head', "#{path}.git", branch + Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'update-head', + "#{path}.git", branch]) end # Fork repository to new namespace @@ -63,7 +67,8 @@ module Gitlab # fork_repository("gitlab/gitlab-ci", "randx") # def fork_repository(path, fork_namespace) - system gitlab_shell_projects_path, 'fork-project', "#{path}.git", fork_namespace + Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'fork-project', + "#{path}.git", fork_namespace]) end # Remove repository from file system @@ -74,7 +79,8 @@ module Gitlab # remove_repository("gitlab/gitlab-ci") # def remove_repository(name) - system gitlab_shell_projects_path, 'rm-project', "#{name}.git" + Gitlab::Utils.system_silent([gitlab_shell_projects_path, + 'rm-project', "#{name}.git"]) end # Add repository branch from passed ref @@ -87,7 +93,8 @@ module Gitlab # add_branch("gitlab/gitlab-ci", "4-0-stable", "master") # def add_branch(path, branch_name, ref) - system gitlab_shell_projects_path, 'create-branch', "#{path}.git", branch_name, ref + Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'create-branch', + "#{path}.git", branch_name, ref]) end # Remove repository branch @@ -99,7 +106,8 @@ module Gitlab # rm_branch("gitlab/gitlab-ci", "4-0-stable") # def rm_branch(path, branch_name) - system gitlab_shell_projects_path, 'rm-branch', "#{path}.git", branch_name + Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'rm-branch', + "#{path}.git", branch_name]) end # Add repository tag from passed ref @@ -117,7 +125,7 @@ module Gitlab cmd = %W(#{gitlab_shell_path}/bin/gitlab-projects create-tag #{path}.git #{tag_name} #{ref}) cmd << message unless message.nil? || message.empty? - system *cmd + Gitlab::Utils.system_silent(cmd) end # Remove repository tag @@ -129,7 +137,8 @@ module Gitlab # rm_tag("gitlab/gitlab-ci", "v4.0") # def rm_tag(path, tag_name) - system gitlab_shell_projects_path, 'rm-tag', "#{path}.git", tag_name + Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'rm-tag', + "#{path}.git", tag_name]) end # Add new key to gitlab-shell @@ -138,7 +147,8 @@ module Gitlab # add_key("key-42", "sha-rsa ...") # def add_key(key_id, key_content) - system gitlab_shell_keys_path, 'add-key', key_id, key_content + Gitlab::Utils.system_silent([gitlab_shell_keys_path, + 'add-key', key_id, key_content]) end # Batch-add keys to authorized_keys @@ -157,7 +167,8 @@ module Gitlab # remove_key("key-342", "sha-rsa ...") # def remove_key(key_id, key_content) - system gitlab_shell_keys_path, 'rm-key', key_id, key_content + Gitlab::Utils.system_silent([gitlab_shell_keys_path, + 'rm-key', key_id, key_content]) end # Remove all ssh keys from gitlab shell @@ -166,7 +177,7 @@ module Gitlab # remove_all_keys # def remove_all_keys - system gitlab_shell_keys_path, 'clear' + Gitlab::Utils.system_silent([gitlab_shell_keys_path, 'clear']) end # Add empty directory for storing repositories diff --git a/lib/gitlab/git_ref_validator.rb b/lib/gitlab/git_ref_validator.rb index 13cb08948bb..0fdd4dbe577 100644 --- a/lib/gitlab/git_ref_validator.rb +++ b/lib/gitlab/git_ref_validator.rb @@ -5,7 +5,8 @@ module Gitlab # # Returns true for a valid reference name, false otherwise def validate(ref_name) - system *%W(git check-ref-format refs/#{ref_name}) + Gitlab::Utils.system_silent( + %W(git check-ref-format refs/#{ref_name})) == 0 end end end diff --git a/lib/gitlab/utils.rb b/lib/gitlab/utils.rb new file mode 100644 index 00000000000..bc30364550a --- /dev/null +++ b/lib/gitlab/utils.rb @@ -0,0 +1,14 @@ +module Gitlab + module Utils + extend self + + # Run system command without outputting to stdout. + # + # @param cmd [Array] + # @return [Integer] exit status + def system_silent(cmd) + IO.popen(cmd).close + $?.exitstatus + end + end +end -- cgit v1.2.1 From f36db59d97b375744ee1c05d07792a8d64ae945b Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Wed, 5 Nov 2014 17:14:22 +0100 Subject: Factor GITLAB_SHELL_VERSION get method --- lib/gitlab/backend/shell.rb | 7 +++++++ lib/tasks/gitlab/check.rake | 10 +++------- lib/tasks/gitlab/shell.rake | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/backend/shell.rb b/lib/gitlab/backend/shell.rb index cc320da751c..aabc7f1e69a 100644 --- a/lib/gitlab/backend/shell.rb +++ b/lib/gitlab/backend/shell.rb @@ -8,6 +8,13 @@ module Gitlab end end + class << self + def version_required + @version_required ||= File.read(Rails.root. + join('GITLAB_SHELL_VERSION')).strip + end + end + # Init new repository # # name - project path with namespace diff --git a/lib/tasks/gitlab/check.rake b/lib/tasks/gitlab/check.rake index 56e8ff44988..7ff23a7600a 100644 --- a/lib/tasks/gitlab/check.rake +++ b/lib/tasks/gitlab/check.rake @@ -574,20 +574,16 @@ namespace :gitlab do Gitlab::Shell.new.version end - def required_gitlab_shell_version - File.read(File.join(Rails.root, "GITLAB_SHELL_VERSION")).strip - end - def gitlab_shell_major_version - required_gitlab_shell_version.split(".")[0].to_i + Gitlab::Shell.version_required.split('.')[0].to_i end def gitlab_shell_minor_version - required_gitlab_shell_version.split(".")[1].to_i + Gitlab::Shell.version_required.split('.')[1].to_i end def gitlab_shell_patch_version - required_gitlab_shell_version.split(".")[2].to_i + Gitlab::Shell.version_required.split('.')[2].to_i end def has_gitlab_shell3? diff --git a/lib/tasks/gitlab/shell.rake b/lib/tasks/gitlab/shell.rake index 55f338add6a..1e2d64b56c9 100644 --- a/lib/tasks/gitlab/shell.rake +++ b/lib/tasks/gitlab/shell.rake @@ -4,7 +4,7 @@ namespace :gitlab do task :install, [:tag, :repo] => :environment do |t, args| warn_user_is_not_gitlab - default_version = File.read(File.join(Rails.root, "GITLAB_SHELL_VERSION")).strip + default_version = Gitlab::Shell.version_required args.with_defaults(tag: 'v' + default_version, repo: "https://gitlab.com/gitlab-org/gitlab-shell.git") user = Gitlab.config.gitlab.user -- cgit v1.2.1 From 586590d20ed7e47465460c0fbcd0df1b9ea45afc Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Wed, 5 Nov 2014 17:24:20 +0100 Subject: Remove unused has_gitlab_shell3? method --- lib/tasks/gitlab/check.rake | 4 ---- 1 file changed, 4 deletions(-) (limited to 'lib') diff --git a/lib/tasks/gitlab/check.rake b/lib/tasks/gitlab/check.rake index 56e8ff44988..f2705256f73 100644 --- a/lib/tasks/gitlab/check.rake +++ b/lib/tasks/gitlab/check.rake @@ -589,10 +589,6 @@ namespace :gitlab do def gitlab_shell_patch_version required_gitlab_shell_version.split(".")[2].to_i end - - def has_gitlab_shell3? - gitlab_shell_version.try(:start_with?, "v3.") - end end -- cgit v1.2.1 From e4a38e447169069f3d5042d3341ceb4bdc51bf1b Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Wed, 5 Nov 2014 17:51:08 +0100 Subject: Factor using Repository#path_to_repo --- lib/backup/repository.rb | 2 +- lib/tasks/gitlab/shell.rake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/backup/repository.rb b/lib/backup/repository.rb index 380beac708d..0bb02f1a357 100644 --- a/lib/backup/repository.rb +++ b/lib/backup/repository.rb @@ -91,7 +91,7 @@ module Backup protected def path_to_repo(project) - File.join(repos_path, project.path_with_namespace + '.git') + project.repository.path_to_repo end def path_to_bundle(project) diff --git a/lib/tasks/gitlab/shell.rake b/lib/tasks/gitlab/shell.rake index 55f338add6a..6b8f9e377fe 100644 --- a/lib/tasks/gitlab/shell.rake +++ b/lib/tasks/gitlab/shell.rake @@ -76,7 +76,7 @@ namespace :gitlab do desc "GITLAB | Build missing projects" task build_missing_projects: :environment do Project.find_each(batch_size: 1000) do |project| - path_to_repo = File.join(Gitlab.config.gitlab_shell.repos_path, "#{project.path_with_namespace}.git") + path_to_repo = project.repository.path_to_repo if File.exists?(path_to_repo) print '-' else -- cgit v1.2.1 From b33d4bc2f1d26ee3526b9d7f530f468a9d5b5a5e Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Thu, 6 Nov 2014 11:58:00 +0200 Subject: Revert "Don't output to stdout from lib non-interactive methods" This reverts commit 0b1084a4538bc46684c8620410988d3b1093e7ab. --- lib/gitlab/backend/shell.rb | 37 +++++++++++++------------------------ lib/gitlab/git_ref_validator.rb | 3 +-- lib/gitlab/utils.rb | 14 -------------- 3 files changed, 14 insertions(+), 40 deletions(-) delete mode 100644 lib/gitlab/utils.rb (limited to 'lib') diff --git a/lib/gitlab/backend/shell.rb b/lib/gitlab/backend/shell.rb index cc320da751c..ddb1ac61bf5 100644 --- a/lib/gitlab/backend/shell.rb +++ b/lib/gitlab/backend/shell.rb @@ -16,8 +16,7 @@ module Gitlab # add_repository("gitlab/gitlab-ci") # def add_repository(name) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, - 'add-project', "#{name}.git"]) + system gitlab_shell_projects_path, 'add-project', "#{name}.git" end # Import repository @@ -28,8 +27,7 @@ module Gitlab # import_repository("gitlab/gitlab-ci", "https://github.com/randx/six.git") # def import_repository(name, url) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'import-project', - "#{name}.git", url, '240']) + system gitlab_shell_projects_path, 'import-project', "#{name}.git", url, '240' end # Move repository @@ -41,8 +39,7 @@ module Gitlab # mv_repository("gitlab/gitlab-ci", "randx/gitlab-ci-new.git") # def mv_repository(path, new_path) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'mv-project', - "#{path}.git", "#{new_path}.git"]) + system gitlab_shell_projects_path, 'mv-project', "#{path}.git", "#{new_path}.git" end # Update HEAD for repository @@ -54,8 +51,7 @@ module Gitlab # update_repository_head("gitlab/gitlab-ci", "3-1-stable") # def update_repository_head(path, branch) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'update-head', - "#{path}.git", branch]) + system gitlab_shell_projects_path, 'update-head', "#{path}.git", branch end # Fork repository to new namespace @@ -67,8 +63,7 @@ module Gitlab # fork_repository("gitlab/gitlab-ci", "randx") # def fork_repository(path, fork_namespace) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'fork-project', - "#{path}.git", fork_namespace]) + system gitlab_shell_projects_path, 'fork-project', "#{path}.git", fork_namespace end # Remove repository from file system @@ -79,8 +74,7 @@ module Gitlab # remove_repository("gitlab/gitlab-ci") # def remove_repository(name) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, - 'rm-project', "#{name}.git"]) + system gitlab_shell_projects_path, 'rm-project', "#{name}.git" end # Add repository branch from passed ref @@ -93,8 +87,7 @@ module Gitlab # add_branch("gitlab/gitlab-ci", "4-0-stable", "master") # def add_branch(path, branch_name, ref) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'create-branch', - "#{path}.git", branch_name, ref]) + system gitlab_shell_projects_path, 'create-branch', "#{path}.git", branch_name, ref end # Remove repository branch @@ -106,8 +99,7 @@ module Gitlab # rm_branch("gitlab/gitlab-ci", "4-0-stable") # def rm_branch(path, branch_name) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'rm-branch', - "#{path}.git", branch_name]) + system gitlab_shell_projects_path, 'rm-branch', "#{path}.git", branch_name end # Add repository tag from passed ref @@ -125,7 +117,7 @@ module Gitlab cmd = %W(#{gitlab_shell_path}/bin/gitlab-projects create-tag #{path}.git #{tag_name} #{ref}) cmd << message unless message.nil? || message.empty? - Gitlab::Utils.system_silent(cmd) + system *cmd end # Remove repository tag @@ -137,8 +129,7 @@ module Gitlab # rm_tag("gitlab/gitlab-ci", "v4.0") # def rm_tag(path, tag_name) - Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'rm-tag', - "#{path}.git", tag_name]) + system gitlab_shell_projects_path, 'rm-tag', "#{path}.git", tag_name end # Add new key to gitlab-shell @@ -147,8 +138,7 @@ module Gitlab # add_key("key-42", "sha-rsa ...") # def add_key(key_id, key_content) - Gitlab::Utils.system_silent([gitlab_shell_keys_path, - 'add-key', key_id, key_content]) + system gitlab_shell_keys_path, 'add-key', key_id, key_content end # Batch-add keys to authorized_keys @@ -167,8 +157,7 @@ module Gitlab # remove_key("key-342", "sha-rsa ...") # def remove_key(key_id, key_content) - Gitlab::Utils.system_silent([gitlab_shell_keys_path, - 'rm-key', key_id, key_content]) + system gitlab_shell_keys_path, 'rm-key', key_id, key_content end # Remove all ssh keys from gitlab shell @@ -177,7 +166,7 @@ module Gitlab # remove_all_keys # def remove_all_keys - Gitlab::Utils.system_silent([gitlab_shell_keys_path, 'clear']) + system gitlab_shell_keys_path, 'clear' end # Add empty directory for storing repositories diff --git a/lib/gitlab/git_ref_validator.rb b/lib/gitlab/git_ref_validator.rb index 0fdd4dbe577..13cb08948bb 100644 --- a/lib/gitlab/git_ref_validator.rb +++ b/lib/gitlab/git_ref_validator.rb @@ -5,8 +5,7 @@ module Gitlab # # Returns true for a valid reference name, false otherwise def validate(ref_name) - Gitlab::Utils.system_silent( - %W(git check-ref-format refs/#{ref_name})) == 0 + system *%W(git check-ref-format refs/#{ref_name}) end end end diff --git a/lib/gitlab/utils.rb b/lib/gitlab/utils.rb deleted file mode 100644 index bc30364550a..00000000000 --- a/lib/gitlab/utils.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Gitlab - module Utils - extend self - - # Run system command without outputting to stdout. - # - # @param cmd [Array] - # @return [Integer] exit status - def system_silent(cmd) - IO.popen(cmd).close - $?.exitstatus - end - end -end -- cgit v1.2.1 From d1b489e048e2bd9304ae335d9105e6efde99012b Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Thu, 6 Nov 2014 13:07:16 +0200 Subject: Revert "Revert "Don't output to stdout from lib non-interactive methods"" This reverts commit b33d4bc2f1d26ee3526b9d7f530f468a9d5b5a5e. --- lib/gitlab/backend/shell.rb | 37 ++++++++++++++++++++++++------------- lib/gitlab/git_ref_validator.rb | 3 ++- lib/gitlab/utils.rb | 14 ++++++++++++++ 3 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 lib/gitlab/utils.rb (limited to 'lib') diff --git a/lib/gitlab/backend/shell.rb b/lib/gitlab/backend/shell.rb index ddb1ac61bf5..cc320da751c 100644 --- a/lib/gitlab/backend/shell.rb +++ b/lib/gitlab/backend/shell.rb @@ -16,7 +16,8 @@ module Gitlab # add_repository("gitlab/gitlab-ci") # def add_repository(name) - system gitlab_shell_projects_path, 'add-project', "#{name}.git" + Gitlab::Utils.system_silent([gitlab_shell_projects_path, + 'add-project', "#{name}.git"]) end # Import repository @@ -27,7 +28,8 @@ module Gitlab # import_repository("gitlab/gitlab-ci", "https://github.com/randx/six.git") # def import_repository(name, url) - system gitlab_shell_projects_path, 'import-project', "#{name}.git", url, '240' + Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'import-project', + "#{name}.git", url, '240']) end # Move repository @@ -39,7 +41,8 @@ module Gitlab # mv_repository("gitlab/gitlab-ci", "randx/gitlab-ci-new.git") # def mv_repository(path, new_path) - system gitlab_shell_projects_path, 'mv-project', "#{path}.git", "#{new_path}.git" + Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'mv-project', + "#{path}.git", "#{new_path}.git"]) end # Update HEAD for repository @@ -51,7 +54,8 @@ module Gitlab # update_repository_head("gitlab/gitlab-ci", "3-1-stable") # def update_repository_head(path, branch) - system gitlab_shell_projects_path, 'update-head', "#{path}.git", branch + Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'update-head', + "#{path}.git", branch]) end # Fork repository to new namespace @@ -63,7 +67,8 @@ module Gitlab # fork_repository("gitlab/gitlab-ci", "randx") # def fork_repository(path, fork_namespace) - system gitlab_shell_projects_path, 'fork-project', "#{path}.git", fork_namespace + Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'fork-project', + "#{path}.git", fork_namespace]) end # Remove repository from file system @@ -74,7 +79,8 @@ module Gitlab # remove_repository("gitlab/gitlab-ci") # def remove_repository(name) - system gitlab_shell_projects_path, 'rm-project', "#{name}.git" + Gitlab::Utils.system_silent([gitlab_shell_projects_path, + 'rm-project', "#{name}.git"]) end # Add repository branch from passed ref @@ -87,7 +93,8 @@ module Gitlab # add_branch("gitlab/gitlab-ci", "4-0-stable", "master") # def add_branch(path, branch_name, ref) - system gitlab_shell_projects_path, 'create-branch', "#{path}.git", branch_name, ref + Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'create-branch', + "#{path}.git", branch_name, ref]) end # Remove repository branch @@ -99,7 +106,8 @@ module Gitlab # rm_branch("gitlab/gitlab-ci", "4-0-stable") # def rm_branch(path, branch_name) - system gitlab_shell_projects_path, 'rm-branch', "#{path}.git", branch_name + Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'rm-branch', + "#{path}.git", branch_name]) end # Add repository tag from passed ref @@ -117,7 +125,7 @@ module Gitlab cmd = %W(#{gitlab_shell_path}/bin/gitlab-projects create-tag #{path}.git #{tag_name} #{ref}) cmd << message unless message.nil? || message.empty? - system *cmd + Gitlab::Utils.system_silent(cmd) end # Remove repository tag @@ -129,7 +137,8 @@ module Gitlab # rm_tag("gitlab/gitlab-ci", "v4.0") # def rm_tag(path, tag_name) - system gitlab_shell_projects_path, 'rm-tag', "#{path}.git", tag_name + Gitlab::Utils.system_silent([gitlab_shell_projects_path, 'rm-tag', + "#{path}.git", tag_name]) end # Add new key to gitlab-shell @@ -138,7 +147,8 @@ module Gitlab # add_key("key-42", "sha-rsa ...") # def add_key(key_id, key_content) - system gitlab_shell_keys_path, 'add-key', key_id, key_content + Gitlab::Utils.system_silent([gitlab_shell_keys_path, + 'add-key', key_id, key_content]) end # Batch-add keys to authorized_keys @@ -157,7 +167,8 @@ module Gitlab # remove_key("key-342", "sha-rsa ...") # def remove_key(key_id, key_content) - system gitlab_shell_keys_path, 'rm-key', key_id, key_content + Gitlab::Utils.system_silent([gitlab_shell_keys_path, + 'rm-key', key_id, key_content]) end # Remove all ssh keys from gitlab shell @@ -166,7 +177,7 @@ module Gitlab # remove_all_keys # def remove_all_keys - system gitlab_shell_keys_path, 'clear' + Gitlab::Utils.system_silent([gitlab_shell_keys_path, 'clear']) end # Add empty directory for storing repositories diff --git a/lib/gitlab/git_ref_validator.rb b/lib/gitlab/git_ref_validator.rb index 13cb08948bb..0fdd4dbe577 100644 --- a/lib/gitlab/git_ref_validator.rb +++ b/lib/gitlab/git_ref_validator.rb @@ -5,7 +5,8 @@ module Gitlab # # Returns true for a valid reference name, false otherwise def validate(ref_name) - system *%W(git check-ref-format refs/#{ref_name}) + Gitlab::Utils.system_silent( + %W(git check-ref-format refs/#{ref_name})) == 0 end end end diff --git a/lib/gitlab/utils.rb b/lib/gitlab/utils.rb new file mode 100644 index 00000000000..bc30364550a --- /dev/null +++ b/lib/gitlab/utils.rb @@ -0,0 +1,14 @@ +module Gitlab + module Utils + extend self + + # Run system command without outputting to stdout. + # + # @param cmd [Array] + # @return [Integer] exit status + def system_silent(cmd) + IO.popen(cmd).close + $?.exitstatus + end + end +end -- cgit v1.2.1 From bf8b87411701667a8d9e608b2e7b3171c4c3e551 Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Thu, 6 Nov 2014 11:47:38 +0200 Subject: fix system silent call --- lib/gitlab/git_ref_validator.rb | 2 +- lib/gitlab/utils.rb | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/git_ref_validator.rb b/lib/gitlab/git_ref_validator.rb index 0fdd4dbe577..39d17def930 100644 --- a/lib/gitlab/git_ref_validator.rb +++ b/lib/gitlab/git_ref_validator.rb @@ -6,7 +6,7 @@ module Gitlab # Returns true for a valid reference name, false otherwise def validate(ref_name) Gitlab::Utils.system_silent( - %W(git check-ref-format refs/#{ref_name})) == 0 + %W(git check-ref-format refs/#{ref_name})) end end end diff --git a/lib/gitlab/utils.rb b/lib/gitlab/utils.rb index bc30364550a..bd184c27187 100644 --- a/lib/gitlab/utils.rb +++ b/lib/gitlab/utils.rb @@ -5,10 +5,9 @@ module Gitlab # Run system command without outputting to stdout. # # @param cmd [Array] - # @return [Integer] exit status + # @return [Boolean] def system_silent(cmd) - IO.popen(cmd).close - $?.exitstatus + Popen::popen(cmd).last.zero? end end end -- cgit v1.2.1 From 271a3520794d0d977ba3907963871da59cee554f Mon Sep 17 00:00:00 2001 From: Ben Bodenmiller Date: Sat, 8 Nov 2014 23:33:27 -0800 Subject: minor updates & formatting changes minor updates @ formatting changes to match other versions of file. Unify formatting of https://github.com/gitlabhq/gitlabhq/blob/master/lib/support/nginx/gitlab, https://github.com/gitlabhq/gitlabhq/blob/master/lib/support/nginx/gitlab-ssl, & https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/files/gitlab-cookbooks/gitlab/templates/default/nginx-gitlab-http.conf.erb --- lib/support/nginx/gitlab | 7 +++++-- lib/support/nginx/gitlab-ssl | 24 ++++++++++-------------- 2 files changed, 15 insertions(+), 16 deletions(-) (limited to 'lib') diff --git a/lib/support/nginx/gitlab b/lib/support/nginx/gitlab index 49a68c62293..eeba62c6b10 100644 --- a/lib/support/nginx/gitlab +++ b/lib/support/nginx/gitlab @@ -1,5 +1,5 @@ ## GitLab -## Maintainer: @randx +## Contributors: randx, yin8086, sashkab, orkoden, axilleas, bbodenmiller ## ## Lines starting with two hashes (##) are comments with information. ## Lines starting with one hash (#) are configuration parameters that can be uncommented. @@ -15,7 +15,7 @@ ## - installing an old version of Nginx with the chunkin module [2] compiled in, or ## - using a newer version of Nginx. ## -## At the time of writing we do not know if either of these theoretical solutions works. +## At the time of writing we do not know if either of these theoretical solutions works. ## As a workaround users can use Git over SSH to push large files. ## ## [0] https://git.kernel.org/cgit/git/git.git/tree/Documentation/technical/http-protocol.txt#n99 @@ -26,6 +26,7 @@ ## configuration ## ################################### ## +## See installation.md#using-https for additional HTTPS configuration details. upstream gitlab { server unix:/home/git/gitlab/tmp/sockets/gitlab.socket fail_timeout=0; @@ -42,6 +43,8 @@ server { ## Or if you want to accept large git objects over http client_max_body_size 20m; + ## See app/controllers/application_controller.rb for headers set + ## Individual nginx logs for this GitLab vhost access_log /var/log/nginx/gitlab_access.log; error_log /var/log/nginx/gitlab_error.log; diff --git a/lib/support/nginx/gitlab-ssl b/lib/support/nginx/gitlab-ssl index cbb198086b5..979e032a1c5 100644 --- a/lib/support/nginx/gitlab-ssl +++ b/lib/support/nginx/gitlab-ssl @@ -1,5 +1,5 @@ ## GitLab -## Contributors: randx, yin8086, sashkab, orkoden, axilleas +## Contributors: randx, yin8086, sashkab, orkoden, axilleas, bbodenmiller ## ## Modified from nginx http version ## Modified from http://blog.phusion.nl/2012/04/21/tutorial-setting-up-gitlab-on-debian-6/ @@ -26,9 +26,8 @@ ## [1] https://github.com/agentzh/chunkin-nginx-module#status ## [2] https://github.com/agentzh/chunkin-nginx-module ## -## ################################### -## SSL configuration ## +## configuration ## ################################### ## ## See installation.md#using-https for additional HTTPS configuration details. @@ -37,22 +36,22 @@ upstream gitlab { server unix:/home/git/gitlab/tmp/sockets/gitlab.socket fail_timeout=0; } -## Normal HTTP host +## Redirects all HTTP traffic to the HTTPS host server { listen *:80 default_server; server_name YOUR_SERVER_FQDN; ## Replace this with something like gitlab.example.com server_tokens off; ## Don't show the nginx version number, a security best practice - - ## Redirects all traffic to the HTTPS host - root /nowhere; ## root doesn't have to be a valid path since we are redirecting - rewrite ^ https://$server_name$request_uri? permanent; + return 301 https://$server_name$request_uri; + access_log /var/log/nginx/gitlab_access.log; + error_log /var/log/nginx/gitlab_error.log; } + ## HTTPS host server { listen 443 ssl; server_name YOUR_SERVER_FQDN; ## Replace this with something like gitlab.example.com - server_tokens off; + server_tokens off; ## Don't show the nginx version number, a security best practice root /home/git/gitlab/public; ## Increase this if you want to upload large attachments @@ -70,12 +69,9 @@ server { ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; + ssl_session_timeout 5m; - ## [WARNING] The following header states that the browser should only communicate - ## with your server over a secure connection for the next 24 months. - add_header Strict-Transport-Security max-age=63072000; - add_header X-Frame-Options SAMEORIGIN; - add_header X-Content-Type-Options nosniff; + ## See app/controllers/application_controller.rb for headers set ## [Optional] If your certficate has OCSP, enable OCSP stapling to reduce the overhead and latency of running SSL. ## Replace with your ssl_trusted_certificate. For more info see: -- cgit v1.2.1 From f56541de9a488dec68f4e98f738f90c51d898fc9 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 10 Nov 2014 16:17:04 +0200 Subject: Revert "Create dev fixture projects with fixed visibility" This reverts commit a9fadce361163e97eb1de0ec62e4235ff0fa3daa. --- lib/gitlab/seeder.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/seeder.rb b/lib/gitlab/seeder.rb index e816eedab9e..31aa3528c4c 100644 --- a/lib/gitlab/seeder.rb +++ b/lib/gitlab/seeder.rb @@ -1,13 +1,9 @@ -require 'sidekiq/testing' - module Gitlab class Seeder def self.quiet mute_mailer SeedFu.quiet = true - Sidekiq::Testing.inline! do - yield - end + yield SeedFu.quiet = false puts "\nOK".green end -- cgit v1.2.1 From 1f902c2464a4f5c68f1b42be597d4e3e25a32130 Mon Sep 17 00:00:00 2001 From: Marvin Frick Date: Wed, 12 Nov 2014 12:06:24 +0100 Subject: fixes the `block_removed_ldap_users` rake task In e23a26a (and later 1bc9936) the API for Gitlab::LDAP::Adapter was changed. I assume this rake task was an oversight in the refactoring of the changed class. While being on it, I noticed that already blocked users cannot be blocked again. --- lib/tasks/gitlab/cleanup.rake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/tasks/gitlab/cleanup.rake b/lib/tasks/gitlab/cleanup.rake index 63dcdc52370..189ad6090a4 100644 --- a/lib/tasks/gitlab/cleanup.rake +++ b/lib/tasks/gitlab/cleanup.rake @@ -92,11 +92,11 @@ namespace :gitlab do User.ldap.each do |ldap_user| print "#{ldap_user.name} (#{ldap_user.extern_uid}) ..." - if Gitlab::LDAP::Access.open { |access| access.allowed?(ldap_user) } + if Gitlab::LDAP::Access.allowed?(ldap_user) puts " [OK]".green else if block_flag - ldap_user.block! + ldap_user.block! unless ldap_user.blocked? puts " [BLOCKED]".red else puts " [NOT IN LDAP]".yellow -- cgit v1.2.1 From 4a5044e30269f8b3c6c075093cd4646a478231c7 Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Thu, 13 Nov 2014 13:09:47 +0100 Subject: Correctly restore empty repositories. If a project is being restored, but there is no bundle file, the project was empty when it was backed up. In this case, just use git init --base to create a new bare repository. --- lib/backup/repository.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/backup/repository.rb b/lib/backup/repository.rb index 0bb02f1a357..faa1b3b4099 100644 --- a/lib/backup/repository.rb +++ b/lib/backup/repository.rb @@ -59,7 +59,13 @@ module Backup project.namespace.ensure_dir_exist if project.namespace - if system(*%W(git clone --bare #{path_to_bundle(project)} #{path_to_repo(project)}), silent) + if File.exists?(path_to_bundle(project)) + cmd = %W(git clone --bare #{path_to_bundle(project)} #{path_to_repo(project)}) + else + cmd = %W(git init --bare #{path_to_repo(project)}) + end + + if system(*cmd, silent) puts "[DONE]".green else puts "[FAILED]".red -- cgit v1.2.1 From e375d0de65894a03d382c462fe99bbe66915dba7 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Fri, 14 Nov 2014 09:32:49 +0100 Subject: Typo in project API events comment --- lib/api/projects.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 7fcf97d1ad6..e0123dc1ea5 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -66,7 +66,7 @@ module API # Parameters: # id (required) - The ID of a project # Example Request: - # GET /projects/:id + # GET /projects/:id/events get ":id/events" do limit = (params[:per_page] || 20).to_i offset = (params[:page] || 0).to_i * limit -- cgit v1.2.1 From 533f4cdf30b38c587f7a91f0dfd898b907ecd944 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Wed, 22 Oct 2014 10:54:59 +0200 Subject: gitlab shell works if multiple rubies installed Before this it would fail because git hooks automatically prepend things to the path, which can lead the wrong Ruby version to be called in which dependencies are not installed. To make sure that this is correct, the forked_merge_requests commented out test that depends on this change was uncommented. For that test to pass, it is also necessary to setup the mock server on port 3001 under test_env.rb. --- lib/gitlab/backend/shell.rb | 21 +++++++++++++++++++++ lib/tasks/gitlab/shell.rake | 12 +++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/backend/shell.rb b/lib/gitlab/backend/shell.rb index aabc7f1e69a..7b10ab539eb 100644 --- a/lib/gitlab/backend/shell.rb +++ b/lib/gitlab/backend/shell.rb @@ -1,3 +1,5 @@ +require 'securerandom' + module Gitlab class Shell class AccessDenied < StandardError; end @@ -13,6 +15,25 @@ module Gitlab @version_required ||= File.read(Rails.root. join('GITLAB_SHELL_VERSION')).strip end + + # Be sure to restart your server when you modify this method. + def setup_secret_token + secret_file = Rails.root.join('.gitlab_shell_secret') + gitlab_shell_symlink = File.join(Gitlab.config.gitlab_shell.path, + '.gitlab_shell_secret') + + unless File.exist? secret_file + # Generate a new token of 16 random hexadecimal characters + # and store it in secret_file. + token = SecureRandom.hex(16) + File.write(secret_file, token) + end + + if File.exist?(Gitlab.config.gitlab_shell.path) && + !File.exist?(gitlab_shell_symlink) + FileUtils.symlink(secret_file, gitlab_shell_symlink) + end + end end # Init new repository diff --git a/lib/tasks/gitlab/shell.rake b/lib/tasks/gitlab/shell.rake index 202e55c89ad..d3cc7135c54 100644 --- a/lib/tasks/gitlab/shell.rake +++ b/lib/tasks/gitlab/shell.rake @@ -22,10 +22,14 @@ namespace :gitlab do # Make sure we're on the right tag Dir.chdir(target_dir) do + # Allows to change the origin URL to the fork + # when developing gitlab-shell. + sh(*%W(git remote set-url origin #{args.repo})) + # First try to checkout without fetching # to avoid stalling tests if the Internet is down. - reset = "git reset --hard $(git describe #{args.tag} || git describe origin/#{args.tag})" - sh "#{reset} || git fetch origin && #{reset}" + reset = "(rev=\"$(git describe #{args.tag} || git describe \"origin/#{args.tag}\")\" && git reset --hard \"$rev\")" + sh "#{reset} || (git fetch --tags origin && #{reset})" config = { user: user, @@ -37,7 +41,7 @@ namespace :gitlab do bin: %x{which redis-cli}.chomp, namespace: "resque:gitlab" }.stringify_keys, - log_level: "INFO", + log_level: Rails.env.test? ? 'DEBUG' : 'INFO', audit_usernames: false }.stringify_keys @@ -66,6 +70,8 @@ namespace :gitlab do File.open(File.join(home_dir, ".ssh", "environment"), "w+") do |f| f.puts "PATH=#{ENV['PATH']}" end + + Gitlab::Shell.setup_secret_token end desc "GITLAB | Setup gitlab-shell" -- cgit v1.2.1 From 53bf52f191612df92d993cbcd3c4d6c89ab9c95a Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Fri, 14 Nov 2014 18:23:55 +0200 Subject: Better message for failed pushes because of git hooks Conflicts: lib/gitlab/git_access.rb spec/lib/gitlab/git_access_spec.rb --- lib/api/internal.rb | 2 +- lib/gitlab/backend/grack_auth.rb | 2 +- lib/gitlab/git_access.rb | 51 +++++++++++++++++++++++----------------- lib/gitlab/git_access_status.rb | 15 ++++++++++++ lib/gitlab/git_access_wiki.rb | 8 +++++-- 5 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 lib/gitlab/git_access_status.rb (limited to 'lib') diff --git a/lib/api/internal.rb b/lib/api/internal.rb index ebf2296097d..1648834f034 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -43,7 +43,7 @@ module API return false unless actor - access.allowed?( + access.check( actor, params[:action], project, diff --git a/lib/gitlab/backend/grack_auth.rb b/lib/gitlab/backend/grack_auth.rb index df1461a45c9..762639414e0 100644 --- a/lib/gitlab/backend/grack_auth.rb +++ b/lib/gitlab/backend/grack_auth.rb @@ -80,7 +80,7 @@ module Grack case git_cmd when *Gitlab::GitAccess::DOWNLOAD_COMMANDS if user - Gitlab::GitAccess.new.download_allowed?(user, project) + Gitlab::GitAccess.new.download_access_check(user, project).allowed? elsif project.public? # Allow clone/fetch for public projects true diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index 129881060d5..3452240dad8 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -5,61 +5,60 @@ module Gitlab attr_reader :params, :project, :git_cmd, :user - def allowed?(actor, cmd, project, changes = nil) + def check(actor, cmd, project, changes = nil) case cmd when *DOWNLOAD_COMMANDS if actor.is_a? User - download_allowed?(actor, project) + download_access_check(actor, project) elsif actor.is_a? DeployKey actor.projects.include?(project) elsif actor.is_a? Key - download_allowed?(actor.user, project) + download_access_check(actor.user, project) else raise 'Wrong actor' end when *PUSH_COMMANDS if actor.is_a? User - push_allowed?(actor, project, changes) + push_access_check(actor, project, changes) elsif actor.is_a? DeployKey - # Deploy key not allowed to push - return false + return build_status_object(false, "Deploy key not allowed to push") elsif actor.is_a? Key - push_allowed?(actor.user, project, changes) + push_access_check(actor.user, project, changes) else raise 'Wrong actor' end else - false + return build_status_object(false, "Wrong command") end end - def download_allowed?(user, project) - if user && user_allowed?(user) - user.can?(:download_code, project) + def download_access_check(user, project) + if user && user_allowed?(user) && user.can?(:download_code, project) + build_status_object(true) else - false + build_status_object(false, "You don't have access") end end - def push_allowed?(user, project, changes) - return false unless user && user_allowed?(user) - return true if changes.blank? + def push_access_check(user, project, changes) + return build_status_object(false, "You don't have access") unless user && user_allowed?(user) + return build_status_object(true) if changes.blank? changes = changes.lines if changes.kind_of?(String) # Iterate over all changes to find if user allowed all of them to be applied changes.each do |change| - unless change_allowed?(user, project, change) + status = change_access_check(user, project, change) + unless status.allowed? # If user does not have access to make at least one change - cancel all push - return false + return status end end - # If user has access to make all changes - true + return build_status_object(true) end - def change_allowed?(user, project, change) + def change_access_check(user, project, change) oldrev, newrev, ref = change.split(' ') action = if project.protected_branch?(branch_name(ref)) @@ -79,7 +78,11 @@ module Gitlab :push_code end - user.can?(action, project) + if user.can?(action, project) + build_status_object(true) + else + build_status_object(false, "You don't have permission") + end end def forced_push?(project, oldrev, newrev) @@ -116,5 +119,11 @@ module Gitlab nil end end + + protected + + def build_status_object(status, message = '') + GitAccessStatus.new(status, message) + end end end diff --git a/lib/gitlab/git_access_status.rb b/lib/gitlab/git_access_status.rb new file mode 100644 index 00000000000..3d451ecebee --- /dev/null +++ b/lib/gitlab/git_access_status.rb @@ -0,0 +1,15 @@ +module Gitlab + class GitAccessStatus + attr_accessor :status, :message + alias_method :allowed?, :status + + def initialize(status, message = '') + @status = status + @message = message + end + + def to_json + {status: @status, message: @message}.to_json + end + end +end \ No newline at end of file diff --git a/lib/gitlab/git_access_wiki.rb b/lib/gitlab/git_access_wiki.rb index 9f0eb3be20f..f7d1428deb2 100644 --- a/lib/gitlab/git_access_wiki.rb +++ b/lib/gitlab/git_access_wiki.rb @@ -1,7 +1,11 @@ module Gitlab class GitAccessWiki < GitAccess - def change_allowed?(user, project, change) - user.can?(:write_wiki, project) + def change_allowed_check(user, project, change) + if user.can?(:write_wiki, project) + build_status_object(true) + else + build_status_object(false, "You don't have access") + end end end end -- cgit v1.2.1 From f7bf892cca6bb8106194c14bef1ed9ddfc26ec91 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 18 Nov 2014 17:14:36 +0200 Subject: Revert "gitlab shell works if multiple rubies installed" This reverts commit 533f4cdf30b38c587f7a91f0dfd898b907ecd944. --- lib/gitlab/backend/shell.rb | 21 --------------------- lib/tasks/gitlab/shell.rake | 12 +++--------- 2 files changed, 3 insertions(+), 30 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/backend/shell.rb b/lib/gitlab/backend/shell.rb index 7b10ab539eb..aabc7f1e69a 100644 --- a/lib/gitlab/backend/shell.rb +++ b/lib/gitlab/backend/shell.rb @@ -1,5 +1,3 @@ -require 'securerandom' - module Gitlab class Shell class AccessDenied < StandardError; end @@ -15,25 +13,6 @@ module Gitlab @version_required ||= File.read(Rails.root. join('GITLAB_SHELL_VERSION')).strip end - - # Be sure to restart your server when you modify this method. - def setup_secret_token - secret_file = Rails.root.join('.gitlab_shell_secret') - gitlab_shell_symlink = File.join(Gitlab.config.gitlab_shell.path, - '.gitlab_shell_secret') - - unless File.exist? secret_file - # Generate a new token of 16 random hexadecimal characters - # and store it in secret_file. - token = SecureRandom.hex(16) - File.write(secret_file, token) - end - - if File.exist?(Gitlab.config.gitlab_shell.path) && - !File.exist?(gitlab_shell_symlink) - FileUtils.symlink(secret_file, gitlab_shell_symlink) - end - end end # Init new repository diff --git a/lib/tasks/gitlab/shell.rake b/lib/tasks/gitlab/shell.rake index d3cc7135c54..202e55c89ad 100644 --- a/lib/tasks/gitlab/shell.rake +++ b/lib/tasks/gitlab/shell.rake @@ -22,14 +22,10 @@ namespace :gitlab do # Make sure we're on the right tag Dir.chdir(target_dir) do - # Allows to change the origin URL to the fork - # when developing gitlab-shell. - sh(*%W(git remote set-url origin #{args.repo})) - # First try to checkout without fetching # to avoid stalling tests if the Internet is down. - reset = "(rev=\"$(git describe #{args.tag} || git describe \"origin/#{args.tag}\")\" && git reset --hard \"$rev\")" - sh "#{reset} || (git fetch --tags origin && #{reset})" + reset = "git reset --hard $(git describe #{args.tag} || git describe origin/#{args.tag})" + sh "#{reset} || git fetch origin && #{reset}" config = { user: user, @@ -41,7 +37,7 @@ namespace :gitlab do bin: %x{which redis-cli}.chomp, namespace: "resque:gitlab" }.stringify_keys, - log_level: Rails.env.test? ? 'DEBUG' : 'INFO', + log_level: "INFO", audit_usernames: false }.stringify_keys @@ -70,8 +66,6 @@ namespace :gitlab do File.open(File.join(home_dir, ".ssh", "environment"), "w+") do |f| f.puts "PATH=#{ENV['PATH']}" end - - Gitlab::Shell.setup_secret_token end desc "GITLAB | Setup gitlab-shell" -- cgit v1.2.1 From 7c54c63ac14eb8f5ce0e364d709988fcfe4dda64 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Thu, 20 Nov 2014 15:46:04 +0100 Subject: Add CRON=1 backup setting for quiet backups --- lib/backup/database.rb | 12 ++++++------ lib/backup/manager.rb | 32 ++++++++++++++++---------------- lib/backup/repository.rb | 41 +++++++++++++++++++++++++---------------- lib/tasks/gitlab/backup.rake | 35 +++++++++++++++++++++++------------ 4 files changed, 70 insertions(+), 50 deletions(-) (limited to 'lib') diff --git a/lib/backup/database.rb b/lib/backup/database.rb index ea659e3b605..9ab6aca276d 100644 --- a/lib/backup/database.rb +++ b/lib/backup/database.rb @@ -13,10 +13,10 @@ module Backup def dump success = case config["adapter"] when /^mysql/ then - print "Dumping MySQL database #{config['database']} ... " + $progress.print "Dumping MySQL database #{config['database']} ... " system('mysqldump', *mysql_args, config['database'], out: db_file_name) when "postgresql" then - print "Dumping PostgreSQL database #{config['database']} ... " + $progress.print "Dumping PostgreSQL database #{config['database']} ... " pg_env system('pg_dump', config['database'], out: db_file_name) end @@ -27,10 +27,10 @@ module Backup def restore success = case config["adapter"] when /^mysql/ then - print "Restoring MySQL database #{config['database']} ... " + $progress.print "Restoring MySQL database #{config['database']} ... " system('mysql', *mysql_args, config['database'], in: db_file_name) when "postgresql" then - print "Restoring PostgreSQL database #{config['database']} ... " + $progress.print "Restoring PostgreSQL database #{config['database']} ... " # Drop all tables because PostgreSQL DB dumps do not contain DROP TABLE # statements like MySQL. Rake::Task["gitlab:db:drop_all_tables"].invoke @@ -69,9 +69,9 @@ module Backup def report_success(success) if success - puts '[DONE]'.green + $progress.puts '[DONE]'.green else - puts '[FAILED]'.red + $progress.puts '[FAILED]'.red end end end diff --git a/lib/backup/manager.rb b/lib/backup/manager.rb index 03fe0f0b02f..ab8db4e9837 100644 --- a/lib/backup/manager.rb +++ b/lib/backup/manager.rb @@ -18,11 +18,11 @@ module Backup end # create archive - print "Creating backup archive: #{tar_file} ... " + $progress.print "Creating backup archive: #{tar_file} ... " if Kernel.system('tar', '-cf', tar_file, *BACKUP_CONTENTS) - puts "done".green + $progress.puts "done".green else - puts "failed".red + puts "creating archive #{tar_file} failed".red abort 'Backup failed' end @@ -31,37 +31,37 @@ module Backup def upload(tar_file) remote_directory = Gitlab.config.backup.upload.remote_directory - print "Uploading backup archive to remote storage #{remote_directory} ... " + $progress.print "Uploading backup archive to remote storage #{remote_directory} ... " connection_settings = Gitlab.config.backup.upload.connection if connection_settings.blank? - puts "skipped".yellow + $progress.puts "skipped".yellow return end connection = ::Fog::Storage.new(connection_settings) directory = connection.directories.get(remote_directory) if directory.files.create(key: tar_file, body: File.open(tar_file), public: false) - puts "done".green + $progress.puts "done".green else - puts "failed".red + puts "uploading backup to #{remote_directory} failed".red abort 'Backup failed' end end def cleanup - print "Deleting tmp directories ... " + $progress.print "Deleting tmp directories ... " if Kernel.system('rm', '-rf', *BACKUP_CONTENTS) - puts "done".green + $progress.puts "done".green else - puts "failed".red + puts "deleting tmp directory failed".red abort 'Backup failed' end end def remove_old # delete backups - print "Deleting old backups ... " + $progress.print "Deleting old backups ... " keep_time = Gitlab.config.backup.keep_time.to_i path = Gitlab.config.backup.path @@ -76,9 +76,9 @@ module Backup end end end - puts "done. (#{removed} removed)".green + $progress.puts "done. (#{removed} removed)".green else - puts "skipping".yellow + $progress.puts "skipping".yellow end end @@ -101,12 +101,12 @@ module Backup exit 1 end - print "Unpacking backup ... " + $progress.print "Unpacking backup ... " unless Kernel.system(*%W(tar -xf #{tar_file})) - puts "failed".red + puts "unpacking backup failed".red exit 1 else - puts "done".green + $progress.puts "done".green end settings = YAML.load_file("backup_information.yml") diff --git a/lib/backup/repository.rb b/lib/backup/repository.rb index faa1b3b4099..f39fba23cf5 100644 --- a/lib/backup/repository.rb +++ b/lib/backup/repository.rb @@ -8,19 +8,21 @@ module Backup prepare Project.find_each(batch_size: 1000) do |project| - print " * #{project.path_with_namespace} ... " + $progress.print " * #{project.path_with_namespace} ... " # Create namespace dir if missing FileUtils.mkdir_p(File.join(backup_repos_path, project.namespace.path)) if project.namespace if project.empty_repo? - puts "[SKIPPED]".cyan + $progress.puts "[SKIPPED]".cyan else - output, status = Gitlab::Popen.popen(%W(git --git-dir=#{path_to_repo(project)} bundle create #{path_to_bundle(project)} --all)) + cmd = %W(git --git-dir=#{path_to_repo(project)} bundle create #{path_to_bundle(project)} --all) + output, status = Gitlab::Popen.popen(cmd) if status.zero? - puts "[DONE]".green + $progress.puts "[DONE]".green else puts "[FAILED]".red + puts "failed: #{cmd.join(' ')}" puts output abort 'Backup failed' end @@ -29,15 +31,17 @@ module Backup wiki = ProjectWiki.new(project) if File.exists?(path_to_repo(wiki)) - print " * #{wiki.path_with_namespace} ... " + $progress.print " * #{wiki.path_with_namespace} ... " if wiki.repository.empty? - puts " [SKIPPED]".cyan + $progress.puts " [SKIPPED]".cyan else - output, status = Gitlab::Popen.popen(%W(git --git-dir=#{path_to_repo(wiki)} bundle create #{path_to_bundle(wiki)} --all)) + cmd = %W(git --git-dir=#{path_to_repo(wiki)} bundle create #{path_to_bundle(wiki)} --all) + output, status = Gitlab::Popen.popen(cmd) if status.zero? - puts " [DONE]".green + $progress.puts " [DONE]".green else puts " [FAILED]".red + puts "failed: #{cmd.join(' ')}" abort 'Backup failed' end end @@ -55,7 +59,7 @@ module Backup FileUtils.mkdir_p(repos_path) Project.find_each(batch_size: 1000) do |project| - print "#{project.path_with_namespace} ... " + $progress.print "#{project.path_with_namespace} ... " project.namespace.ensure_dir_exist if project.namespace @@ -66,30 +70,35 @@ module Backup end if system(*cmd, silent) - puts "[DONE]".green + $progress.puts "[DONE]".green else puts "[FAILED]".red + puts "failed: #{cmd.join(' ')}" abort 'Restore failed' end wiki = ProjectWiki.new(project) if File.exists?(path_to_bundle(wiki)) - print " * #{wiki.path_with_namespace} ... " - if system(*%W(git clone --bare #{path_to_bundle(wiki)} #{path_to_repo(wiki)}), silent) - puts " [DONE]".green + $progress.print " * #{wiki.path_with_namespace} ... " + cmd = %W(git clone --bare #{path_to_bundle(wiki)} #{path_to_repo(wiki)}) + if system(*cmd, silent) + $progress.puts " [DONE]".green else puts " [FAILED]".red + puts "failed: #{cmd.join(' ')}" abort 'Restore failed' end end end - print 'Put GitLab hooks in repositories dirs'.yellow - if system("#{Gitlab.config.gitlab_shell.path}/bin/create-hooks") - puts " [DONE]".green + $progress.print 'Put GitLab hooks in repositories dirs'.yellow + cmd = "#{Gitlab.config.gitlab_shell.path}/bin/create-hooks" + if system(cmd) + $progress.puts " [DONE]".green else puts " [FAILED]".red + puts "failed: #{cmd}" end end diff --git a/lib/tasks/gitlab/backup.rake b/lib/tasks/gitlab/backup.rake index 2eff1260b61..99e84f62c66 100644 --- a/lib/tasks/gitlab/backup.rake +++ b/lib/tasks/gitlab/backup.rake @@ -6,6 +6,7 @@ namespace :gitlab do desc "GITLAB | Create a backup of the GitLab system" task create: :environment do warn_user_is_not_gitlab + configure_cron_mode Rake::Task["gitlab:backup:db:create"].invoke Rake::Task["gitlab:backup:repo:create"].invoke @@ -21,6 +22,7 @@ namespace :gitlab do desc "GITLAB | Restore a previously created backup" task restore: :environment do warn_user_is_not_gitlab + configure_cron_mode backup = Backup::Manager.new backup.unpack @@ -35,43 +37,52 @@ namespace :gitlab do namespace :repo do task create: :environment do - puts "Dumping repositories ...".blue + $progress.puts "Dumping repositories ...".blue Backup::Repository.new.dump - puts "done".green + $progress.puts "done".green end task restore: :environment do - puts "Restoring repositories ...".blue + $progress.puts "Restoring repositories ...".blue Backup::Repository.new.restore - puts "done".green + $progress.puts "done".green end end namespace :db do task create: :environment do - puts "Dumping database ... ".blue + $progress.puts "Dumping database ... ".blue Backup::Database.new.dump - puts "done".green + $progress.puts "done".green end task restore: :environment do - puts "Restoring database ... ".blue + $progress.puts "Restoring database ... ".blue Backup::Database.new.restore - puts "done".green + $progress.puts "done".green end end namespace :uploads do task create: :environment do - puts "Dumping uploads ... ".blue + $progress.puts "Dumping uploads ... ".blue Backup::Uploads.new.dump - puts "done".green + $progress.puts "done".green end task restore: :environment do - puts "Restoring uploads ... ".blue + $progress.puts "Restoring uploads ... ".blue Backup::Uploads.new.restore - puts "done".green + $progress.puts "done".green + end + end + + def configure_cron_mode + if ENV['CRON'] + require 'stringio' + $progress = StringIO.new + else + $progress = $stdout end end end # namespace end: backup -- cgit v1.2.1 From 458f8c1f80ff20ba3d6e439c65500ed1c1d81ba4 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Thu, 20 Nov 2014 15:54:39 +0100 Subject: Explain why we create a StringIO --- lib/tasks/gitlab/backup.rake | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib') diff --git a/lib/tasks/gitlab/backup.rake b/lib/tasks/gitlab/backup.rake index 99e84f62c66..0230fbb010b 100644 --- a/lib/tasks/gitlab/backup.rake +++ b/lib/tasks/gitlab/backup.rake @@ -79,6 +79,8 @@ namespace :gitlab do def configure_cron_mode if ENV['CRON'] + # We need an object we can say 'puts' and 'print' to; let's use a + # StringIO. require 'stringio' $progress = StringIO.new else -- cgit v1.2.1 From ef944e83ec8c439350df03c3bb9b5bbb3f68f406 Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Mon, 24 Nov 2014 16:21:35 +0200 Subject: Git hook messages: wiki access fix --- lib/gitlab/git_access_wiki.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/git_access_wiki.rb b/lib/gitlab/git_access_wiki.rb index f7d1428deb2..a2177c8d548 100644 --- a/lib/gitlab/git_access_wiki.rb +++ b/lib/gitlab/git_access_wiki.rb @@ -1,6 +1,6 @@ module Gitlab class GitAccessWiki < GitAccess - def change_allowed_check(user, project, change) + def change_access_check(user, project, change) if user.can?(:write_wiki, project) build_status_object(true) else -- cgit v1.2.1 From 434c4a2b5d0a6b89d050f3b7b4e4e4442ffde733 Mon Sep 17 00:00:00 2001 From: sbeh Date: Wed, 26 Nov 2014 00:31:50 +0100 Subject: Socket [::]:123 on Linux listens on IPv4 and IPv6 This will ensure nginx starts up without the following errors messages: nginx: [emerg] bind() to [::]:443 failed (98: Address already in use) nginx: [emerg] bind() to [::]:443 failed (98: Address already in use) nginx: [emerg] bind() to [::]:443 failed (98: Address already in use) nginx: [emerg] bind() to [::]:443 failed (98: Address already in use) nginx: [emerg] bind() to [::]:443 failed (98: Address already in use) nginx: [emerg] still could not bind() Googling for them leads you to this site: https://chrisjean.com/2014/02/10/fix-nginx-emerg-bind-to-80-failed-98-address-already-in-use/ --- lib/support/nginx/gitlab-ssl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/support/nginx/gitlab-ssl b/lib/support/nginx/gitlab-ssl index 4e53d5e8b50..19af010a9f7 100644 --- a/lib/support/nginx/gitlab-ssl +++ b/lib/support/nginx/gitlab-ssl @@ -39,7 +39,7 @@ upstream gitlab { ## Redirects all HTTP traffic to the HTTPS host server { listen 0.0.0.0:80; - listen [::]:80 default_server; + listen [::]:80 ipv6only=on default_server; server_name YOUR_SERVER_FQDN; ## Replace this with something like gitlab.example.com server_tokens off; ## Don't show the nginx version number, a security best practice return 301 https://$server_name$request_uri; @@ -51,7 +51,7 @@ server { ## HTTPS host server { listen 0.0.0.0:443 ssl; - listen [::]:443 ssl default_server; + listen [::]:443 ipv6only=on ssl default_server; server_name YOUR_SERVER_FQDN; ## Replace this with something like gitlab.example.com server_tokens off; ## Don't show the nginx version number, a security best practice root /home/git/gitlab/public; -- cgit v1.2.1 From 64ab6c9ed54d1c0a86f4c3bb6b87fcac882da0c0 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Fri, 28 Nov 2014 15:01:41 +0100 Subject: Add 'MemoryKiller' Sidekiq middleware When enabled, this middleware allows Sidekiq to detect that its RSS has exceeded a maximum value, triggering a graceful shutdown. This middleware should be combined with external process supervision that will restart Sidekiq after the graceful shutdown, such as Runit. --- lib/gitlab/sidekiq_middleware/memory_killer.rb | 45 ++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lib/gitlab/sidekiq_middleware/memory_killer.rb (limited to 'lib') diff --git a/lib/gitlab/sidekiq_middleware/memory_killer.rb b/lib/gitlab/sidekiq_middleware/memory_killer.rb new file mode 100644 index 00000000000..3ef46627916 --- /dev/null +++ b/lib/gitlab/sidekiq_middleware/memory_killer.rb @@ -0,0 +1,45 @@ +module Gitlab + module SidekiqMiddleware + class MemoryKiller + # Wait 30 seconds for running jobs to finish during graceful shutdown + GRACEFUL_SHUTDOWN_WAIT = 30 + + def call(worker, job, queue) + yield + current_rss = get_rss + return unless max_rss > 0 && current_rss > max_rss + + Sidekiq.logger.warn "current RSS #{current_rss} exceeds maximum RSS "\ + "#{max_rss}" + Sidekiq.logger.warn "sending SIGUSR1 to PID #{Process.pid}" + Process.kill('SIGUSR1', Process.pid) + + Sidekiq.logger.warn "spawning thread that will send SIGTERM to PID "\ + "#{Process.pid} in #{graceful_shutdown_wait} seconds" + Thread.new do + sleep(graceful_shutdown_wait) + Process.kill('SIGTERM', Process.pid) + end + end + + private + + def get_rss + output, status = Gitlab::Popen.popen(%W(ps -o rss= -p #{Process.pid})) + return 0 unless status.zero? + + output.to_i + end + + def max_rss + @max_rss ||= ENV['SIDEKIQ_MAX_RSS'].to_s.to_i + end + + def graceful_shutdown_wait + @graceful_shutdown_wait ||= ( + ENV['SIDEKIQ_GRACEFUL_SHUTDOWN_WAIT'] || GRACEFUL_SHUTDOWN_WAIT + ).to_i + end + end + end +end -- cgit v1.2.1 From d336127a20ce22a9512123595a887c4207b748e9 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Fri, 28 Nov 2014 15:19:03 +0100 Subject: Add comments to the MemoryKiller middleware --- lib/gitlab/sidekiq_middleware/memory_killer.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib') diff --git a/lib/gitlab/sidekiq_middleware/memory_killer.rb b/lib/gitlab/sidekiq_middleware/memory_killer.rb index 3ef46627916..0fb09d3f228 100644 --- a/lib/gitlab/sidekiq_middleware/memory_killer.rb +++ b/lib/gitlab/sidekiq_middleware/memory_killer.rb @@ -12,10 +12,13 @@ module Gitlab Sidekiq.logger.warn "current RSS #{current_rss} exceeds maximum RSS "\ "#{max_rss}" Sidekiq.logger.warn "sending SIGUSR1 to PID #{Process.pid}" + # SIGUSR1 tells Sidekiq to stop accepting new jobs Process.kill('SIGUSR1', Process.pid) Sidekiq.logger.warn "spawning thread that will send SIGTERM to PID "\ "#{Process.pid} in #{graceful_shutdown_wait} seconds" + # Send the final shutdown signal to Sidekiq from a separate thread so + # that the current job can finish Thread.new do sleep(graceful_shutdown_wait) Process.kill('SIGTERM', Process.pid) -- cgit v1.2.1 From 880478b21e7c9b0068b3e14b8f7fb58ada2c232e Mon Sep 17 00:00:00 2001 From: Achilleas Pipinellis Date: Sat, 29 Nov 2014 21:59:28 +0200 Subject: Proper wiki restore. Fixes #845 --- lib/backup/repository.rb | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/backup/repository.rb b/lib/backup/repository.rb index f39fba23cf5..6b04b23cf46 100644 --- a/lib/backup/repository.rb +++ b/lib/backup/repository.rb @@ -79,16 +79,20 @@ module Backup wiki = ProjectWiki.new(project) + $progress.print " * #{wiki.path_with_namespace} ... " + if File.exists?(path_to_bundle(wiki)) - $progress.print " * #{wiki.path_with_namespace} ... " cmd = %W(git clone --bare #{path_to_bundle(wiki)} #{path_to_repo(wiki)}) - if system(*cmd, silent) - $progress.puts " [DONE]".green - else - puts " [FAILED]".red - puts "failed: #{cmd.join(' ')}" - abort 'Restore failed' - end + else + cmd = %W(git init --bare #{path_to_repo(wiki)}) + end + + if system(*cmd, silent) + $progress.puts " [DONE]".green + else + puts " [FAILED]".red + puts "failed: #{cmd.join(' ')}" + abort 'Restore failed' end end -- cgit v1.2.1 From 191aa9712eeb8fe39e8947dc681cefe4221044ec Mon Sep 17 00:00:00 2001 From: Achilleas Pipinellis Date: Sun, 30 Nov 2014 18:24:05 +0200 Subject: Properly fix wiki restore. ProjectWiki.new() creates a new wiki git repository, so any tries to bare clone a bundle fail. With this patch we remove the newly created wiki.git before restoring from the backup bundle. --- lib/backup/repository.rb | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/backup/repository.rb b/lib/backup/repository.rb index 6b04b23cf46..e18bc804437 100644 --- a/lib/backup/repository.rb +++ b/lib/backup/repository.rb @@ -59,7 +59,7 @@ module Backup FileUtils.mkdir_p(repos_path) Project.find_each(batch_size: 1000) do |project| - $progress.print "#{project.path_with_namespace} ... " + $progress.print " * #{project.path_with_namespace} ... " project.namespace.ensure_dir_exist if project.namespace @@ -79,20 +79,22 @@ module Backup wiki = ProjectWiki.new(project) - $progress.print " * #{wiki.path_with_namespace} ... " - if File.exists?(path_to_bundle(wiki)) + $progress.print " * #{wiki.path_with_namespace} ... " + + # If a wiki bundle exists, first remove the empty repo + # that was initialized with ProjectWiki.new() and then + # try to restore with 'git clone --bare'. + FileUtils.rm_rf(path_to_repo(wiki)) cmd = %W(git clone --bare #{path_to_bundle(wiki)} #{path_to_repo(wiki)}) - else - cmd = %W(git init --bare #{path_to_repo(wiki)}) - end - if system(*cmd, silent) - $progress.puts " [DONE]".green - else - puts " [FAILED]".red - puts "failed: #{cmd.join(' ')}" - abort 'Restore failed' + if system(*cmd, silent) + $progress.puts " [DONE]".green + else + puts " [FAILED]".red + puts "failed: #{cmd.join(' ')}" + abort 'Restore failed' + end end end -- cgit v1.2.1 From 06b7907c2afe0cb0fa25f4cdef0ff470710de2f9 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 1 Dec 2014 16:25:10 +0200 Subject: Fix deploy keys permission check in internal api Signed-off-by: Dmitriy Zaporozhets --- lib/gitlab/git_access.rb | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index 3452240dad8..5f8cb19efdf 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -8,15 +8,7 @@ module Gitlab def check(actor, cmd, project, changes = nil) case cmd when *DOWNLOAD_COMMANDS - if actor.is_a? User - download_access_check(actor, project) - elsif actor.is_a? DeployKey - actor.projects.include?(project) - elsif actor.is_a? Key - download_access_check(actor.user, project) - else - raise 'Wrong actor' - end + download_access_check(actor, project) when *PUSH_COMMANDS if actor.is_a? User push_access_check(actor, project, changes) @@ -32,7 +24,23 @@ module Gitlab end end - def download_access_check(user, project) + def download_access_check(actor, project) + if actor.is_a?(User) + user_download_access_check(actor, project) + elsif actor.is_a?(DeployKey) + if actor.projects.include?(project) + build_status_object(true) + else + build_status_object(false, "Deploy key not allowed to access this project") + end + elsif actor.is_a? Key + user_download_access_check(actor.user, project) + else + raise 'Wrong actor' + end + end + + def user_download_access_check(user, project) if user && user_allowed?(user) && user.can?(:download_code, project) build_status_object(true) else -- cgit v1.2.1 From 612b8806ddc7881421e26a9dbfe465d6445fb3d6 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 1 Dec 2014 16:55:33 +0200 Subject: Fix internal API for missing project or key Signed-off-by: Dmitriy Zaporozhets --- lib/api/internal.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/api/internal.rb b/lib/api/internal.rb index 1648834f034..180e50611cf 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -33,15 +33,20 @@ module API end project = Project.find_with_namespace(project_path) - return false unless project + + unless project + return Gitlab::GitAccessStatus.new(false, 'No such project') + end actor = if params[:key_id] - Key.find(params[:key_id]) + Key.find_by(id: params[:key_id]) elsif params[:user_id] - User.find(params[:user_id]) + User.find_by(id: params[:user_id]) end - return false unless actor + unless actor + return Gitlab::GitAccessStatus.new(false, 'No such user or key') + end access.check( actor, -- cgit v1.2.1 From 835cbc06d8d9c773a3b405eca81650378a8ccdcd Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 2 Dec 2014 17:42:56 +0200 Subject: Reload mr code on force push too Signed-off-by: Dmitriy Zaporozhets --- lib/gitlab/force_push_check.rb | 15 +++++++++++++++ lib/gitlab/git_access.rb | 9 +-------- 2 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 lib/gitlab/force_push_check.rb (limited to 'lib') diff --git a/lib/gitlab/force_push_check.rb b/lib/gitlab/force_push_check.rb new file mode 100644 index 00000000000..6a52cdba608 --- /dev/null +++ b/lib/gitlab/force_push_check.rb @@ -0,0 +1,15 @@ +module Gitlab + class ForcePushCheck + def self.force_push?(project, oldrev, newrev) + return false if project.empty_repo? + + if oldrev != Gitlab::Git::BLANK_SHA && newrev != Gitlab::Git::BLANK_SHA + missed_refs = IO.popen(%W(git --git-dir=#{project.repository.path_to_repo} rev-list #{oldrev} ^#{newrev})).read + missed_refs.split("\n").size > 0 + else + false + end + end + end +end + diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index 5f8cb19efdf..8b4729896b5 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -94,14 +94,7 @@ module Gitlab end def forced_push?(project, oldrev, newrev) - return false if project.empty_repo? - - if oldrev != Gitlab::Git::BLANK_SHA && newrev != Gitlab::Git::BLANK_SHA - missed_refs = IO.popen(%W(git --git-dir=#{project.repository.path_to_repo} rev-list #{oldrev} ^#{newrev})).read - missed_refs.split("\n").size > 0 - else - false - end + Gitlab::ForcePushCheck.force_push?(project, oldrev, newrev) end private -- cgit v1.2.1 From 1a80d13a3990937580c97e2b0ba8fb98f69bc055 Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Tue, 25 Nov 2014 18:15:30 +0200 Subject: Multi-provider auth. LDAP is not reworked --- lib/gitlab/ldap/user.rb | 5 +++-- lib/gitlab/oauth/user.rb | 21 +++++++-------------- 2 files changed, 10 insertions(+), 16 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ldap/user.rb b/lib/gitlab/ldap/user.rb index 3176e9790a7..827a33b5217 100644 --- a/lib/gitlab/ldap/user.rb +++ b/lib/gitlab/ldap/user.rb @@ -12,9 +12,10 @@ module Gitlab class << self def find_by_uid_and_provider(uid, provider) # LDAP distinguished name is case-insensitive - ::User. + identity = ::Identity. where(provider: [provider, :ldap]). where('lower(extern_uid) = ?', uid.downcase).last + identity && identity.user end end @@ -34,7 +35,7 @@ module Gitlab end def find_by_email - model.find_by(email: auth_hash.email) + User.find_by(email: auth_hash.email) end def update_user_attributes diff --git a/lib/gitlab/oauth/user.rb b/lib/gitlab/oauth/user.rb index 47f62153a50..7c1970eb8e5 100644 --- a/lib/gitlab/oauth/user.rb +++ b/lib/gitlab/oauth/user.rb @@ -27,11 +27,9 @@ module Gitlab def save unauthorized_to_create unless gl_user + gl_user.save! if needs_blocking? - gl_user.save! gl_user.block - else - gl_user.save! end log.info "(OAuth) saving user #{auth_hash.email} from login with extern_uid => #{auth_hash.uid}" @@ -70,24 +68,23 @@ module Gitlab end def find_by_uid_and_provider - model.where(provider: auth_hash.provider, extern_uid: auth_hash.uid).last + identity = Identity.find_by(provider: auth_hash.provider, extern_uid: auth_hash.uid) + identity && identity.user end def build_new_user - model.new(user_attributes).tap do |user| - user.skip_confirmation! - end + user = User.new(user_attributes) + user.skip_confirmation! + user.identities.new(extern_uid: auth_hash.uid, provider: auth_hash.provider) end def user_attributes { - extern_uid: auth_hash.uid, - provider: auth_hash.provider, name: auth_hash.name, username: auth_hash.username, email: auth_hash.email, password: auth_hash.password, - password_confirmation: auth_hash.password, + password_confirmation: auth_hash.password } end @@ -95,10 +92,6 @@ module Gitlab Gitlab::AppLogger end - def model - ::User - end - def raise_unauthorized_to_create raise StandardError.new("Unauthorized to create user, signup disabled for #{auth_hash.provider}") end -- cgit v1.2.1 From 3a5ed5260b24051939575d1934ce9b8392cac09f Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Thu, 27 Nov 2014 13:34:39 +0200 Subject: Supporting for multiple omniauth provider for the same user --- lib/api/entities.rb | 8 ++++++-- lib/api/users.rb | 12 ++++++++---- lib/gitlab/ldap/access.rb | 8 ++++---- lib/gitlab/ldap/user.rb | 10 ++++------ lib/gitlab/oauth/user.rb | 13 +++++++++---- 5 files changed, 31 insertions(+), 20 deletions(-) (limited to 'lib') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 42e4442365d..2fea151aeb3 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -14,10 +14,14 @@ module API expose :bio, :skype, :linkedin, :twitter, :website_url end + class Identity < Grape::Entity + expose :provider, :extern_uid + end + class UserFull < User expose :email - expose :theme_id, :color_scheme_id, :extern_uid, :provider, \ - :projects_limit + expose :theme_id, :color_scheme_id, :projects_limit + expose :identities, using: Entities::Identity expose :can_create_group?, as: :can_create_group expose :can_create_project?, as: :can_create_project end diff --git a/lib/api/users.rb b/lib/api/users.rb index d07815a8a97..37b36ddcf94 100644 --- a/lib/api/users.rb +++ b/lib/api/users.rb @@ -59,10 +59,16 @@ module API 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, :can_create_group, :admin] + attrs = attributes_for_keys [:email, :name, :password, :skype, :linkedin, :twitter, :projects_limit, :username, :bio, :can_create_group, :admin] user = User.build_user(attrs) admin = attrs.delete(:admin) user.admin = admin unless admin.nil? + + identity_attrs = attributes_for_keys [:provider, :extern_uid] + if identity_attrs.any? + user.identities.build(identity_attrs) + end + if user.save present user, with: Entities::UserFull else @@ -89,8 +95,6 @@ module API # twitter - Twitter account # website_url - Website url # projects_limit - Limit projects each user can create - # 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 @@ -99,7 +103,7 @@ module API put ":id" do authenticated_as_admin! - attrs = attributes_for_keys [:email, :name, :password, :skype, :linkedin, :twitter, :website_url, :projects_limit, :username, :extern_uid, :provider, :bio, :can_create_group, :admin] + attrs = attributes_for_keys [:email, :name, :password, :skype, :linkedin, :twitter, :website_url, :projects_limit, :username, :bio, :can_create_group, :admin] user = User.find(params[:id]) not_found!('User') unless user diff --git a/lib/gitlab/ldap/access.rb b/lib/gitlab/ldap/access.rb index eb2c4e48ff2..0c85acf7e69 100644 --- a/lib/gitlab/ldap/access.rb +++ b/lib/gitlab/ldap/access.rb @@ -8,7 +8,7 @@ module Gitlab attr_reader :adapter, :provider, :user def self.open(user, &block) - Gitlab::LDAP::Adapter.open(user.provider) do |adapter| + Gitlab::LDAP::Adapter.open(user.ldap_identity.provider) do |adapter| block.call(self.new(user, adapter)) end end @@ -28,13 +28,13 @@ module Gitlab def initialize(user, adapter=nil) @adapter = adapter @user = user - @provider = user.provider + @provider = user.ldap_identity.provider end def allowed? - if Gitlab::LDAP::Person.find_by_dn(user.extern_uid, adapter) + if Gitlab::LDAP::Person.find_by_dn(user.ldap_identity.extern_uid, adapter) return true unless ldap_config.active_directory - !Gitlab::LDAP::Person.disabled_via_active_directory?(user.extern_uid, adapter) + !Gitlab::LDAP::Person.disabled_via_active_directory?(user.ldap_identity.extern_uid, adapter) else false end diff --git a/lib/gitlab/ldap/user.rb b/lib/gitlab/ldap/user.rb index 827a33b5217..3ef494ba137 100644 --- a/lib/gitlab/ldap/user.rb +++ b/lib/gitlab/ldap/user.rb @@ -35,15 +35,13 @@ module Gitlab end def find_by_email - User.find_by(email: auth_hash.email) + ::User.find_by(email: auth_hash.email) end def update_user_attributes - gl_user.attributes = { - extern_uid: auth_hash.uid, - provider: auth_hash.provider, - email: auth_hash.email - } + gl_user.email = auth_hash.email + gl_user.identities.build(provider: auth_hash.provider, extern_uid: auth_hash.uid) + gl_user end def changed? diff --git a/lib/gitlab/oauth/user.rb b/lib/gitlab/oauth/user.rb index 7c1970eb8e5..6861427864e 100644 --- a/lib/gitlab/oauth/user.rb +++ b/lib/gitlab/oauth/user.rb @@ -5,6 +5,8 @@ # module Gitlab module OAuth + class ForbiddenAction < StandardError; end + class User attr_accessor :auth_hash, :gl_user @@ -27,9 +29,11 @@ module Gitlab def save unauthorized_to_create unless gl_user - gl_user.save! if needs_blocking? + gl_user.save! gl_user.block + else + gl_user.save! end log.info "(OAuth) saving user #{auth_hash.email} from login with extern_uid => #{auth_hash.uid}" @@ -73,9 +77,10 @@ module Gitlab end def build_new_user - user = User.new(user_attributes) + user = ::User.new(user_attributes) user.skip_confirmation! user.identities.new(extern_uid: auth_hash.uid, provider: auth_hash.provider) + user end def user_attributes @@ -92,8 +97,8 @@ module Gitlab Gitlab::AppLogger end - def raise_unauthorized_to_create - raise StandardError.new("Unauthorized to create user, signup disabled for #{auth_hash.provider}") + def unauthorized_to_create + raise ForbiddenAction.new("Unauthorized to create user, signup disabled for #{auth_hash.provider}") end end end -- cgit v1.2.1 From cdc62cffcb86dfd939c119cba2acaf266af39f23 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Thu, 4 Dec 2014 15:22:10 +0100 Subject: Add rake task for google schema whitelisting. --- .../gitlab/mail_google_schema_whitelisting.rake | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 lib/tasks/gitlab/mail_google_schema_whitelisting.rake (limited to 'lib') diff --git a/lib/tasks/gitlab/mail_google_schema_whitelisting.rake b/lib/tasks/gitlab/mail_google_schema_whitelisting.rake new file mode 100644 index 00000000000..f40bba24da8 --- /dev/null +++ b/lib/tasks/gitlab/mail_google_schema_whitelisting.rake @@ -0,0 +1,73 @@ +require "#{Rails.root}/app/helpers/emails_helper" +require 'action_view/helpers' +extend ActionView::Helpers + +include ActionView::Context +include EmailsHelper + +namespace :gitlab do + desc "Email google whitelisting email with example email for actions in inbox" + task mail_google_schema_whitelisting: :environment do + subject = "Rails | Implemented feature" + url = "#{Gitlab.config.gitlab.url}/base/rails-project/issues/#{rand(1..100)}#note_#{rand(10..1000)}" + schema = email_action(url) + body = email_template(schema, url) + mail = Notify.test_email("schema.whitelisting+sample@gmail.com", subject, body.html_safe) + if send_now + mail.deliver + else + puts "WOULD SEND:" + end + puts mail + end + + def email_template(schema, url) + " + + + + GitLab + + + + + +
+
+

I like it :+1:

+
+
+ + + + " + end + + def send_now + if ENV['SEND'] == "true" + true + else + false + end + end +end -- cgit v1.2.1 From 4491a3d12b414a52f32175e328df8dc48987d0fd Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Fri, 5 Dec 2014 18:17:51 +0200 Subject: Decline push if repository does not exist Signed-off-by: Dmitriy Zaporozhets --- lib/gitlab/git_access.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index 8b4729896b5..875f8d8b3a3 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -49,8 +49,17 @@ module Gitlab end def push_access_check(user, project, changes) - return build_status_object(false, "You don't have access") unless user && user_allowed?(user) - return build_status_object(true) if changes.blank? + unless user && user_allowed?(user) + return build_status_object(false, "You don't have access") + end + + if changes.blank? + return build_status_object(true) + end + + unless project.repository.exists? + return build_status_object(false, "Repository does not exist") + end changes = changes.lines if changes.kind_of?(String) @@ -79,7 +88,7 @@ module Gitlab else :push_code_to_protected_branches end - elsif project.repository && project.repository.tag_names.include?(tag_name(ref)) + elsif project.repository.tag_names.include?(tag_name(ref)) # Prevent any changes to existing git tag unless user has permissions :admin_project else -- cgit v1.2.1 From 4f9a14061b707f39d8a98dae328089bbfbc09e70 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Fri, 5 Dec 2014 17:29:34 +0100 Subject: Wait 15 minutes before Sidekiq MemoryKiller action --- lib/gitlab/sidekiq_middleware/memory_killer.rb | 48 ++++++++++++++++++-------- 1 file changed, 33 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/sidekiq_middleware/memory_killer.rb b/lib/gitlab/sidekiq_middleware/memory_killer.rb index 0fb09d3f228..df8968cf677 100644 --- a/lib/gitlab/sidekiq_middleware/memory_killer.rb +++ b/lib/gitlab/sidekiq_middleware/memory_killer.rb @@ -1,26 +1,38 @@ module Gitlab module SidekiqMiddleware class MemoryKiller + # Give Sidekiq 15 minutes of grace time after exceeding the RSS limit + GRACE_TIME = 15 * 60 # Wait 30 seconds for running jobs to finish during graceful shutdown - GRACEFUL_SHUTDOWN_WAIT = 30 + SHUTDOWN_WAIT = 30 + # Create a mutex so that there will be only one thread waiting to shut + # Sidekiq down + MUTEX = Mutex.new def call(worker, job, queue) yield current_rss = get_rss + return unless max_rss > 0 && current_rss > max_rss - Sidekiq.logger.warn "current RSS #{current_rss} exceeds maximum RSS "\ - "#{max_rss}" - Sidekiq.logger.warn "sending SIGUSR1 to PID #{Process.pid}" - # SIGUSR1 tells Sidekiq to stop accepting new jobs - Process.kill('SIGUSR1', Process.pid) - - Sidekiq.logger.warn "spawning thread that will send SIGTERM to PID "\ - "#{Process.pid} in #{graceful_shutdown_wait} seconds" - # Send the final shutdown signal to Sidekiq from a separate thread so - # that the current job can finish - Thread.new do - sleep(graceful_shutdown_wait) + Tread.new do + # Return if another thread is already waiting to shut Sidekiq down + return unless MUTEX.try_lock + + Sidekiq.logger.warn "current RSS #{current_rss} exceeds maximum RSS "\ + "#{max_rss}" + Sidekiq.logger.warn "spawned thread that will shut down PID "\ + "#{Process.pid} in #{grace_time} seconds" + sleep(grace_time) + + Sidekiq.logger.warn "sending SIGUSR1 to PID #{Process.pid}" + Process.kill('SIGUSR1', Process.pid) + + Sidekiq.logger.warn "waiting #{shutdown_wait} seconds before sending "\ + "SIGTERM to PID #{Process.pid}" + sleep(shutdown_wait) + + Sidekiq.logger.warn "sending SIGTERM to PID #{Process.pid}" Process.kill('SIGTERM', Process.pid) end end @@ -38,9 +50,15 @@ module Gitlab @max_rss ||= ENV['SIDEKIQ_MAX_RSS'].to_s.to_i end - def graceful_shutdown_wait + def shutdown_wait @graceful_shutdown_wait ||= ( - ENV['SIDEKIQ_GRACEFUL_SHUTDOWN_WAIT'] || GRACEFUL_SHUTDOWN_WAIT + ENV['SIDEKIQ_MEMORY_KILLER_SHUTDOWN_WAIT'] || SHUTDOWN_WAIT + ).to_i + end + + def grace_time + @grace_time ||= ( + ENV['SIDEKIQ_MEMORY_KILLER_GRACE_TIME'] || GRACE_TIME ).to_i end end -- cgit v1.2.1 From 3dd86b83baccc2a2aecc12a1f4a4819438c62a81 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Mon, 8 Dec 2014 13:19:31 +0100 Subject: Use constants instead of getters --- lib/gitlab/sidekiq_middleware/memory_killer.rb | 39 +++++++++----------------- 1 file changed, 13 insertions(+), 26 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/sidekiq_middleware/memory_killer.rb b/lib/gitlab/sidekiq_middleware/memory_killer.rb index df8968cf677..f5c65e75af0 100644 --- a/lib/gitlab/sidekiq_middleware/memory_killer.rb +++ b/lib/gitlab/sidekiq_middleware/memory_killer.rb @@ -1,36 +1,39 @@ module Gitlab module SidekiqMiddleware class MemoryKiller + # Default the RSS limit to 0, meaning the MemoryKiller is disabled + MAX_RSS = (ENV['SIDEKIQ_MEMORY_KILLER_MAX_RSS'] || 0).to_s.to_i # Give Sidekiq 15 minutes of grace time after exceeding the RSS limit - GRACE_TIME = 15 * 60 + GRACE_TIME = (ENV['SIDEKIQ_MEMORY_KILLER_GRACE_TIME'] || 15 * 60).to_s.to_i # Wait 30 seconds for running jobs to finish during graceful shutdown - SHUTDOWN_WAIT = 30 - # Create a mutex so that there will be only one thread waiting to shut - # Sidekiq down + SHUTDOWN_WAIT = (ENV['SIDEKIQ_MEMORY_KILLER_SHUTDOWN_WAIT'] || 30).to_s.to_i + + # Create a mutex used to ensure there will be only one thread waiting to + # shut Sidekiq down MUTEX = Mutex.new def call(worker, job, queue) yield current_rss = get_rss - return unless max_rss > 0 && current_rss > max_rss + return unless MAX_RSS > 0 && current_rss > MAX_RSS Tread.new do # Return if another thread is already waiting to shut Sidekiq down return unless MUTEX.try_lock Sidekiq.logger.warn "current RSS #{current_rss} exceeds maximum RSS "\ - "#{max_rss}" + "#{MAX_RSS}" Sidekiq.logger.warn "spawned thread that will shut down PID "\ - "#{Process.pid} in #{grace_time} seconds" - sleep(grace_time) + "#{Process.pid} in #{GRACE_TIME} seconds" + sleep(GRACE_TIME) Sidekiq.logger.warn "sending SIGUSR1 to PID #{Process.pid}" Process.kill('SIGUSR1', Process.pid) - Sidekiq.logger.warn "waiting #{shutdown_wait} seconds before sending "\ + Sidekiq.logger.warn "waiting #{SHUTDOWN_WAIT} seconds before sending "\ "SIGTERM to PID #{Process.pid}" - sleep(shutdown_wait) + sleep(SHUTDOWN_WAIT) Sidekiq.logger.warn "sending SIGTERM to PID #{Process.pid}" Process.kill('SIGTERM', Process.pid) @@ -45,22 +48,6 @@ module Gitlab output.to_i end - - def max_rss - @max_rss ||= ENV['SIDEKIQ_MAX_RSS'].to_s.to_i - end - - def shutdown_wait - @graceful_shutdown_wait ||= ( - ENV['SIDEKIQ_MEMORY_KILLER_SHUTDOWN_WAIT'] || SHUTDOWN_WAIT - ).to_i - end - - def grace_time - @grace_time ||= ( - ENV['SIDEKIQ_MEMORY_KILLER_GRACE_TIME'] || GRACE_TIME - ).to_i - end end end end -- cgit v1.2.1 From 2fcef3278ce4ccb62cefa590b0f65509028fbcc0 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Mon, 8 Dec 2014 13:39:18 +0100 Subject: Fix typo --- lib/gitlab/sidekiq_middleware/memory_killer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/sidekiq_middleware/memory_killer.rb b/lib/gitlab/sidekiq_middleware/memory_killer.rb index f5c65e75af0..0f2db50e98c 100644 --- a/lib/gitlab/sidekiq_middleware/memory_killer.rb +++ b/lib/gitlab/sidekiq_middleware/memory_killer.rb @@ -18,7 +18,7 @@ module Gitlab return unless MAX_RSS > 0 && current_rss > MAX_RSS - Tread.new do + Thread.new do # Return if another thread is already waiting to shut Sidekiq down return unless MUTEX.try_lock -- cgit v1.2.1 From cd4c65c159627ead220bcadbe1a58ced0017851b Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Thu, 11 Dec 2014 13:17:43 +0100 Subject: Use shell invocation according to the shell commands guidelines. --- lib/tasks/gitlab/shell.rake | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/tasks/gitlab/shell.rake b/lib/tasks/gitlab/shell.rake index 202e55c89ad..84bc9e304b6 100644 --- a/lib/tasks/gitlab/shell.rake +++ b/lib/tasks/gitlab/shell.rake @@ -17,15 +17,19 @@ namespace :gitlab do # Clone if needed unless File.directory?(target_dir) - sh(*%W(git clone #{args.repo} #{target_dir})) + Gitlab::Popen.popen(%W(git clone -- #{args.repo} #{target_dir})) end # Make sure we're on the right tag Dir.chdir(target_dir) do # First try to checkout without fetching # to avoid stalling tests if the Internet is down. - reset = "git reset --hard $(git describe #{args.tag} || git describe origin/#{args.tag})" - sh "#{reset} || git fetch origin && #{reset}" + reset_status = reset_to_commit(args) + + if reset_status != 0 + Gitlab::Popen.popen(%W(git fetch origin)) + reset_to_commit(args) + end config = { user: user, @@ -54,7 +58,7 @@ namespace :gitlab do File.open("config.yml", "w+") {|f| f.puts config.to_yaml} # Launch installation process - sh "bin/install" + Gitlab::Popen.popen(%W(bin/install)) end # Required for debian packaging with PKGR: Setup .ssh/environment with @@ -118,5 +122,17 @@ namespace :gitlab do puts "Quitting...".red exit 1 end + + def reset_to_commit(args) + tag, status = Gitlab::Popen.popen(%W(git describe -- #{args.tag})) + + if status != 0 + tag, status = Gitlab::Popen.popen(%W(git describe -- origin/#{args.tag})) + end + + tag = tag.strip + reset, reset_status = Gitlab::Popen.popen(%W(git reset --hard #{tag})) + reset_status + end end -- cgit v1.2.1 From bd43cf065384745e4386237ad0f5d4eb14868034 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Fri, 12 Dec 2014 10:17:07 +0100 Subject: Use system where only return result is needed. --- lib/tasks/gitlab/shell.rake | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/tasks/gitlab/shell.rake b/lib/tasks/gitlab/shell.rake index 84bc9e304b6..ce5bec39c9d 100644 --- a/lib/tasks/gitlab/shell.rake +++ b/lib/tasks/gitlab/shell.rake @@ -17,17 +17,17 @@ namespace :gitlab do # Clone if needed unless File.directory?(target_dir) - Gitlab::Popen.popen(%W(git clone -- #{args.repo} #{target_dir})) + system(%W(git clone -- #{args.repo} #{target_dir})) end # Make sure we're on the right tag Dir.chdir(target_dir) do # First try to checkout without fetching # to avoid stalling tests if the Internet is down. - reset_status = reset_to_commit(args) + reseted = reset_to_commit(args) - if reset_status != 0 - Gitlab::Popen.popen(%W(git fetch origin)) + unless reseted + system(%W(git fetch origin)) reset_to_commit(args) end @@ -58,7 +58,7 @@ namespace :gitlab do File.open("config.yml", "w+") {|f| f.puts config.to_yaml} # Launch installation process - Gitlab::Popen.popen(%W(bin/install)) + system(%W(bin/install)) end # Required for debian packaging with PKGR: Setup .ssh/environment with @@ -126,13 +126,12 @@ namespace :gitlab do def reset_to_commit(args) tag, status = Gitlab::Popen.popen(%W(git describe -- #{args.tag})) - if status != 0 + unless status.zero? tag, status = Gitlab::Popen.popen(%W(git describe -- origin/#{args.tag})) end tag = tag.strip - reset, reset_status = Gitlab::Popen.popen(%W(git reset --hard #{tag})) - reset_status + system(%W(git reset --hard #{tag})) end end -- cgit v1.2.1 From e5951cf4aec15fc58730f88454d2b6d71ff25802 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Fri, 12 Dec 2014 10:42:55 +0100 Subject: Don't forget to splat. --- lib/tasks/gitlab/shell.rake | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/tasks/gitlab/shell.rake b/lib/tasks/gitlab/shell.rake index ce5bec39c9d..9af93300e08 100644 --- a/lib/tasks/gitlab/shell.rake +++ b/lib/tasks/gitlab/shell.rake @@ -17,7 +17,7 @@ namespace :gitlab do # Clone if needed unless File.directory?(target_dir) - system(%W(git clone -- #{args.repo} #{target_dir})) + system(*%W(git clone -- #{args.repo} #{target_dir})) end # Make sure we're on the right tag @@ -27,7 +27,7 @@ namespace :gitlab do reseted = reset_to_commit(args) unless reseted - system(%W(git fetch origin)) + system(*%W(git fetch origin)) reset_to_commit(args) end @@ -58,7 +58,7 @@ namespace :gitlab do File.open("config.yml", "w+") {|f| f.puts config.to_yaml} # Launch installation process - system(%W(bin/install)) + system(*%W(bin/install)) end # Required for debian packaging with PKGR: Setup .ssh/environment with @@ -131,7 +131,7 @@ namespace :gitlab do end tag = tag.strip - system(%W(git reset --hard #{tag})) + system(*%W(git reset --hard #{tag})) end end -- cgit v1.2.1 From 62ea02740d2fff83d636eb659eb5f80dbf1bd888 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Mon, 15 Dec 2014 18:47:26 +0100 Subject: Block Git HTTP Basic Auth after 10 failed attempts --- lib/gitlab/backend/grack_auth.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/backend/grack_auth.rb b/lib/gitlab/backend/grack_auth.rb index 762639414e0..ab5d2ef3da4 100644 --- a/lib/gitlab/backend/grack_auth.rb +++ b/lib/gitlab/backend/grack_auth.rb @@ -72,8 +72,18 @@ module Grack end def authenticate_user(login, password) - auth = Gitlab::Auth.new - auth.find(login, password) + user = Gitlab::Auth.new.find(login, password) + return user if user.present? + + # At this point, we know the credentials were wrong. We let Rack::Attack + # know there was a failed authentication attempt from this IP + Rack::Attack::Allow2Ban.filter(@request.ip, Gitlab.config.rack_attack.git_basic_auth) do + # Return true, so that Allow2Ban increments the counter (stored in + # Rails.cache) for the IP + true + end + + nil # No user was found end def authorized_request? -- cgit v1.2.1 From f06f69b9da81337db14324783b45ea5f55fcf735 Mon Sep 17 00:00:00 2001 From: Drew Blessing Date: Sun, 14 Dec 2014 19:01:59 -0600 Subject: Add theme type css class --- lib/gitlab/theme.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'lib') diff --git a/lib/gitlab/theme.rb b/lib/gitlab/theme.rb index b7c50cb734d..a7c83a880f6 100644 --- a/lib/gitlab/theme.rb +++ b/lib/gitlab/theme.rb @@ -19,5 +19,19 @@ module Gitlab return themes[id] end + + def self.type_css_class_by_id(id) + types = { + BASIC => 'light_theme', + MARS => 'dark_theme', + MODERN => 'dark_theme', + GRAY => 'dark_theme', + COLOR => 'dark_theme' + } + + id ||= Gitlab.config.gitlab.default_theme + + types[id] + end end end -- cgit v1.2.1 From c8b2def2be44771ffb479ad989acc7eccf4012f8 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Thu, 18 Dec 2014 11:08:11 +0100 Subject: Add more comments explaining how we block IPs --- lib/gitlab/backend/grack_auth.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/backend/grack_auth.rb b/lib/gitlab/backend/grack_auth.rb index ab5d2ef3da4..7bc745bf97e 100644 --- a/lib/gitlab/backend/grack_auth.rb +++ b/lib/gitlab/backend/grack_auth.rb @@ -76,7 +76,10 @@ module Grack return user if user.present? # At this point, we know the credentials were wrong. We let Rack::Attack - # know there was a failed authentication attempt from this IP + # know there was a failed authentication attempt from this IP. This + # information is stored in the Rails cache (Redis) and will be used by + # the Rack::Attack middleware to decide whether to block requests from + # this IP. Rack::Attack::Allow2Ban.filter(@request.ip, Gitlab.config.rack_attack.git_basic_auth) do # Return true, so that Allow2Ban increments the counter (stored in # Rails.cache) for the IP -- cgit v1.2.1 From 18d9172edc3bb3a1cfd7640ea0555e887ce5bde5 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Mon, 22 Dec 2014 10:03:52 +0100 Subject: Use a different name of the method to check if sanitize is enabled in check task. --- lib/tasks/gitlab/check.rake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/tasks/gitlab/check.rake b/lib/tasks/gitlab/check.rake index 1da5f4b980f..43115915de1 100644 --- a/lib/tasks/gitlab/check.rake +++ b/lib/tasks/gitlab/check.rake @@ -786,14 +786,14 @@ namespace :gitlab do end def sanitized_message(project) - if sanitize + if should_sanitize? "#{project.namespace_id.to_s.yellow}/#{project.id.to_s.yellow} ... " else "#{project.name_with_namespace.yellow} ... " end end - def sanitize + def should_sanitize? if ENV['SANITIZE'] == "true" true else -- cgit v1.2.1 From e41dadcb33fda44ee274daa673bd933e13aa90eb Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Fri, 19 Dec 2014 16:15:29 +0200 Subject: Doorkeeper integration --- lib/api/api.rb | 1 + lib/api/api_guard.rb | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/api/helpers.rb | 2 +- 3 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 lib/api/api_guard.rb (limited to 'lib') diff --git a/lib/api/api.rb b/lib/api/api.rb index d26667ba3f7..cb46f477ff9 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -2,6 +2,7 @@ Dir["#{Rails.root}/lib/api/*.rb"].each {|file| require file} module API class API < Grape::API + include APIGuard version 'v3', using: :path rescue_from ActiveRecord::RecordNotFound do diff --git a/lib/api/api_guard.rb b/lib/api/api_guard.rb new file mode 100644 index 00000000000..23975518181 --- /dev/null +++ b/lib/api/api_guard.rb @@ -0,0 +1,175 @@ +# Guard API with OAuth 2.0 Access Token + +require 'rack/oauth2' + +module APIGuard + extend ActiveSupport::Concern + + included do |base| + # OAuth2 Resource Server Authentication + use Rack::OAuth2::Server::Resource::Bearer, 'The API' do |request| + # The authenticator only fetches the raw token string + + # Must yield access token to store it in the env + request.access_token + end + + helpers HelperMethods + + install_error_responders(base) + end + + # Helper Methods for Grape Endpoint + module HelperMethods + # Invokes the doorkeeper guard. + # + # If token is presented and valid, then it sets @current_user. + # + # If the token does not have sufficient scopes to cover the requred scopes, + # then it raises InsufficientScopeError. + # + # If the token is expired, then it raises ExpiredError. + # + # If the token is revoked, then it raises RevokedError. + # + # If the token is not found (nil), then it raises TokenNotFoundError. + # + # Arguments: + # + # scopes: (optional) scopes required for this guard. + # Defaults to empty array. + # + def doorkeeper_guard!(scopes: []) + if (access_token = find_access_token).nil? + raise TokenNotFoundError + + else + case validate_access_token(access_token, scopes) + when Oauth2::AccessTokenValidationService::INSUFFICIENT_SCOPE + raise InsufficientScopeError.new(scopes) + + when Oauth2::AccessTokenValidationService::EXPIRED + raise ExpiredError + + when Oauth2::AccessTokenValidationService::REVOKED + raise RevokedError + + when Oauth2::AccessTokenValidationService::VALID + @current_user = User.find(access_token.resource_owner_id) + + end + end + end + + def doorkeeper_guard(scopes: []) + if access_token = find_access_token + case validate_access_token(access_token, scopes) + when Oauth2::AccessTokenValidationService::INSUFFICIENT_SCOPE + raise InsufficientScopeError.new(scopes) + + when Oauth2::AccessTokenValidationService::EXPIRED + raise ExpiredError + + when Oauth2::AccessTokenValidationService::REVOKED + raise RevokedError + + when Oauth2::AccessTokenValidationService::VALID + @current_user = User.find(access_token.resource_owner_id) + end + end + end + + def current_user + @current_user + end + + private + def find_access_token + @access_token ||= Doorkeeper.authenticate(doorkeeper_request, Doorkeeper.configuration.access_token_methods) + end + + def doorkeeper_request + @doorkeeper_request ||= ActionDispatch::Request.new(env) + end + + def validate_access_token(access_token, scopes) + Oauth2::AccessTokenValidationService.validate(access_token, scopes: scopes) + end + end + + module ClassMethods + # Installs the doorkeeper guard on the whole Grape API endpoint. + # + # Arguments: + # + # scopes: (optional) scopes required for this guard. + # Defaults to empty array. + # + def guard_all!(scopes: []) + before do + guard! scopes: scopes + end + end + + private + def install_error_responders(base) + error_classes = [ MissingTokenError, TokenNotFoundError, + ExpiredError, RevokedError, InsufficientScopeError] + + base.send :rescue_from, *error_classes, oauth2_bearer_token_error_handler + end + + def oauth2_bearer_token_error_handler + Proc.new {|e| + response = case e + when MissingTokenError + Rack::OAuth2::Server::Resource::Bearer::Unauthorized.new + + when TokenNotFoundError + Rack::OAuth2::Server::Resource::Bearer::Unauthorized.new( + :invalid_token, + "Bad Access Token.") + + when ExpiredError + Rack::OAuth2::Server::Resource::Bearer::Unauthorized.new( + :invalid_token, + "Token is expired. You can either do re-authorization or token refresh.") + + when RevokedError + Rack::OAuth2::Server::Resource::Bearer::Unauthorized.new( + :invalid_token, + "Token was revoked. You have to re-authorize from the user.") + + when InsufficientScopeError + # FIXME: ForbiddenError (inherited from Bearer::Forbidden of Rack::Oauth2) + # does not include WWW-Authenticate header, which breaks the standard. + Rack::OAuth2::Server::Resource::Bearer::Forbidden.new( + :insufficient_scope, + Rack::OAuth2::Server::Resource::ErrorMethods::DEFAULT_DESCRIPTION[:insufficient_scope], + { :scope => e.scopes}) + end + + response.finish + } + end + end + + # + # Exceptions + # + + class MissingTokenError < StandardError; end + + class TokenNotFoundError < StandardError; end + + class ExpiredError < StandardError; end + + class RevokedError < StandardError; end + + class InsufficientScopeError < StandardError + attr_reader :scopes + def initialize(scopes) + @scopes = scopes + end + end +end \ No newline at end of file diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 027fb20ec46..2f2342840fd 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -11,7 +11,7 @@ module API def current_user private_token = (params[PRIVATE_TOKEN_PARAM] || env[PRIVATE_TOKEN_HEADER]).to_s - @current_user ||= User.find_by(authentication_token: private_token) + @current_user ||= (User.find_by(authentication_token: private_token) || doorkeeper_guard) unless @current_user && Gitlab::UserAccess.allowed?(@current_user) return nil -- cgit v1.2.1 From 1fbc01024123c44740e1c94cab5a74faf2856a21 Mon Sep 17 00:00:00 2001 From: uran Date: Tue, 2 Sep 2014 18:12:13 +0300 Subject: Implemented notes (body) patching in API. --- lib/api/notes.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'lib') diff --git a/lib/api/notes.rb b/lib/api/notes.rb index 0ef9a3c4beb..b29c054a044 100644 --- a/lib/api/notes.rb +++ b/lib/api/notes.rb @@ -64,6 +64,39 @@ module API not_found! end end + + # Modify existing +noteable+ note + # + # Parameters: + # id (required) - The ID of a project + # noteable_id (required) - The ID of an issue or snippet + # node_id (required) - The ID of a note + # body (required) - New content of a note + # Example Request: + # PUT /projects/:id/issues/:noteable_id/notes/:note_id + # PUT /projects/:id/snippets/:noteable_id/notes/:node_id + put ":id/#{noteables_str}/:#{noteable_id_str}/notes/:note_id" do + required_attributes! [:body] + + authorize! :admin_note, user_project.notes.find(params[:note_id]) + + opts = { + note: params[:body], + note_id: params[:note_id], + noteable_type: noteables_str.classify, + noteable_id: params[noteable_id_str] + } + + @note = ::Notes::UpdateService.new(user_project, current_user, + opts).execute + + if @note.valid? + present @note, with: Entities::Note + else + bad_request!('Invalid note') + end + end + end end end -- cgit v1.2.1 From 61b4214e94116501424e1c9daaeef32566453b13 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Fri, 26 Dec 2014 09:35:49 +0100 Subject: Allow regular code push for developers if the protected branch allows it. --- lib/gitlab/git_access.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib') diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index 875f8d8b3a3..09724ae2e92 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -85,6 +85,8 @@ module Gitlab # and we dont allow remove of protected branch elsif newrev == Gitlab::Git::BLANK_SHA :remove_protected_branches + elsif project.developers_can_push_to_protected_branch?(branch_name(ref)) + :push_code else :push_code_to_protected_branches end -- cgit v1.2.1 From 770b2a5cfbec1081756bfa2d8bf046b7b16bb638 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Fri, 26 Dec 2014 09:52:39 +0100 Subject: Move protected branch actions into a method. --- lib/gitlab/git_access.rb | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index 09724ae2e92..d66dcad88bd 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -79,18 +79,8 @@ module Gitlab oldrev, newrev, ref = change.split(' ') action = if project.protected_branch?(branch_name(ref)) - # we dont allow force push to protected branch - if forced_push?(project, oldrev, newrev) - :force_push_code_to_protected_branches - # and we dont allow remove of protected branch - elsif newrev == Gitlab::Git::BLANK_SHA - :remove_protected_branches - elsif project.developers_can_push_to_protected_branch?(branch_name(ref)) - :push_code - else - :push_code_to_protected_branches - end - elsif project.repository.tag_names.include?(tag_name(ref)) + protected_branch_action(project, oldrev, newrev, branch_name(ref)) + elsif protected_tag?(tag_name(ref)) # Prevent any changes to existing git tag unless user has permissions :admin_project else @@ -110,6 +100,24 @@ module Gitlab private + def protected_branch_action(project, oldrev, newrev, branch_name) + # we dont allow force push to protected branch + if forced_push?(project, oldrev, newrev) + :force_push_code_to_protected_branches + # and we dont allow remove of protected branch + elsif newrev == Gitlab::Git::BLANK_SHA + :remove_protected_branches + elsif project.developers_can_push_to_protected_branch?(branch_name) + :push_code + else + :push_code_to_protected_branches + end + end + + def protected_tag?(tag_name) + project.repository.tag_names.include?(tag_name) + end + def user_allowed?(user) Gitlab::UserAccess.allowed?(user) end -- cgit v1.2.1 From 92eb3974ac28aff7c78f4ca0cbafbad842fc7160 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Fri, 26 Dec 2014 11:39:12 +0100 Subject: Add option to disable/enable developers push to already protected branches. --- lib/gitlab/git_access.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index d66dcad88bd..d47ef61fd11 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -80,7 +80,7 @@ module Gitlab action = if project.protected_branch?(branch_name(ref)) protected_branch_action(project, oldrev, newrev, branch_name(ref)) - elsif protected_tag?(tag_name(ref)) + elsif protected_tag?(project, tag_name(ref)) # Prevent any changes to existing git tag unless user has permissions :admin_project else @@ -114,7 +114,7 @@ module Gitlab end end - def protected_tag?(tag_name) + def protected_tag?(project, tag_name) project.repository.tag_names.include?(tag_name) end -- cgit v1.2.1 From cd688a60111853f63413a87ad6632ad57368e886 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Sun, 19 Oct 2014 16:09:38 +0200 Subject: Replace regex methods by string ones since faster and more readable. --- lib/api/internal.rb | 4 ++-- lib/tasks/gitlab/import.rake | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/api/internal.rb b/lib/api/internal.rb index 180e50611cf..a999cff09c0 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -25,8 +25,8 @@ module API # project. This applies the correct project permissions to # the wiki repository as well. access = - if project_path =~ /\.wiki\Z/ - project_path.sub!(/\.wiki\Z/, '') + if project_path.end_with?('.wiki') + project_path.chomp!('.wiki') Gitlab::GitAccessWiki.new else Gitlab::GitAccess.new diff --git a/lib/tasks/gitlab/import.rake b/lib/tasks/gitlab/import.rake index 3c693546c09..fef2afd7333 100644 --- a/lib/tasks/gitlab/import.rake +++ b/lib/tasks/gitlab/import.rake @@ -25,7 +25,7 @@ namespace :gitlab do puts "Processing #{repo_path}".yellow - if path =~ /\.wiki\Z/ + if path.end_with?('.wiki') puts " * Skipping wiki repo" next end -- cgit v1.2.1 From 6b507219465e50ceff726535f92b75fa9567906d Mon Sep 17 00:00:00 2001 From: Stephan van Leeuwen Date: Fri, 19 Dec 2014 13:27:27 +0100 Subject: Updated projects api to allow ordering Added support for order_by and sort parameters, to sort the projects by the specified values. Updated projects api documentation including the order_by and sort parameters --- lib/api/projects.rb | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 7fcf97d1ad6..2b6ec5e1b94 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -23,6 +23,19 @@ module API get do @projects = current_user.authorized_projects + sort = case params["sort"] + when 'desc' then 'DESC' + else 'ASC' + end + + @projects = case params["order_by"] + when 'id' then @projects.reorder("id #{sort}") + when 'name' then @projects.reorder("name #{sort}") + when 'created_at' then @projects.reorder("created_at #{sort}") + when 'last_activity_at' then @projects.reorder("last_activity_at #{sort}") + else @projects + end + # If the archived parameter is passed, limit results accordingly if params[:archived].present? @projects = @projects.where(archived: parse_boolean(params[:archived])) @@ -37,7 +50,21 @@ module API # Example Request: # GET /projects/owned get '/owned' do - @projects = paginate current_user.owned_projects + sort = case params["sort"] + when 'desc' then 'DESC' + else 'ASC' + end + + @projects = current_user.owned_projects + @projects = case params["order_by"] + when 'id' then @projects.reorder("id #{sort}") + when 'name' then @projects.reorder("name #{sort}") + when 'created_at' then @projects.reorder("created_at #{sort}") + when 'last_activity_at' then @projects.reorder("last_activity_at #{sort}") + else @projects + end + + @projects = paginate @projects present @projects, with: Entities::Project end @@ -47,7 +74,21 @@ module API # GET /projects/all get '/all' do authenticated_as_admin! - @projects = paginate Project + + sort = case params["sort"] + when 'desc' then 'DESC' + else 'ASC' + end + + @projects = case params["order_by"] + when 'id' then Project.order("id #{sort}") + when 'name' then Project.order("name #{sort}") + when 'created_at' then Project.order("created_at #{sort}") + when 'last_activity_at' then Project.order("last_activity_at #{sort}") + else Project + end + + @projects = paginate @projects present @projects, with: Entities::Project end @@ -227,6 +268,20 @@ module API ids = current_user.authorized_projects.map(&:id) visibility_levels = [ Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC ] projects = Project.where("(id in (?) OR visibility_level in (?)) AND (name LIKE (?))", ids, visibility_levels, "%#{params[:query]}%") + + sort = case params["sort"] + when 'desc' then 'DESC' + else 'ASC' + end + + projects = case params["order_by"] + when 'id' then projects.order("id #{sort}") + when 'name' then projects.order("name #{sort}") + when 'created_at' then projects.order("created_at #{sort}") + when 'last_activity_at' then projects.order("last_activity_at #{sort}") + else projects + end + present paginate(projects), with: Entities::Project end -- cgit v1.2.1 From 6af34b0f71898f4a93473584a40cdea6e075e92b Mon Sep 17 00:00:00 2001 From: Stephan van Leeuwen Date: Fri, 19 Dec 2014 19:26:19 +0100 Subject: Changed setting the sort variable Changed from using cases to set the sort variable, to use a one line if/else statement --- lib/api/projects.rb | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) (limited to 'lib') diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 2b6ec5e1b94..c5f57b9f8da 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -22,11 +22,7 @@ module API # GET /projects get do @projects = current_user.authorized_projects - - sort = case params["sort"] - when 'desc' then 'DESC' - else 'ASC' - end + sort = params[:sort] == 'desc' ? 'desc' : 'asc' @projects = case params["order_by"] when 'id' then @projects.reorder("id #{sort}") @@ -50,11 +46,7 @@ module API # Example Request: # GET /projects/owned get '/owned' do - sort = case params["sort"] - when 'desc' then 'DESC' - else 'ASC' - end - + sort = params[:sort] == 'desc' ? 'desc' : 'asc' @projects = current_user.owned_projects @projects = case params["order_by"] when 'id' then @projects.reorder("id #{sort}") @@ -74,11 +66,7 @@ module API # GET /projects/all get '/all' do authenticated_as_admin! - - sort = case params["sort"] - when 'desc' then 'DESC' - else 'ASC' - end + sort = params[:sort] == 'desc' ? 'desc' : 'asc' @projects = case params["order_by"] when 'id' then Project.order("id #{sort}") @@ -268,11 +256,7 @@ module API ids = current_user.authorized_projects.map(&:id) visibility_levels = [ Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC ] projects = Project.where("(id in (?) OR visibility_level in (?)) AND (name LIKE (?))", ids, visibility_levels, "%#{params[:query]}%") - - sort = case params["sort"] - when 'desc' then 'DESC' - else 'ASC' - end + sort = params[:sort] == 'desc' ? 'desc' : 'asc' projects = case params["order_by"] when 'id' then projects.order("id #{sort}") -- cgit v1.2.1 From 180fda3d0a911724bdd15a7b2d5aeee444054d10 Mon Sep 17 00:00:00 2001 From: Stephan van Leeuwen Date: Mon, 22 Dec 2014 13:48:00 +0100 Subject: Updated indentation on case when statements. --- lib/api/projects.rb | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'lib') diff --git a/lib/api/projects.rb b/lib/api/projects.rb index c5f57b9f8da..d6dd03656a6 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -25,11 +25,11 @@ module API sort = params[:sort] == 'desc' ? 'desc' : 'asc' @projects = case params["order_by"] - when 'id' then @projects.reorder("id #{sort}") - when 'name' then @projects.reorder("name #{sort}") - when 'created_at' then @projects.reorder("created_at #{sort}") - when 'last_activity_at' then @projects.reorder("last_activity_at #{sort}") - else @projects + when 'id' then @projects.reorder("id #{sort}") + when 'name' then @projects.reorder("name #{sort}") + when 'created_at' then @projects.reorder("created_at #{sort}") + when 'last_activity_at' then @projects.reorder("last_activity_at #{sort}") + else @projects end # If the archived parameter is passed, limit results accordingly @@ -49,11 +49,11 @@ module API sort = params[:sort] == 'desc' ? 'desc' : 'asc' @projects = current_user.owned_projects @projects = case params["order_by"] - when 'id' then @projects.reorder("id #{sort}") - when 'name' then @projects.reorder("name #{sort}") - when 'created_at' then @projects.reorder("created_at #{sort}") - when 'last_activity_at' then @projects.reorder("last_activity_at #{sort}") - else @projects + when 'id' then @projects.reorder("id #{sort}") + when 'name' then @projects.reorder("name #{sort}") + when 'created_at' then @projects.reorder("created_at #{sort}") + when 'last_activity_at' then @projects.reorder("last_activity_at #{sort}") + else @projects end @projects = paginate @projects @@ -69,11 +69,11 @@ module API sort = params[:sort] == 'desc' ? 'desc' : 'asc' @projects = case params["order_by"] - when 'id' then Project.order("id #{sort}") - when 'name' then Project.order("name #{sort}") - when 'created_at' then Project.order("created_at #{sort}") - when 'last_activity_at' then Project.order("last_activity_at #{sort}") - else Project + when 'id' then Project.order("id #{sort}") + when 'name' then Project.order("name #{sort}") + when 'created_at' then Project.order("created_at #{sort}") + when 'last_activity_at' then Project.order("last_activity_at #{sort}") + else Project end @projects = paginate @projects @@ -259,11 +259,11 @@ module API sort = params[:sort] == 'desc' ? 'desc' : 'asc' projects = case params["order_by"] - when 'id' then projects.order("id #{sort}") - when 'name' then projects.order("name #{sort}") - when 'created_at' then projects.order("created_at #{sort}") - when 'last_activity_at' then projects.order("last_activity_at #{sort}") - else projects + when 'id' then projects.order("id #{sort}") + when 'name' then projects.order("name #{sort}") + when 'created_at' then projects.order("created_at #{sort}") + when 'last_activity_at' then projects.order("last_activity_at #{sort}") + else projects end present paginate(projects), with: Entities::Project -- cgit v1.2.1 From 2660e83c97c46b7303a71b1110c693fbfbc9662c Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 30 Dec 2014 11:30:56 +0200 Subject: Add group filtering by name for API Signed-off-by: Dmitriy Zaporozhets --- lib/api/groups.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/api/groups.rb b/lib/api/groups.rb index f0ab6938b1c..a2d915a7eca 100644 --- a/lib/api/groups.rb +++ b/lib/api/groups.rb @@ -25,11 +25,14 @@ module API # Example Request: # GET /groups get do - if current_user.admin - @groups = paginate Group - else - @groups = paginate current_user.groups - end + @groups = if current_user.admin + Group.all + else + current_user.groups + end + + @groups = @groups.search(params[:search]) if params[:search].present? + @groups = paginate @groups present @groups, with: Entities::Group end -- cgit v1.2.1 From 7fa80b5bd01caff61c08c70b052c9965893cce5a Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Tue, 30 Dec 2014 13:36:13 +0100 Subject: Update branch api not found messages to 'Branch not found'. --- lib/api/branches.rb | 9 +++++---- lib/api/helpers.rb | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/api/branches.rb b/lib/api/branches.rb index 6ec1a753a69..b52d786e020 100644 --- a/lib/api/branches.rb +++ b/lib/api/branches.rb @@ -14,7 +14,8 @@ module API # Example Request: # GET /projects/:id/repository/branches get ":id/repository/branches" do - present user_project.repository.branches.sort_by(&:name), with: Entities::RepoObject, project: user_project + branches = user_project.repository.branches.sort_by(&:name) + present branches, with: Entities::RepoObject, project: user_project end # Get a single branch @@ -26,7 +27,7 @@ module API # GET /projects/:id/repository/branches/:branch get ':id/repository/branches/:branch', requirements: { branch: /.*/ } do @branch = user_project.repository.branches.find { |item| item.name == params[:branch] } - not_found!("Branch does not exist") if @branch.nil? + not_found!("Branch") unless @branch present @branch, with: Entities::RepoObject, project: user_project end @@ -43,7 +44,7 @@ module API authorize_admin_project @branch = user_project.repository.find_branch(params[:branch]) - not_found! unless @branch + not_found!("Branch") unless @branch protected_branch = user_project.protected_branches.find_by(name: @branch.name) user_project.protected_branches.create(name: @branch.name) unless protected_branch @@ -63,7 +64,7 @@ module API authorize_admin_project @branch = user_project.repository.find_branch(params[:branch]) - not_found! unless @branch + not_found!("Branch does not exist") unless @branch protected_branch = user_project.protected_branches.find_by(name: @branch.name) protected_branch.destroy if protected_branch diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 2f2342840fd..62c26ef76ce 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -42,7 +42,7 @@ module API def user_project @project ||= find_project(params[:id]) - @project || not_found! + @project || not_found!("Project") end def find_project(id) -- cgit v1.2.1 From d4b613ded728bbd45e5d67e5af555d661581ecf0 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Tue, 30 Dec 2014 14:00:07 +0100 Subject: Clearer message if adding comment to commit via api fails. --- lib/api/commits.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/api/commits.rb b/lib/api/commits.rb index 6c5391b98c8..1aea6943000 100644 --- a/lib/api/commits.rb +++ b/lib/api/commits.rb @@ -108,7 +108,7 @@ module API if note.save present note, with: Entities::CommitNote else - not_found! + error!("Failed to save note", 422) end end end -- cgit v1.2.1 From ed464edabeb62e35363ebadd0a5bb5ff394b6781 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Tue, 30 Dec 2014 14:29:55 +0100 Subject: Message for api files and groups. --- lib/api/commits.rb | 2 +- lib/api/files.rb | 4 ++-- lib/api/groups.rb | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/api/commits.rb b/lib/api/commits.rb index 1aea6943000..8e528e266bf 100644 --- a/lib/api/commits.rb +++ b/lib/api/commits.rb @@ -108,7 +108,7 @@ module API if note.save present note, with: Entities::CommitNote else - error!("Failed to save note", 422) + render_api_error!("Failed to save note #{note.errors.messages}", 422) end end end diff --git a/lib/api/files.rb b/lib/api/files.rb index 84e1d311781..e6e71bac367 100644 --- a/lib/api/files.rb +++ b/lib/api/files.rb @@ -35,7 +35,7 @@ module API file_path = attrs.delete(:file_path) commit = user_project.repository.commit(ref) - not_found! "Commit" unless commit + not_found! 'Commit' unless commit blob = user_project.repository.blob_at(commit.sha, file_path) @@ -53,7 +53,7 @@ module API commit_id: commit.id, } else - render_api_error!('File not found', 404) + not_found! 'File' end end diff --git a/lib/api/groups.rb b/lib/api/groups.rb index a2d915a7eca..cee51c82ad5 100644 --- a/lib/api/groups.rb +++ b/lib/api/groups.rb @@ -54,7 +54,7 @@ module API if @group.save present @group, with: Entities::Group else - not_found! + render_api_error!("Failed to save group #{@group.errors.messages}", 422) end end @@ -97,7 +97,7 @@ module API if result present group else - not_found! + render_api_error!("Failed to transfer project #{project.errors.messages}", 422) end end end -- cgit v1.2.1 From 7240150c8986c7aa21d0eb3140ac9a4c7674a4d2 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Tue, 30 Dec 2014 15:17:46 +0100 Subject: Forward the messages in api response. --- lib/api/milestones.rb | 4 ++-- lib/api/notes.rb | 2 +- lib/api/project_hooks.rb | 4 ++-- lib/api/project_members.rb | 2 +- lib/api/projects.rb | 2 +- lib/api/repositories.rb | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/api/milestones.rb b/lib/api/milestones.rb index a4fdb752d69..4d79f5a69ae 100644 --- a/lib/api/milestones.rb +++ b/lib/api/milestones.rb @@ -48,7 +48,7 @@ module API if milestone.valid? present milestone, with: Entities::Milestone else - not_found! + not_found!("Milestone #{milestone.errors.messages}") end end @@ -72,7 +72,7 @@ module API if milestone.valid? present milestone, with: Entities::Milestone else - not_found! + not_found!("Milestone #{milestone.errors.messages}") end end end diff --git a/lib/api/notes.rb b/lib/api/notes.rb index b29c054a044..b04d623c695 100644 --- a/lib/api/notes.rb +++ b/lib/api/notes.rb @@ -61,7 +61,7 @@ module API if @note.valid? present @note, with: Entities::Note else - not_found! + not_found!("Note #{@note.errors.messages}") end end diff --git a/lib/api/project_hooks.rb b/lib/api/project_hooks.rb index 7d056b9bf58..be9850367b9 100644 --- a/lib/api/project_hooks.rb +++ b/lib/api/project_hooks.rb @@ -53,7 +53,7 @@ module API if @hook.errors[:url].present? error!("Invalid url given", 422) end - not_found! + not_found!("Project hook #{@hook.errors.messages}") end end @@ -82,7 +82,7 @@ module API if @hook.errors[:url].present? error!("Invalid url given", 422) end - not_found! + not_found!("Project hook #{@hook.errors.messages}") end end diff --git a/lib/api/project_members.rb b/lib/api/project_members.rb index 1595ed0bc36..8e32f124ea5 100644 --- a/lib/api/project_members.rb +++ b/lib/api/project_members.rb @@ -9,7 +9,7 @@ module API if errors[:access_level].any? error!(errors[:access_level], 422) end - not_found! + not_found!(errors) end end diff --git a/lib/api/projects.rb b/lib/api/projects.rb index d6dd03656a6..e1cc2348865 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -227,7 +227,7 @@ module API render_api_error!("Project already forked", 409) end else - not_found! + not_found!("Source Project") end end diff --git a/lib/api/repositories.rb b/lib/api/repositories.rb index a1a7721b288..03a556a2c55 100644 --- a/lib/api/repositories.rb +++ b/lib/api/repositories.rb @@ -133,7 +133,7 @@ module API env['api.format'] = :binary present data else - not_found! + not_found!('File') end end -- cgit v1.2.1 From 56f211aa50246ff167894fcd050acad88d81f59e Mon Sep 17 00:00:00 2001 From: mattes Date: Fri, 5 Sep 2014 03:57:28 +0200 Subject: allow for private repositories --- lib/support/nginx/gitlab | 15 +++++++++++++++ lib/support/nginx/gitlab-ssl | 15 +++++++++++++++ 2 files changed, 30 insertions(+) (limited to 'lib') diff --git a/lib/support/nginx/gitlab b/lib/support/nginx/gitlab index c8b769ace8e..ab6ca6e6260 100644 --- a/lib/support/nginx/gitlab +++ b/lib/support/nginx/gitlab @@ -56,6 +56,21 @@ server { try_files $uri $uri/index.html $uri.html @gitlab; } + ## If ``go get`` detected, return go-import meta tag. + ## This works for public and for private repositories. + ## See also http://golang.org/cmd/go/#hdr-Remote_import_paths + if ($http_user_agent ~* "Go") { + return 200 " + + + + + + + + "; + } + ## If a file, which is not found in the root folder is requested, ## then the proxy passes the request to the upsteam (gitlab unicorn). location @gitlab { diff --git a/lib/support/nginx/gitlab-ssl b/lib/support/nginx/gitlab-ssl index 4e53d5e8b50..1903c9aa4fb 100644 --- a/lib/support/nginx/gitlab-ssl +++ b/lib/support/nginx/gitlab-ssl @@ -101,6 +101,21 @@ server { try_files $uri $uri/index.html $uri.html @gitlab; } + ## If ``go get`` detected, return go-import meta tag. + ## This works for public and for private repositories. + ## See also http://golang.org/cmd/go/#hdr-Remote_import_paths + if ($http_user_agent ~* "Go") { + return 200 " + + + + + + + + "; + } + ## If a file, which is not found in the root folder is requested, ## then the proxy passes the request to the upsteam (gitlab unicorn). location @gitlab { -- cgit v1.2.1 From 2c9b35732409c2a73150788067e1b03b91101f39 Mon Sep 17 00:00:00 2001 From: mattes Date: Fri, 5 Sep 2014 11:43:52 +0200 Subject: remove optional html tags --- lib/support/nginx/gitlab | 7 +------ lib/support/nginx/gitlab-ssl | 7 +------ 2 files changed, 2 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/support/nginx/gitlab b/lib/support/nginx/gitlab index ab6ca6e6260..80827150bee 100644 --- a/lib/support/nginx/gitlab +++ b/lib/support/nginx/gitlab @@ -62,12 +62,7 @@ server { if ($http_user_agent ~* "Go") { return 200 " - - - - - - + "; } diff --git a/lib/support/nginx/gitlab-ssl b/lib/support/nginx/gitlab-ssl index 1903c9aa4fb..7fb4d568d21 100644 --- a/lib/support/nginx/gitlab-ssl +++ b/lib/support/nginx/gitlab-ssl @@ -107,12 +107,7 @@ server { if ($http_user_agent ~* "Go") { return 200 " - - - - - - + "; } -- cgit v1.2.1 From 52bc4e79f83e56f7f90563aa2bd97b98b4cc2715 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Wed, 17 Dec 2014 12:18:11 +0100 Subject: Close standard input in Gitlab::Popen.popen --- lib/gitlab/popen.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib') diff --git a/lib/gitlab/popen.rb b/lib/gitlab/popen.rb index e2fbafb3899..fea4d2d55d2 100644 --- a/lib/gitlab/popen.rb +++ b/lib/gitlab/popen.rb @@ -21,6 +21,9 @@ module Gitlab @cmd_output = "" @cmd_status = 0 Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr| + # We are not using stdin so we should close it, in case the command we + # are running waits for input. + stdin.close @cmd_output << stdout.read @cmd_output << stderr.read @cmd_status = wait_thr.value.exitstatus -- cgit v1.2.1 From af56c1dd323ee418eb8dbfa9eb35c7ec9ac58a66 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Tue, 6 Jan 2015 16:56:56 +0100 Subject: White-list requests from 127.0.0.1 On some misconfigured GitLab servers, if you look in production.log it looks like all requests come from 127.0.0.1. To avoid unwanted banning we white-list 127.0.0.1 with this commit. --- lib/gitlab/backend/grack_auth.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/backend/grack_auth.rb b/lib/gitlab/backend/grack_auth.rb index 7bc745bf97e..1f71906bc8e 100644 --- a/lib/gitlab/backend/grack_auth.rb +++ b/lib/gitlab/backend/grack_auth.rb @@ -80,10 +80,15 @@ module Grack # information is stored in the Rails cache (Redis) and will be used by # the Rack::Attack middleware to decide whether to block requests from # this IP. - Rack::Attack::Allow2Ban.filter(@request.ip, Gitlab.config.rack_attack.git_basic_auth) do - # Return true, so that Allow2Ban increments the counter (stored in - # Rails.cache) for the IP - true + config = Gitlab.config.rack_attack.git_basic_auth + Rack::Attack::Allow2Ban.filter(@request.ip, config) do + # Unless the IP is whitelisted, return true so that Allow2Ban + # increments the counter (stored in Rails.cache) for the IP + if config.ip_whitelist.include?(@request.ip) + false + else + true + end end nil # No user was found -- cgit v1.2.1 From cd0aed3d54fc01d0c361a8cf282d2de48297f66a Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Wed, 7 Jan 2015 10:46:00 +0100 Subject: Add a message when unable to save an object through api. --- lib/api/commits.rb | 2 +- lib/api/deploy_keys.rb | 2 +- lib/api/groups.rb | 4 ++-- lib/api/issues.rb | 8 ++++---- lib/api/labels.rb | 4 ++-- lib/api/merge_requests.rb | 4 ++-- lib/api/milestones.rb | 4 ++-- lib/api/notes.rb | 2 +- 8 files changed, 15 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/api/commits.rb b/lib/api/commits.rb index 8e528e266bf..0de4e720ffe 100644 --- a/lib/api/commits.rb +++ b/lib/api/commits.rb @@ -108,7 +108,7 @@ module API if note.save present note, with: Entities::CommitNote else - render_api_error!("Failed to save note #{note.errors.messages}", 422) + render_api_error!("Failed to save note #{note.errors.messages}", 400) end end end diff --git a/lib/api/deploy_keys.rb b/lib/api/deploy_keys.rb index 06eb7756841..dd4b761feb2 100644 --- a/lib/api/deploy_keys.rb +++ b/lib/api/deploy_keys.rb @@ -58,7 +58,7 @@ module API if key.valid? && user_project.deploy_keys << key present key, with: Entities::SSHKey else - render_validation_error!(key) + render_api_error!("Failed to add key #{key.errors.messages}", 400) end end diff --git a/lib/api/groups.rb b/lib/api/groups.rb index cee51c82ad5..bda60b3b7d5 100644 --- a/lib/api/groups.rb +++ b/lib/api/groups.rb @@ -54,7 +54,7 @@ module API if @group.save present @group, with: Entities::Group else - render_api_error!("Failed to save group #{@group.errors.messages}", 422) + render_api_error!("Failed to save group #{@group.errors.messages}", 400) end end @@ -97,7 +97,7 @@ module API if result present group else - render_api_error!("Failed to transfer project #{project.errors.messages}", 422) + render_api_error!("Failed to transfer project #{project.errors.messages}", 400) end end end diff --git a/lib/api/issues.rb b/lib/api/issues.rb index d2828b24c36..01496c39955 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -104,7 +104,7 @@ module API # Validate label names in advance if (errors = validate_label_params(params)).any? - render_api_error!({ labels: errors }, 400) + render_api_error!("Unable to validate label: #{errors}"}, 400) end issue = ::Issues::CreateService.new(user_project, current_user, attrs).execute @@ -118,7 +118,7 @@ module API present issue, with: Entities::Issue else - render_validation_error!(issue) + render_api_error!("Unable to create issue #{issue.errors.messages}", 400) end end @@ -142,7 +142,7 @@ module API # Validate label names in advance if (errors = validate_label_params(params)).any? - render_api_error!({ labels: errors }, 400) + render_api_error!("Unable to validate label: #{errors}"}, 400) end issue = ::Issues::UpdateService.new(user_project, current_user, attrs).execute(issue) @@ -158,7 +158,7 @@ module API present issue, with: Entities::Issue else - render_validation_error!(issue) + render_api_error!("Unable to update issue #{issue.errors.messages}", 400) end end diff --git a/lib/api/labels.rb b/lib/api/labels.rb index 78ca58ad0d1..e8ded662253 100644 --- a/lib/api/labels.rb +++ b/lib/api/labels.rb @@ -37,7 +37,7 @@ module API if label.valid? present label, with: Entities::Label else - render_validation_error!(label) + render_api_error!("Unable to create label #{label.errors.messages}", 400) end end @@ -90,7 +90,7 @@ module API if label.update(attrs) present label, with: Entities::Label else - render_validation_error!(label) + render_api_error!("Unable to create label #{label.errors.messages}", 400) end end end diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb index a365f1db00f..1a73c4943b8 100644 --- a/lib/api/merge_requests.rb +++ b/lib/api/merge_requests.rb @@ -137,7 +137,7 @@ module API # Validate label names in advance if (errors = validate_label_params(params)).any? - render_api_error!({ labels: errors }, 400) + render_api_error!("Unable to validate label: #{errors}"}, 400) end merge_request = ::MergeRequests::UpdateService.new(user_project, current_user, attrs).execute(merge_request) @@ -233,7 +233,7 @@ module API if note.save present note, with: Entities::MRNote else - render_validation_error!(note) + render_api_error!("Failed to save note #{note.errors.messages}", 400) end end end diff --git a/lib/api/milestones.rb b/lib/api/milestones.rb index 4d79f5a69ae..2ea49359df0 100644 --- a/lib/api/milestones.rb +++ b/lib/api/milestones.rb @@ -48,7 +48,7 @@ module API if milestone.valid? present milestone, with: Entities::Milestone else - not_found!("Milestone #{milestone.errors.messages}") + render_api_error!("Failed to create milestone #{milestone.errors.messages}", 400) end end @@ -72,7 +72,7 @@ module API if milestone.valid? present milestone, with: Entities::Milestone else - not_found!("Milestone #{milestone.errors.messages}") + render_api_error!("Failed to update milestone #{milestone.errors.messages}", 400) end end end diff --git a/lib/api/notes.rb b/lib/api/notes.rb index b04d623c695..3726be7c537 100644 --- a/lib/api/notes.rb +++ b/lib/api/notes.rb @@ -93,7 +93,7 @@ module API if @note.valid? present @note, with: Entities::Note else - bad_request!('Invalid note') + render_api_error!("Failed to save note #{note.errors.messages}", 400) end end -- cgit v1.2.1 From 8dd672776ee72990fd41b37559a8ba102595d6ca Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Wed, 7 Jan 2015 11:39:20 +0100 Subject: Fix failing tests due to updates on the return messages. --- lib/api/deploy_keys.rb | 2 +- lib/api/issues.rb | 8 ++++---- lib/api/labels.rb | 4 ++-- lib/api/merge_requests.rb | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/api/deploy_keys.rb b/lib/api/deploy_keys.rb index dd4b761feb2..06eb7756841 100644 --- a/lib/api/deploy_keys.rb +++ b/lib/api/deploy_keys.rb @@ -58,7 +58,7 @@ module API if key.valid? && user_project.deploy_keys << key present key, with: Entities::SSHKey else - render_api_error!("Failed to add key #{key.errors.messages}", 400) + render_validation_error!(key) end end diff --git a/lib/api/issues.rb b/lib/api/issues.rb index 01496c39955..d2828b24c36 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -104,7 +104,7 @@ module API # Validate label names in advance if (errors = validate_label_params(params)).any? - render_api_error!("Unable to validate label: #{errors}"}, 400) + render_api_error!({ labels: errors }, 400) end issue = ::Issues::CreateService.new(user_project, current_user, attrs).execute @@ -118,7 +118,7 @@ module API present issue, with: Entities::Issue else - render_api_error!("Unable to create issue #{issue.errors.messages}", 400) + render_validation_error!(issue) end end @@ -142,7 +142,7 @@ module API # Validate label names in advance if (errors = validate_label_params(params)).any? - render_api_error!("Unable to validate label: #{errors}"}, 400) + render_api_error!({ labels: errors }, 400) end issue = ::Issues::UpdateService.new(user_project, current_user, attrs).execute(issue) @@ -158,7 +158,7 @@ module API present issue, with: Entities::Issue else - render_api_error!("Unable to update issue #{issue.errors.messages}", 400) + render_validation_error!(issue) end end diff --git a/lib/api/labels.rb b/lib/api/labels.rb index e8ded662253..78ca58ad0d1 100644 --- a/lib/api/labels.rb +++ b/lib/api/labels.rb @@ -37,7 +37,7 @@ module API if label.valid? present label, with: Entities::Label else - render_api_error!("Unable to create label #{label.errors.messages}", 400) + render_validation_error!(label) end end @@ -90,7 +90,7 @@ module API if label.update(attrs) present label, with: Entities::Label else - render_api_error!("Unable to create label #{label.errors.messages}", 400) + render_validation_error!(label) end end end diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb index 1a73c4943b8..81038d05f12 100644 --- a/lib/api/merge_requests.rb +++ b/lib/api/merge_requests.rb @@ -137,7 +137,7 @@ module API # Validate label names in advance if (errors = validate_label_params(params)).any? - render_api_error!("Unable to validate label: #{errors}"}, 400) + render_api_error!({ labels: errors }, 400) end merge_request = ::MergeRequests::UpdateService.new(user_project, current_user, attrs).execute(merge_request) -- cgit v1.2.1 From 57a65ede77b7bbae6e3b2a7aa52135de7b0c2f8e Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 8 Jan 2015 09:53:35 -0800 Subject: Improve application settings and write tests --- lib/gitlab/current_settings.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 lib/gitlab/current_settings.rb (limited to 'lib') diff --git a/lib/gitlab/current_settings.rb b/lib/gitlab/current_settings.rb new file mode 100644 index 00000000000..3467bb892fc --- /dev/null +++ b/lib/gitlab/current_settings.rb @@ -0,0 +1,7 @@ +module Gitlab + module CurrentSettings + def current_application_settings + ApplicationSetting.current + end + end +end -- cgit v1.2.1 From 8133e44998236438c46e1b662bd284323287f415 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 8 Jan 2015 10:30:35 -0800 Subject: Hack for migrating to new settings --- lib/gitlab/current_settings.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/current_settings.rb b/lib/gitlab/current_settings.rb index 3467bb892fc..60efc70aa40 100644 --- a/lib/gitlab/current_settings.rb +++ b/lib/gitlab/current_settings.rb @@ -1,7 +1,17 @@ module Gitlab module CurrentSettings def current_application_settings - ApplicationSetting.current + if ActiveRecord::Base.connection.table_exists?('application_settings') + ApplicationSetting.current + else + OpenStruct.new( + default_projects_limit: Settings.gitlab['default_projects_limit'], + signup_enabled: Settings.gitlab['signup_enabled'], + signin_enabled: Settings.gitlab['signin_enabled'], + gravatar_enabled: Settings.gravatar['enabled'], + sign_in_text: Settings.extra['sign_in_text'], + ) + end end end end -- cgit v1.2.1 From d0a50985ec613584821806062df4eaa39337449c Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 8 Jan 2015 11:26:16 -0800 Subject: Create ApplicationSettings if does not exist in runtime --- lib/gitlab/current_settings.rb | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/current_settings.rb b/lib/gitlab/current_settings.rb index 60efc70aa40..f3b9dcacdee 100644 --- a/lib/gitlab/current_settings.rb +++ b/lib/gitlab/current_settings.rb @@ -2,16 +2,21 @@ module Gitlab module CurrentSettings def current_application_settings if ActiveRecord::Base.connection.table_exists?('application_settings') - ApplicationSetting.current + ApplicationSetting.current || + ApplicationSetting.create_from_defaults else - OpenStruct.new( - default_projects_limit: Settings.gitlab['default_projects_limit'], - signup_enabled: Settings.gitlab['signup_enabled'], - signin_enabled: Settings.gitlab['signin_enabled'], - gravatar_enabled: Settings.gravatar['enabled'], - sign_in_text: Settings.extra['sign_in_text'], - ) + fake_application_settings end end + + def fake_application_settings + OpenStruct.new( + default_projects_limit: Settings.gitlab['default_projects_limit'], + signup_enabled: Settings.gitlab['signup_enabled'], + signin_enabled: Settings.gitlab['signin_enabled'], + gravatar_enabled: Settings.gravatar['enabled'], + sign_in_text: Settings.extra['sign_in_text'], + ) + end end end -- cgit v1.2.1 From bc95576e2cbbd2def7a6fce1bde2d0263a3d7da1 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Fri, 9 Jan 2015 11:26:26 +0100 Subject: Rescue missing database errors While loading the Rails app we cannot assume that the gitlabhq_xxx database exists already. If we do, `rake gitlab:setup` breaks! This is a quick hack to make sure that fresh development setups of GitLab (from master) will work again. --- lib/gitlab/current_settings.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/current_settings.rb b/lib/gitlab/current_settings.rb index f3b9dcacdee..5d88a601dea 100644 --- a/lib/gitlab/current_settings.rb +++ b/lib/gitlab/current_settings.rb @@ -1,10 +1,14 @@ module Gitlab module CurrentSettings def current_application_settings - if ActiveRecord::Base.connection.table_exists?('application_settings') - ApplicationSetting.current || - ApplicationSetting.create_from_defaults - else + begin + if ActiveRecord::Base.connection.table_exists?('application_settings') + ApplicationSetting.current || + ApplicationSetting.create_from_defaults + else + fake_application_settings + end + rescue ActiveRecord::NoDatabaseError fake_application_settings end end -- cgit v1.2.1 From a9f7fd2c1a7052247333b89f6a22a883b480370d Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Wed, 31 Dec 2014 15:07:48 +0200 Subject: Github Importer --- lib/gitlab/github/client.rb | 29 ++++++++++++++++++++++ lib/gitlab/github/importer.rb | 48 ++++++++++++++++++++++++++++++++++++ lib/gitlab/github/project_creator.rb | 37 +++++++++++++++++++++++++++ lib/gitlab/regex.rb | 2 +- 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 lib/gitlab/github/client.rb create mode 100644 lib/gitlab/github/importer.rb create mode 100644 lib/gitlab/github/project_creator.rb (limited to 'lib') diff --git a/lib/gitlab/github/client.rb b/lib/gitlab/github/client.rb new file mode 100644 index 00000000000..c6935a0b0ba --- /dev/null +++ b/lib/gitlab/github/client.rb @@ -0,0 +1,29 @@ +module Gitlab + module Github + class Client + attr_reader :client + + def initialize + @client = ::OAuth2::Client.new( + config.app_id, + config.app_secret, + github_options + ) + end + + private + + def config + Gitlab.config.omniauth.providers.select{|provider| provider.name == "github"}.first + end + + def github_options + { + :site => 'https://api.github.com', + :authorize_url => 'https://github.com/login/oauth/authorize', + :token_url => 'https://github.com/login/oauth/access_token' + } + end + end + end +end diff --git a/lib/gitlab/github/importer.rb b/lib/gitlab/github/importer.rb new file mode 100644 index 00000000000..c72a1c25e9e --- /dev/null +++ b/lib/gitlab/github/importer.rb @@ -0,0 +1,48 @@ +module Gitlab + module Github + class Importer + attr_reader :project + + def initialize(project) + @project = project + end + + def execute + client = octo_client(project.creator.github_access_token) + + #Issues && Comments + client.list_issues(project.import_source, state: :all).each do |issue| + if issue.pull_request.nil? + body = "*Created by: #{issue.user.login}*\n\n#{issue.body}" + + if issue.comments > 0 + body += "\n\n\n**Imported comments:**\n" + client.issue_comments(project.import_source, issue.number).each do |c| + body += "\n\n*By #{c.user.login} on #{c.created_at}*\n\n#{c.body}" + end + end + + project.issues.create!( + description: body, + title: issue.title, + state: issue.state == 'closed' ? 'closed' : 'opened', + author_id: gl_user_id(project, issue.user.id) + ) + end + end + end + + private + + def octo_client(access_token) + ::Octokit.auto_paginate = true + ::Octokit::Client.new(:access_token => access_token) + end + + def gl_user_id(project, github_id) + user = User.joins(:identities).find_by("identities.extern_uid = ?", github_id.to_s) + (user && user.id) || project.creator_id + end + end + end +end diff --git a/lib/gitlab/github/project_creator.rb b/lib/gitlab/github/project_creator.rb new file mode 100644 index 00000000000..682ef389e44 --- /dev/null +++ b/lib/gitlab/github/project_creator.rb @@ -0,0 +1,37 @@ +module Gitlab + module Github + class ProjectCreator + attr_reader :repo, :namespace, :current_user + + def initialize(repo, namespace, current_user) + @repo = repo + @namespace = namespace + @current_user = current_user + end + + def execute + @project = Project.new( + name: repo.name, + path: repo.name, + description: repo.description, + namespace: namespace, + creator: current_user, + visibility_level: repo.private ? Gitlab::VisibilityLevel::PRIVATE : Gitlab::VisibilityLevel::PUBLIC, + import_type: "github", + import_source: repo.full_name, + import_url: repo.clone_url.sub("https://", "https://#{current_user.github_access_token}@") + ) + + if @project.save! + @project.reload + + if @project.import_failed? + @project.import_retry + else + @project.import_start + end + end + end + end + end +end diff --git a/lib/gitlab/regex.rb b/lib/gitlab/regex.rb index c4d0d85b7f5..cf6e260f257 100644 --- a/lib/gitlab/regex.rb +++ b/lib/gitlab/regex.rb @@ -11,7 +11,7 @@ module Gitlab end def project_name_regex - /\A[a-zA-Z0-9_][a-zA-Z0-9_\-\. ]*\z/ + /\A[a-zA-Z0-9_.][a-zA-Z0-9_\-\. ]*\z/ end def project_regex_message -- cgit v1.2.1 From 319704451233f4abfbb0e4bcc9bb3e0a756f5eb1 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Sun, 11 Jan 2015 23:51:31 -0800 Subject: Refactor push data builder. Moved it to separate class Also execute GitLab CI on creating tag via UI --- lib/gitlab/push_data_builder.rb | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 lib/gitlab/push_data_builder.rb (limited to 'lib') diff --git a/lib/gitlab/push_data_builder.rb b/lib/gitlab/push_data_builder.rb new file mode 100644 index 00000000000..72c42a6a254 --- /dev/null +++ b/lib/gitlab/push_data_builder.rb @@ -0,0 +1,63 @@ +module Gitlab + class PushDataBuilder + # Produce a hash of post-receive data + # + # data = { + # before: String, + # after: String, + # ref: String, + # user_id: String, + # user_name: String, + # project_id: String, + # repository: { + # name: String, + # url: String, + # description: String, + # homepage: String, + # }, + # commits: Array, + # total_commits_count: Fixnum + # } + # + def self.build(project, user, oldrev, newrev, ref, commits = []) + # Total commits count + commits_count = commits.size + + # Get latest 20 commits ASC + commits_limited = commits.last(20) + + # Hash to be passed as post_receive_data + data = { + before: oldrev, + after: newrev, + ref: ref, + user_id: user.id, + user_name: user.name, + project_id: project.id, + repository: { + name: project.name, + url: project.url_to_repo, + description: project.description, + homepage: project.web_url, + }, + commits: [], + total_commits_count: commits_count + } + + # For performance purposes maximum 20 latest commits + # will be passed as post receive hook data. + commits_limited.each do |commit| + data[:commits] << commit.hook_attrs(project) + end + + data + end + + # This method provide a sample data generated with + # existing project and commits to test web hooks + def self.build_sample(project, user) + commits = project.repository.commits(project.default_branch, nil, 3) + build(project, user, commits.last.id, commits.first.id, "refs/heads/#{project.default_branch}", commits) + end + end +end -- cgit v1.2.1 From 8689ce1efef8438debeec2a3a6d669f4d5a435c4 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Mon, 12 Jan 2015 11:08:53 +0100 Subject: Add search filter option on project api for authorized projects. --- lib/api/projects.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/api/projects.rb b/lib/api/projects.rb index e1cc2348865..b9c95c785f2 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -15,9 +15,6 @@ module API # Get a projects list for authenticated user # - # Parameters: - # archived (optional) - if passed, limit by archived status - # # Example Request: # GET /projects get do @@ -37,6 +34,10 @@ module API @projects = @projects.where(archived: parse_boolean(params[:archived])) end + if params[:search].present? + @projects = @projects.search(params[:search]) + end + @projects = paginate @projects present @projects, with: Entities::Project end -- cgit v1.2.1 From 1e37e8924ab38cfbb2a838c2bc6589b03f72dbcd Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 13 Jan 2015 11:44:17 -0800 Subject: Improve github import page UI --- lib/gitlab/github/client.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/github/client.rb b/lib/gitlab/github/client.rb index c6935a0b0ba..d6b936c649c 100644 --- a/lib/gitlab/github/client.rb +++ b/lib/gitlab/github/client.rb @@ -19,9 +19,9 @@ module Gitlab def github_options { - :site => 'https://api.github.com', - :authorize_url => 'https://github.com/login/oauth/authorize', - :token_url => 'https://github.com/login/oauth/access_token' + site: 'https://api.github.com', + authorize_url: 'https://github.com/login/oauth/authorize', + token_url: 'https://github.com/login/oauth/access_token' } end end -- cgit v1.2.1 From e348af1d4c29f2aa7fb03a7910fd816850caab11 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Wed, 14 Jan 2015 09:03:25 +0100 Subject: Rescue database error in application settings if the database still doesn't exist. --- lib/gitlab/current_settings.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/current_settings.rb b/lib/gitlab/current_settings.rb index 5d88a601dea..22ad7ef8c8b 100644 --- a/lib/gitlab/current_settings.rb +++ b/lib/gitlab/current_settings.rb @@ -8,7 +8,7 @@ module Gitlab else fake_application_settings end - rescue ActiveRecord::NoDatabaseError + rescue ActiveRecord::NoDatabaseError, database_adapter.constantize::Error fake_application_settings end end @@ -22,5 +22,16 @@ module Gitlab sign_in_text: Settings.extra['sign_in_text'], ) end + + # We need to check which database is setup + # but we cannot assume that the database exists already. + # Not checking this will break "rake gitlab:setup". + def database_adapter + if Rails.configuration.database_configuration[Rails.env]['adapter'] == 'mysql2' + "Mysql2" + else + "PG" + end + end end end -- cgit v1.2.1 From 02d8575a610176bbb79eac91b5ab783872ef098a Mon Sep 17 00:00:00 2001 From: DJ Mountney Date: Tue, 13 Jan 2015 10:58:32 -0800 Subject: Check for database connection before loading current application settings --- lib/gitlab/current_settings.rb | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/current_settings.rb b/lib/gitlab/current_settings.rb index 22ad7ef8c8b..2c5660df373 100644 --- a/lib/gitlab/current_settings.rb +++ b/lib/gitlab/current_settings.rb @@ -1,14 +1,10 @@ module Gitlab module CurrentSettings def current_application_settings - begin - if ActiveRecord::Base.connection.table_exists?('application_settings') - ApplicationSetting.current || - ApplicationSetting.create_from_defaults - else - fake_application_settings - end - rescue ActiveRecord::NoDatabaseError, database_adapter.constantize::Error + if ActiveRecord::Base.connected? && ActiveRecord::Base.connection.table_exists?('application_settings') + ApplicationSetting.current || + ApplicationSetting.create_from_defaults + else fake_application_settings end end @@ -22,16 +18,5 @@ module Gitlab sign_in_text: Settings.extra['sign_in_text'], ) end - - # We need to check which database is setup - # but we cannot assume that the database exists already. - # Not checking this will break "rake gitlab:setup". - def database_adapter - if Rails.configuration.database_configuration[Rails.env]['adapter'] == 'mysql2' - "Mysql2" - else - "PG" - end - end end end -- cgit v1.2.1 From f8b97b454b8eae343bd7ea6e92fd2257eeae45b0 Mon Sep 17 00:00:00 2001 From: Sytse Sijbrandij Date: Wed, 14 Jan 2015 21:12:16 -0800 Subject: Make view link come first so I don't have to mouse to the end of the email line. --- lib/tasks/gitlab/mail_google_schema_whitelisting.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/tasks/gitlab/mail_google_schema_whitelisting.rake b/lib/tasks/gitlab/mail_google_schema_whitelisting.rake index f40bba24da8..102c6ae55d5 100644 --- a/lib/tasks/gitlab/mail_google_schema_whitelisting.rake +++ b/lib/tasks/gitlab/mail_google_schema_whitelisting.rake @@ -54,8 +54,8 @@ namespace :gitlab do -- cgit v1.2.1 From bf079c24afb8ad2991a4eaf60a71a7bc45dd775d Mon Sep 17 00:00:00 2001 From: Stefan Tatschner Date: Wed, 3 Dec 2014 15:27:31 +0100 Subject: Replace highlight.js with rouge-fork rugments I decided to create a fork of rouge as rouge lacks a HTML formatter with the required options such as wrapping a line with tags. Furthermore I was not really convinced about the clarity of rouge's source code. Rugments 1.0.0beta3 for now only includes some basic linting and a new HTML formatter. Everything else should behave the same. --- lib/redcarpet/render/gitlab_html.rb | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'lib') diff --git a/lib/redcarpet/render/gitlab_html.rb b/lib/redcarpet/render/gitlab_html.rb index 54d740908d5..714261f815c 100644 --- a/lib/redcarpet/render/gitlab_html.rb +++ b/lib/redcarpet/render/gitlab_html.rb @@ -21,23 +21,22 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML text.gsub("'", "’") end + # Stolen from Rugments::Plugins::Redcarpet as this module is not required + # from Rugments's gem root. def block_code(code, language) - # New lines are placed to fix an rendering issue - # with code wrapped inside

tag for next case: - # - # # Title kinda h1 - # - # ruby code here - # - <<-HTML + lexer = Rugments::Lexer.find_fancy(language, code) || Rugments::Lexers::PlainText -
-
-
#{h.send(:html_escape, code)}
-
-
+ # XXX HACK: Redcarpet strips hard tabs out of code blocks, + # so we assume you're not using leading spaces that aren't tabs, + # and just replace them here. + if lexer.tag == 'make' + code.gsub! /^ /, "\t" + end - HTML + formatter = Rugments::Formatters::HTML.new( + cssclass: "code highlight white #{lexer.tag}" + ) + formatter.format(lexer.lex(code)) end def link(link, title, content) -- cgit v1.2.1 From a0d4235c04e8f47e8625a6f46d64b65df599b370 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 15 Jan 2015 10:26:33 -0800 Subject: Send checkout sha for web hooks and services --- lib/gitlab/git.rb | 4 ++ lib/gitlab/push_data_builder.rb | 118 ++++++++++++++++++++++------------------ 2 files changed, 70 insertions(+), 52 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/git.rb b/lib/gitlab/git.rb index 67aca5e36e9..4a712c6345f 100644 --- a/lib/gitlab/git.rb +++ b/lib/gitlab/git.rb @@ -1,5 +1,9 @@ module Gitlab module Git BLANK_SHA = '0' * 40 + + def self.extract_ref_name(ref) + ref.gsub(/\Arefs\/(tags|heads)\//, '') + end end end diff --git a/lib/gitlab/push_data_builder.rb b/lib/gitlab/push_data_builder.rb index 72c42a6a254..7f5d71376f1 100644 --- a/lib/gitlab/push_data_builder.rb +++ b/lib/gitlab/push_data_builder.rb @@ -1,63 +1,77 @@ module Gitlab class PushDataBuilder - # Produce a hash of post-receive data - # - # data = { - # before: String, - # after: String, - # ref: String, - # user_id: String, - # user_name: String, - # project_id: String, - # repository: { - # name: String, - # url: String, - # description: String, - # homepage: String, - # }, - # commits: Array, - # total_commits_count: Fixnum - # } - # - def self.build(project, user, oldrev, newrev, ref, commits = []) - # Total commits count - commits_count = commits.size + class << self + # Produce a hash of post-receive data + # + # data = { + # before: String, + # after: String, + # ref: String, + # user_id: String, + # user_name: String, + # project_id: String, + # repository: { + # name: String, + # url: String, + # description: String, + # homepage: String, + # }, + # commits: Array, + # total_commits_count: Fixnum + # } + # + def build(project, user, oldrev, newrev, ref, commits = []) + # Total commits count + commits_count = commits.size - # Get latest 20 commits ASC - commits_limited = commits.last(20) + # Get latest 20 commits ASC + commits_limited = commits.last(20) - # Hash to be passed as post_receive_data - data = { - before: oldrev, - after: newrev, - ref: ref, - user_id: user.id, - user_name: user.name, - project_id: project.id, - repository: { - name: project.name, - url: project.url_to_repo, - description: project.description, - homepage: project.web_url, - }, - commits: [], - total_commits_count: commits_count - } + # Hash to be passed as post_receive_data + data = { + before: oldrev, + after: newrev, + ref: ref, + checkout_sha: checkout_sha(project.repository, newrev, ref), + user_id: user.id, + user_name: user.name, + project_id: project.id, + repository: { + name: project.name, + url: project.url_to_repo, + description: project.description, + homepage: project.web_url, + }, + commits: [], + total_commits_count: commits_count + } - # For performance purposes maximum 20 latest commits - # will be passed as post receive hook data. - commits_limited.each do |commit| - data[:commits] << commit.hook_attrs(project) + # For performance purposes maximum 20 latest commits + # will be passed as post receive hook data. + commits_limited.each do |commit| + data[:commits] << commit.hook_attrs(project) + end + + data end - data - end + # This method provide a sample data generated with + # existing project and commits to test web hooks + def build_sample(project, user) + commits = project.repository.commits(project.default_branch, nil, 3) + build(project, user, commits.last.id, commits.first.id, "refs/heads/#{project.default_branch}", commits) + end - # This method provide a sample data generated with - # existing project and commits to test web hooks - def self.build_sample(project, user) - commits = project.repository.commits(project.default_branch, nil, 3) - build(project, user, commits.last.id, commits.first.id, "refs/heads/#{project.default_branch}", commits) + def checkout_sha(repository, newrev, ref) + if newrev != Gitlab::Git::BLANK_SHA && ref.start_with?('refs/tags/') + tag_name = Gitlab::Git.extract_ref_name(ref) + tag = repository.find_tag(tag_name) + commit = repository.commit(tag.target) + commit.try(:sha) + else + newrev + end + end end end end -- cgit v1.2.1 From de27375d6cb2772b91459f5e706aed5b03b35a54 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 15 Jan 2015 11:17:47 -0800 Subject: Test git builder over annotated tag --- lib/gitlab/push_data_builder.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/push_data_builder.rb b/lib/gitlab/push_data_builder.rb index 7f5d71376f1..faea6ae375c 100644 --- a/lib/gitlab/push_data_builder.rb +++ b/lib/gitlab/push_data_builder.rb @@ -66,8 +66,11 @@ module Gitlab if newrev != Gitlab::Git::BLANK_SHA && ref.start_with?('refs/tags/') tag_name = Gitlab::Git.extract_ref_name(ref) tag = repository.find_tag(tag_name) - commit = repository.commit(tag.target) - commit.try(:sha) + + if tag + commit = repository.commit(tag.target) + commit.try(:sha) + end else newrev end -- cgit v1.2.1 From 6ac8bb0f7ccd7b1f10909aea62b1d8493fc0574a Mon Sep 17 00:00:00 2001 From: Daniel Serodio Date: Tue, 16 Dec 2014 20:03:28 -0200 Subject: Add description attribute to group API (GET and POST) --- lib/api/entities.rb | 2 +- lib/api/groups.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 2fea151aeb3..ac166ed4fba 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -65,7 +65,7 @@ module API end class Group < Grape::Entity - expose :id, :name, :path, :owner_id + expose :id, :name, :path, :owner_id, :description end class GroupDetail < Group diff --git a/lib/api/groups.rb b/lib/api/groups.rb index bda60b3b7d5..730dfad52c8 100644 --- a/lib/api/groups.rb +++ b/lib/api/groups.rb @@ -47,7 +47,7 @@ module API authenticated_as_admin! required_attributes! [:name, :path] - attrs = attributes_for_keys [:name, :path] + attrs = attributes_for_keys [:name, :path, :description] @group = Group.new(attrs) @group.owner = current_user -- cgit v1.2.1 From 39e54e21cb5dfaddaf4c83bc4509c951675091ea Mon Sep 17 00:00:00 2001 From: jubianchi Date: Sun, 18 Jan 2015 22:17:10 +0100 Subject: Handle errors on API when a project does not have a repository (Closes #6289) --- lib/api/repositories.rb | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/api/repositories.rb b/lib/api/repositories.rb index 03a556a2c55..b259914a01c 100644 --- a/lib/api/repositories.rb +++ b/lib/api/repositories.rb @@ -58,11 +58,13 @@ module API # ref_name (optional) - The name of a repository branch or tag, if not given the default branch is used # Example Request: # GET /projects/:id/repository/tree - get ":id/repository/tree" do + get ':id/repository/tree' do ref = params[:ref_name] || user_project.try(:default_branch) || 'master' path = params[:path] || nil commit = user_project.repository.commit(ref) + not_found!('Tree') unless commit + tree = user_project.repository.tree(commit.id, path) present tree.sorted_entries, with: Entities::RepoTreeObject @@ -100,14 +102,18 @@ module API # sha (required) - The blob's sha # Example Request: # GET /projects/:id/repository/raw_blobs/:sha - get ":id/repository/raw_blobs/:sha" do + get ':id/repository/raw_blobs/:sha' do ref = params[:sha] repo = user_project.repository - blob = Gitlab::Git::Blob.raw(repo, ref) + begin + blob = Gitlab::Git::Blob.raw(repo, ref) + rescue + not_found! 'Blob' + end - not_found! "Blob" unless blob + not_found! 'Blob' unless blob env['api.format'] = :txt @@ -122,13 +128,23 @@ module API # sha (optional) - the commit sha to download defaults to the tip of the default branch # Example Request: # GET /projects/:id/repository/archive - get ":id/repository/archive", requirements: { format: Gitlab::Regex.archive_formats_regex } do + get ':id/repository/archive', + requirements: { format: Gitlab::Regex.archive_formats_regex } do authorize! :download_code, user_project - file_path = ArchiveRepositoryService.new.execute(user_project, params[:sha], params[:format]) + + begin + file_path = ArchiveRepositoryService.new.execute( + user_project, + params[:sha], + params[:format]) + rescue + not_found!('File') + end if file_path && File.exists?(file_path) data = File.open(file_path, 'rb').read - header["Content-Disposition"] = "attachment; filename=\"#{File.basename(file_path)}\"" + basename = File.basename(file_path) + header['Content-Disposition'] = "attachment; filename=\"#{basename}\"" content_type MIME::Types.type_for(file_path).first.content_type env['api.format'] = :binary present data @@ -161,7 +177,12 @@ module API get ':id/repository/contributors' do authorize! :download_code, user_project - present user_project.repository.contributors, with: Entities::Contributor + begin + present user_project.repository.contributors, + with: Entities::Contributor + rescue + not_found! + end end end end -- cgit v1.2.1 From b21a2d821a4e16aba1609dfa1e01ba455e8ccd8f Mon Sep 17 00:00:00 2001 From: jubianchi Date: Wed, 17 Sep 2014 19:08:35 +0200 Subject: Allow commit messages to close several issues at once (thanks @123Haynes for his work and help) --- lib/gitlab/closing_issue_extractor.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/closing_issue_extractor.rb b/lib/gitlab/closing_issue_extractor.rb index 401e6e047b1..a9fd59f03d9 100644 --- a/lib/gitlab/closing_issue_extractor.rb +++ b/lib/gitlab/closing_issue_extractor.rb @@ -3,14 +3,19 @@ module Gitlab ISSUE_CLOSING_REGEX = Regexp.new(Gitlab.config.gitlab.issue_closing_pattern) def self.closed_by_message_in_project(message, project) - md = ISSUE_CLOSING_REGEX.match(message) - if md - extractor = Gitlab::ReferenceExtractor.new - extractor.analyze(md[0], project) - extractor.issues_for(project) - else - [] + issues = [] + + unless message.nil? + md = message.scan(ISSUE_CLOSING_REGEX) + + md.each do |ref| + extractor = Gitlab::ReferenceExtractor.new + extractor.analyze(ref[0], project) + issues += extractor.issues_for(project) + end end + + issues.uniq end end end -- cgit v1.2.1 From 6da0ab7d60ecdf189190ac095aecdf174e73a03e Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Tue, 20 Jan 2015 11:52:55 -0800 Subject: Github Importer: AJAX update status --- lib/gitlab/github/project_creator.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib') diff --git a/lib/gitlab/github/project_creator.rb b/lib/gitlab/github/project_creator.rb index 682ef389e44..7b04926071f 100644 --- a/lib/gitlab/github/project_creator.rb +++ b/lib/gitlab/github/project_creator.rb @@ -31,6 +31,8 @@ module Gitlab @project.import_start end end + + @project end end end -- cgit v1.2.1 From ab7a79bf3bb47fd1c9d82da0bb29a3cdf0246cdc Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Tue, 20 Jan 2015 15:23:37 -0800 Subject: developer can push to protected branches --- lib/api/merge_requests.rb | 8 ++------ lib/gitlab/git_access.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb index 81038d05f12..2a5b10c6f52 100644 --- a/lib/api/merge_requests.rb +++ b/lib/api/merge_requests.rb @@ -167,13 +167,9 @@ module API put ":id/merge_request/:merge_request_id/merge" do merge_request = user_project.merge_requests.find(params[:merge_request_id]) - action = if user_project.protected_branch?(merge_request.target_branch) - :push_code_to_protected_branches - else - :push_code - end + allowed = ::Gitlab::GitAccess.can_push_to_branch?(current_user, user_project, merge_request.target_branch) - if can?(current_user, action, user_project) + if allowed if merge_request.unchecked? merge_request.check_if_can_be_merged end diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index d47ef61fd11..c7bf2efc628 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -5,6 +5,15 @@ module Gitlab attr_reader :params, :project, :git_cmd, :user + def self.can_push_to_branch?(user, project, ref) + if project.protected_branch?(ref) && + !(project.developers_can_push_to_protected_branch?(ref) && project.team.developer?(user)) + user.can?(:push_code_to_protected_branches, project) + else + user.can?(:push_code, project) + end + end + def check(actor, cmd, project, changes = nil) case cmd when *DOWNLOAD_COMMANDS -- cgit v1.2.1 From 9d271538a8e9ddff892a084e5c8a881bf2fdb0b0 Mon Sep 17 00:00:00 2001 From: Justin Whear Date: Mon, 2 Jun 2014 16:10:38 -0700 Subject: Add per-milestone issues API call --- lib/api/milestones.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib') diff --git a/lib/api/milestones.rb b/lib/api/milestones.rb index 2ea49359df0..ca01fa4a57d 100644 --- a/lib/api/milestones.rb +++ b/lib/api/milestones.rb @@ -75,6 +75,21 @@ module API render_api_error!("Failed to update milestone #{milestone.errors.messages}", 400) end end + + # Get all issues for single project milestone + # + # Parameters: + # id (required) - The ID of a project + # milestone_id (required) - The ID of a project milestone + # Example Request: + # GET /projects/:id/milestones/:milestone_id/issues + get ":id/milestones/:milestone_id/issues" do + authorize! :read_milestone, user_project + + @milestone = user_project.milestones.find(params[:milestone_id]) + present paginate(@milestone.issues), with: Entities::Issue + end + end end end -- cgit v1.2.1 From e03f1af00a513a15085a69374a84a2f2df4689d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20Rosen=C3=B6gger?= <123haynes@gmail.com> Date: Wed, 21 Jan 2015 23:59:30 +0100 Subject: Fix the test and add documentation for the "per-milestone issues API call" --- lib/api/milestones.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/api/milestones.rb b/lib/api/milestones.rb index ca01fa4a57d..c5cd73943fb 100644 --- a/lib/api/milestones.rb +++ b/lib/api/milestones.rb @@ -76,7 +76,7 @@ module API end end - # Get all issues for single project milestone + # Get all issues for a single project milestone # # Parameters: # id (required) - The ID of a project -- cgit v1.2.1 From f937e059493037f3e18896a81693de81cf6a69a1 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Thu, 22 Jan 2015 15:51:20 +0100 Subject: Stop git zombie creation during force push check --- lib/gitlab/force_push_check.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/force_push_check.rb b/lib/gitlab/force_push_check.rb index 6a52cdba608..6ba2c3ad00a 100644 --- a/lib/gitlab/force_push_check.rb +++ b/lib/gitlab/force_push_check.rb @@ -4,7 +4,7 @@ module Gitlab return false if project.empty_repo? if oldrev != Gitlab::Git::BLANK_SHA && newrev != Gitlab::Git::BLANK_SHA - missed_refs = IO.popen(%W(git --git-dir=#{project.repository.path_to_repo} rev-list #{oldrev} ^#{newrev})).read + missed_refs, _ = Gitlab::Popen.popen(%W(git --git-dir=#{project.repository.path_to_repo} rev-list #{oldrev} ^#{newrev})) missed_refs.split("\n").size > 0 else false -- cgit v1.2.1 From 7dd5656a5b352dd5df5dabeeebdb21d7ffd9ef03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=20M=C3=A4enp=C3=A4=C3=A4?= Date: Wed, 15 Oct 2014 09:57:35 +0300 Subject: Implement edit via API for projects --- lib/api/projects.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'lib') diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 5b0c31f1898..d96288bb982 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -200,6 +200,49 @@ module API end end + # Update an existing project + # + # Parameters: + # id (required) - the id of a project + # name (optional) - name of a project + # path (optional) - path of a project + # description (optional) - short project description + # issues_enabled (optional) + # merge_requests_enabled (optional) + # wiki_enabled (optional) + # snippets_enabled (optional) + # public (optional) - if true same as setting visibility_level = 20 + # visibility_level (optional) - visibility level of a project + # Example Request + # PUT /projects/:id + put ':id' do + attrs = attributes_for_keys [:name, + :path, + :description, + :default_branch, + :issues_enabled, + :merge_requests_enabled, + :wiki_enabled, + :snippets_enabled, + :public, + :visibility_level] + attrs = map_public_to_visibility_level(attrs) + authorize_admin_project + authorize! :rename_project, user_project if attrs[:name].present? + if attrs[:visibility_level].present? + authorize! :change_visibility_level, user_project + end + + ::Projects::UpdateService.new(user_project, + current_user, attrs).execute + + if user_project.valid? + present user_project, with: Entities::Project + else + render_validation_error!(user_project) + end + end + # Remove project # # Parameters: -- cgit v1.2.1 From 103a1bb06d00c0b3ee1f0148ee8fc809f4f276f8 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Fri, 23 Jan 2015 10:28:38 -0800 Subject: Use service settings instead of config file settings to present issues. --- lib/gitlab/markdown.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb index 068c342398b..5987ee8da99 100644 --- a/lib/gitlab/markdown.rb +++ b/lib/gitlab/markdown.rb @@ -208,7 +208,7 @@ module Gitlab end def reference_issue(identifier, project = @project, prefix_text = nil) - if project.used_default_issues_tracker? || !external_issues_tracker_enabled? + if project.used_default_issues_tracker? || !project.external_issues_tracker_enabled? if project.issue_exists? identifier url = url_for_issue(identifier, project) title = title_for_issue(identifier, project) @@ -220,8 +220,7 @@ module Gitlab link_to("#{prefix_text}##{identifier}", url, options) end else - config = Gitlab.config - external_issue_tracker = config.issues_tracker[project.issues_tracker] + external_issue_tracker = project.external_issue_tracker if external_issue_tracker.present? reference_external_issue(identifier, external_issue_tracker, project, prefix_text) @@ -270,7 +269,7 @@ module Gitlab def reference_external_issue(identifier, issue_tracker, project = @project, prefix_text = nil) url = url_for_issue(identifier, project) - title = issue_tracker['title'] + title = issue_tracker.title options = html_options.merge( title: "Issue in #{title}", -- cgit v1.2.1 From 7c701acf573f95cce7f8b1b7756de5d73404f09b Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Fri, 23 Jan 2015 11:01:09 -0800 Subject: Do a check which issue tracker is used inside the project. --- lib/gitlab/markdown.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb index 5987ee8da99..6ba7a0c18fb 100644 --- a/lib/gitlab/markdown.rb +++ b/lib/gitlab/markdown.rb @@ -208,7 +208,7 @@ module Gitlab end def reference_issue(identifier, project = @project, prefix_text = nil) - if project.used_default_issues_tracker? || !project.external_issues_tracker_enabled? + if project.using_issue_tracker? if project.issue_exists? identifier url = url_for_issue(identifier, project) title = title_for_issue(identifier, project) -- cgit v1.2.1 From aad6ceaef9ccfba8e058012a0877b80c103a3838 Mon Sep 17 00:00:00 2001 From: Marco Wessel Date: Sun, 25 Jan 2015 16:33:54 +0100 Subject: Allow configuring protection of the default branch upon first push --- lib/gitlab/access.rb | 16 ++++++++++++++++ lib/gitlab/current_settings.rb | 1 + 2 files changed, 17 insertions(+) (limited to 'lib') diff --git a/lib/gitlab/access.rb b/lib/gitlab/access.rb index 411b2b9a3cc..ad05bfadafe 100644 --- a/lib/gitlab/access.rb +++ b/lib/gitlab/access.rb @@ -11,6 +11,11 @@ module Gitlab MASTER = 40 OWNER = 50 + # Branch protection settings + PROTECTION_NONE = 0 + PROTECTION_DEV_CAN_PUSH = 1 + PROTECTION_FULL = 2 + class << self def values options.values @@ -43,6 +48,17 @@ module Gitlab master: MASTER, } end + + def protection_options + { + "None" => PROTECTION_NONE, + "Protect, developers can push" => PROTECTION_DEV_CAN_PUSH, + "Full protection" => PROTECTION_FULL, + } + end + def protection_values + protection_options.values + end end def human_access diff --git a/lib/gitlab/current_settings.rb b/lib/gitlab/current_settings.rb index 2c5660df373..75afc024a62 100644 --- a/lib/gitlab/current_settings.rb +++ b/lib/gitlab/current_settings.rb @@ -12,6 +12,7 @@ module Gitlab def fake_application_settings OpenStruct.new( default_projects_limit: Settings.gitlab['default_projects_limit'], + default_branch_protection: Settings.gitlab['default_branch_protection'], signup_enabled: Settings.gitlab['signup_enabled'], signin_enabled: Settings.gitlab['signin_enabled'], gravatar_enabled: Settings.gravatar['enabled'], -- cgit v1.2.1 From b20fc14133957d0e71bcf48aed4b426d439681db Mon Sep 17 00:00:00 2001 From: Marco Wessel Date: Sun, 25 Jan 2015 18:34:02 +0100 Subject: Fix indentation --- lib/gitlab/current_settings.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/current_settings.rb b/lib/gitlab/current_settings.rb index 75afc024a62..90f0d648f98 100644 --- a/lib/gitlab/current_settings.rb +++ b/lib/gitlab/current_settings.rb @@ -12,7 +12,7 @@ module Gitlab def fake_application_settings OpenStruct.new( default_projects_limit: Settings.gitlab['default_projects_limit'], - default_branch_protection: Settings.gitlab['default_branch_protection'], + default_branch_protection: Settings.gitlab['default_branch_protection'], signup_enabled: Settings.gitlab['signup_enabled'], signin_enabled: Settings.gitlab['signin_enabled'], gravatar_enabled: Settings.gravatar['enabled'], -- cgit v1.2.1 From afce47923cf184bd11a6122c25fb57fe98f73148 Mon Sep 17 00:00:00 2001 From: Marco Wessel Date: Sun, 25 Jan 2015 18:38:35 +0100 Subject: actually fix indentation --- lib/gitlab/current_settings.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/current_settings.rb b/lib/gitlab/current_settings.rb index 90f0d648f98..93e7edf508c 100644 --- a/lib/gitlab/current_settings.rb +++ b/lib/gitlab/current_settings.rb @@ -12,7 +12,7 @@ module Gitlab def fake_application_settings OpenStruct.new( default_projects_limit: Settings.gitlab['default_projects_limit'], - default_branch_protection: Settings.gitlab['default_branch_protection'], + default_branch_protection: Settings.gitlab['default_branch_protection'], signup_enabled: Settings.gitlab['signup_enabled'], signin_enabled: Settings.gitlab['signin_enabled'], gravatar_enabled: Settings.gravatar['enabled'], -- cgit v1.2.1 From 65e700472b471242475eb9d9e3a340c6ce24615a Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Mon, 26 Jan 2015 11:39:32 -0800 Subject: Update the issue tracker attribute on issue tracker change. --- lib/gitlab/markdown.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb index 6ba7a0c18fb..2f041336471 100644 --- a/lib/gitlab/markdown.rb +++ b/lib/gitlab/markdown.rb @@ -208,7 +208,7 @@ module Gitlab end def reference_issue(identifier, project = @project, prefix_text = nil) - if project.using_issue_tracker? + if project.default_issues_tracker? if project.issue_exists? identifier url = url_for_issue(identifier, project) title = title_for_issue(identifier, project) -- cgit v1.2.1 From 3e47ea5064f7e93cadb0ef347dfa27517552a4a0 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 26 Jan 2015 13:31:20 -0800 Subject: Files::CreateService can now commit file to empty repository --- lib/gitlab/satellite/files/new_file_action.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/satellite/files/new_file_action.rb b/lib/gitlab/satellite/files/new_file_action.rb index 15e9b7a6f77..c230239d390 100644 --- a/lib/gitlab/satellite/files/new_file_action.rb +++ b/lib/gitlab/satellite/files/new_file_action.rb @@ -14,7 +14,14 @@ module Gitlab prepare_satellite!(repo) # create target branch in satellite at the corresponding commit from bare repo - repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") + current_ref = + if repo.commits.any? + repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") + ref + else + # skip this step if we want to add first file to empty repo + Satellite::PARKING_BRANCH + end file_path_in_satellite = File.join(repo.working_dir, file_path) dir_name_in_satellite = File.dirname(file_path_in_satellite) @@ -38,10 +45,9 @@ module Gitlab # will raise CommandFailed when commit fails repo.git.commit(raise: true, timeout: true, a: true, m: commit_message) - # push commit back to bare repo # will raise CommandFailed when push fails - repo.git.push({raise: true, timeout: true}, :origin, ref) + repo.git.push({raise: true, timeout: true}, :origin, "#{current_ref}:#{ref}") # everything worked true -- cgit v1.2.1 From c916124178645412a554a6b8b39c05bbd42269c8 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 26 Jan 2015 15:01:51 -0800 Subject: Explicitly set before_filter for ref-related controllers --- lib/extracts_path.rb | 8 -------- 1 file changed, 8 deletions(-) (limited to 'lib') diff --git a/lib/extracts_path.rb b/lib/extracts_path.rb index e51cb30bdd9..19215cfb7e6 100644 --- a/lib/extracts_path.rb +++ b/lib/extracts_path.rb @@ -1,17 +1,9 @@ # Module providing methods for dealing with separating a tree-ish string and a # file path string when combined in a request parameter module ExtractsPath - extend ActiveSupport::Concern - # Raised when given an invalid file path class InvalidPathError < StandardError; end - included do - if respond_to?(:before_filter) - before_filter :assign_ref_vars - end - end - # Given a string containing both a Git tree-ish, such as a branch or tag, and # a filesystem path joined by forward slashes, attempts to separate the two. # -- cgit v1.2.1 From 00a0d5aeeaf19ea4d72fd1890afac099026f1706 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Mon, 26 Jan 2015 16:24:11 -0800 Subject: Move repetition to the parent. --- lib/gitlab/markdown.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb index 2f041336471..c0e83fb3078 100644 --- a/lib/gitlab/markdown.rb +++ b/lib/gitlab/markdown.rb @@ -220,9 +220,8 @@ module Gitlab link_to("#{prefix_text}##{identifier}", url, options) end else - external_issue_tracker = project.external_issue_tracker - if external_issue_tracker.present? - reference_external_issue(identifier, external_issue_tracker, project, + if project.external_issue_tracker.present? + reference_external_issue(identifier, project, prefix_text) end end @@ -266,10 +265,10 @@ module Gitlab end end - def reference_external_issue(identifier, issue_tracker, project = @project, + def reference_external_issue(identifier, project = @project, prefix_text = nil) url = url_for_issue(identifier, project) - title = issue_tracker.title + title = project.external_issue_tracker.title options = html_options.merge( title: "Issue in #{title}", -- cgit v1.2.1 From a3d879d427c1236d26832dcd0312b3e0d6158bbe Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 26 Jan 2015 20:57:42 -0800 Subject: Refactor web editor --- lib/gitlab/satellite/files/new_file_action.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/satellite/files/new_file_action.rb b/lib/gitlab/satellite/files/new_file_action.rb index c230239d390..5b657c7aba2 100644 --- a/lib/gitlab/satellite/files/new_file_action.rb +++ b/lib/gitlab/satellite/files/new_file_action.rb @@ -15,12 +15,12 @@ module Gitlab # create target branch in satellite at the corresponding commit from bare repo current_ref = - if repo.commits.any? - repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") - ref - else + if @project.empty_repo? # skip this step if we want to add first file to empty repo Satellite::PARKING_BRANCH + else + repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") + ref end file_path_in_satellite = File.join(repo.working_dir, file_path) -- cgit v1.2.1 From 54f6d8c7b5a1c67a222011c35ad70909da0e686d Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Tue, 27 Jan 2015 15:37:19 -0800 Subject: an ability to clone project with oauth2 token --- lib/gitlab/backend/grack_auth.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/backend/grack_auth.rb b/lib/gitlab/backend/grack_auth.rb index 1f71906bc8e..2e393f753e8 100644 --- a/lib/gitlab/backend/grack_auth.rb +++ b/lib/gitlab/backend/grack_auth.rb @@ -34,7 +34,7 @@ module Grack def auth! if @auth.provided? return bad_request unless @auth.basic? - + # Authentication with username and password login, password = @auth.credentials @@ -71,8 +71,20 @@ module Grack false end + def oauth_access_token_check(login, password) + if login == "oauth2" && git_cmd == 'git-upload-pack' && password.present? + token = Doorkeeper::AccessToken.by_token(password) + token && token.accessible? && User.find_by(id: token.resource_owner_id) + end + end + def authenticate_user(login, password) user = Gitlab::Auth.new.find(login, password) + + unless user + user = oauth_access_token_check(login, password) + end + return user if user.present? # At this point, we know the credentials were wrong. We let Rack::Attack -- cgit v1.2.1 From 792ced2f4190226c3335967a8e5a30d3b72bd4ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20Rosen=C3=B6gger?= <123haynes@gmail.com> Date: Wed, 28 Jan 2015 22:18:22 +0100 Subject: Add a commit calendar to the user profile --- lib/gitlab/commits_calendar.rb | 79 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 lib/gitlab/commits_calendar.rb (limited to 'lib') diff --git a/lib/gitlab/commits_calendar.rb b/lib/gitlab/commits_calendar.rb new file mode 100644 index 00000000000..a862e67a598 --- /dev/null +++ b/lib/gitlab/commits_calendar.rb @@ -0,0 +1,79 @@ +module Gitlab + class CommitsCalendar + def self.create_timestamp(repositories, user, show_activity) + timestamps = {} + repositories.each do |raw_repository| + if raw_repository.exists? + commits_log = raw_repository.commits_log_of_user_by_date(user) + + populated_timestamps = + if show_activity + populate_timestamps_by_project( + commits_log, + timestamps, + raw_repository + ) + else + populate_timestamps(commits_log, timestamps) + end + timestamps.merge!(populated_timestamps) + end + end + timestamps + end + + def self.populate_timestamps(commits_log, timestamps) + commits_log.each do |timestamp_date, commits_count| + hash = { "#{timestamp_date}" => commits_count } + if timestamps.has_key?("#{timestamp_date}") + timestamps.merge!(hash) do |timestamp_date, commits_count, + new_commits_count| commits_count = commits_count.to_i + + new_commits_count + end + else + timestamps.merge!(hash) + end + end + timestamps + end + + def self.populate_timestamps_by_project(commits_log, timestamps, + project) + commits_log.each do |timestamp_date, commits_count| + if timestamps.has_key?("#{timestamp_date}") + timestamps["#{timestamp_date}"]. + merge!(project.path_with_namespace => commits_count) + else + hash = { "#{timestamp_date}" => { project.path_with_namespace => + commits_count } } + timestamps.merge!(hash) + end + end + timestamps + end + + def self.latest_commit_date(timestamps) + if timestamps.nil? || timestamps.empty? + DateTime.now.to_date + else + Time.at(timestamps.keys.first.to_i).to_date + end + end + + def self.starting_year(timestamps) + DateTime.now.to_date - 1 + end + + def self.starting_month(timestamps) + Date.today.strftime("%m").to_i + end + + def self.last_commit_date(timestamps) + latest_commit_date(timestamps).to_formatted_s(:long).to_s + end + + def self.commit_activity_match(user_activities, date) + user_activities.select { |x| Time.at(x.to_i) == Time.parse(date) } + end + end +end -- cgit v1.2.1 From 953c1fff8f242f09f3f16998112931d48d6a5ecc Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Wed, 28 Jan 2015 17:00:40 -0800 Subject: Be more careful with parsing changes from gitlab-shell --- lib/gitlab/git_access.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index c7bf2efc628..ea96d04c5ab 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -73,7 +73,7 @@ module Gitlab changes = changes.lines if changes.kind_of?(String) # Iterate over all changes to find if user allowed all of them to be applied - changes.each do |change| + changes.map(&:strip).reject(&:blank?).each do |change| status = change_access_check(user, project, change) unless status.allowed? # If user does not have access to make at least one change - cancel all push -- cgit v1.2.1 From c39f80bdb412bc9cc7646de0929efe8cb5b870d4 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Wed, 28 Jan 2015 23:00:41 -0800 Subject: Refactor commit calendar a bit. Fixed dates --- lib/gitlab/commits_calendar.rb | 8 -------- 1 file changed, 8 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/commits_calendar.rb b/lib/gitlab/commits_calendar.rb index a862e67a598..93256187fd2 100644 --- a/lib/gitlab/commits_calendar.rb +++ b/lib/gitlab/commits_calendar.rb @@ -60,14 +60,6 @@ module Gitlab end end - def self.starting_year(timestamps) - DateTime.now.to_date - 1 - end - - def self.starting_month(timestamps) - Date.today.strftime("%m").to_i - end - def self.last_commit_date(timestamps) latest_commit_date(timestamps).to_formatted_s(:long).to_s end -- cgit v1.2.1 From a9288e554e55e843b95ab6f8109a4c610af64c83 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 29 Jan 2015 00:53:43 -0800 Subject: Cleanup and make contribution calendar faster --- lib/gitlab/commits_calendar.rb | 72 ++++++++---------------------------------- 1 file changed, 13 insertions(+), 59 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/commits_calendar.rb b/lib/gitlab/commits_calendar.rb index 93256187fd2..b6699c585f6 100644 --- a/lib/gitlab/commits_calendar.rb +++ b/lib/gitlab/commits_calendar.rb @@ -1,71 +1,25 @@ module Gitlab class CommitsCalendar - def self.create_timestamp(repositories, user, show_activity) - timestamps = {} - repositories.each do |raw_repository| - if raw_repository.exists? - commits_log = raw_repository.commits_log_of_user_by_date(user) + attr_reader :timestamps - populated_timestamps = - if show_activity - populate_timestamps_by_project( - commits_log, - timestamps, - raw_repository - ) - else - populate_timestamps(commits_log, timestamps) - end - timestamps.merge!(populated_timestamps) - end - end - timestamps - end + def initialize(repositories, user) + @timestamps = {} + date_timestamps = [] - def self.populate_timestamps(commits_log, timestamps) - commits_log.each do |timestamp_date, commits_count| - hash = { "#{timestamp_date}" => commits_count } - if timestamps.has_key?("#{timestamp_date}") - timestamps.merge!(hash) do |timestamp_date, commits_count, - new_commits_count| commits_count = commits_count.to_i + - new_commits_count - end - else - timestamps.merge!(hash) - end + repositories.select(&:exists?).reject(&:empty?).each do |raw_repository| + commits_log = raw_repository.commits_per_day_for_user(user) + date_timestamps << commits_log end - timestamps - end - def self.populate_timestamps_by_project(commits_log, timestamps, - project) - commits_log.each do |timestamp_date, commits_count| - if timestamps.has_key?("#{timestamp_date}") - timestamps["#{timestamp_date}"]. - merge!(project.path_with_namespace => commits_count) - else - hash = { "#{timestamp_date}" => { project.path_with_namespace => - commits_count } } - timestamps.merge!(hash) - end + date_timestamps = date_timestamps.inject do |collection, date| + collection.merge(date) { |k, old_v, new_v| old_v + new_v } end - timestamps - end - def self.latest_commit_date(timestamps) - if timestamps.nil? || timestamps.empty? - DateTime.now.to_date - else - Time.at(timestamps.keys.first.to_i).to_date + date_timestamps ||= [] + date_timestamps.each do |date, commits| + timestamp = Date.parse(date).to_time.to_i.to_s + @timestamps[timestamp] = commits end end - - def self.last_commit_date(timestamps) - latest_commit_date(timestamps).to_formatted_s(:long).to_s - end - - def self.commit_activity_match(user_activities, date) - user_activities.select { |x| Time.at(x.to_i) == Time.parse(date) } - end end end -- cgit v1.2.1 From c9f18d4587f16eb16b8b902d69576520c08b7f5a Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 29 Jan 2015 01:03:20 -0800 Subject: Make sure we dont have exception on date parsing --- lib/gitlab/commits_calendar.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/commits_calendar.rb b/lib/gitlab/commits_calendar.rb index b6699c585f6..ccc80d080af 100644 --- a/lib/gitlab/commits_calendar.rb +++ b/lib/gitlab/commits_calendar.rb @@ -17,8 +17,8 @@ module Gitlab date_timestamps ||= [] date_timestamps.each do |date, commits| - timestamp = Date.parse(date).to_time.to_i.to_s - @timestamps[timestamp] = commits + timestamp = Date.parse(date).to_time.to_i.to_s rescue nil + @timestamps[timestamp] = commits if timestamp end end end -- cgit v1.2.1 From 4eafc188437e0214c09d59083586ea871b625b14 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Wed, 28 Jan 2015 23:08:28 -0500 Subject: Refactor Repository to use new RepositoryCache class Abstracts away the lower-level implementation details from the Repository model. --- lib/repository_cache.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 lib/repository_cache.rb (limited to 'lib') diff --git a/lib/repository_cache.rb b/lib/repository_cache.rb new file mode 100644 index 00000000000..0d52f50be91 --- /dev/null +++ b/lib/repository_cache.rb @@ -0,0 +1,25 @@ +# Interface to the Redis-backed cache store used by the Repository model +class RepositoryCache + attr_reader :namespace + + def initialize(namespace, backend = Rails.cache) + @namespace = namespace + @backend = backend + end + + def cache_key(type) + "#{type}:#{namespace}" + end + + def expire(key) + backend.delete(cache_key(key)) + end + + def fetch(key, &block) + backend.fetch(cache_key(key), &block) + end + + private + + attr_reader :backend +end -- cgit v1.2.1 From ca701a964971a3291270e60669757c9853e3cf66 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 29 Jan 2015 13:28:41 -0800 Subject: Improvements to LDAP::User model * method #changed? also tracks changes of identites (fixes issue with email mapping) * find ldap identity before initialize one --- lib/gitlab/ldap/user.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ldap/user.rb b/lib/gitlab/ldap/user.rb index 3ef494ba137..cfa8692659d 100644 --- a/lib/gitlab/ldap/user.rb +++ b/lib/gitlab/ldap/user.rb @@ -40,12 +40,16 @@ module Gitlab def update_user_attributes gl_user.email = auth_hash.email - gl_user.identities.build(provider: auth_hash.provider, extern_uid: auth_hash.uid) + + # Build new identity only if we dont have have same one + gl_user.identities.find_or_initialize_by(provider: auth_hash.provider, + extern_uid: auth_hash.uid) + gl_user end def changed? - gl_user.changed? + gl_user.changed? || gl_user.identities.any?(&:changed?) end def needs_blocking? -- cgit v1.2.1 From f1cf49218fb40b61f82ff74dbb7eaba32b439a5a Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 29 Jan 2015 17:07:44 -0800 Subject: Improve contribution calendar on user page * cache user contributions for day * ignore forks in calendar contribtuions --- lib/gitlab/commits_calendar.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/commits_calendar.rb b/lib/gitlab/commits_calendar.rb index ccc80d080af..2f30d238e6b 100644 --- a/lib/gitlab/commits_calendar.rb +++ b/lib/gitlab/commits_calendar.rb @@ -2,15 +2,15 @@ module Gitlab class CommitsCalendar attr_reader :timestamps - def initialize(repositories, user) + def initialize(projects, user) @timestamps = {} date_timestamps = [] - repositories.select(&:exists?).reject(&:empty?).each do |raw_repository| - commits_log = raw_repository.commits_per_day_for_user(user) - date_timestamps << commits_log + projects.reject(&:forked?).each do |project| + date_timestamps << ProjectContributions.new(project, user).commits_log end + # Sumarrize commits from all projects per days date_timestamps = date_timestamps.inject do |collection, date| collection.merge(date) { |k, old_v, new_v| old_v + new_v } end @@ -21,5 +21,13 @@ module Gitlab @timestamps[timestamp] = commits if timestamp end end + + def starting_year + (Time.now - 1.year).strftime("%Y") + end + + def starting_month + Date.today.strftime("%m").to_i + end end end -- cgit v1.2.1 From 23c31a0b12eaae7f7f25f30eca1066968bcb6059 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 29 Jan 2015 23:06:43 -0800 Subject: Skip tricky test for semaphore --- lib/tasks/spinach.rake | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/tasks/spinach.rake b/lib/tasks/spinach.rake index 507b315759d..ac885f315b9 100644 --- a/lib/tasks/spinach.rake +++ b/lib/tasks/spinach.rake @@ -2,9 +2,15 @@ Rake::Task["spinach"].clear if Rake::Task.task_defined?('spinach') desc "GITLAB | Run spinach" task :spinach do + tags = if ENV['SEMAPHORE'] + '~@tricky,~@wip' + else + '~@wip' + end + cmds = [ %W(rake gitlab:setup), - %W(spinach), + %W(spinach --tags #{tags}), ] run_commands(cmds) end -- cgit v1.2.1 From b21565f18d297c167f05f4c7861e01c916c15682 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 29 Jan 2015 23:28:38 -0800 Subject: Fix semaphore spinach tags --- lib/tasks/spinach.rake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/tasks/spinach.rake b/lib/tasks/spinach.rake index ac885f315b9..4aefc18ce14 100644 --- a/lib/tasks/spinach.rake +++ b/lib/tasks/spinach.rake @@ -3,9 +3,9 @@ Rake::Task["spinach"].clear if Rake::Task.task_defined?('spinach') desc "GITLAB | Run spinach" task :spinach do tags = if ENV['SEMAPHORE'] - '~@tricky,~@wip' + '~@tricky' else - '~@wip' + '~@semaphore' end cmds = [ -- cgit v1.2.1 From 8ac227bab2334c41784c567faf7aafb1ebd75890 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Fri, 30 Jan 2015 10:17:55 -0500 Subject: Fix RepositoryCache backend attr_reader --- lib/repository_cache.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/repository_cache.rb b/lib/repository_cache.rb index 0d52f50be91..fa016a170cd 100644 --- a/lib/repository_cache.rb +++ b/lib/repository_cache.rb @@ -1,6 +1,6 @@ # Interface to the Redis-backed cache store used by the Repository model class RepositoryCache - attr_reader :namespace + attr_reader :namespace, :backend def initialize(namespace, backend = Rails.cache) @namespace = namespace @@ -18,8 +18,4 @@ class RepositoryCache def fetch(key, &block) backend.fetch(cache_key(key), &block) end - - private - - attr_reader :backend end -- cgit v1.2.1 From 4f1d1fc51baf396d49f6b159c84e15194706847c Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 2 Feb 2015 19:30:09 -0800 Subject: Convert hashes to ruby 1.9 style --- lib/api/api_guard.rb | 4 ++-- lib/api/entities.rb | 2 +- lib/gitlab/github/importer.rb | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/api/api_guard.rb b/lib/api/api_guard.rb index 23975518181..28765b142f6 100644 --- a/lib/api/api_guard.rb +++ b/lib/api/api_guard.rb @@ -146,7 +146,7 @@ module APIGuard Rack::OAuth2::Server::Resource::Bearer::Forbidden.new( :insufficient_scope, Rack::OAuth2::Server::Resource::ErrorMethods::DEFAULT_DESCRIPTION[:insufficient_scope], - { :scope => e.scopes}) + { scope: e.scopes}) end response.finish @@ -172,4 +172,4 @@ module APIGuard @scopes = scopes end end -end \ No newline at end of file +end diff --git a/lib/api/entities.rb b/lib/api/entities.rb index ac166ed4fba..58339908fd2 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -55,7 +55,7 @@ module API expose :path, :path_with_namespace expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :snippets_enabled, :created_at, :last_activity_at expose :namespace - expose :forked_from_project, using: Entities::ForkedFromProject, :if => lambda{ | project, options | project.forked? } + expose :forked_from_project, using: Entities::ForkedFromProject, if: lambda{ | project, options | project.forked? } end class ProjectMember < UserBasic diff --git a/lib/gitlab/github/importer.rb b/lib/gitlab/github/importer.rb index c72a1c25e9e..9f0fc6c4471 100644 --- a/lib/gitlab/github/importer.rb +++ b/lib/gitlab/github/importer.rb @@ -9,12 +9,12 @@ module Gitlab def execute client = octo_client(project.creator.github_access_token) - + #Issues && Comments client.list_issues(project.import_source, state: :all).each do |issue| if issue.pull_request.nil? body = "*Created by: #{issue.user.login}*\n\n#{issue.body}" - + if issue.comments > 0 body += "\n\n\n**Imported comments:**\n" client.issue_comments(project.import_source, issue.number).each do |c| @@ -23,7 +23,7 @@ module Gitlab end project.issues.create!( - description: body, + description: body, title: issue.title, state: issue.state == 'closed' ? 'closed' : 'opened', author_id: gl_user_id(project, issue.user.id) @@ -36,7 +36,7 @@ module Gitlab def octo_client(access_token) ::Octokit.auto_paginate = true - ::Octokit::Client.new(:access_token => access_token) + ::Octokit::Client.new(access_token: access_token) end def gl_user_id(project, github_id) -- cgit v1.2.1 From 84a5a548a5e1377f34b7989fc546eaedf86c3510 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 2 Feb 2015 20:08:23 -0800 Subject: Add rubocop to rake test and rake test_ci --- lib/tasks/gitlab/test.rake | 1 + lib/tasks/rubocop.rake | 2 ++ lib/tasks/test.rake | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 lib/tasks/rubocop.rake (limited to 'lib') diff --git a/lib/tasks/gitlab/test.rake b/lib/tasks/gitlab/test.rake index c01b00bd1c0..b4076f8238f 100644 --- a/lib/tasks/gitlab/test.rake +++ b/lib/tasks/gitlab/test.rake @@ -2,6 +2,7 @@ namespace :gitlab do desc "GITLAB | Run all tests" task :test do cmds = [ + %W(rake rubocop), %W(rake spinach), %W(rake spec), %W(rake jasmine:ci) diff --git a/lib/tasks/rubocop.rake b/lib/tasks/rubocop.rake new file mode 100644 index 00000000000..c28e529f86d --- /dev/null +++ b/lib/tasks/rubocop.rake @@ -0,0 +1,2 @@ +require 'rubocop/rake_task' +RuboCop::RakeTask.new diff --git a/lib/tasks/test.rake b/lib/tasks/test.rake index 583f4a876da..3ea9290a814 100644 --- a/lib/tasks/test.rake +++ b/lib/tasks/test.rake @@ -9,5 +9,5 @@ unless Rails.env.production? require 'coveralls/rake/task' Coveralls::RakeTask.new desc "GITLAB | Run all tests on CI with simplecov" - task :test_ci => [:spinach, :spec, 'coveralls:push'] + task :test_ci => [:rubocop, :spinach, :spec, 'coveralls:push'] end -- cgit v1.2.1 From e89058268118e3b2be4ebaf5d7bf2c684b590437 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 2 Feb 2015 20:36:54 -0800 Subject: Rubocop enabled for: Use spaces inside hash literal braces --- lib/api/api.rb | 4 ++-- lib/api/api_guard.rb | 2 +- lib/api/helpers.rb | 2 +- lib/api/project_members.rb | 2 +- lib/gitlab/backend/grack_auth.rb | 8 ++++---- lib/gitlab/git_access_status.rb | 4 ++-- lib/gitlab/satellite/action.rb | 2 +- lib/gitlab/satellite/files/delete_file_action.rb | 4 ++-- lib/gitlab/satellite/files/edit_file_action.rb | 4 ++-- lib/gitlab/satellite/files/new_file_action.rb | 4 ++-- lib/gitlab/satellite/merge_action.rb | 6 +++--- lib/gitlab/satellite/satellite.rb | 6 +++--- lib/gitlab/upgrader.rb | 2 +- 13 files changed, 25 insertions(+), 25 deletions(-) (limited to 'lib') diff --git a/lib/api/api.rb b/lib/api/api.rb index cb46f477ff9..60858a39407 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -6,7 +6,7 @@ module API version 'v3', using: :path rescue_from ActiveRecord::RecordNotFound do - rack_response({'message' => '404 Not found'}.to_json, 404) + rack_response({ 'message' => '404 Not found' }.to_json, 404) end rescue_from :all do |exception| @@ -19,7 +19,7 @@ module API message << " " << trace.join("\n ") API.logger.add Logger::FATAL, message - rack_response({'message' => '500 Internal Server Error'}, 500) + rack_response({ 'message' => '500 Internal Server Error' }, 500) end format :json diff --git a/lib/api/api_guard.rb b/lib/api/api_guard.rb index 28765b142f6..be3d053efca 100644 --- a/lib/api/api_guard.rb +++ b/lib/api/api_guard.rb @@ -146,7 +146,7 @@ module APIGuard Rack::OAuth2::Server::Resource::Bearer::Forbidden.new( :insufficient_scope, Rack::OAuth2::Server::Resource::ErrorMethods::DEFAULT_DESCRIPTION[:insufficient_scope], - { scope: e.scopes}) + { scope: e.scopes }) end response.finish diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 62c26ef76ce..1ded63d136f 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -173,7 +173,7 @@ module API end def render_api_error!(message, status) - error!({'message' => message}, status) + error!({ 'message' => message }, status) end private diff --git a/lib/api/project_members.rb b/lib/api/project_members.rb index 8e32f124ea5..1e890f9e199 100644 --- a/lib/api/project_members.rb +++ b/lib/api/project_members.rb @@ -106,7 +106,7 @@ module API unless team_member.nil? team_member.destroy else - {message: "Access revoked", id: params[:user_id].to_i} + { message: "Access revoked", id: params[:user_id].to_i } end end end diff --git a/lib/gitlab/backend/grack_auth.rb b/lib/gitlab/backend/grack_auth.rb index 2e393f753e8..3f207c56631 100644 --- a/lib/gitlab/backend/grack_auth.rb +++ b/lib/gitlab/backend/grack_auth.rb @@ -34,7 +34,7 @@ module Grack def auth! if @auth.provided? return bad_request unless @auth.basic? - + # Authentication with username and password login, password = @auth.credentials @@ -80,11 +80,11 @@ module Grack def authenticate_user(login, password) user = Gitlab::Auth.new.find(login, password) - + unless user user = oauth_access_token_check(login, password) end - + return user if user.present? # At this point, we know the credentials were wrong. We let Rack::Attack @@ -154,7 +154,7 @@ module Grack end def render_not_found - [404, {"Content-Type" => "text/plain"}, ["Not Found"]] + [404, { "Content-Type" => "text/plain" }, ["Not Found"]] end end end diff --git a/lib/gitlab/git_access_status.rb b/lib/gitlab/git_access_status.rb index 3d451ecebee..5a806ff6e0d 100644 --- a/lib/gitlab/git_access_status.rb +++ b/lib/gitlab/git_access_status.rb @@ -9,7 +9,7 @@ module Gitlab end def to_json - {status: @status, message: @message}.to_json + { status: @status, message: @message }.to_json end end -end \ No newline at end of file +end diff --git a/lib/gitlab/satellite/action.rb b/lib/gitlab/satellite/action.rb index be45cb5c98e..4890ccf21e6 100644 --- a/lib/gitlab/satellite/action.rb +++ b/lib/gitlab/satellite/action.rb @@ -44,7 +44,7 @@ module Gitlab end def default_options(options = {}) - {raise: true, timeout: true}.merge(options) + { raise: true, timeout: true }.merge(options) end def handle_exception(exception) diff --git a/lib/gitlab/satellite/files/delete_file_action.rb b/lib/gitlab/satellite/files/delete_file_action.rb index 30462999aa3..0d37b9dea85 100644 --- a/lib/gitlab/satellite/files/delete_file_action.rb +++ b/lib/gitlab/satellite/files/delete_file_action.rb @@ -13,7 +13,7 @@ module Gitlab prepare_satellite!(repo) # create target branch in satellite at the corresponding commit from bare repo - repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") + repo.git.checkout({ raise: true, timeout: true, b: true }, ref, "origin/#{ref}") # update the file in the satellite's working dir file_path_in_satellite = File.join(repo.working_dir, file_path) @@ -36,7 +36,7 @@ module Gitlab # push commit back to bare repo # will raise CommandFailed when push fails - repo.git.push({raise: true, timeout: true}, :origin, ref) + repo.git.push({ raise: true, timeout: true }, :origin, ref) # everything worked true diff --git a/lib/gitlab/satellite/files/edit_file_action.rb b/lib/gitlab/satellite/files/edit_file_action.rb index cbdf70f7d12..2834b722b27 100644 --- a/lib/gitlab/satellite/files/edit_file_action.rb +++ b/lib/gitlab/satellite/files/edit_file_action.rb @@ -15,7 +15,7 @@ module Gitlab prepare_satellite!(repo) # create target branch in satellite at the corresponding commit from bare repo - repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") + repo.git.checkout({ raise: true, timeout: true, b: true }, ref, "origin/#{ref}") # update the file in the satellite's working dir file_path_in_satellite = File.join(repo.working_dir, file_path) @@ -36,7 +36,7 @@ module Gitlab # push commit back to bare repo # will raise CommandFailed when push fails - repo.git.push({raise: true, timeout: true}, :origin, ref) + repo.git.push({ raise: true, timeout: true }, :origin, ref) # everything worked true diff --git a/lib/gitlab/satellite/files/new_file_action.rb b/lib/gitlab/satellite/files/new_file_action.rb index 5b657c7aba2..69f7ffa94e4 100644 --- a/lib/gitlab/satellite/files/new_file_action.rb +++ b/lib/gitlab/satellite/files/new_file_action.rb @@ -19,7 +19,7 @@ module Gitlab # skip this step if we want to add first file to empty repo Satellite::PARKING_BRANCH else - repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") + repo.git.checkout({ raise: true, timeout: true, b: true }, ref, "origin/#{ref}") ref end @@ -47,7 +47,7 @@ module Gitlab # push commit back to bare repo # will raise CommandFailed when push fails - repo.git.push({raise: true, timeout: true}, :origin, "#{current_ref}:#{ref}") + repo.git.push({ raise: true, timeout: true }, :origin, "#{current_ref}:#{ref}") # everything worked true diff --git a/lib/gitlab/satellite/merge_action.rb b/lib/gitlab/satellite/merge_action.rb index e9141f735aa..25122666f5e 100644 --- a/lib/gitlab/satellite/merge_action.rb +++ b/lib/gitlab/satellite/merge_action.rb @@ -86,7 +86,7 @@ module Gitlab in_locked_and_timed_satellite do |merge_repo| prepare_satellite!(merge_repo) update_satellite_source_and_target!(merge_repo) - patch = merge_repo.git.format_patch(default_options({stdout: true}), "origin/#{merge_request.target_branch}..source/#{merge_request.source_branch}") + patch = merge_repo.git.format_patch(default_options({ stdout: true }), "origin/#{merge_request.target_branch}..source/#{merge_request.source_branch}") end rescue Grit::Git::CommandFailed => ex handle_exception(ex) @@ -128,7 +128,7 @@ module Gitlab # merge the source branch into the satellite # will raise CommandFailed when merge fails - repo.git.merge(default_options({no_ff: true}), "-m#{message}", "source/#{merge_request.source_branch}") + repo.git.merge(default_options({ no_ff: true }), "-m#{message}", "source/#{merge_request.source_branch}") rescue Grit::Git::CommandFailed => ex handle_exception(ex) end @@ -137,7 +137,7 @@ module Gitlab def update_satellite_source_and_target!(repo) repo.remote_add('source', merge_request.source_project.repository.path_to_repo) repo.remote_fetch('source') - repo.git.checkout(default_options({b: true}), merge_request.target_branch, "origin/#{merge_request.target_branch}") + repo.git.checkout(default_options({ b: true }), merge_request.target_branch, "origin/#{merge_request.target_branch}") rescue Grit::Git::CommandFailed => ex handle_exception(ex) end diff --git a/lib/gitlab/satellite/satellite.rb b/lib/gitlab/satellite/satellite.rb index 1de84309d15..62d1bb364d3 100644 --- a/lib/gitlab/satellite/satellite.rb +++ b/lib/gitlab/satellite/satellite.rb @@ -98,13 +98,13 @@ module Gitlab if heads.include? PARKING_BRANCH repo.git.checkout({}, PARKING_BRANCH) else - repo.git.checkout(default_options({b: true}), PARKING_BRANCH) + repo.git.checkout(default_options({ b: true }), PARKING_BRANCH) end # remove the parking branch from the list of heads ... heads.delete(PARKING_BRANCH) # ... and delete all others - heads.each { |head| repo.git.branch(default_options({D: true}), head) } + heads.each { |head| repo.git.branch(default_options({ D: true }), head) } end # Deletes all remotes except origin @@ -126,7 +126,7 @@ module Gitlab end def default_options(options = {}) - {raise: true, timeout: true}.merge(options) + { raise: true, timeout: true }.merge(options) end # Create directory for storing diff --git a/lib/gitlab/upgrader.rb b/lib/gitlab/upgrader.rb index 74b049b5143..0570c2fbeb5 100644 --- a/lib/gitlab/upgrader.rb +++ b/lib/gitlab/upgrader.rb @@ -62,7 +62,7 @@ module Gitlab end def env - {'RAILS_ENV' => 'production'} + { 'RAILS_ENV' => 'production' } end def upgrade -- cgit v1.2.1 From c427bf08e41342957632289e084604f53e65e353 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 2 Feb 2015 20:58:28 -0800 Subject: Rubocop: Style/AlignArray enabled --- lib/gitlab/diff/parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/diff/parser.rb b/lib/gitlab/diff/parser.rb index f7c1f20d762..0242e09a515 100644 --- a/lib/gitlab/diff/parser.rb +++ b/lib/gitlab/diff/parser.rb @@ -4,7 +4,7 @@ module Gitlab include Enumerable def parse(lines) - @lines = lines, + @lines = lines lines_obj = [] line_obj_index = 0 line_old = 1 -- cgit v1.2.1 From cc39bca3fa71930421f1c46844b4d02d5ff93e8b Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 2 Feb 2015 21:15:44 -0800 Subject: Rubocop: Style/AlignHash enabled --- lib/gitlab/ldap/adapter.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/ldap/adapter.rb b/lib/gitlab/ldap/adapter.rb index 256cdb4c2f1..577a890a7d9 100644 --- a/lib/gitlab/ldap/adapter.rb +++ b/lib/gitlab/ldap/adapter.rb @@ -63,8 +63,10 @@ module Gitlab end def dn_matches_filter?(dn, filter) - ldap_search(base: dn, filter: filter, - scope: Net::LDAP::SearchScope_BaseObject, attributes: %w{dn}).any? + ldap_search(base: dn, + filter: filter, + scope: Net::LDAP::SearchScope_BaseObject, + attributes: %w{dn}).any? end def ldap_search(*args) -- cgit v1.2.1 From da884aabc7674c68eee23699efb357bc94aef8d3 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 2 Feb 2015 21:22:57 -0800 Subject: Avoid using {...} for multi-line blocks --- lib/api/api_guard.rb | 4 ++-- lib/api/internal.rb | 4 +--- lib/api/namespaces.rb | 4 ++-- lib/api/system_hooks.rb | 4 ++-- 4 files changed, 7 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/api/api_guard.rb b/lib/api/api_guard.rb index be3d053efca..cb20bf0720d 100644 --- a/lib/api/api_guard.rb +++ b/lib/api/api_guard.rb @@ -120,7 +120,7 @@ module APIGuard end def oauth2_bearer_token_error_handler - Proc.new {|e| + Proc.new do |e| response = case e when MissingTokenError Rack::OAuth2::Server::Resource::Bearer::Unauthorized.new @@ -150,7 +150,7 @@ module APIGuard end response.finish - } + end end end diff --git a/lib/api/internal.rb b/lib/api/internal.rb index a999cff09c0..7a89a26facc 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -1,9 +1,7 @@ module API # Internal access API class Internal < Grape::API - before { - authenticate_by_gitlab_shell_token! - } + before { authenticate_by_gitlab_shell_token! } namespace 'internal' do # Check if git command is allowed to project diff --git a/lib/api/namespaces.rb b/lib/api/namespaces.rb index f9f2ed90ccc..b90ed6af5fb 100644 --- a/lib/api/namespaces.rb +++ b/lib/api/namespaces.rb @@ -1,10 +1,10 @@ module API # namespaces API class Namespaces < Grape::API - before { + before do authenticate! authenticated_as_admin! - } + end resource :namespaces do # Get a namespaces list diff --git a/lib/api/system_hooks.rb b/lib/api/system_hooks.rb index 3e239c5afe7..518964db50d 100644 --- a/lib/api/system_hooks.rb +++ b/lib/api/system_hooks.rb @@ -1,10 +1,10 @@ module API # Hooks API class SystemHooks < Grape::API - before { + before do authenticate! authenticated_as_admin! - } + end resource :hooks do # Get the list of system hooks -- cgit v1.2.1 From 368e9a0862dd7d58b009956e8f1ac51d2a549cda Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 2 Feb 2015 21:26:40 -0800 Subject: Rubocop: Style/CaseIndentation enabled --- lib/api/api_guard.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/api/api_guard.rb b/lib/api/api_guard.rb index cb20bf0720d..b9994fcefda 100644 --- a/lib/api/api_guard.rb +++ b/lib/api/api_guard.rb @@ -47,16 +47,12 @@ module APIGuard case validate_access_token(access_token, scopes) when Oauth2::AccessTokenValidationService::INSUFFICIENT_SCOPE raise InsufficientScopeError.new(scopes) - when Oauth2::AccessTokenValidationService::EXPIRED raise ExpiredError - when Oauth2::AccessTokenValidationService::REVOKED raise RevokedError - when Oauth2::AccessTokenValidationService::VALID @current_user = User.find(access_token.resource_owner_id) - end end end @@ -121,7 +117,8 @@ module APIGuard def oauth2_bearer_token_error_handler Proc.new do |e| - response = case e + response = + case e when MissingTokenError Rack::OAuth2::Server::Resource::Bearer::Unauthorized.new -- cgit v1.2.1 From 7558fe98759ec28c2fd97ae10cb1610a1a6c38cd Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 2 Feb 2015 21:31:03 -0800 Subject: More rubocop rules enable --- lib/email_validator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/email_validator.rb b/lib/email_validator.rb index 0a67ebcd795..f509f0a5843 100644 --- a/lib/email_validator.rb +++ b/lib/email_validator.rb @@ -1,5 +1,5 @@ # Based on https://github.com/balexand/email_validator -# +# # Extended to use only strict mode with following allowed characters: # ' - apostrophe # -- cgit v1.2.1 From 7d48205c1a472c07969e4dc43965fa3090b84376 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 2 Feb 2015 21:34:16 -0800 Subject: Rubocop: comment indentation --- lib/gitlab/git_access.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index ea96d04c5ab..0530923b202 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -113,8 +113,8 @@ module Gitlab # we dont allow force push to protected branch if forced_push?(project, oldrev, newrev) :force_push_code_to_protected_branches - # and we dont allow remove of protected branch elsif newrev == Gitlab::Git::BLANK_SHA + # and we dont allow remove of protected branch :remove_protected_branches elsif project.developers_can_push_to_protected_branch?(branch_name) :push_code -- cgit v1.2.1 From d04344373b899c1e54948ca46478f7b907a576d2 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 2 Feb 2015 21:53:27 -0800 Subject: Rubocop: no trailing newlines --- lib/gitlab/backend/shell_adapter.rb | 1 - lib/gitlab/force_push_check.rb | 1 - 2 files changed, 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/backend/shell_adapter.rb b/lib/gitlab/backend/shell_adapter.rb index f247f4593d7..fbe2a7a0d72 100644 --- a/lib/gitlab/backend/shell_adapter.rb +++ b/lib/gitlab/backend/shell_adapter.rb @@ -9,4 +9,3 @@ module Gitlab end end end - diff --git a/lib/gitlab/force_push_check.rb b/lib/gitlab/force_push_check.rb index 6ba2c3ad00a..eae9773a067 100644 --- a/lib/gitlab/force_push_check.rb +++ b/lib/gitlab/force_push_check.rb @@ -12,4 +12,3 @@ module Gitlab end end end - -- cgit v1.2.1 From 61cc6a9244f316f684cd887febd9dae1030a04b0 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 2 Feb 2015 21:59:28 -0800 Subject: Rubocop: indentation fixes Yay!!! --- lib/gitlab/diff/parser.rb | 2 +- lib/gitlab/git_access.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/diff/parser.rb b/lib/gitlab/diff/parser.rb index 0242e09a515..887ed76b36c 100644 --- a/lib/gitlab/diff/parser.rb +++ b/lib/gitlab/diff/parser.rb @@ -74,7 +74,7 @@ module Gitlab def html_escape(str) replacements = { '&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => ''' } - str.gsub(/[&"'><]/, replacements) + str.gsub(/[&"'><]/, replacements) end end end diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb index 0530923b202..6444cec7eb5 100644 --- a/lib/gitlab/git_access.rb +++ b/lib/gitlab/git_access.rb @@ -112,14 +112,14 @@ module Gitlab def protected_branch_action(project, oldrev, newrev, branch_name) # we dont allow force push to protected branch if forced_push?(project, oldrev, newrev) - :force_push_code_to_protected_branches + :force_push_code_to_protected_branches elsif newrev == Gitlab::Git::BLANK_SHA - # and we dont allow remove of protected branch - :remove_protected_branches + # and we dont allow remove of protected branch + :remove_protected_branches elsif project.developers_can_push_to_protected_branch?(branch_name) - :push_code + :push_code else - :push_code_to_protected_branches + :push_code_to_protected_branches end end -- cgit v1.2.1 From 4e97f26649a7756bef843fca74e3c58eadd117e1 Mon Sep 17 00:00:00 2001 From: jubianchi Date: Fri, 30 Jan 2015 10:46:08 +0100 Subject: Acces groups with their path in API --- lib/api/group_members.rb | 16 ---------------- lib/api/groups.rb | 16 ---------------- lib/api/helpers.rb | 25 +++++++++++++++++++++++-- 3 files changed, 23 insertions(+), 34 deletions(-) (limited to 'lib') diff --git a/lib/api/group_members.rb b/lib/api/group_members.rb index d596517c816..4373070083a 100644 --- a/lib/api/group_members.rb +++ b/lib/api/group_members.rb @@ -3,22 +3,6 @@ module API before { authenticate! } resource :groups do - helpers do - def find_group(id) - group = Group.find(id) - - if can?(current_user, :read_group, group) - group - else - render_api_error!("403 Forbidden - #{current_user.username} lacks sufficient access to #{group.name}", 403) - end - end - - def validate_access_level?(level) - Gitlab::Access.options_with_owner.values.include? level.to_i - end - end - # Get a list of group members viewable by the authenticated user. # # Example Request: diff --git a/lib/api/groups.rb b/lib/api/groups.rb index 730dfad52c8..384a28e41f5 100644 --- a/lib/api/groups.rb +++ b/lib/api/groups.rb @@ -4,22 +4,6 @@ module API before { authenticate! } resource :groups do - helpers do - def find_group(id) - group = Group.find(id) - - if can?(current_user, :read_group, group) - group - else - render_api_error!("403 Forbidden - #{current_user.username} lacks sufficient access to #{group.name}", 403) - end - end - - def validate_access_level?(level) - Gitlab::Access.options_with_owner.values.include? level.to_i - end - end - # Get a groups list # # Example Request: diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 62c26ef76ce..96249ea8cfe 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -55,6 +55,21 @@ module API end end + def find_group(id) + begin + group = Group.find(id) + rescue ActiveRecord::RecordNotFound + group = Group.find_by!(path: id) + end + + if can?(current_user, :read_group, group) + group + else + forbidden!("#{current_user.username} lacks sufficient "\ + "access to #{group.name}") + end + end + def paginate(relation) per_page = params[:per_page].to_i paginated = relation.page(params[:page]).per(per_page) @@ -135,10 +150,16 @@ module API errors end + def validate_access_level?(level) + Gitlab::Access.options_with_owner.values.include? level.to_i + end + # error helpers - def forbidden! - render_api_error!('403 Forbidden', 403) + def forbidden!(reason = nil) + message = ['403 Forbidden'] + message << " - #{reason}" if reason + render_api_error!(message.join(' '), 403) end def bad_request!(attribute) -- cgit v1.2.1 From 490cf7bfcefdb2e275c537699717c12e440f57ec Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 3 Feb 2015 18:12:20 -0800 Subject: Improve protected branches selectbox options --- lib/gitlab/access.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/access.rb b/lib/gitlab/access.rb index ad05bfadafe..424541b4a04 100644 --- a/lib/gitlab/access.rb +++ b/lib/gitlab/access.rb @@ -50,12 +50,13 @@ module Gitlab end def protection_options - { - "None" => PROTECTION_NONE, - "Protect, developers can push" => PROTECTION_DEV_CAN_PUSH, - "Full protection" => PROTECTION_FULL, - } + { + "Not protected, developers and masters can (force) push and delete the branch" => PROTECTION_NONE, + "Partially protected, developers can also push but prevent all force pushes and deletion" => PROTECTION_DEV_CAN_PUSH, + "Fully protected, only masters can push and prevent all force pushes and deletion" => PROTECTION_FULL, + } end + def protection_values protection_options.values end -- cgit v1.2.1 From 655fbc6bddb1d5f8df3e50f2896e5c6c276628b8 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 3 Feb 2015 19:25:57 -0800 Subject: Dont load rubocop in prod env --- lib/tasks/rubocop.rake | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/tasks/rubocop.rake b/lib/tasks/rubocop.rake index c28e529f86d..ddfaf5d51f2 100644 --- a/lib/tasks/rubocop.rake +++ b/lib/tasks/rubocop.rake @@ -1,2 +1,4 @@ -require 'rubocop/rake_task' -RuboCop::RakeTask.new +unless Rails.env.production? + require 'rubocop/rake_task' + RuboCop::RakeTask.new +end -- cgit v1.2.1 From b60d06eb2c91a61b91a214dac0f0f526b146f8d7 Mon Sep 17 00:00:00 2001 From: Jeroen van Baarsen Date: Mon, 2 Feb 2015 22:08:10 +0100 Subject: Added a way to retrieve MR files Signed-off-by: Jeroen van Baarsen --- lib/api/entities.rb | 16 +++++++++++----- lib/api/merge_requests.rb | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index ac166ed4fba..96920718ab5 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -147,6 +147,11 @@ module API expose :state, :created_at, :updated_at end + class RepoDiff < Grape::Entity + expose :old_path, :new_path, :a_mode, :b_mode, :diff + expose :new_file, :renamed_file, :deleted_file + end + class Milestone < ProjectEntity expose :due_date end @@ -166,6 +171,12 @@ module API expose :milestone, using: Entities::Milestone end + class MergeRequestChanges < MergeRequest + expose :diffs, as: :changes, using: Entities::RepoDiff do |compare, _| + compare.diffs + end + end + class SSHKey < Grape::Entity expose :id, :title, :key, :created_at end @@ -236,11 +247,6 @@ module API expose :name, :color end - class RepoDiff < Grape::Entity - expose :old_path, :new_path, :a_mode, :b_mode, :diff - expose :new_file, :renamed_file, :deleted_file - end - class Compare < Grape::Entity expose :commit, using: Entities::RepoCommit do |compare, options| Commit.decorate(compare.commits).last diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb index 2a5b10c6f52..a0ebd8d0c1b 100644 --- a/lib/api/merge_requests.rb +++ b/lib/api/merge_requests.rb @@ -75,6 +75,22 @@ module API present merge_request, with: Entities::MergeRequest end + # Show MR changes + # + # Parameters: + # id (required) - The ID of a project + # merge_request_id (required) - The ID of MR + # + # Example: + # GET /projects/:id/merge_request/:merge_request_id/changes + # + get ':id/merge_request/:merge_request_id/changes' do + merge_request = user_project.merge_requests. + find(params[:merge_request_id]) + authorize! :read_merge_request, merge_request + present merge_request, with: Entities::MergeRequestChanges + end + # Create MR # # Parameters: -- cgit v1.2.1 From 9910b7ff99c3d7f89f512c1915ce40ed0c1696e3 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Wed, 4 Feb 2015 17:10:39 +0100 Subject: Allow groups to be mentioned. Resolves #1673. --- lib/gitlab/markdown.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb index c0e83fb3078..78627f413c2 100644 --- a/lib/gitlab/markdown.rb +++ b/lib/gitlab/markdown.rb @@ -202,8 +202,15 @@ module Gitlab if identifier == "all" link_to("@all", project_url(project), options) - elsif User.find_by(username: identifier) - link_to("@#{identifier}", user_url(identifier), options) + elsif namespace = Namespace.find_by(path: identifier) + url = + if namespace.type == "Group" + group_url(identifier) + else + user_url(identifier) + end + + link_to("@#{identifier}", url, options) end end -- cgit v1.2.1 From 5194214e3a2f97accf0c8119b4cb39fd4fcef5db Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Tue, 27 Jan 2015 15:37:19 -0800 Subject: GitLab integration. Importer --- lib/gitlab/gitlab_import/client.rb | 82 +++++++++++++++++++++++++++++ lib/gitlab/gitlab_import/importer.rb | 48 +++++++++++++++++ lib/gitlab/gitlab_import/project_creator.rb | 39 ++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 lib/gitlab/gitlab_import/client.rb create mode 100644 lib/gitlab/gitlab_import/importer.rb create mode 100644 lib/gitlab/gitlab_import/project_creator.rb (limited to 'lib') diff --git a/lib/gitlab/gitlab_import/client.rb b/lib/gitlab/gitlab_import/client.rb new file mode 100644 index 00000000000..64e369e9c12 --- /dev/null +++ b/lib/gitlab/gitlab_import/client.rb @@ -0,0 +1,82 @@ +module Gitlab + module GitlabImport + class Client + attr_reader :client, :api + + PER_PAGE = 100 + + def initialize(access_token) + @client = ::OAuth2::Client.new( + config.app_id, + config.app_secret, + github_options + ) + + if access_token + @api = OAuth2::AccessToken.from_hash(@client, :access_token => access_token) + end + end + + def authorize_url(redirect_uri) + client.auth_code.authorize_url({ + redirect_uri: redirect_uri, + scope: "api" + }) + end + + def get_token(code, redirect_uri) + client.auth_code.get_token(code, redirect_uri: redirect_uri).token + end + + def issues(project_identifier) + lazy_page_iterator(PER_PAGE) do |page| + api.get("/api/v3/projects/#{project_identifier}/issues?per_page=#{PER_PAGE}&page=#{page}").parsed + end + end + + def issue_comments(project_identifier, issue_id) + lazy_page_iterator(PER_PAGE) do |page| + api.get("/api/v3/projects/#{project_identifier}/issues/#{issue_id}/notes?per_page=#{PER_PAGE}&page=#{page}").parsed + end + end + + def project(id) + api.get("/api/v3/projects/#{id}").parsed + end + + def projects + lazy_page_iterator(PER_PAGE) do |page| + api.get("/api/v3/projects?per_page=#{PER_PAGE}&page=#{page}").parsed + end + end + + private + + def lazy_page_iterator(per_page) + Enumerator.new do |y| + page = 1 + loop do + items = yield(page) + items.each do |item| + y << item + end + break if items.empty? || items.size < per_page + page += 1 + end + end + end + + def config + Gitlab.config.omniauth.providers.select{|provider| provider.name == "gitlab"}.first + end + + def github_options + { + site: 'https://gitlab.com/', + authorize_url: 'oauth/authorize', + token_url: 'oauth/token' + } + end + end + end +end diff --git a/lib/gitlab/gitlab_import/importer.rb b/lib/gitlab/gitlab_import/importer.rb new file mode 100644 index 00000000000..3e9087a556c --- /dev/null +++ b/lib/gitlab/gitlab_import/importer.rb @@ -0,0 +1,48 @@ +module Gitlab + module GitlabImport + class Importer + attr_reader :project, :client + + def initialize(project) + @project = project + @client = Client.new(project.creator.gitlab_access_token) + end + + def execute + project_identifier = URI.encode(project.import_source, '/') + + #Issues && Comments + issues = client.issues(project_identifier) + + issues.each do |issue| + body = "*Created by: #{issue["author"]["name"]}*\n\n#{issue["description"]}" + + + comments = client.issue_comments(project_identifier, issue["id"]) + if comments.any? + body += "\n\n\n**Imported comments:**\n" + end + comments.each do |comment| + body += "\n\n*By #{comment["author"]["name"]} on #{comment["created_at"]}*\n\n#{comment["body"]}" + end + + project.issues.create!( + description: body, + title: issue["title"], + state: issue["state"], + author_id: gl_user_id(project, issue["author"]["id"]) + ) + end + + true + end + + private + + def gl_user_id(project, gitlab_id) + user = User.joins(:identities).find_by("identities.extern_uid = ?", gitlab_id.to_s) + (user && user.id) || project.creator_id + end + end + end +end diff --git a/lib/gitlab/gitlab_import/project_creator.rb b/lib/gitlab/gitlab_import/project_creator.rb new file mode 100644 index 00000000000..affd828e816 --- /dev/null +++ b/lib/gitlab/gitlab_import/project_creator.rb @@ -0,0 +1,39 @@ +module Gitlab + module GitlabImport + class ProjectCreator + attr_reader :repo, :namespace, :current_user + + def initialize(repo, namespace, current_user) + @repo = repo + @namespace = namespace + @current_user = current_user + end + + def execute + @project = Project.new( + name: repo["name"], + path: repo["path"], + description: repo["description"], + namespace: namespace, + creator: current_user, + visibility_level: repo["visibility_level"], + import_type: "gitlab", + import_source: repo["path_with_namespace"], + import_url: repo["http_url_to_repo"]#.sub("://", "://oauth2@#{current_user.gitlab_access_token}") + ) + + if @project.save! + @project.reload + + if @project.import_failed? + @project.import_retry + else + @project.import_start + end + end + + @project + end + end + end +end -- cgit v1.2.1 From 7ddba92a394b5e5fd67eda50b6ee25b38e53e7b9 Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Mon, 2 Feb 2015 13:52:44 -0800 Subject: Gitlab integration: added tests --- lib/gitlab/gitlab_import/project_creator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/gitlab_import/project_creator.rb b/lib/gitlab/gitlab_import/project_creator.rb index affd828e816..6424d56f8f1 100644 --- a/lib/gitlab/gitlab_import/project_creator.rb +++ b/lib/gitlab/gitlab_import/project_creator.rb @@ -19,7 +19,7 @@ module Gitlab visibility_level: repo["visibility_level"], import_type: "gitlab", import_source: repo["path_with_namespace"], - import_url: repo["http_url_to_repo"]#.sub("://", "://oauth2@#{current_user.gitlab_access_token}") + import_url: repo["http_url_to_repo"].sub("://", "://oauth2:#{current_user.gitlab_access_token}@") ) if @project.save! -- cgit v1.2.1 From 18231b0bb353fffa77b492e4b04fa61c9b3a25bb Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Mon, 2 Feb 2015 14:26:29 -0800 Subject: GitLab.com integration: refactoring --- lib/gitlab/github/client.rb | 29 ----------------- lib/gitlab/github/importer.rb | 48 ----------------------------- lib/gitlab/github/project_creator.rb | 39 ----------------------- lib/gitlab/github_import/client.rb | 29 +++++++++++++++++ lib/gitlab/github_import/importer.rb | 48 +++++++++++++++++++++++++++++ lib/gitlab/github_import/project_creator.rb | 39 +++++++++++++++++++++++ 6 files changed, 116 insertions(+), 116 deletions(-) delete mode 100644 lib/gitlab/github/client.rb delete mode 100644 lib/gitlab/github/importer.rb delete mode 100644 lib/gitlab/github/project_creator.rb create mode 100644 lib/gitlab/github_import/client.rb create mode 100644 lib/gitlab/github_import/importer.rb create mode 100644 lib/gitlab/github_import/project_creator.rb (limited to 'lib') diff --git a/lib/gitlab/github/client.rb b/lib/gitlab/github/client.rb deleted file mode 100644 index d6b936c649c..00000000000 --- a/lib/gitlab/github/client.rb +++ /dev/null @@ -1,29 +0,0 @@ -module Gitlab - module Github - class Client - attr_reader :client - - def initialize - @client = ::OAuth2::Client.new( - config.app_id, - config.app_secret, - github_options - ) - end - - private - - def config - Gitlab.config.omniauth.providers.select{|provider| provider.name == "github"}.first - end - - def github_options - { - site: 'https://api.github.com', - authorize_url: 'https://github.com/login/oauth/authorize', - token_url: 'https://github.com/login/oauth/access_token' - } - end - end - end -end diff --git a/lib/gitlab/github/importer.rb b/lib/gitlab/github/importer.rb deleted file mode 100644 index 9f0fc6c4471..00000000000 --- a/lib/gitlab/github/importer.rb +++ /dev/null @@ -1,48 +0,0 @@ -module Gitlab - module Github - class Importer - attr_reader :project - - def initialize(project) - @project = project - end - - def execute - client = octo_client(project.creator.github_access_token) - - #Issues && Comments - client.list_issues(project.import_source, state: :all).each do |issue| - if issue.pull_request.nil? - body = "*Created by: #{issue.user.login}*\n\n#{issue.body}" - - if issue.comments > 0 - body += "\n\n\n**Imported comments:**\n" - client.issue_comments(project.import_source, issue.number).each do |c| - body += "\n\n*By #{c.user.login} on #{c.created_at}*\n\n#{c.body}" - end - end - - project.issues.create!( - description: body, - title: issue.title, - state: issue.state == 'closed' ? 'closed' : 'opened', - author_id: gl_user_id(project, issue.user.id) - ) - end - end - end - - private - - def octo_client(access_token) - ::Octokit.auto_paginate = true - ::Octokit::Client.new(access_token: access_token) - end - - def gl_user_id(project, github_id) - user = User.joins(:identities).find_by("identities.extern_uid = ?", github_id.to_s) - (user && user.id) || project.creator_id - end - end - end -end diff --git a/lib/gitlab/github/project_creator.rb b/lib/gitlab/github/project_creator.rb deleted file mode 100644 index 7b04926071f..00000000000 --- a/lib/gitlab/github/project_creator.rb +++ /dev/null @@ -1,39 +0,0 @@ -module Gitlab - module Github - class ProjectCreator - attr_reader :repo, :namespace, :current_user - - def initialize(repo, namespace, current_user) - @repo = repo - @namespace = namespace - @current_user = current_user - end - - def execute - @project = Project.new( - name: repo.name, - path: repo.name, - description: repo.description, - namespace: namespace, - creator: current_user, - visibility_level: repo.private ? Gitlab::VisibilityLevel::PRIVATE : Gitlab::VisibilityLevel::PUBLIC, - import_type: "github", - import_source: repo.full_name, - import_url: repo.clone_url.sub("https://", "https://#{current_user.github_access_token}@") - ) - - if @project.save! - @project.reload - - if @project.import_failed? - @project.import_retry - else - @project.import_start - end - end - - @project - end - end - end -end diff --git a/lib/gitlab/github_import/client.rb b/lib/gitlab/github_import/client.rb new file mode 100644 index 00000000000..2e454e7c10f --- /dev/null +++ b/lib/gitlab/github_import/client.rb @@ -0,0 +1,29 @@ +module Gitlab + module GithubImport + class Client + attr_reader :client + + def initialize + @client = ::OAuth2::Client.new( + config.app_id, + config.app_secret, + github_options + ) + end + + private + + def config + Gitlab.config.omniauth.providers.select{|provider| provider.name == "github"}.first + end + + def github_options + { + site: 'https://api.github.com', + authorize_url: 'https://github.com/login/oauth/authorize', + token_url: 'https://github.com/login/oauth/access_token' + } + end + end + end +end diff --git a/lib/gitlab/github_import/importer.rb b/lib/gitlab/github_import/importer.rb new file mode 100644 index 00000000000..180ad6c3018 --- /dev/null +++ b/lib/gitlab/github_import/importer.rb @@ -0,0 +1,48 @@ +module Gitlab + module GithubImport + class Importer + attr_reader :project + + def initialize(project) + @project = project + end + + def execute + client = octo_client(project.creator.github_access_token) + + #Issues && Comments + client.list_issues(project.import_source, state: :all).each do |issue| + if issue.pull_request.nil? + body = "*Created by: #{issue.user.login}*\n\n#{issue.body}" + + if issue.comments > 0 + body += "\n\n\n**Imported comments:**\n" + client.issue_comments(project.import_source, issue.number).each do |c| + body += "\n\n*By #{c.user.login} on #{c.created_at}*\n\n#{c.body}" + end + end + + project.issues.create!( + description: body, + title: issue.title, + state: issue.state == 'closed' ? 'closed' : 'opened', + author_id: gl_user_id(project, issue.user.id) + ) + end + end + end + + private + + def octo_client(access_token) + ::Octokit.auto_paginate = true + ::Octokit::Client.new(access_token: access_token) + end + + def gl_user_id(project, github_id) + user = User.joins(:identities).find_by("identities.extern_uid = ?", github_id.to_s) + (user && user.id) || project.creator_id + end + end + end +end diff --git a/lib/gitlab/github_import/project_creator.rb b/lib/gitlab/github_import/project_creator.rb new file mode 100644 index 00000000000..9439ca6cbf4 --- /dev/null +++ b/lib/gitlab/github_import/project_creator.rb @@ -0,0 +1,39 @@ +module Gitlab + module GithubImport + class ProjectCreator + attr_reader :repo, :namespace, :current_user + + def initialize(repo, namespace, current_user) + @repo = repo + @namespace = namespace + @current_user = current_user + end + + def execute + @project = Project.new( + name: repo.name, + path: repo.name, + description: repo.description, + namespace: namespace, + creator: current_user, + visibility_level: repo.private ? Gitlab::VisibilityLevel::PRIVATE : Gitlab::VisibilityLevel::PUBLIC, + import_type: "github", + import_source: repo.full_name, + import_url: repo.clone_url.sub("https://", "https://#{current_user.github_access_token}@") + ) + + if @project.save! + @project.reload + + if @project.import_failed? + @project.import_retry + else + @project.import_start + end + end + + @project + end + end + end +end -- cgit v1.2.1 From 33349dd54928a0b074b4ae3ebfabf214799fc085 Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Mon, 2 Feb 2015 17:01:07 -0800 Subject: GitLab.com integration: refactoring --- lib/gitlab/github_import/client.rb | 2 +- lib/gitlab/github_import/importer.rb | 9 ++++++--- lib/gitlab/gitlab_import/client.rb | 4 ++-- lib/gitlab/gitlab_import/importer.rb | 10 ++++++---- lib/gitlab/import_formatter.rb | 15 +++++++++++++++ 5 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 lib/gitlab/import_formatter.rb (limited to 'lib') diff --git a/lib/gitlab/github_import/client.rb b/lib/gitlab/github_import/client.rb index 2e454e7c10f..cf43d36c6c3 100644 --- a/lib/gitlab/github_import/client.rb +++ b/lib/gitlab/github_import/client.rb @@ -14,7 +14,7 @@ module Gitlab private def config - Gitlab.config.omniauth.providers.select{|provider| provider.name == "github"}.first + Gitlab.config.omniauth.providers.find{|provider| provider.name == "github"} end def github_options diff --git a/lib/gitlab/github_import/importer.rb b/lib/gitlab/github_import/importer.rb index 180ad6c3018..91a4595b9ea 100644 --- a/lib/gitlab/github_import/importer.rb +++ b/lib/gitlab/github_import/importer.rb @@ -5,6 +5,7 @@ module Gitlab def initialize(project) @project = project + @formatter = Gitlab::ImportFormatter.new end def execute @@ -13,12 +14,14 @@ module Gitlab #Issues && Comments client.list_issues(project.import_source, state: :all).each do |issue| if issue.pull_request.nil? - body = "*Created by: #{issue.user.login}*\n\n#{issue.body}" + + body = @formatter.author_line(issue.user.login, issue.body) if issue.comments > 0 - body += "\n\n\n**Imported comments:**\n" + body += @formatter.comments_header + client.issue_comments(project.import_source, issue.number).each do |c| - body += "\n\n*By #{c.user.login} on #{c.created_at}*\n\n#{c.body}" + body += @formatter.comment_to_md(c.user.login, c.created_at, c.body) end end diff --git a/lib/gitlab/gitlab_import/client.rb b/lib/gitlab/gitlab_import/client.rb index 64e369e9c12..2206b68da99 100644 --- a/lib/gitlab/gitlab_import/client.rb +++ b/lib/gitlab/gitlab_import/client.rb @@ -13,7 +13,7 @@ module Gitlab ) if access_token - @api = OAuth2::AccessToken.from_hash(@client, :access_token => access_token) + @api = OAuth2::AccessToken.from_hash(@client, access_token: access_token) end end @@ -67,7 +67,7 @@ module Gitlab end def config - Gitlab.config.omniauth.providers.select{|provider| provider.name == "gitlab"}.first + Gitlab.config.omniauth.providers.find{|provider| provider.name == "gitlab"} end def github_options diff --git a/lib/gitlab/gitlab_import/importer.rb b/lib/gitlab/gitlab_import/importer.rb index 3e9087a556c..a529483c1e2 100644 --- a/lib/gitlab/gitlab_import/importer.rb +++ b/lib/gitlab/gitlab_import/importer.rb @@ -6,6 +6,7 @@ module Gitlab def initialize(project) @project = project @client = Client.new(project.creator.gitlab_access_token) + @formatter = Gitlab::ImportFormatter.new end def execute @@ -15,15 +16,16 @@ module Gitlab issues = client.issues(project_identifier) issues.each do |issue| - body = "*Created by: #{issue["author"]["name"]}*\n\n#{issue["description"]}" - + body = @formatter.author_line(issue["author"]["name"], issue["description"]) comments = client.issue_comments(project_identifier, issue["id"]) + if comments.any? - body += "\n\n\n**Imported comments:**\n" + body += @formatter.comments_header end + comments.each do |comment| - body += "\n\n*By #{comment["author"]["name"]} on #{comment["created_at"]}*\n\n#{comment["body"]}" + body += @formatter.comment_to_md(comment["author"]["name"], comment["created_at"], comment["body"]) end project.issues.create!( diff --git a/lib/gitlab/import_formatter.rb b/lib/gitlab/import_formatter.rb new file mode 100644 index 00000000000..a9283eaf2a5 --- /dev/null +++ b/lib/gitlab/import_formatter.rb @@ -0,0 +1,15 @@ +module Gitlab + class ImportFormatter + def comment_to_md(author, date, body) + "\n\n*By #{author} on #{date}*\n\n#{body}" + end + + def comments_header + "\n\n\n**Imported comments:**\n" + end + + def author_line(author, body) + "*Created by: #{author}*\n\n#{body}" + end + end +end \ No newline at end of file -- cgit v1.2.1 From 93585661b1699384060616f0a19433ededadf3fe Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Tue, 3 Feb 2015 09:42:56 -0800 Subject: code folding --- lib/gitlab/import_formatter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/gitlab/import_formatter.rb b/lib/gitlab/import_formatter.rb index a9283eaf2a5..ebb4b87f7e3 100644 --- a/lib/gitlab/import_formatter.rb +++ b/lib/gitlab/import_formatter.rb @@ -12,4 +12,4 @@ module Gitlab "*Created by: #{author}*\n\n#{body}" end end -end \ No newline at end of file +end -- cgit v1.2.1 From 62ed1c537e9b8aa85d354b377f18083fb71b8e05 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 5 Feb 2015 14:20:55 -0800 Subject: Explicitly define ordering in models using default_scope --- lib/api/issues.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'lib') diff --git a/lib/api/issues.rb b/lib/api/issues.rb index d2828b24c36..e2c2cd4c3da 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -39,7 +39,6 @@ module API issues = current_user.issues issues = filter_issues_state(issues, params[:state]) unless params[:state].nil? issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil? - issues = issues.order('issues.id DESC') present paginate(issues), with: Entities::Issue end @@ -70,7 +69,6 @@ module API unless params[:milestone].nil? issues = filter_issues_milestone(issues, params[:milestone]) end - issues = issues.order('issues.id DESC') present paginate(issues), with: Entities::Issue end -- cgit v1.2.1 From 1ac20698a5122111c8e12de4cc59da837b0f9573 Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Thu, 5 Feb 2015 10:31:36 -0800 Subject: gitlab.com importer: refactorig --- lib/gitlab/github_import/importer.rb | 3 ++- lib/gitlab/gitlab_import/importer.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/github_import/importer.rb b/lib/gitlab/github_import/importer.rb index 91a4595b9ea..1f02ee49b6a 100644 --- a/lib/gitlab/github_import/importer.rb +++ b/lib/gitlab/github_import/importer.rb @@ -43,7 +43,8 @@ module Gitlab end def gl_user_id(project, github_id) - user = User.joins(:identities).find_by("identities.extern_uid = ?", github_id.to_s) + user = User.joins(:identities). + find_by("identities.extern_uid = ? AND identities.provider = 'github'", github_id.to_s) (user && user.id) || project.creator_id end end diff --git a/lib/gitlab/gitlab_import/importer.rb b/lib/gitlab/gitlab_import/importer.rb index a529483c1e2..5f9b14399a4 100644 --- a/lib/gitlab/gitlab_import/importer.rb +++ b/lib/gitlab/gitlab_import/importer.rb @@ -42,7 +42,7 @@ module Gitlab private def gl_user_id(project, gitlab_id) - user = User.joins(:identities).find_by("identities.extern_uid = ?", gitlab_id.to_s) + user = User.joins(:identities).find_by("identities.extern_uid = ? AND identities.provider = 'gitlab'", gitlab_id.to_s) (user && user.id) || project.creator_id end end -- cgit v1.2.1 From b3c90dd51418d0c41df4ccd57d9480ea44b35eec Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Thu, 5 Feb 2015 16:57:27 -0800 Subject: GitHub importer refactoring --- lib/gitlab/github_import/client.rb | 32 ++++++++++++++++++++++++++++++-- lib/gitlab/github_import/importer.rb | 10 ++-------- 2 files changed, 32 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/github_import/client.rb b/lib/gitlab/github_import/client.rb index cf43d36c6c3..c9904fe8779 100644 --- a/lib/gitlab/github_import/client.rb +++ b/lib/gitlab/github_import/client.rb @@ -1,14 +1,42 @@ module Gitlab module GithubImport class Client - attr_reader :client + attr_reader :client, :api - def initialize + def initialize(access_token) @client = ::OAuth2::Client.new( config.app_id, config.app_secret, github_options ) + + if access_token + ::Octokit.auto_paginate = true + @api = ::Octokit::Client.new(access_token: access_token) + end + end + + def authorize_url(redirect_uri) + client.auth_code.authorize_url({ + redirect_uri: redirect_uri, + scope: "repo, user, user:email" + }) + end + + def get_token(code) + client.auth_code.get_token(code).token + end + + def method_missing(method, *args, &block) + if api.respond_to?(method) + api.send(method, *args, &block) + else + super(method, *args, &block) + end + end + + def respond_to?(method) + api.respond_to?(method) || super end private diff --git a/lib/gitlab/github_import/importer.rb b/lib/gitlab/github_import/importer.rb index 1f02ee49b6a..bc2b645b2d9 100644 --- a/lib/gitlab/github_import/importer.rb +++ b/lib/gitlab/github_import/importer.rb @@ -1,16 +1,15 @@ module Gitlab module GithubImport class Importer - attr_reader :project + attr_reader :project, :client def initialize(project) @project = project + @client = Client.new(project.creator.github_access_token) @formatter = Gitlab::ImportFormatter.new end def execute - client = octo_client(project.creator.github_access_token) - #Issues && Comments client.list_issues(project.import_source, state: :all).each do |issue| if issue.pull_request.nil? @@ -37,11 +36,6 @@ module Gitlab private - def octo_client(access_token) - ::Octokit.auto_paginate = true - ::Octokit::Client.new(access_token: access_token) - end - def gl_user_id(project, github_id) user = User.joins(:identities). find_by("identities.extern_uid = ? AND identities.provider = 'github'", github_id.to_s) -- cgit v1.2.1 From bdfb349ff70f0fde6d4dc7b4317c3bc7ead580a4 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 5 Feb 2015 22:00:54 -0800 Subject: Refactor and improve sorting objects in API for projects, issues and merge requests --- lib/api/helpers.rb | 16 +++++++++++ lib/api/issues.rb | 10 +++++-- lib/api/merge_requests.rb | 31 +++++++++----------- lib/api/projects.rb | 73 +++++++++++++++++++++++------------------------ 4 files changed, 72 insertions(+), 58 deletions(-) (limited to 'lib') diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index be9e4280d65..8fa30460ba6 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -154,6 +154,22 @@ module API Gitlab::Access.options_with_owner.values.include? level.to_i end + def issuable_order_by + if params["order_by"] == 'updated_at' + 'updated_at' + else + 'created_at' + end + end + + def issuable_sort + if params["sort"] == 'asc' + :asc + else + :desc + end + end + # error helpers def forbidden!(reason = nil) diff --git a/lib/api/issues.rb b/lib/api/issues.rb index e2c2cd4c3da..ff062be6040 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -27,7 +27,9 @@ module API # Parameters: # state (optional) - Return "opened" or "closed" issues # labels (optional) - Comma-separated list of label names - + # order_by (optional) - Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at` + # sort (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc` + # # Example Requests: # GET /issues # GET /issues?state=opened @@ -39,7 +41,7 @@ module API issues = current_user.issues issues = filter_issues_state(issues, params[:state]) unless params[:state].nil? issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil? - + issues.reorder(issuable_order_by => issuable_sort) present paginate(issues), with: Entities::Issue end end @@ -52,6 +54,8 @@ module API # state (optional) - Return "opened" or "closed" issues # labels (optional) - Comma-separated list of label names # milestone (optional) - Milestone title + # order_by (optional) - Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at` + # sort (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc` # # Example Requests: # GET /projects/:id/issues @@ -66,10 +70,12 @@ module API issues = user_project.issues issues = filter_issues_state(issues, params[:state]) unless params[:state].nil? issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil? + unless params[:milestone].nil? issues = filter_issues_milestone(issues, params[:milestone]) end + issues.reorder(issuable_order_by => issuable_sort) present paginate(issues), with: Entities::Issue end diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb index a0ebd8d0c1b..25b7857f4b1 100644 --- a/lib/api/merge_requests.rb +++ b/lib/api/merge_requests.rb @@ -25,6 +25,8 @@ module API # Parameters: # id (required) - The ID of a project # state (optional) - Return requests "merged", "opened" or "closed" + # order_by (optional) - Return requests ordered by `created_at` or `updated_at` fields. Default is `created_at` + # sort (optional) - Return requests sorted in `asc` or `desc` order. Default is `desc` # # Example: # GET /projects/:id/merge_requests @@ -37,25 +39,18 @@ module API # get ":id/merge_requests" do authorize! :read_merge_request, user_project + merge_requests = user_project.merge_requests + + merge_requests = + case params["state"] + when "opened" then merge_requests.opened + when "closed" then merge_requests.closed + when "merged" then merge_requests.merged + else merge_requests + end - mrs = case params["state"] - when "opened" then user_project.merge_requests.opened - when "closed" then user_project.merge_requests.closed - when "merged" then user_project.merge_requests.merged - else user_project.merge_requests - end - - sort = case params["sort"] - when 'desc' then 'DESC' - else 'ASC' - end - - mrs = case params["order_by"] - when 'updated_at' then mrs.order("updated_at #{sort}") - else mrs.order("created_at #{sort}") - end - - present paginate(mrs), with: Entities::MergeRequest + merge_requests.reorder(issuable_order_by => issuable_sort) + present paginate(merge_requests), with: Entities::MergeRequest end # Show MR diff --git a/lib/api/projects.rb b/lib/api/projects.rb index d96288bb982..0677e85beab 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -11,6 +11,37 @@ module API attrs[:visibility_level] = Gitlab::VisibilityLevel::PUBLIC if !attrs[:visibility_level].present? && publik == true attrs end + + def filter_projects(projects) + # If the archived parameter is passed, limit results accordingly + if params[:archived].present? + projects = projects.where(archived: parse_boolean(params[:archived])) + end + + if params[:search].present? + projects = projects.search(params[:search]) + end + + projects.reorder(project_order_by => project_sort) + end + + def project_order_by + order_fields = %w(id name path created_at updated_at last_activity_at) + + if order_fields.include?(params['order_by']) + params['order_by'] + else + 'created_at' + end + end + + def project_sort + if params["sort"] == 'asc' + :asc + else + :desc + end + end end # Get a projects list for authenticated user @@ -19,25 +50,7 @@ module API # GET /projects get do @projects = current_user.authorized_projects - sort = params[:sort] == 'desc' ? 'desc' : 'asc' - - @projects = case params["order_by"] - when 'id' then @projects.reorder("id #{sort}") - when 'name' then @projects.reorder("name #{sort}") - when 'created_at' then @projects.reorder("created_at #{sort}") - when 'last_activity_at' then @projects.reorder("last_activity_at #{sort}") - else @projects - end - - # If the archived parameter is passed, limit results accordingly - if params[:archived].present? - @projects = @projects.where(archived: parse_boolean(params[:archived])) - end - - if params[:search].present? - @projects = @projects.search(params[:search]) - end - + @projects = filter_projects(@projects) @projects = paginate @projects present @projects, with: Entities::Project end @@ -47,16 +60,8 @@ module API # Example Request: # GET /projects/owned get '/owned' do - sort = params[:sort] == 'desc' ? 'desc' : 'asc' @projects = current_user.owned_projects - @projects = case params["order_by"] - when 'id' then @projects.reorder("id #{sort}") - when 'name' then @projects.reorder("name #{sort}") - when 'created_at' then @projects.reorder("created_at #{sort}") - when 'last_activity_at' then @projects.reorder("last_activity_at #{sort}") - else @projects - end - + @projects = filter_projects(@projects) @projects = paginate @projects present @projects, with: Entities::Project end @@ -67,16 +72,8 @@ module API # GET /projects/all get '/all' do authenticated_as_admin! - sort = params[:sort] == 'desc' ? 'desc' : 'asc' - - @projects = case params["order_by"] - when 'id' then Project.order("id #{sort}") - when 'name' then Project.order("name #{sort}") - when 'created_at' then Project.order("created_at #{sort}") - when 'last_activity_at' then Project.order("last_activity_at #{sort}") - else Project - end - + @projects = Project.all + @projects = filter_projects(@projects) @projects = paginate @projects present @projects, with: Entities::Project end -- cgit v1.2.1 From 42422dcc6aab156cc3e89c75c7ed2a71c715b169 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Sat, 7 Feb 2015 16:41:30 +0100 Subject: Add internal broadcast message API. --- lib/api/entities.rb | 4 ++++ lib/api/internal.rb | 8 ++++++++ 2 files changed, 12 insertions(+) (limited to 'lib') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index fa76a54c2d8..8d0664386b4 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -270,5 +270,9 @@ module API class Contributor < Grape::Entity expose :name, :email, :commits, :additions, :deletions end + + class BroadcastMessage < Grape::Entity + expose :message, :starts_at, :ends_at, :color, :font + end end end diff --git a/lib/api/internal.rb b/lib/api/internal.rb index 7a89a26facc..b5542c1874b 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -69,6 +69,14 @@ module API gitlab_rev: Gitlab::REVISION, } end + + get "/broadcast_message" do + if message = BroadcastMessage.current + present message, with: Entities::BroadcastMessage + else + not_found! + end + end end end end -- cgit v1.2.1 From 8681cb3137511e51e19f76aef9839be28f8fcd6a Mon Sep 17 00:00:00 2001 From: Nikita Verkhovin Date: Sat, 7 Feb 2015 17:14:55 +0600 Subject: Add labels notes --- lib/gitlab/markdown.rb | 18 ++++++++++++++++-- lib/gitlab/reference_extractor.rb | 11 +++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/gitlab/markdown.rb b/lib/gitlab/markdown.rb index 78627f413c2..fb0218a2778 100644 --- a/lib/gitlab/markdown.rb +++ b/lib/gitlab/markdown.rb @@ -92,7 +92,7 @@ module Gitlab allowed_tags = ActionView::Base.sanitized_allowed_tags sanitize text.html_safe, - attributes: allowed_attributes + %w(id class), + attributes: allowed_attributes + %w(id class style), tags: allowed_tags + %w(table tr td th) end @@ -128,6 +128,7 @@ module Gitlab (?\W)? # Prefix ( # Reference @(?#{NAME_STR}) # User name + |~(?