From 31e76baf610e1307090a6bac3a7b3d525bce057a Mon Sep 17 00:00:00 2001 From: David Padilla Date: Mon, 29 Feb 2016 23:29:20 -0600 Subject: Fix #2364. Fall back to In-Reply-To header when reply key not available --- lib/gitlab/email/receiver.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib/gitlab') diff --git a/lib/gitlab/email/receiver.rb b/lib/gitlab/email/receiver.rb index d4b6f6d120d..d55bacde5b0 100644 --- a/lib/gitlab/email/receiver.rb +++ b/lib/gitlab/email/receiver.rb @@ -63,6 +63,10 @@ module Gitlab end def reply_key + key_from_to_address || key_from_in_reply_to_header + end + + def key_from_to_address key = nil message.to.each do |address| key = Gitlab::IncomingEmail.key_from_address(address) @@ -72,6 +76,17 @@ module Gitlab key end + def key_from_in_reply_to_header + reply_key = nil + + message[:in_reply_to].message_ids.each do |message_id| + reply_key = Gitlab::IncomingEmail.key_from_address(message_id) + break if reply_key + end + + reply_key + end + def sent_notification return nil unless reply_key -- cgit v1.2.1 From 9f218fc184894d61c10f738c59bce97780f06e25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Thu, 17 Mar 2016 20:03:51 +0100 Subject: Improve and finish the fallback to the In-Reply-To and References header for the reply-by-email feature A few things to note: - The IncomingEmail feature is now enabled even without a correctly-formatted sub-address - Message-ID for new thread mail are kept the same so that subsequent notifications to this thread are grouped in the thread by the email service that receives the notification (i.e. In-Reply-To of the answer == Message-ID of the first thread message) - To maximize our chance to be able to retrieve the reply key, we look for it in the In-Reply-To header and the References header - The pattern for the fallback reply message id is "reply-[key]@[gitlab_host]" - Improve docs thanks to Axil --- lib/gitlab/email/receiver.rb | 10 +++++----- lib/gitlab/incoming_email.rb | 16 ++++++++++------ 2 files changed, 15 insertions(+), 11 deletions(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/email/receiver.rb b/lib/gitlab/email/receiver.rb index d55bacde5b0..97ef9851d71 100644 --- a/lib/gitlab/email/receiver.rb +++ b/lib/gitlab/email/receiver.rb @@ -63,10 +63,10 @@ module Gitlab end def reply_key - key_from_to_address || key_from_in_reply_to_header + key_from_to_header || key_from_additional_headers end - def key_from_to_address + def key_from_to_header key = nil message.to.each do |address| key = Gitlab::IncomingEmail.key_from_address(address) @@ -76,11 +76,11 @@ module Gitlab key end - def key_from_in_reply_to_header + def key_from_additional_headers reply_key = nil - message[:in_reply_to].message_ids.each do |message_id| - reply_key = Gitlab::IncomingEmail.key_from_address(message_id) + Array(message.references).each do |message_id| + reply_key = Gitlab::IncomingEmail.key_from_fallback_reply_message_id(message_id) break if reply_key end diff --git a/lib/gitlab/incoming_email.rb b/lib/gitlab/incoming_email.rb index 9068d79c95e..8ce9d32abe0 100644 --- a/lib/gitlab/incoming_email.rb +++ b/lib/gitlab/incoming_email.rb @@ -1,13 +1,10 @@ module Gitlab module IncomingEmail class << self - def enabled? - config.enabled && address_formatted_correctly? - end + FALLBACK_REPLY_MESSAGE_ID_REGEX = /\Areply\-(.+)@#{Gitlab.config.gitlab.host}\Z/.freeze - def address_formatted_correctly? - config.address && - config.address.include?("%{key}") + def enabled? + config.enabled && config.address end def reply_address(key) @@ -24,6 +21,13 @@ module Gitlab match[1] end + def key_from_fallback_reply_message_id(message_id) + match = message_id.match(FALLBACK_REPLY_MESSAGE_ID_REGEX) + return unless match + + match[1] + end + def config Gitlab.config.incoming_email end -- cgit v1.2.1 From 4cd1b9f4d82efe3ffe810dabf6929a749c36c1bf Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 23 Mar 2016 11:24:18 +0100 Subject: Refactor builds badge, encapsulate inside a class --- lib/gitlab/badge/build.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 lib/gitlab/badge/build.rb (limited to 'lib/gitlab') diff --git a/lib/gitlab/badge/build.rb b/lib/gitlab/badge/build.rb new file mode 100644 index 00000000000..28a2391dbf8 --- /dev/null +++ b/lib/gitlab/badge/build.rb @@ -0,0 +1,24 @@ +module Gitlab + module Badge + ## + # Build badge + # + class Build + def initialize(project, ref) + @image = ::Ci::ImageForBuildService.new.execute(project, ref: ref) + end + + def to_s + @image[:name].sub(/\.svg$/, '') + end + + def type + 'image/svg+xml' + end + + def data + File.read(@image[:path]) + end + end + end +end -- cgit v1.2.1 From 701976e0815c273ff4a4c6e4d3489db0ce2f0860 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Thu, 24 Mar 2016 12:28:43 +0100 Subject: Add uploads rewriter and use it when moving issue --- lib/gitlab/gfm/uploads_rewriter.rb | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 lib/gitlab/gfm/uploads_rewriter.rb (limited to 'lib/gitlab') diff --git a/lib/gitlab/gfm/uploads_rewriter.rb b/lib/gitlab/gfm/uploads_rewriter.rb new file mode 100644 index 00000000000..778b6fe9f9d --- /dev/null +++ b/lib/gitlab/gfm/uploads_rewriter.rb @@ -0,0 +1,56 @@ +module Gitlab + module Gfm + ## + # Class that rewrites markdown links for uploads + # + # Using a pattern defined in `FileUploader` copies files to a new project + # and rewrites all links to uploads in ain a given text. + # + class UploadsRewriter + def initialize(text, source_project, _current_user) + @text = text + @source_project = source_project + @pattern = FileUploader::MARKDOWN_PATTERN + end + + def rewrite(target_project) + return unless @text + + new_uploader = file_uploader(target_project) + @text.gsub(@pattern) do |markdown_link| + old_file = find_file(@source_project, $~[:secret], $~[:file]) + return markdown_link unless old_file.exists? + + new_uploader.store!(old_file) + new_uploader.to_h[:markdown] + end + end + + def has_uploads? + !(@text =~ @pattern).nil? + end + + def files + referenced_files = @text.scan(@pattern).map do + find_file(@source_project, $~[:secret], $~[:file]) + end + + referenced_files.compact.select(&:exists?) + end + + private + + def find_file(project, secret, file) + uploader = file_uploader(project, secret) + uploader.retrieve_from_store!(file) + uploader.file + end + + def file_uploader(*args) + uploader = FileUploader.new(*args) + uploader.define_singleton_method(:move_to_store) { false } + uploader + end + end + end +end -- cgit v1.2.1 From f2674c7b98c69668093583e4590223b7040b5b33 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 29 Mar 2016 13:21:57 +0200 Subject: Refactor uploads rewriter used when moving issue --- lib/gitlab/gfm/reference_rewriter.rb | 9 ++++++--- lib/gitlab/gfm/uploads_rewriter.rb | 21 +++++++++++---------- 2 files changed, 17 insertions(+), 13 deletions(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/gfm/reference_rewriter.rb b/lib/gitlab/gfm/reference_rewriter.rb index a1c6ee7bd69..5f906d07177 100644 --- a/lib/gitlab/gfm/reference_rewriter.rb +++ b/lib/gitlab/gfm/reference_rewriter.rb @@ -34,16 +34,19 @@ module Gitlab @source_project = source_project @current_user = current_user @original_html = markdown(text) + @pattern = Gitlab::ReferenceExtractor.references_pattern end def rewrite(target_project) - pattern = Gitlab::ReferenceExtractor.references_pattern - - @text.gsub(pattern) do |reference| + @text.gsub(@pattern) do |reference| unfold_reference(reference, Regexp.last_match, target_project) end end + def needs_rewrite? + !(@text =~ @pattern).nil? + end + private def unfold_reference(reference, match, target_project) diff --git a/lib/gitlab/gfm/uploads_rewriter.rb b/lib/gitlab/gfm/uploads_rewriter.rb index 778b6fe9f9d..5818766c974 100644 --- a/lib/gitlab/gfm/uploads_rewriter.rb +++ b/lib/gitlab/gfm/uploads_rewriter.rb @@ -3,8 +3,9 @@ module Gitlab ## # Class that rewrites markdown links for uploads # - # Using a pattern defined in `FileUploader` copies files to a new project - # and rewrites all links to uploads in ain a given text. + # Using a pattern defined in `FileUploader` it copies files to a new + # project and rewrites all links to uploads in in a given text. + # # class UploadsRewriter def initialize(text, source_project, _current_user) @@ -17,17 +18,17 @@ module Gitlab return unless @text new_uploader = file_uploader(target_project) - @text.gsub(@pattern) do |markdown_link| - old_file = find_file(@source_project, $~[:secret], $~[:file]) - return markdown_link unless old_file.exists? + @text.gsub(@pattern) do |markdown| + file = find_file(@source_project, $~[:secret], $~[:file]) + return markdown unless file.try(:exists?) - new_uploader.store!(old_file) + new_uploader.store!(file) new_uploader.to_h[:markdown] end end - def has_uploads? - !(@text =~ @pattern).nil? + def needs_rewrite? + files.any? end def files @@ -46,8 +47,8 @@ module Gitlab uploader.file end - def file_uploader(*args) - uploader = FileUploader.new(*args) + def file_uploader(project, secret = nil) + uploader = FileUploader.new(project, secret) uploader.define_singleton_method(:move_to_store) { false } uploader end -- cgit v1.2.1 From e64b1e52a23016e51d581b87c08beaa4b18da689 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 30 Mar 2016 10:42:39 +0200 Subject: Check if GFM rewriters need rewrite internally --- lib/gitlab/gfm/reference_rewriter.rb | 2 ++ lib/gitlab/gfm/uploads_rewriter.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/gfm/reference_rewriter.rb b/lib/gitlab/gfm/reference_rewriter.rb index 5f906d07177..47e1aa67976 100644 --- a/lib/gitlab/gfm/reference_rewriter.rb +++ b/lib/gitlab/gfm/reference_rewriter.rb @@ -38,6 +38,8 @@ module Gitlab end def rewrite(target_project) + return @text unless needs_rewrite? + @text.gsub(@pattern) do |reference| unfold_reference(reference, Regexp.last_match, target_project) end diff --git a/lib/gitlab/gfm/uploads_rewriter.rb b/lib/gitlab/gfm/uploads_rewriter.rb index 5818766c974..bdf054a6192 100644 --- a/lib/gitlab/gfm/uploads_rewriter.rb +++ b/lib/gitlab/gfm/uploads_rewriter.rb @@ -15,7 +15,7 @@ module Gitlab end def rewrite(target_project) - return unless @text + return @text unless needs_rewrite? new_uploader = file_uploader(target_project) @text.gsub(@pattern) do |markdown| -- cgit v1.2.1 From 99ee822857cf3fdf0a2ac91c0d13ea68c79e8ba8 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 30 Mar 2016 10:56:25 +0200 Subject: Add method that returns markdown in file uploader --- lib/gitlab/gfm/reference_rewriter.rb | 2 +- lib/gitlab/gfm/uploads_rewriter.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/gfm/reference_rewriter.rb b/lib/gitlab/gfm/reference_rewriter.rb index 47e1aa67976..78d7a4f27cf 100644 --- a/lib/gitlab/gfm/reference_rewriter.rb +++ b/lib/gitlab/gfm/reference_rewriter.rb @@ -46,7 +46,7 @@ module Gitlab end def needs_rewrite? - !(@text =~ @pattern).nil? + @text =~ @pattern end private diff --git a/lib/gitlab/gfm/uploads_rewriter.rb b/lib/gitlab/gfm/uploads_rewriter.rb index bdf054a6192..2e61f799a03 100644 --- a/lib/gitlab/gfm/uploads_rewriter.rb +++ b/lib/gitlab/gfm/uploads_rewriter.rb @@ -23,7 +23,7 @@ module Gitlab return markdown unless file.try(:exists?) new_uploader.store!(file) - new_uploader.to_h[:markdown] + new_uploader.to_markdown end end -- cgit v1.2.1 From b9f57192853d100c90b1d46491838a98d5ae4bae Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 30 Mar 2016 12:11:27 +0200 Subject: Remove reduntant `move_to_store` override --- lib/gitlab/gfm/uploads_rewriter.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/gfm/uploads_rewriter.rb b/lib/gitlab/gfm/uploads_rewriter.rb index 2e61f799a03..abc8c8c55e6 100644 --- a/lib/gitlab/gfm/uploads_rewriter.rb +++ b/lib/gitlab/gfm/uploads_rewriter.rb @@ -17,11 +17,11 @@ module Gitlab def rewrite(target_project) return @text unless needs_rewrite? - new_uploader = file_uploader(target_project) @text.gsub(@pattern) do |markdown| file = find_file(@source_project, $~[:secret], $~[:file]) return markdown unless file.try(:exists?) + new_uploader = FileUploader.new(target_project) new_uploader.store!(file) new_uploader.to_markdown end @@ -42,16 +42,10 @@ module Gitlab private def find_file(project, secret, file) - uploader = file_uploader(project, secret) + uploader = FileUploader.new(project, secret) uploader.retrieve_from_store!(file) uploader.file end - - def file_uploader(project, secret = nil) - uploader = FileUploader.new(project, secret) - uploader.define_singleton_method(:move_to_store) { false } - uploader - end end end end -- cgit v1.2.1 From 2d544d5445d12e45aea1341ab96059ca377484c1 Mon Sep 17 00:00:00 2001 From: James Lopez Date: Wed, 30 Mar 2016 18:48:28 +0200 Subject: spec and fix for fogbugz lonely user problem --- lib/gitlab/fogbugz_import/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/fogbugz_import/client.rb b/lib/gitlab/fogbugz_import/client.rb index 431d50882fd..2152182b37f 100644 --- a/lib/gitlab/fogbugz_import/client.rb +++ b/lib/gitlab/fogbugz_import/client.rb @@ -26,7 +26,7 @@ module Gitlab def user_map users = {} res = @api.command(:listPeople) - res['people']['person'].each do |user| + [res['people']['person']].flatten.each do |user| users[user['ixPerson']] = { name: user['sFullName'], email: user['sEmail'] } end users -- cgit v1.2.1 From 091b8a6ede2515bb555ec8662b9d933d70bda3e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Thu, 31 Mar 2016 09:20:27 +0200 Subject: Rename Note#for_project_snippet? to #for_snippet? MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- lib/gitlab/note_data_builder.rb | 2 +- lib/gitlab/url_builder.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/note_data_builder.rb b/lib/gitlab/note_data_builder.rb index 71cf6a0d886..18523e0aefe 100644 --- a/lib/gitlab/note_data_builder.rb +++ b/lib/gitlab/note_data_builder.rb @@ -41,7 +41,7 @@ module Gitlab data[:issue] = note.noteable.hook_attrs elsif note.for_merge_request? data[:merge_request] = note.noteable.hook_attrs - elsif note.for_project_snippet? + elsif note.for_snippet? data[:snippet] = note.noteable.hook_attrs end diff --git a/lib/gitlab/url_builder.rb b/lib/gitlab/url_builder.rb index 6f0d02cafd1..7486510a4af 100644 --- a/lib/gitlab/url_builder.rb +++ b/lib/gitlab/url_builder.rb @@ -46,7 +46,7 @@ module Gitlab merge_request = MergeRequest.find(note.noteable_id) merge_request_url(merge_request, anchor: "note_#{note.id}") - elsif note.for_project_snippet? + elsif note.for_snippet? snippet = Snippet.find(note.noteable_id) project_snippet_url(snippet, anchor: "note_#{note.id}") -- cgit v1.2.1 From e60f034126712b7e5a3b3ff9c5e92359aaf96e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Thu, 31 Mar 2016 09:21:20 +0200 Subject: Fix view of notes in search results when noteable is a snippet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also, streamline the view. Signed-off-by: Rémy Coutable --- lib/gitlab/url_builder.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/url_builder.rb b/lib/gitlab/url_builder.rb index 7486510a4af..e157bb96f2a 100644 --- a/lib/gitlab/url_builder.rb +++ b/lib/gitlab/url_builder.rb @@ -2,6 +2,7 @@ module Gitlab class UrlBuilder include Gitlab::Application.routes.url_helpers include GitlabRoutingHelper + include ActionView::RecordIdentifier def initialize(type) @type = type @@ -37,19 +38,16 @@ module Gitlab namespace_project_commit_url(namespace_id: note.project.namespace, id: note.commit_id, project_id: note.project, - anchor: "note_#{note.id}") + anchor: dom_id(note)) elsif note.for_issue? issue = Issue.find(note.noteable_id) - issue_url(issue, - anchor: "note_#{note.id}") + issue_url(issue, anchor: dom_id(note)) elsif note.for_merge_request? merge_request = MergeRequest.find(note.noteable_id) - merge_request_url(merge_request, - anchor: "note_#{note.id}") + merge_request_url(merge_request, anchor: dom_id(note)) elsif note.for_snippet? snippet = Snippet.find(note.noteable_id) - project_snippet_url(snippet, - anchor: "note_#{note.id}") + project_snippet_url(snippet, anchor: dom_id(note)) end end end -- cgit v1.2.1 From 85cc1729596ac1e5b31d8cfa1daa07477db6033d Mon Sep 17 00:00:00 2001 From: connorshea Date: Thu, 31 Mar 2016 16:40:39 -0600 Subject: Remove "Congratulations!" tweet button on newly-created project. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I’ve removed everything related to the feature based on this commit: ce08f919f34fd8849834365 Resolves #10857. --- lib/gitlab/current_settings.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/current_settings.rb b/lib/gitlab/current_settings.rb index 761b63e98f6..1acc22fe5bf 100644 --- a/lib/gitlab/current_settings.rb +++ b/lib/gitlab/current_settings.rb @@ -21,7 +21,6 @@ module Gitlab default_branch_protection: Settings.gitlab['default_branch_protection'], signup_enabled: Settings.gitlab['signup_enabled'], signin_enabled: Settings.gitlab['signin_enabled'], - twitter_sharing_enabled: Settings.gitlab['twitter_sharing_enabled'], gravatar_enabled: Settings.gravatar['enabled'], sign_in_text: Settings.extra['sign_in_text'], restricted_visibility_levels: Settings.gitlab['restricted_visibility_levels'], -- cgit v1.2.1 From 84b0ab77667b85a42db8a5a02d9758657af66f16 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 24 Mar 2016 17:00:26 +0100 Subject: Added & use Gitlab::Routing for URL helpers Rails' "url_helpers" method creates an anonymous Module (which a bunch of methods) on every call. By caching the output of this method in a dedicated method we can shave off about 10 seconds of loading time for an issue with around 200 comments. --- lib/gitlab/email/message/repository_push.rb | 2 +- lib/gitlab/routing.rb | 13 +++++++++++++ lib/gitlab/url_builder.rb | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 lib/gitlab/routing.rb (limited to 'lib/gitlab') diff --git a/lib/gitlab/email/message/repository_push.rb b/lib/gitlab/email/message/repository_push.rb index 41f0edcaf7e..8f9be6cd9a3 100644 --- a/lib/gitlab/email/message/repository_push.rb +++ b/lib/gitlab/email/message/repository_push.rb @@ -5,7 +5,7 @@ module Gitlab attr_accessor :recipient attr_reader :author_id, :ref, :action - include Gitlab::Application.routes.url_helpers + include Gitlab::Routing.url_helpers delegate :namespace, :name_with_namespace, to: :project, prefix: :project delegate :name, to: :author, prefix: :author diff --git a/lib/gitlab/routing.rb b/lib/gitlab/routing.rb new file mode 100644 index 00000000000..5132177de51 --- /dev/null +++ b/lib/gitlab/routing.rb @@ -0,0 +1,13 @@ +module Gitlab + module Routing + # Returns the URL helpers Module. + # + # This method caches the output as Rails' "url_helpers" method creates an + # anonymous module every time it's called. + # + # Returns a Module. + def self.url_helpers + @url_helpers ||= Gitlab::Application.routes.url_helpers + end + end +end diff --git a/lib/gitlab/url_builder.rb b/lib/gitlab/url_builder.rb index 6f0d02cafd1..22c91be9207 100644 --- a/lib/gitlab/url_builder.rb +++ b/lib/gitlab/url_builder.rb @@ -1,6 +1,6 @@ module Gitlab class UrlBuilder - include Gitlab::Application.routes.url_helpers + include Gitlab::Routing.url_helpers include GitlabRoutingHelper def initialize(type) -- cgit v1.2.1 From 1a168279fa3eb87c2061917707397af21e7b26ea Mon Sep 17 00:00:00 2001 From: Patricio Cano Date: Mon, 4 Apr 2016 19:09:12 -0500 Subject: Prepare SAML for group retrieval --- lib/gitlab/saml/auth_hash.rb | 17 +++++++++++++++++ lib/gitlab/saml/config.rb | 22 ++++++++++++++++++++++ lib/gitlab/saml/user.rb | 43 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 lib/gitlab/saml/auth_hash.rb create mode 100644 lib/gitlab/saml/config.rb (limited to 'lib/gitlab') diff --git a/lib/gitlab/saml/auth_hash.rb b/lib/gitlab/saml/auth_hash.rb new file mode 100644 index 00000000000..5ffccc0e100 --- /dev/null +++ b/lib/gitlab/saml/auth_hash.rb @@ -0,0 +1,17 @@ +module Gitlab + module Saml + class AuthHash < Gitlab::OAuth::AuthHash + + def groups + get_raw(Gitlab::Saml::Config.groups) + end + + private + + def get_raw(key) + auth_hash.extra[:raw_info][key] + end + + end + end +end diff --git a/lib/gitlab/saml/config.rb b/lib/gitlab/saml/config.rb new file mode 100644 index 00000000000..dade4c0fa6a --- /dev/null +++ b/lib/gitlab/saml/config.rb @@ -0,0 +1,22 @@ +# Load a specific server configuration +module Gitlab + module Saml + class Config + + class << self + def options + Gitlab.config.omniauth.providers.find { |provider| provider.name == 'saml' } + end + + def groups + options['groups_attribute'] + end + + def external_groups + options['external_groups'] + end + end + + end + end +end diff --git a/lib/gitlab/saml/user.rb b/lib/gitlab/saml/user.rb index b1e30110ef5..14eda337d9a 100644 --- a/lib/gitlab/saml/user.rb +++ b/lib/gitlab/saml/user.rb @@ -7,6 +7,11 @@ module Gitlab module Saml class User < Gitlab::OAuth::User + def initialize(auth_hash) + super + update_user_attributes + end + def save super('SAML') end @@ -18,7 +23,7 @@ module Gitlab @user ||= find_or_create_ldap_user end - if auto_link_saml_enabled? + if auto_link_saml_user? @user ||= find_by_email end @@ -37,11 +42,45 @@ module Gitlab end end + def changed? + gl_user.changed? || gl_user.identities.any?(&:changed?) + end + protected - def auto_link_saml_enabled? + def build_new_user + user = super + if external_users_enabled? + unless (auth_hash.groups & Gitlab::Saml::Config.external_groups).empty? + user.external = true + end + end + user + end + + def auto_link_saml_user? Gitlab.config.omniauth.auto_link_saml_user end + + def external_users_enabled? + !Gitlab::Saml::Config.external_groups.nil? + end + + def auth_hash=(auth_hash) + @auth_hash = Gitlab::Saml::AuthHash.new(auth_hash) + end + + def update_user_attributes + if persisted? + if external_users_enabled? + if (auth_hash.groups & Gitlab::Saml::Config.external_groups).empty? + gl_user.external = false + else + gl_user.external = true + end + end + end + end end end end -- cgit v1.2.1 From 5ee6badade3c453c7090e9c1f1f4d636c5bb068e Mon Sep 17 00:00:00 2001 From: Gabriel Mazetto Date: Tue, 5 Apr 2016 16:33:37 -0300 Subject: Unblocks user when active_directory is disabled and it can be found --- lib/gitlab/ldap/access.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/ldap/access.rb b/lib/gitlab/ldap/access.rb index da4435c7308..f2b649e50a2 100644 --- a/lib/gitlab/ldap/access.rb +++ b/lib/gitlab/ldap/access.rb @@ -33,7 +33,10 @@ module Gitlab def allowed? if ldap_user - return true unless ldap_config.active_directory + unless ldap_config.active_directory + user.activate if user.ldap_blocked? + return true + end # Block user in GitLab if he/she was blocked in AD if Gitlab::LDAP::Person.disabled_via_active_directory?(user.ldap_identity.extern_uid, adapter) -- cgit v1.2.1 From 518ec6b2660c55beba2833ce71b93774ed0a6c2a Mon Sep 17 00:00:00 2001 From: Patricio Cano Date: Tue, 5 Apr 2016 19:20:18 -0500 Subject: Changed config syntax and improved how chaanges in group memberships are handled when external groups is set up --- lib/gitlab/saml/config.rb | 4 ++-- lib/gitlab/saml/user.rb | 39 ++++++++++++--------------------------- 2 files changed, 14 insertions(+), 29 deletions(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/saml/config.rb b/lib/gitlab/saml/config.rb index dade4c0fa6a..2b3cf840f61 100644 --- a/lib/gitlab/saml/config.rb +++ b/lib/gitlab/saml/config.rb @@ -9,11 +9,11 @@ module Gitlab end def groups - options['groups_attribute'] + options[:groups_attribute] end def external_groups - options['external_groups'] + options[:external_groups] end end diff --git a/lib/gitlab/saml/user.rb b/lib/gitlab/saml/user.rb index 14eda337d9a..6ab165cf518 100644 --- a/lib/gitlab/saml/user.rb +++ b/lib/gitlab/saml/user.rb @@ -7,11 +7,6 @@ module Gitlab module Saml class User < Gitlab::OAuth::User - def initialize(auth_hash) - super - update_user_attributes - end - def save super('SAML') end @@ -31,6 +26,18 @@ module Gitlab @user ||= build_new_user end + if external_users_enabled? + # Check if there is overlap between the user's groups and the external groups + # setting and set user as external or internal. + if (auth_hash.groups & Gitlab::Saml::Config.external_groups).empty? + # Avoid an unnecessary change of values and the subsequent save + @user.external = false if @user.external + else + # Avoid an unnecessary change of values and the subsequent save + @user.external = true unless @user.external + end + end + @user end @@ -48,16 +55,6 @@ module Gitlab protected - def build_new_user - user = super - if external_users_enabled? - unless (auth_hash.groups & Gitlab::Saml::Config.external_groups).empty? - user.external = true - end - end - user - end - def auto_link_saml_user? Gitlab.config.omniauth.auto_link_saml_user end @@ -69,18 +66,6 @@ module Gitlab def auth_hash=(auth_hash) @auth_hash = Gitlab::Saml::AuthHash.new(auth_hash) end - - def update_user_attributes - if persisted? - if external_users_enabled? - if (auth_hash.groups & Gitlab::Saml::Config.external_groups).empty? - gl_user.external = false - else - gl_user.external = true - end - end - end - end end end end -- cgit v1.2.1 From 1af6cf28c031cec7813d3fc090476c088de57173 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Mon, 4 Apr 2016 14:00:35 +0200 Subject: Measure Ruby blocks using Gitlab::Metrics This allows measuring of timings of arbitrary Ruby blocks, this allows for more fine grained performance monitoring. Custom values and tags can also be attached to a block. --- lib/gitlab/metrics.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'lib/gitlab') diff --git a/lib/gitlab/metrics.rb b/lib/gitlab/metrics.rb index 88a265c6af2..4a3f47b5a95 100644 --- a/lib/gitlab/metrics.rb +++ b/lib/gitlab/metrics.rb @@ -70,6 +70,32 @@ module Gitlab value.to_s.gsub('=', '\\=') end + # Measures the execution time of a block. + # + # Example: + # + # Gitlab::Metrics.measure(:find_by_username_timings) do + # User.find_by_username(some_username) + # end + # + # series - The name of the series to store the data in. + # values - A Hash containing extra values to add to the metric. + # tags - A Hash containing extra tags to add to the metric. + # + # Returns the value yielded by the supplied block. + def self.measure(series, values = {}, tags = {}) + return yield unless Transaction.current + + start = Time.now.to_f + retval = yield + duration = (Time.now.to_f - start) * 1000.0 + values = values.merge(duration: duration) + + Transaction.current.add_metric(series, values, tags) + + retval + end + # When enabled this should be set before being used as the usual pattern # "@foo ||= bar" is _not_ thread-safe. if enabled? -- cgit v1.2.1 From b7fa7c4d59b2fbdc49db81aa2d6a2531c931a2fe Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Fri, 1 Apr 2016 13:03:14 +0200 Subject: Extend build status badge, add html/markdown methods --- lib/gitlab/badge/build.rb | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/badge/build.rb b/lib/gitlab/badge/build.rb index 28a2391dbf8..e5e9fab3f5c 100644 --- a/lib/gitlab/badge/build.rb +++ b/lib/gitlab/badge/build.rb @@ -4,14 +4,15 @@ module Gitlab # Build badge # class Build + include Gitlab::Application.routes.url_helpers + include ActionView::Helpers::AssetTagHelper + include ActionView::Helpers::UrlHelper + def initialize(project, ref) + @project, @ref = project, ref @image = ::Ci::ImageForBuildService.new.execute(project, ref: ref) end - def to_s - @image[:name].sub(/\.svg$/, '') - end - def type 'image/svg+xml' end @@ -19,6 +20,27 @@ module Gitlab def data File.read(@image[:path]) end + + def to_s + @image[:name].sub(/\.svg$/, '') + end + + def to_html + link_to(image_tag(image_url, alt: 'build status'), link_url) + end + + def to_markdown + "[![build status](#{image_url})](#{link_url})" + end + + def image_url + build_namespace_project_badges_url(@project.namespace, + @project, @ref, format: :svg) + end + + def link_url + namespace_project_commits_url(@project.namespace, @project, id: @ref) + end end end end -- cgit v1.2.1 From 3a36fa895724aedb4bd919ec91cc00a24415e712 Mon Sep 17 00:00:00 2001 From: Patricio Cano Date: Wed, 6 Apr 2016 16:03:35 -0500 Subject: Fix error that was causing only one group to be returned and corrected specs to use the proper attribute type --- lib/gitlab/saml/auth_hash.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/saml/auth_hash.rb b/lib/gitlab/saml/auth_hash.rb index 5ffccc0e100..3414d24ca73 100644 --- a/lib/gitlab/saml/auth_hash.rb +++ b/lib/gitlab/saml/auth_hash.rb @@ -9,7 +9,9 @@ module Gitlab private def get_raw(key) - auth_hash.extra[:raw_info][key] + # Needs to call `all` because of https://github.com/onelogin/ruby-saml/blob/master/lib/onelogin/ruby-saml/attributes.rb#L78 + # otherwise just the first value is returned + auth_hash.extra[:raw_info].all[key] end end -- cgit v1.2.1 From eb0f1de36c6ebf13d86540bb79d7fb07fc3642f0 Mon Sep 17 00:00:00 2001 From: Patricio Cano Date: Wed, 6 Apr 2016 16:21:58 -0500 Subject: Added CHANGELOG item --- lib/gitlab/saml/user.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/saml/user.rb b/lib/gitlab/saml/user.rb index 6ab165cf518..73fc443a02b 100644 --- a/lib/gitlab/saml/user.rb +++ b/lib/gitlab/saml/user.rb @@ -33,7 +33,6 @@ module Gitlab # Avoid an unnecessary change of values and the subsequent save @user.external = false if @user.external else - # Avoid an unnecessary change of values and the subsequent save @user.external = true unless @user.external end end -- cgit v1.2.1 From 8110e7530902de8744ff985f08938306e2c38367 Mon Sep 17 00:00:00 2001 From: Patricio Cano Date: Wed, 6 Apr 2016 18:12:25 -0500 Subject: Implemented suggested fixes --- lib/gitlab/saml/auth_hash.rb | 2 +- lib/gitlab/saml/config.rb | 1 - lib/gitlab/saml/user.rb | 7 +++---- 3 files changed, 4 insertions(+), 6 deletions(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/saml/auth_hash.rb b/lib/gitlab/saml/auth_hash.rb index 3414d24ca73..32c1c9ec5bb 100644 --- a/lib/gitlab/saml/auth_hash.rb +++ b/lib/gitlab/saml/auth_hash.rb @@ -9,7 +9,7 @@ module Gitlab private def get_raw(key) - # Needs to call `all` because of https://github.com/onelogin/ruby-saml/blob/master/lib/onelogin/ruby-saml/attributes.rb#L78 + # Needs to call `all` because of https://git.io/vVo4u # otherwise just the first value is returned auth_hash.extra[:raw_info].all[key] end diff --git a/lib/gitlab/saml/config.rb b/lib/gitlab/saml/config.rb index 2b3cf840f61..0f40c00f547 100644 --- a/lib/gitlab/saml/config.rb +++ b/lib/gitlab/saml/config.rb @@ -1,4 +1,3 @@ -# Load a specific server configuration module Gitlab module Saml class Config diff --git a/lib/gitlab/saml/user.rb b/lib/gitlab/saml/user.rb index 73fc443a02b..c1072452abe 100644 --- a/lib/gitlab/saml/user.rb +++ b/lib/gitlab/saml/user.rb @@ -28,12 +28,11 @@ module Gitlab if external_users_enabled? # Check if there is overlap between the user's groups and the external groups - # setting and set user as external or internal. + # setting then set user as external or internal. if (auth_hash.groups & Gitlab::Saml::Config.external_groups).empty? - # Avoid an unnecessary change of values and the subsequent save - @user.external = false if @user.external + @user.external = false else - @user.external = true unless @user.external + @user.external = true end end -- cgit v1.2.1 From aa7cddc4fcd490ccd192d7d04fb67b375705b586 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 8 Apr 2016 16:25:17 +0200 Subject: Use more accurate timestamps for InfluxDB. This changes the timestamp of metrics to be more accurate/unique by using Time#to_f combined with a small random jitter value. This combination hopefully reduces the amount of collisions, though there's no way to fully prevent any from occurring. Fixes gitlab-com/operations#175 --- lib/gitlab/metrics/metric.rb | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'lib/gitlab') diff --git a/lib/gitlab/metrics/metric.rb b/lib/gitlab/metrics/metric.rb index 7ea9555cc8c..1cd1ca30f70 100644 --- a/lib/gitlab/metrics/metric.rb +++ b/lib/gitlab/metrics/metric.rb @@ -2,6 +2,8 @@ module Gitlab module Metrics # Class for storing details of a single metric (label, value, etc). class Metric + JITTER_RANGE = 0.000001..0.001 + attr_reader :series, :values, :tags, :created_at # series - The name of the series (as a String) to store the metric in. @@ -16,11 +18,29 @@ module Gitlab # Returns a Hash in a format that can be directly written to InfluxDB. def to_hash + # InfluxDB overwrites an existing point if a new point has the same + # series, tag set, and timestamp. In a highly concurrent environment + # this means that using the number of seconds since the Unix epoch is + # inevitably going to collide with another timestamp. For example, two + # Rails requests processed by different processes may end up generating + # metrics using the _exact_ same timestamp (in seconds). + # + # Due to the way InfluxDB is set up there's no solution to this problem, + # all we can do is lower the amount of collisions. We do this by using + # Time#to_f which returns the seconds as a Float providing greater + # accuracy. We then add a small random value that is large enough to + # distinguish most timestamps but small enough to not alter the amount + # of seconds. + # + # See https://gitlab.com/gitlab-com/operations/issues/175 for more + # information. + time = @created_at.to_f + rand(JITTER_RANGE) + { series: @series, tags: @tags, values: @values, - timestamp: @created_at.to_i * 1_000_000_000 + timestamp: (time * 1_000_000_000).to_i } end end -- cgit v1.2.1