From c1828eaed56159998d1eaafdaa135f1b3480549b Mon Sep 17 00:00:00 2001 From: Dylan Griffith Date: Mon, 12 Feb 2018 14:22:15 +1100 Subject: Persist external IP of ingress controller created for GKE (#42643) --- app/models/clusters/applications/ingress.rb | 7 +++++++ app/models/clusters/concerns/application_core.rb | 4 ++++ 2 files changed, 11 insertions(+) (limited to 'app/models') diff --git a/app/models/clusters/applications/ingress.rb b/app/models/clusters/applications/ingress.rb index aa5cf97756f..5e9086aecca 100644 --- a/app/models/clusters/applications/ingress.rb +++ b/app/models/clusters/applications/ingress.rb @@ -13,6 +13,8 @@ module Clusters nginx: 1 } + IP_ADDRESS_FETCH_RETRIES = 3 + def chart 'stable/nginx-ingress' end @@ -24,6 +26,11 @@ module Clusters def install_command Gitlab::Kubernetes::Helm::InstallCommand.new(name, chart: chart, chart_values_file: chart_values_file) end + + def post_install + ClusterWaitForIngressIpAddressWorker.perform_in( + ClusterWaitForIngressIpAddressWorker::INTERVAL, name, id, IP_ADDRESS_FETCH_RETRIES) + end end end end diff --git a/app/models/clusters/concerns/application_core.rb b/app/models/clusters/concerns/application_core.rb index a98fa85a5ff..b047fbce214 100644 --- a/app/models/clusters/concerns/application_core.rb +++ b/app/models/clusters/concerns/application_core.rb @@ -23,6 +23,10 @@ module Clusters def name self.class.application_name end + + def post_install + # Override for any extra work that needs to be done after install + end end end end -- cgit v1.2.1 From ba4114d25f538d198df2f681b9cb08567494207e Mon Sep 17 00:00:00 2001 From: Dylan Griffith Date: Tue, 20 Feb 2018 12:42:05 +1100 Subject: Refactor ingress IP address waiting code (#42643) --- app/models/clusters/applications/ingress.rb | 15 ++++++++++----- app/models/clusters/concerns/application_core.rb | 4 ---- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'app/models') diff --git a/app/models/clusters/applications/ingress.rb b/app/models/clusters/applications/ingress.rb index 5e9086aecca..418ce7d1504 100644 --- a/app/models/clusters/applications/ingress.rb +++ b/app/models/clusters/applications/ingress.rb @@ -5,6 +5,7 @@ module Clusters include ::Clusters::Concerns::ApplicationCore include ::Clusters::Concerns::ApplicationStatus + include AfterCommitQueue default_value_for :ingress_type, :nginx default_value_for :version, :nginx @@ -15,6 +16,15 @@ module Clusters IP_ADDRESS_FETCH_RETRIES = 3 + state_machine :status do + before_transition any => [:installed] do |application| + application.run_after_commit do + ClusterWaitForIngressIpAddressWorker.perform_in( + ClusterWaitForIngressIpAddressWorker::INTERVAL, application.name, application.id, IP_ADDRESS_FETCH_RETRIES) + end + end + end + def chart 'stable/nginx-ingress' end @@ -26,11 +36,6 @@ module Clusters def install_command Gitlab::Kubernetes::Helm::InstallCommand.new(name, chart: chart, chart_values_file: chart_values_file) end - - def post_install - ClusterWaitForIngressIpAddressWorker.perform_in( - ClusterWaitForIngressIpAddressWorker::INTERVAL, name, id, IP_ADDRESS_FETCH_RETRIES) - end end end end diff --git a/app/models/clusters/concerns/application_core.rb b/app/models/clusters/concerns/application_core.rb index b047fbce214..a98fa85a5ff 100644 --- a/app/models/clusters/concerns/application_core.rb +++ b/app/models/clusters/concerns/application_core.rb @@ -23,10 +23,6 @@ module Clusters def name self.class.application_name end - - def post_install - # Override for any extra work that needs to be done after install - end end end end -- cgit v1.2.1 From 3d3d09fa9d0ea81f0e20037f64bd43b0cd1e5891 Mon Sep 17 00:00:00 2001 From: Dylan Griffith Date: Tue, 20 Feb 2018 13:49:35 +1100 Subject: Schedule Ingress IP address fetch from K8s after clusters page load (#42643) --- app/models/clusters/applications/ingress.rb | 8 ++++++++ app/models/clusters/concerns/application_core.rb | 5 +++++ 2 files changed, 13 insertions(+) (limited to 'app/models') diff --git a/app/models/clusters/applications/ingress.rb b/app/models/clusters/applications/ingress.rb index 418ce7d1504..e36fe9019c2 100644 --- a/app/models/clusters/applications/ingress.rb +++ b/app/models/clusters/applications/ingress.rb @@ -36,6 +36,14 @@ module Clusters def install_command Gitlab::Kubernetes::Helm::InstallCommand.new(name, chart: chart, chart_values_file: chart_values_file) end + + def sync_details + return unless installed? + return if external_ip + + ClusterWaitForIngressIpAddressWorker.perform_in( + ClusterWaitForIngressIpAddressWorker::INTERVAL, name, id, IP_ADDRESS_FETCH_RETRIES) + end end end end diff --git a/app/models/clusters/concerns/application_core.rb b/app/models/clusters/concerns/application_core.rb index a98fa85a5ff..44b0fca3d01 100644 --- a/app/models/clusters/concerns/application_core.rb +++ b/app/models/clusters/concerns/application_core.rb @@ -23,6 +23,11 @@ module Clusters def name self.class.application_name end + + def sync_details + # Override if you need extra data synchronized + # from K8s after installation + end end end end -- cgit v1.2.1 From 44fed816ee510c0aa56406c9221a0ffb2601d8f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Wed, 14 Feb 2018 23:09:18 +0000 Subject: Do not validate individual Variables when saving Project/Group --- app/models/group.rb | 2 +- app/models/project.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/group.rb b/app/models/group.rb index 75bf013ecd2..45160875dfb 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -26,7 +26,7 @@ class Group < Namespace has_many :shared_projects, through: :project_group_links, source: :project has_many :notification_settings, dependent: :destroy, as: :source # rubocop:disable Cop/ActiveRecordDependent has_many :labels, class_name: 'GroupLabel' - has_many :variables, class_name: 'Ci::GroupVariable' + has_many :variables, class_name: 'Ci::GroupVariable', validate: false has_many :custom_attributes, class_name: 'GroupCustomAttribute' has_many :uploads, as: :model, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent diff --git a/app/models/project.rb b/app/models/project.rb index 4ad6f025e5c..fd09bb6a4d3 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -210,7 +210,7 @@ class Project < ActiveRecord::Base has_many :build_trace_section_names, class_name: 'Ci::BuildTraceSectionName' has_many :runner_projects, class_name: 'Ci::RunnerProject' has_many :runners, through: :runner_projects, source: :runner, class_name: 'Ci::Runner' - has_many :variables, class_name: 'Ci::Variable' + has_many :variables, class_name: 'Ci::Variable', validate: false has_many :triggers, class_name: 'Ci::Trigger' has_many :environments has_many :deployments -- cgit v1.2.1 From 4319b15a78be70ccabb31a25ffba37f77de27b28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Thu, 15 Feb 2018 13:59:11 +0100 Subject: Condition associated variable validation in Project and Group --- app/models/group.rb | 3 ++- app/models/project.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/group.rb b/app/models/group.rb index 45160875dfb..e7640d1c8ec 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -26,7 +26,7 @@ class Group < Namespace has_many :shared_projects, through: :project_group_links, source: :project has_many :notification_settings, dependent: :destroy, as: :source # rubocop:disable Cop/ActiveRecordDependent has_many :labels, class_name: 'GroupLabel' - has_many :variables, class_name: 'Ci::GroupVariable', validate: false + has_many :variables, class_name: 'Ci::GroupVariable' has_many :custom_attributes, class_name: 'GroupCustomAttribute' has_many :uploads, as: :model, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent @@ -37,6 +37,7 @@ class Group < Namespace validate :visibility_level_allowed_by_sub_groups validate :visibility_level_allowed_by_parent validates :variables, variable_duplicates: true + validates_associated :variables, if: proc { |group| group.errors[:variables].nil? } validates :two_factor_grace_period, presence: true, numericality: { greater_than_or_equal_to: 0 } diff --git a/app/models/project.rb b/app/models/project.rb index fd09bb6a4d3..4ad6f025e5c 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -210,7 +210,7 @@ class Project < ActiveRecord::Base has_many :build_trace_section_names, class_name: 'Ci::BuildTraceSectionName' has_many :runner_projects, class_name: 'Ci::RunnerProject' has_many :runners, through: :runner_projects, source: :runner, class_name: 'Ci::Runner' - has_many :variables, class_name: 'Ci::Variable', validate: false + has_many :variables, class_name: 'Ci::Variable' has_many :triggers, class_name: 'Ci::Trigger' has_many :environments has_many :deployments -- cgit v1.2.1 From c65529e8f66bf5367ad2d989a556bf766701d7b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Sun, 18 Feb 2018 21:54:55 +0100 Subject: Skip variables duplicates validator if variable is already a duplicate --- app/models/group.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'app/models') diff --git a/app/models/group.rb b/app/models/group.rb index e7640d1c8ec..75bf013ecd2 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -37,7 +37,6 @@ class Group < Namespace validate :visibility_level_allowed_by_sub_groups validate :visibility_level_allowed_by_parent validates :variables, variable_duplicates: true - validates_associated :variables, if: proc { |group| group.errors[:variables].nil? } validates :two_factor_grace_period, presence: true, numericality: { greater_than_or_equal_to: 0 } -- cgit v1.2.1 From 3b320d675fe058311d921e26cd89b2e703310b21 Mon Sep 17 00:00:00 2001 From: Dylan Griffith Date: Fri, 23 Feb 2018 09:08:12 +1100 Subject: Simplify retrying for ClusterWaitForIngressIpAddressWorker and style changes (#42643) --- app/models/clusters/applications/ingress.rb | 6 +++--- app/models/clusters/concerns/application_core.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'app/models') diff --git a/app/models/clusters/applications/ingress.rb b/app/models/clusters/applications/ingress.rb index e36fe9019c2..57ced0f5c44 100644 --- a/app/models/clusters/applications/ingress.rb +++ b/app/models/clusters/applications/ingress.rb @@ -37,12 +37,12 @@ module Clusters Gitlab::Kubernetes::Helm::InstallCommand.new(name, chart: chart, chart_values_file: chart_values_file) end - def sync_details + def schedule_status_update return unless installed? return if external_ip - ClusterWaitForIngressIpAddressWorker.perform_in( - ClusterWaitForIngressIpAddressWorker::INTERVAL, name, id, IP_ADDRESS_FETCH_RETRIES) + ClusterWaitForIngressIpAddressWorker.perform_async( + name, id, IP_ADDRESS_FETCH_RETRIES) end end end diff --git a/app/models/clusters/concerns/application_core.rb b/app/models/clusters/concerns/application_core.rb index 44b0fca3d01..623b836c0ed 100644 --- a/app/models/clusters/concerns/application_core.rb +++ b/app/models/clusters/concerns/application_core.rb @@ -24,7 +24,7 @@ module Clusters self.class.application_name end - def sync_details + def schedule_status_update # Override if you need extra data synchronized # from K8s after installation end -- cgit v1.2.1 From f2695aa84b68cd905eff90c598e452cbf63d98ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Fri, 23 Feb 2018 01:19:46 +0100 Subject: Include CI Variable Key in its uniqueness validation error message --- app/models/ci/group_variable.rb | 5 ++++- app/models/ci/variable.rb | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/ci/group_variable.rb b/app/models/ci/group_variable.rb index afeae69ba39..1dd0e050ba9 100644 --- a/app/models/ci/group_variable.rb +++ b/app/models/ci/group_variable.rb @@ -6,7 +6,10 @@ module Ci belongs_to :group - validates :key, uniqueness: { scope: :group_id } + validates :key, uniqueness: { + scope: :group_id, + message: "(%{value}) has already been taken" + } scope :unprotected, -> { where(protected: false) } end diff --git a/app/models/ci/variable.rb b/app/models/ci/variable.rb index 67d3ec81b6f..7c71291de84 100644 --- a/app/models/ci/variable.rb +++ b/app/models/ci/variable.rb @@ -6,7 +6,10 @@ module Ci belongs_to :project - validates :key, uniqueness: { scope: [:project_id, :environment_scope] } + validates :key, uniqueness: { + scope: [:project_id, :environment_scope], + message: "(%{value}) has already been taken" + } scope :unprotected, -> { where(protected: false) } end -- cgit v1.2.1 From 442a6e880058138b6ae6843d9b70d62cbc5aadb0 Mon Sep 17 00:00:00 2001 From: Tiago Botelho Date: Fri, 16 Feb 2018 16:39:23 +0000 Subject: API method /projects/:id/repository/commits now works over every commit --- app/models/repository.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/repository.rb b/app/models/repository.rb index 299a3f32a85..7888c1019e6 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -139,7 +139,7 @@ class Repository end end - def commits(ref, path: nil, limit: nil, offset: nil, skip_merges: false, after: nil, before: nil) + def commits(ref = nil, path: nil, limit: nil, offset: nil, skip_merges: false, after: nil, before: nil, all: nil) options = { repo: raw_repository, ref: ref, @@ -149,7 +149,8 @@ class Repository after: after, before: before, follow: Array(path).length == 1, - skip_merges: skip_merges + skip_merges: skip_merges, + all: all } commits = Gitlab::Git::Commit.where(options) -- cgit v1.2.1 From 57719d34d3fcc15f39354b0e9dc1da41bbe5d1a8 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 22 Feb 2018 18:34:04 +0100 Subject: Expose ChatName objects to slash commands Instead of only exposing a User to slash commands we now also expose the ChatName object that the User object is retrieved from. This is necessary for GitLab Chatops as we need for example the user ID of the chat user. --- app/models/chat_name.rb | 21 +++++++++++++++++++++ .../project_services/slash_commands_service.rb | 6 +++--- 2 files changed, 24 insertions(+), 3 deletions(-) (limited to 'app/models') diff --git a/app/models/chat_name.rb b/app/models/chat_name.rb index f321db75eeb..fbd0f123341 100644 --- a/app/models/chat_name.rb +++ b/app/models/chat_name.rb @@ -1,4 +1,6 @@ class ChatName < ActiveRecord::Base + LAST_USED_AT_INTERVAL = 1.hour + belongs_to :service belongs_to :user @@ -9,4 +11,23 @@ class ChatName < ActiveRecord::Base validates :user_id, uniqueness: { scope: [:service_id] } validates :chat_id, uniqueness: { scope: [:service_id, :team_id] } + + # Updates the "last_used_timestamp" but only if it wasn't already updated + # recently. + # + # The throttling this method uses is put in place to ensure that high chat + # traffic doesn't result in many UPDATE queries being performed. + def update_last_used_at + return unless update_last_used_at? + + obtained = Gitlab::ExclusiveLease + .new("chat_name/last_used_at/#{id}", timeout: LAST_USED_AT_INTERVAL.to_i) + .try_obtain + + touch(:last_used_at) if obtained + end + + def update_last_used_at? + last_used_at.nil? || last_used_at > LAST_USED_AT_INTERVAL.ago + end end diff --git a/app/models/project_services/slash_commands_service.rb b/app/models/project_services/slash_commands_service.rb index eb4da68bb7e..37ea45109ae 100644 --- a/app/models/project_services/slash_commands_service.rb +++ b/app/models/project_services/slash_commands_service.rb @@ -30,10 +30,10 @@ class SlashCommandsService < Service def trigger(params) return unless valid_token?(params[:token]) - user = find_chat_user(params) + chat_user = find_chat_user(params) - if user - Gitlab::SlashCommands::Command.new(project, user, params).execute + if chat_user&.user + Gitlab::SlashCommands::Command.new(project, chat_user, params).execute else url = authorize_chat_name_url(params) Gitlab::SlashCommands::Presenters::Access.new(url).authorize -- cgit v1.2.1 From 233cf07083faabcc11b2fa8de04123156d712efb Mon Sep 17 00:00:00 2001 From: Dylan Griffith Date: Sun, 25 Feb 2018 12:46:16 +1100 Subject: Remove redundant code and increase delay for ClusterWaitForIngressIpAddressWorker (#42643) --- app/models/clusters/applications/ingress.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'app/models') diff --git a/app/models/clusters/applications/ingress.rb b/app/models/clusters/applications/ingress.rb index 57ced0f5c44..9f583342c19 100644 --- a/app/models/clusters/applications/ingress.rb +++ b/app/models/clusters/applications/ingress.rb @@ -14,13 +14,13 @@ module Clusters nginx: 1 } - IP_ADDRESS_FETCH_RETRIES = 3 + FETCH_IP_ADDRESS_DELAY = 30.seconds state_machine :status do before_transition any => [:installed] do |application| application.run_after_commit do ClusterWaitForIngressIpAddressWorker.perform_in( - ClusterWaitForIngressIpAddressWorker::INTERVAL, application.name, application.id, IP_ADDRESS_FETCH_RETRIES) + FETCH_IP_ADDRESS_DELAY, application.name, application.id) end end end @@ -41,8 +41,7 @@ module Clusters return unless installed? return if external_ip - ClusterWaitForIngressIpAddressWorker.perform_async( - name, id, IP_ADDRESS_FETCH_RETRIES) + ClusterWaitForIngressIpAddressWorker.perform_async(name, id) end end end -- cgit v1.2.1 From df55efda34dafcd85c20ff307ecb20ce1190741a Mon Sep 17 00:00:00 2001 From: Andreas Brandl Date: Thu, 22 Feb 2018 11:12:28 +0100 Subject: Simplify filtering of non-human users (like ghosts). Note that `ghost IS NOT TRUE` is equivalent to `ghost = 'f' OR ghost is null`: (true), (false), (NULL)) AS t (flag); flag | ?column? | ?column? ------+----------+---------- t | f | f f | t | t | t | t (3 rows) This makes it a little easier to create partial indexes for this (as only one condition is needed). Closes #43304. --- app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models') diff --git a/app/models/user.rb b/app/models/user.rb index f5eeba27572..0401a244566 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -431,7 +431,7 @@ class User < ActiveRecord::Base end def self.non_internal - where(Hash[internal_attributes.zip([[false, nil]] * internal_attributes.size)]) + where(internal_attributes.map { |attr| "#{attr} IS NOT TRUE" }.join(" AND ")) end # -- cgit v1.2.1 From c370f53cb68038b469ec219cf2ec248e62a72683 Mon Sep 17 00:00:00 2001 From: Ahmad Sherif Date: Fri, 16 Feb 2018 21:39:43 +0100 Subject: Migrate recursive tree entries fetching to Gitaly --- app/models/tree.rb | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) (limited to 'app/models') diff --git a/app/models/tree.rb b/app/models/tree.rb index c89b8eca9be..4c1856b67a8 100644 --- a/app/models/tree.rb +++ b/app/models/tree.rb @@ -9,10 +9,9 @@ class Tree @repository = repository @sha = sha @path = path - @recursive = recursive git_repo = @repository.raw_repository - @entries = get_entries(git_repo, @sha, @path, recursive: @recursive) + @entries = Gitlab::Git::Tree.where(git_repo, @sha, @path, recursive) end def readme @@ -58,21 +57,4 @@ class Tree def sorted_entries trees + blobs + submodules end - - private - - def get_entries(git_repo, sha, path, recursive: false) - current_path_entries = Gitlab::Git::Tree.where(git_repo, sha, path) - ordered_entries = [] - - current_path_entries.each do |entry| - ordered_entries << entry - - if recursive && entry.dir? - ordered_entries.concat(get_entries(git_repo, sha, entry.path, recursive: true)) - end - end - - ordered_entries - end end -- cgit v1.2.1 From bf41063679b25371b2e64542f2f469b38502edf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Wed, 11 Oct 2017 16:47:08 +0200 Subject: Remove explicit audit event log in MembershipActions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move it to Members::ApproveAccessRequestService. Also, note that there was a double audit event log for access request destruction. Signed-off-by: Rémy Coutable --- app/models/member.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/member.rb b/app/models/member.rb index 2d17795e62d..ba040bbeff0 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -145,9 +145,8 @@ class Member < ActiveRecord::Base ::Members::ApproveAccessRequestService.new( source, current_user, - id: member.id, access_level: access_level - ).execute + ).execute(member) else member.save end -- cgit v1.2.1 From 1c88d92b3fe174a56080575a14d6b473f17f7d8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Fri, 16 Feb 2018 15:10:22 +0100 Subject: Improve Member services MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- app/models/concerns/access_requestable.rb | 2 +- app/models/member.rb | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'app/models') diff --git a/app/models/concerns/access_requestable.rb b/app/models/concerns/access_requestable.rb index 62bc6b809f4..d502e7e54c6 100644 --- a/app/models/concerns/access_requestable.rb +++ b/app/models/concerns/access_requestable.rb @@ -8,6 +8,6 @@ module AccessRequestable extend ActiveSupport::Concern def request_access(user) - Members::RequestAccessService.new(self, user).execute + Members::RequestAccessService.new(user).execute(self) end end diff --git a/app/models/member.rb b/app/models/member.rb index ba040bbeff0..408e8b2d704 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -128,7 +128,7 @@ class Member < ActiveRecord::Base find_by(invite_token: invite_token) end - def add_user(source, user, access_level, existing_members: nil, current_user: nil, expires_at: nil) + def add_user(source, user, access_level, existing_members: nil, current_user: nil, expires_at: nil, ldap: false) # `user` can be either a User object, User ID or an email to be invited member = retrieve_member(source, user, existing_members) access_level = retrieve_access_level(access_level) @@ -143,10 +143,13 @@ class Member < ActiveRecord::Base if member.request? ::Members::ApproveAccessRequestService.new( - source, current_user, access_level: access_level - ).execute(member) + ).execute( + member, + skip_authorization: ldap, + skip_log_audit_event: ldap + ) else member.save end -- cgit v1.2.1 From 059ab73b8eae3a546d0a19fe99ef0c52df5fac01 Mon Sep 17 00:00:00 2001 From: Fatih Acet Date: Wed, 28 Feb 2018 00:10:43 +0000 Subject: Render MR Notes with Vue with behind a cookie --- app/models/note.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'app/models') diff --git a/app/models/note.rb b/app/models/note.rb index cac60845a49..d7a67ec277c 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -133,6 +133,7 @@ class Note < ActiveRecord::Base def find_discussion(discussion_id) notes = where(discussion_id: discussion_id).fresh.to_a + return if notes.empty? Discussion.build(notes) -- cgit v1.2.1 From b5a5fdf0e3b97622789db444bf0bf697e78dbb47 Mon Sep 17 00:00:00 2001 From: Dylan Griffith Date: Thu, 22 Feb 2018 17:03:00 +1100 Subject: Persist runner IP address on contact (#43489) --- app/models/ci/runner.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/ci/runner.rb b/app/models/ci/runner.rb index 13c784bea0d..609620a62bb 100644 --- a/app/models/ci/runner.rb +++ b/app/models/ci/runner.rb @@ -49,7 +49,7 @@ module Ci ref_protected: 1 } - cached_attr_reader :version, :revision, :platform, :architecture, :contacted_at + cached_attr_reader :version, :revision, :platform, :architecture, :contacted_at, :ip_address # Searches for runners matching the given query. # @@ -157,7 +157,7 @@ module Ci end def update_cached_info(values) - values = values&.slice(:version, :revision, :platform, :architecture) || {} + values = values&.slice(:version, :revision, :platform, :architecture, :ip_address) || {} values[:contacted_at] = Time.now cache_attributes(values) -- cgit v1.2.1 From a7b3f11edd626b7ba2fe4101951d2c9fe884b04a Mon Sep 17 00:00:00 2001 From: Tiago Botelho Date: Tue, 30 Jan 2018 11:28:15 +0000 Subject: Adds get all medians to Cycle Analytics model --- app/models/cycle_analytics.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'app/models') diff --git a/app/models/cycle_analytics.rb b/app/models/cycle_analytics.rb index d2e626c22e8..a6191e569a7 100644 --- a/app/models/cycle_analytics.rb +++ b/app/models/cycle_analytics.rb @@ -6,6 +6,12 @@ class CycleAnalytics @options = options end + def self.all_medians_per_stage(projects, options) + STAGES.each_with_object({}) do |stage_name, hsh| + hsh[stage_name] = Gitlab::CycleAnalytics::Stage[stage_name].new(projects: projects, options: options).medians&.values || [] + end + end + def summary @summary ||= ::Gitlab::CycleAnalytics::StageSummary.new(@project, from: @options[:from], -- cgit v1.2.1 From 7c109c575016eb0596e20180b334ca2955bd33e6 Mon Sep 17 00:00:00 2001 From: Tiago Botelho Date: Fri, 9 Feb 2018 14:10:58 +0000 Subject: Refactor CycleAnalytics base stage class and model --- app/models/cycle_analytics.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models') diff --git a/app/models/cycle_analytics.rb b/app/models/cycle_analytics.rb index a6191e569a7..c7f0e5f5cd8 100644 --- a/app/models/cycle_analytics.rb +++ b/app/models/cycle_analytics.rb @@ -31,7 +31,7 @@ class CycleAnalytics end def [](stage_name) - Gitlab::CycleAnalytics::Stage[stage_name].new(project: @project, options: @options) + Gitlab::CycleAnalytics::Stage[stage_name].new(projects: [@project], options: @options) end private -- cgit v1.2.1 From 4fcbcce36475067f4d72ebe9371634e31073a497 Mon Sep 17 00:00:00 2001 From: Tiago Botelho Date: Mon, 12 Feb 2018 12:24:42 +0000 Subject: Add BatchLoader as a way to refactor the base stage code --- app/models/cycle_analytics.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/models') diff --git a/app/models/cycle_analytics.rb b/app/models/cycle_analytics.rb index c7f0e5f5cd8..52eb07ae7d6 100644 --- a/app/models/cycle_analytics.rb +++ b/app/models/cycle_analytics.rb @@ -6,9 +6,9 @@ class CycleAnalytics @options = options end - def self.all_medians_per_stage(projects, options) + def all_medians_per_stage STAGES.each_with_object({}) do |stage_name, hsh| - hsh[stage_name] = Gitlab::CycleAnalytics::Stage[stage_name].new(projects: projects, options: options).medians&.values || [] + hsh[stage_name] = self[stage_name].median end end @@ -31,7 +31,7 @@ class CycleAnalytics end def [](stage_name) - Gitlab::CycleAnalytics::Stage[stage_name].new(projects: [@project], options: @options) + Gitlab::CycleAnalytics::Stage[stage_name].new(project: @project, options: @options) end private -- cgit v1.2.1 From 41bb23ae1d61f1e94749a288d822c93b430ee9c8 Mon Sep 17 00:00:00 2001 From: Tiago Botelho Date: Tue, 13 Feb 2018 15:42:49 +0000 Subject: Restore memoization to base query and add a batch base query method --- app/models/cycle_analytics.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/cycle_analytics.rb b/app/models/cycle_analytics.rb index 52eb07ae7d6..8a57298f824 100644 --- a/app/models/cycle_analytics.rb +++ b/app/models/cycle_analytics.rb @@ -7,8 +7,13 @@ class CycleAnalytics end def all_medians_per_stage - STAGES.each_with_object({}) do |stage_name, hsh| - hsh[stage_name] = self[stage_name].median + medians_per_stage = {} + + # We only need this data for Postgres instances + return medians_per_stage if Gitlab::Database.mysql? + + STAGES.each do |stage_name| + medians_per_stage[stage_name] = self[stage_name].median end end -- cgit v1.2.1 From 3f31da9c69c550d1698a1376e37d36f4e6e309b5 Mon Sep 17 00:00:00 2001 From: Tiago Botelho Date: Tue, 13 Feb 2018 16:52:00 +0000 Subject: Restrict cycle analytics usage data to instances that use postgres only --- app/models/cycle_analytics.rb | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'app/models') diff --git a/app/models/cycle_analytics.rb b/app/models/cycle_analytics.rb index 8a57298f824..52eb07ae7d6 100644 --- a/app/models/cycle_analytics.rb +++ b/app/models/cycle_analytics.rb @@ -7,13 +7,8 @@ class CycleAnalytics end def all_medians_per_stage - medians_per_stage = {} - - # We only need this data for Postgres instances - return medians_per_stage if Gitlab::Database.mysql? - - STAGES.each do |stage_name| - medians_per_stage[stage_name] = self[stage_name].median + STAGES.each_with_object({}) do |stage_name, hsh| + hsh[stage_name] = self[stage_name].median end end -- cgit v1.2.1 From 335ee79a73fafdf00fac6e8ffc286ce4bad273ff Mon Sep 17 00:00:00 2001 From: Tiago Botelho Date: Thu, 15 Feb 2018 13:23:39 +0000 Subject: Refactors median code to work with both single and multiple projects --- app/models/cycle_analytics.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/cycle_analytics.rb b/app/models/cycle_analytics.rb index 52eb07ae7d6..b34d1382d43 100644 --- a/app/models/cycle_analytics.rb +++ b/app/models/cycle_analytics.rb @@ -7,8 +7,8 @@ class CycleAnalytics end def all_medians_per_stage - STAGES.each_with_object({}) do |stage_name, hsh| - hsh[stage_name] = self[stage_name].median + STAGES.each_with_object({}) do |stage_name, medians_per_stage| + medians_per_stage[stage_name] = self[stage_name].median end end -- cgit v1.2.1 From 1ad5df49b1925f1865e99c3fd8576a762aea9cae Mon Sep 17 00:00:00 2001 From: Horatiu Eugen Vlad Date: Fri, 23 Feb 2018 13:10:39 +0100 Subject: Moved o_auth/saml/ldap modules under gitlab/auth --- app/models/identity.rb | 6 +++--- app/models/user.rb | 2 +- app/models/user_synced_attributes_metadata.rb | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'app/models') diff --git a/app/models/identity.rb b/app/models/identity.rb index 2b433e9b988..1011b9f1109 100644 --- a/app/models/identity.rb +++ b/app/models/identity.rb @@ -17,12 +17,12 @@ class Identity < ActiveRecord::Base end def ldap? - Gitlab::OAuth::Provider.ldap_provider?(provider) + Gitlab::Auth::OAuth::Provider.ldap_provider?(provider) end def self.normalize_uid(provider, uid) - if Gitlab::OAuth::Provider.ldap_provider?(provider) - Gitlab::LDAP::Person.normalize_dn(uid) + if Gitlab::Auth::OAuth::Provider.ldap_provider?(provider) + Gitlab::Auth::LDAP::Person.normalize_dn(uid) else uid.to_s end diff --git a/app/models/user.rb b/app/models/user.rb index 8610ca27b7f..8e881e53706 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -728,7 +728,7 @@ class User < ActiveRecord::Base def ldap_user? if identities.loaded? - identities.find { |identity| Gitlab::OAuth::Provider.ldap_provider?(identity.provider) && !identity.extern_uid.nil? } + identities.find { |identity| Gitlab::Auth::OAuth::Provider.ldap_provider?(identity.provider) && !identity.extern_uid.nil? } else identities.exists?(["provider LIKE ? AND extern_uid IS NOT NULL", "ldap%"]) end diff --git a/app/models/user_synced_attributes_metadata.rb b/app/models/user_synced_attributes_metadata.rb index 548b99b69d9..688432a9d67 100644 --- a/app/models/user_synced_attributes_metadata.rb +++ b/app/models/user_synced_attributes_metadata.rb @@ -26,6 +26,6 @@ class UserSyncedAttributesMetadata < ActiveRecord::Base private def sync_profile_from_provider? - Gitlab::OAuth::Provider.sync_profile_from_provider?(provider) + Gitlab::Auth::OAuth::Provider.sync_profile_from_provider?(provider) end end -- cgit v1.2.1 From 4f2cdb51df0f2729055ec4dc6960ae347163da16 Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Thu, 1 Mar 2018 12:39:30 +0100 Subject: Allow CI/CD Jobs being grouped on version strings This makes grouping on version number available. In general I'd like to group based on MAJOR.MINOR version numbers for Gitaly. --- app/models/commit_status.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models') diff --git a/app/models/commit_status.rb b/app/models/commit_status.rb index 3469d5d795c..9fb5b7efec6 100644 --- a/app/models/commit_status.rb +++ b/app/models/commit_status.rb @@ -141,7 +141,7 @@ class CommitStatus < ActiveRecord::Base end def group_name - name.to_s.gsub(%r{\d+[\s:/\\]+\d+\s*}, '').strip + name.to_s.gsub(%r{\d+[\.\s:/\\]+\d+\s*}, '').strip end def failed_but_allowed? -- cgit v1.2.1 From 34fbade4ae67b56061da89fc881781352880b4ed Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Thu, 1 Mar 2018 15:30:41 +0100 Subject: Client implementation for Licensee#key Part of the migration as tracked in: gitlab-org/gitaly#1026 --- app/models/repository.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'app/models') diff --git a/app/models/repository.rb b/app/models/repository.rb index 299a3f32a85..fd881bea612 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -589,15 +589,7 @@ class Repository def license_key return unless exists? - # The licensee gem creates a Rugged object from the path: - # https://github.com/benbalter/licensee/blob/v8.7.0/lib/licensee/projects/git_project.rb - begin - Licensee.license(path).try(:key) - # Normally we would rescue Rugged::Error, but that is banned by lint-rugged - # and we need to migrate this endpoint to Gitaly: - # https://gitlab.com/gitlab-org/gitaly/issues/1026 - rescue - end + raw_repository.license_short_name end cache_method :license_key -- cgit v1.2.1 From 03f3350f3f679122ce4ceacfc840db6e77763dea Mon Sep 17 00:00:00 2001 From: Ahmad Sherif Date: Wed, 28 Feb 2018 17:56:00 +0100 Subject: Fetch commit signatures from Gitaly in batches Closes gitaly#1046 --- app/models/commit.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'app/models') diff --git a/app/models/commit.rb b/app/models/commit.rb index add5fcf0e79..b9106309142 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -19,6 +19,7 @@ class Commit attr_accessor :project, :author attr_accessor :redacted_description_html attr_accessor :redacted_title_html + attr_reader :gpg_commit DIFF_SAFE_LINES = Gitlab::Git::DiffCollection::DEFAULT_LIMITS[:max_lines] @@ -110,6 +111,7 @@ class Commit @raw = raw_commit @project = project @statuses = {} + @gpg_commit = Gitlab::Gpg::Commit.new(self) if project end def id @@ -452,8 +454,4 @@ class Commit def merged_merge_request_no_cache(user) MergeRequestsFinder.new(user, project_id: project.id).find_by(merge_commit_sha: id) if merge_commit? end - - def gpg_commit - @gpg_commit ||= Gitlab::Gpg::Commit.new(self) - end end -- cgit v1.2.1 From c607008ee55e35465e04a938a341f2f24cb6761f Mon Sep 17 00:00:00 2001 From: Mayra Cabrera Date: Thu, 1 Mar 2018 23:46:02 +0000 Subject: Extend Cluster Applications to install GitLab Runner to Kubernetes cluster --- app/models/clusters/applications/helm.rb | 2 +- app/models/clusters/applications/ingress.rb | 11 ++-- app/models/clusters/applications/prometheus.rb | 11 ++-- app/models/clusters/applications/runner.rb | 68 ++++++++++++++++++++++++ app/models/clusters/cluster.rb | 7 ++- app/models/clusters/concerns/application_data.rb | 23 ++++++++ 6 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 app/models/clusters/applications/runner.rb create mode 100644 app/models/clusters/concerns/application_data.rb (limited to 'app/models') diff --git a/app/models/clusters/applications/helm.rb b/app/models/clusters/applications/helm.rb index 193bb48e54d..58de3448577 100644 --- a/app/models/clusters/applications/helm.rb +++ b/app/models/clusters/applications/helm.rb @@ -15,7 +15,7 @@ module Clusters end def install_command - Gitlab::Kubernetes::Helm::InstallCommand.new(name, install_helm: true) + Gitlab::Kubernetes::Helm::InitCommand.new(name) end end end diff --git a/app/models/clusters/applications/ingress.rb b/app/models/clusters/applications/ingress.rb index 9f583342c19..27fc3b85465 100644 --- a/app/models/clusters/applications/ingress.rb +++ b/app/models/clusters/applications/ingress.rb @@ -5,6 +5,7 @@ module Clusters include ::Clusters::Concerns::ApplicationCore include ::Clusters::Concerns::ApplicationStatus + include ::Clusters::Concerns::ApplicationData include AfterCommitQueue default_value_for :ingress_type, :nginx @@ -29,12 +30,12 @@ module Clusters 'stable/nginx-ingress' end - def chart_values_file - "#{Rails.root}/vendor/#{name}/values.yaml" - end - def install_command - Gitlab::Kubernetes::Helm::InstallCommand.new(name, chart: chart, chart_values_file: chart_values_file) + Gitlab::Kubernetes::Helm::InstallCommand.new( + name, + chart: chart, + values: values + ) end def schedule_status_update diff --git a/app/models/clusters/applications/prometheus.rb b/app/models/clusters/applications/prometheus.rb index aa22e9d5d58..89ebd63e605 100644 --- a/app/models/clusters/applications/prometheus.rb +++ b/app/models/clusters/applications/prometheus.rb @@ -7,6 +7,7 @@ module Clusters include ::Clusters::Concerns::ApplicationCore include ::Clusters::Concerns::ApplicationStatus + include ::Clusters::Concerns::ApplicationData default_value_for :version, VERSION @@ -30,12 +31,12 @@ module Clusters 80 end - def chart_values_file - "#{Rails.root}/vendor/#{name}/values.yaml" - end - def install_command - Gitlab::Kubernetes::Helm::InstallCommand.new(name, chart: chart, chart_values_file: chart_values_file) + Gitlab::Kubernetes::Helm::InstallCommand.new( + name, + chart: chart, + values: values + ) end def proxy_client diff --git a/app/models/clusters/applications/runner.rb b/app/models/clusters/applications/runner.rb new file mode 100644 index 00000000000..7adf1663c35 --- /dev/null +++ b/app/models/clusters/applications/runner.rb @@ -0,0 +1,68 @@ +module Clusters + module Applications + class Runner < ActiveRecord::Base + VERSION = '0.1.13'.freeze + + self.table_name = 'clusters_applications_runners' + + include ::Clusters::Concerns::ApplicationCore + include ::Clusters::Concerns::ApplicationStatus + include ::Clusters::Concerns::ApplicationData + + belongs_to :runner, class_name: 'Ci::Runner', foreign_key: :runner_id + delegate :project, to: :cluster + + default_value_for :version, VERSION + + def chart + "#{name}/gitlab-runner" + end + + def repository + 'https://charts.gitlab.io' + end + + def values + content_values.to_yaml + end + + def install_command + Gitlab::Kubernetes::Helm::InstallCommand.new( + name, + chart: chart, + values: values, + repository: repository + ) + end + + private + + def ensure_runner + runner || create_and_assign_runner + end + + def create_and_assign_runner + transaction do + project.runners.create!(name: 'kubernetes-cluster', tag_list: %w(kubernetes cluster)).tap do |runner| + update!(runner_id: runner.id) + end + end + end + + def gitlab_url + Gitlab::Routing.url_helpers.root_url(only_path: false) + end + + def specification + { + "gitlabUrl" => gitlab_url, + "runnerToken" => ensure_runner.token + } + end + + def content_values + specification.merge(YAML.load_file(chart_values_file)) + end + end + end +end diff --git a/app/models/clusters/cluster.rb b/app/models/clusters/cluster.rb index 8678f70f78c..1c0046107d7 100644 --- a/app/models/clusters/cluster.rb +++ b/app/models/clusters/cluster.rb @@ -7,7 +7,8 @@ module Clusters APPLICATIONS = { Applications::Helm.application_name => Applications::Helm, Applications::Ingress.application_name => Applications::Ingress, - Applications::Prometheus.application_name => Applications::Prometheus + Applications::Prometheus.application_name => Applications::Prometheus, + Applications::Runner.application_name => Applications::Runner }.freeze belongs_to :user @@ -23,6 +24,7 @@ module Clusters has_one :application_helm, class_name: 'Clusters::Applications::Helm' has_one :application_ingress, class_name: 'Clusters::Applications::Ingress' has_one :application_prometheus, class_name: 'Clusters::Applications::Prometheus' + has_one :application_runner, class_name: 'Clusters::Applications::Runner' accepts_nested_attributes_for :provider_gcp, update_only: true accepts_nested_attributes_for :platform_kubernetes, update_only: true @@ -68,7 +70,8 @@ module Clusters [ application_helm || build_application_helm, application_ingress || build_application_ingress, - application_prometheus || build_application_prometheus + application_prometheus || build_application_prometheus, + application_runner || build_application_runner ] end diff --git a/app/models/clusters/concerns/application_data.rb b/app/models/clusters/concerns/application_data.rb new file mode 100644 index 00000000000..96ac757e99e --- /dev/null +++ b/app/models/clusters/concerns/application_data.rb @@ -0,0 +1,23 @@ +module Clusters + module Concerns + module ApplicationData + extend ActiveSupport::Concern + + included do + def repository + nil + end + + def values + File.read(chart_values_file) + end + + private + + def chart_values_file + "#{Rails.root}/vendor/#{name}/values.yaml" + end + end + end + end +end -- cgit v1.2.1 From 82ec8eafab2aa66eaf6fe7c9bc6a25bfbb291596 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 1 Mar 2018 17:06:42 +0100 Subject: Revert Project.public_or_visible_to_user changes These changes were introduced in MR https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/17088. In https://gitlab.com/gitlab-com/infrastructure/issues/3772 we discovered these changes lead to a pretty drastic increase in SQL response timings. We'll revert these changes so we can work on a better solution in the mean time without GitLab.com (or other installations) experiecing reduced performance in the mean time. --- app/models/project.rb | 46 +++++++++++----------------------------------- 1 file changed, 11 insertions(+), 35 deletions(-) (limited to 'app/models') diff --git a/app/models/project.rb b/app/models/project.rb index ba278a49688..ad4315e1601 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -317,42 +317,18 @@ class Project < ActiveRecord::Base # Returns a collection of projects that is either public or visible to the # logged in user. - # - # A caller may pass in a block to modify individual parts of - # the query, e.g. to apply .with_feature_available_for_user on top of it. - # This is useful for performance as we can stick those additional filters - # at the bottom of e.g. the UNION. - # - # Optionally, turning `use_where_in` off leads to returning a - # relation using #from instead of #where. This can perform much better - # but leads to trouble when used in conjunction with AR's #merge method. - def self.public_or_visible_to_user(user = nil, use_where_in: true, &block) - # If we don't get a block passed, use identity to avoid if/else repetitions - block = ->(part) { part } unless block_given? - - return block.call(public_to_user) unless user - - # If the user is allowed to see all projects, - # we can shortcut and just return. - return block.call(all) if user.full_private_access? - - authorized = user - .project_authorizations - .select(1) - .where('project_authorizations.project_id = projects.id') - authorized_projects = block.call(where('EXISTS (?)', authorized)) - - levels = Gitlab::VisibilityLevel.levels_for_user(user) - visible_projects = block.call(where(visibility_level: levels)) - - # We use a UNION here instead of OR clauses since this results in better - # performance. - union = Gitlab::SQL::Union.new([authorized_projects.select('projects.id'), visible_projects.select('projects.id')]) - - if use_where_in - where("projects.id IN (#{union.to_sql})") # rubocop:disable GitlabSecurity/SqlInjection + def self.public_or_visible_to_user(user = nil) + if user + authorized = user + .project_authorizations + .select(1) + .where('project_authorizations.project_id = projects.id') + + levels = Gitlab::VisibilityLevel.levels_for_user(user) + + where('EXISTS (?) OR projects.visibility_level IN (?)', authorized, levels) else - from("(#{union.to_sql}) AS #{table_name}") + public_to_user end end -- cgit v1.2.1 From 6f945f20b4c3683bc862ebc476bad9331d72784e Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Tue, 27 Feb 2018 19:15:25 +0000 Subject: Foreground verification of uploads and LFS objects --- app/models/lfs_object.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'app/models') diff --git a/app/models/lfs_object.rb b/app/models/lfs_object.rb index fc586fa216e..b444812a4cf 100644 --- a/app/models/lfs_object.rb +++ b/app/models/lfs_object.rb @@ -15,4 +15,8 @@ class LfsObject < ActiveRecord::Base .where(lfs_objects_projects: { id: nil }) .destroy_all end + + def self.calculate_oid(path) + Digest::SHA256.file(path).hexdigest + end end -- cgit v1.2.1 From 158514bba3445b1a8303c6a4d5d7ba2403ef8a9f Mon Sep 17 00:00:00 2001 From: Mario de la Ossa Date: Fri, 23 Feb 2018 22:13:35 -0600 Subject: SlackService - respect `notify_only_default_branch` for push events --- app/models/project_services/chat_notification_service.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'app/models') diff --git a/app/models/project_services/chat_notification_service.rb b/app/models/project_services/chat_notification_service.rb index 818cfb01b14..e2ffdf72201 100644 --- a/app/models/project_services/chat_notification_service.rb +++ b/app/models/project_services/chat_notification_service.rb @@ -99,7 +99,7 @@ class ChatNotificationService < Service def get_message(object_kind, data) case object_kind when "push", "tag_push" - ChatMessage::PushMessage.new(data) + ChatMessage::PushMessage.new(data) if notify_for_ref?(data) when "issue" ChatMessage::IssueMessage.new(data) unless update?(data) when "merge_request" @@ -145,10 +145,16 @@ class ChatNotificationService < Service end def notify_for_ref?(data) - return true if data[:object_attributes][:tag] + return true if data.dig(:object_attributes, :tag) return true unless notify_only_default_branch? - data[:object_attributes][:ref] == project.default_branch + ref = if data[:ref] + Gitlab::Git.ref_name(data[:ref]) + else + data.dig(:object_attributes, :ref) + end + + ref == project.default_branch end def notify_for_pipeline?(data) -- cgit v1.2.1 From 39011be53daee921dac1648044d1e68b9706197c Mon Sep 17 00:00:00 2001 From: Andreas Brandl Date: Fri, 2 Mar 2018 16:01:06 +0100 Subject: Extract method User#authorizations_for_projects. --- app/models/project.rb | 16 ++++------------ app/models/user.rb | 9 +++++++++ 2 files changed, 13 insertions(+), 12 deletions(-) (limited to 'app/models') diff --git a/app/models/project.rb b/app/models/project.rb index ad4315e1601..5b1f8b2658b 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -319,14 +319,9 @@ class Project < ActiveRecord::Base # logged in user. def self.public_or_visible_to_user(user = nil) if user - authorized = user - .project_authorizations - .select(1) - .where('project_authorizations.project_id = projects.id') - - levels = Gitlab::VisibilityLevel.levels_for_user(user) - - where('EXISTS (?) OR projects.visibility_level IN (?)', authorized, levels) + where('EXISTS (?) OR projects.visibility_level IN (?)', + user.authorizations_for_projects, + Gitlab::VisibilityLevel.levels_for_user(user)) else public_to_user end @@ -347,14 +342,11 @@ class Project < ActiveRecord::Base elsif user column = ProjectFeature.quoted_access_level_column(feature) - authorized = user.project_authorizations.select(1) - .where('project_authorizations.project_id = projects.id') - with_project_feature .where("#{column} IN (?) OR (#{column} = ? AND EXISTS (?))", visible, ProjectFeature::PRIVATE, - authorized) + user.authorizations_for_projects) else with_feature_access_level(feature, visible) end diff --git a/app/models/user.rb b/app/models/user.rb index 9547506d33d..9c60adf0c90 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -601,6 +601,15 @@ class User < ActiveRecord::Base authorized_projects(min_access_level).exists?({ id: project.id }) end + # Typically used in conjunction with projects table to get projects + # a user has been given access to. + # + # Example use: + # `Project.where('EXISTS(?)', user.authorizations_for_projects)` + def authorizations_for_projects + project_authorizations.select(1).where('project_authorizations.project_id = projects.id') + end + # Returns the projects this user has reporter (or greater) access to, limited # to at most the given projects. # -- cgit v1.2.1 From 168ff28506dfe66a13a6c1bc5e3b772445a5bd18 Mon Sep 17 00:00:00 2001 From: Jacopo Date: Wed, 28 Feb 2018 12:16:29 +0100 Subject: Adds updated_at filter to issues and merge_requests API --- app/models/concerns/issuable.rb | 1 + app/models/concerns/updated_at_filterable.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 app/models/concerns/updated_at_filterable.rb (limited to 'app/models') diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb index 7049f340c9d..4560bc23193 100644 --- a/app/models/concerns/issuable.rb +++ b/app/models/concerns/issuable.rb @@ -19,6 +19,7 @@ module Issuable include AfterCommitQueue include Sortable include CreatedAtFilterable + include UpdatedAtFilterable # This object is used to gather issuable meta data for displaying # upvotes, downvotes, notes and closing merge requests count for issues and merge requests diff --git a/app/models/concerns/updated_at_filterable.rb b/app/models/concerns/updated_at_filterable.rb new file mode 100644 index 00000000000..edb423b7828 --- /dev/null +++ b/app/models/concerns/updated_at_filterable.rb @@ -0,0 +1,12 @@ +module UpdatedAtFilterable + extend ActiveSupport::Concern + + included do + scope :updated_before, ->(date) { where(scoped_table[:updated_at].lteq(date)) } + scope :updated_after, ->(date) { where(scoped_table[:updated_at].gteq(date)) } + + def self.scoped_table + arel_table.alias(table_name) + end + end +end -- cgit v1.2.1 From 8fe9995f11010749c5fb183f2d8129f547ae6d4a Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 5 Mar 2018 16:15:26 +0200 Subject: Replace deprecated name_with_namespace with full_name in app and spec Signed-off-by: Dmitriy Zaporozhets --- app/models/event.rb | 2 +- app/models/project_services/asana_service.rb | 2 +- app/models/project_services/campfire_service.rb | 2 +- app/models/project_services/chat_notification_service.rb | 2 +- app/models/project_services/hipchat_service.rb | 4 ++-- app/models/project_services/mattermost_slash_commands_service.rb | 2 +- app/models/project_services/pushover_service.rb | 4 ++-- 7 files changed, 9 insertions(+), 9 deletions(-) (limited to 'app/models') diff --git a/app/models/event.rb b/app/models/event.rb index 75538ba196c..be0fc7efa9a 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -158,7 +158,7 @@ class Event < ActiveRecord::Base def project_name if project - project.name_with_namespace + project.full_name else "(deleted project)" end diff --git a/app/models/project_services/asana_service.rb b/app/models/project_services/asana_service.rb index 109258d1eb7..4f289e6e215 100644 --- a/app/models/project_services/asana_service.rb +++ b/app/models/project_services/asana_service.rb @@ -68,7 +68,7 @@ http://app.asana.com/-/account_api' end user = data[:user_name] - project_name = project.name_with_namespace + project_name = project.full_name data[:commits].each do |commit| push_msg = "#{user} pushed to branch #{branch} of #{project_name} ( #{commit[:url]} ):" diff --git a/app/models/project_services/campfire_service.rb b/app/models/project_services/campfire_service.rb index c3f5b310619..8d7a4fceb08 100644 --- a/app/models/project_services/campfire_service.rb +++ b/app/models/project_services/campfire_service.rb @@ -86,7 +86,7 @@ class CampfireService < Service after = push[:after] message = "" - message << "[#{project.name_with_namespace}] " + message << "[#{project.full_name}] " message << "#{push[:user_name]} " if Gitlab::Git.blank_ref?(before) diff --git a/app/models/project_services/chat_notification_service.rb b/app/models/project_services/chat_notification_service.rb index e2ffdf72201..dab0ea1a681 100644 --- a/app/models/project_services/chat_notification_service.rb +++ b/app/models/project_services/chat_notification_service.rb @@ -129,7 +129,7 @@ class ChatNotificationService < Service end def project_name - project.name_with_namespace.gsub(/\s/, '') + project.full_name.gsub(/\s/, '') end def project_url diff --git a/app/models/project_services/hipchat_service.rb b/app/models/project_services/hipchat_service.rb index bfe7ac29c18..f31c3f02af2 100644 --- a/app/models/project_services/hipchat_service.rb +++ b/app/models/project_services/hipchat_service.rb @@ -120,7 +120,7 @@ class HipchatService < Service else message << "pushed to #{ref_type} #{ref} " - message << "of #{project.name_with_namespace.gsub!(/\s/, '')} " + message << "of #{project.full_name.gsub!(/\s/, '')} " message << "(Compare changes)" push[:commits].take(MAX_COMMITS).each do |commit| @@ -274,7 +274,7 @@ class HipchatService < Service end def project_name - project.name_with_namespace.gsub(/\s/, '') + project.full_name.gsub(/\s/, '') end def project_url diff --git a/app/models/project_services/mattermost_slash_commands_service.rb b/app/models/project_services/mattermost_slash_commands_service.rb index 4d2037286a2..227d430083d 100644 --- a/app/models/project_services/mattermost_slash_commands_service.rb +++ b/app/models/project_services/mattermost_slash_commands_service.rb @@ -37,7 +37,7 @@ class MattermostSlashCommandsService < SlashCommandsService private def command(params) - pretty_project_name = project.name_with_namespace + pretty_project_name = project.full_name params.merge( auto_complete: true, diff --git a/app/models/project_services/pushover_service.rb b/app/models/project_services/pushover_service.rb index aa7bd4c3c84..e3a1ca2d45f 100644 --- a/app/models/project_services/pushover_service.rb +++ b/app/models/project_services/pushover_service.rb @@ -88,10 +88,10 @@ class PushoverService < Service user: user_key, device: device, priority: priority, - title: "#{project.name_with_namespace}", + title: "#{project.full_name}", message: message, url: data[:project][:web_url], - url_title: "See project #{project.name_with_namespace}" + url_title: "See project #{project.full_name}" } # Sound parameter MUST NOT be sent to API if not selected -- cgit v1.2.1 From 0b7d10851456018328da137beeca931767b4fd0a Mon Sep 17 00:00:00 2001 From: Jan Provaznik Date: Mon, 5 Mar 2018 08:25:27 +0100 Subject: Cleanup after adding MR diff's commit_count * processes any pending records which are not migrated yet * bumps import_export version because of new commits_count attribute * removes commits_count fallback method --- app/models/merge_request_diff.rb | 4 ---- 1 file changed, 4 deletions(-) (limited to 'app/models') diff --git a/app/models/merge_request_diff.rb b/app/models/merge_request_diff.rb index c1c27ccf3e5..06aa67c600f 100644 --- a/app/models/merge_request_diff.rb +++ b/app/models/merge_request_diff.rb @@ -197,10 +197,6 @@ class MergeRequestDiff < ActiveRecord::Base CompareService.new(project, head_commit_sha).execute(project, sha, straight: true) end - def commits_count - super || merge_request_diff_commits.size - end - private def create_merge_request_diff_files(diffs) -- cgit v1.2.1 From 2cc43aaaf3162db8c584df3bb9d1a42d92084fae Mon Sep 17 00:00:00 2001 From: Ahmad Sherif Date: Fri, 2 Mar 2018 21:04:32 +0100 Subject: Keep a commit around if its sha is present Closes gitaly#1054 --- app/models/repository.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models') diff --git a/app/models/repository.rb b/app/models/repository.rb index 242d9d5f125..1a14afb951a 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -253,7 +253,7 @@ class Repository # branches or tags, but we want to keep some of these commits around, for # example if they have comments or CI builds. def keep_around(sha) - return unless sha && commit_by(oid: sha) + return unless sha.present? && commit_by(oid: sha) return if kept_around?(sha) -- cgit v1.2.1 From 631eed028bffc55f0a80b72ab3598bc73e49272b Mon Sep 17 00:00:00 2001 From: Sean McGivern Date: Mon, 5 Mar 2018 16:41:48 +0000 Subject: Remove default scope from todos This was causing todo priority sorting to fail. --- app/models/todo.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'app/models') diff --git a/app/models/todo.rb b/app/models/todo.rb index bb5965e20eb..8afacd188e0 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -32,8 +32,6 @@ class Todo < ActiveRecord::Base validates :target_id, presence: true, unless: :for_commit? validates :commit_id, presence: true, if: :for_commit? - default_scope { reorder(id: :desc) } - scope :pending, -> { with_state(:pending) } scope :done, -> { with_state(:done) } @@ -53,10 +51,14 @@ class Todo < ActiveRecord::Base # milestones, but still show something if the user has a URL with that # selected. def sort(method) - case method.to_s - when 'priority', 'label_priority' then order_by_labels_priority - else order_by(method) - end + sorted = + case method.to_s + when 'priority', 'label_priority' then order_by_labels_priority + else order_by(method) + end + + # Break ties with the ID column for pagination + sorted.order(id: :desc) end # Order by priority depending on which issue/merge request the Todo belongs to -- cgit v1.2.1 From 8fe880dc064e0e6cd10f7176ade7c312cfb37b90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Javier=20L=C3=B3pez?= Date: Mon, 5 Mar 2018 17:51:40 +0000 Subject: Projects and groups badges API --- app/models/badge.rb | 51 ++++++++++++++++++++++++++++++++++++++ app/models/badges/group_badge.rb | 5 ++++ app/models/badges/project_badge.rb | 15 +++++++++++ app/models/group.rb | 2 ++ app/models/project.rb | 13 ++++++++++ 5 files changed, 86 insertions(+) create mode 100644 app/models/badge.rb create mode 100644 app/models/badges/group_badge.rb create mode 100644 app/models/badges/project_badge.rb (limited to 'app/models') diff --git a/app/models/badge.rb b/app/models/badge.rb new file mode 100644 index 00000000000..f7e10c2ebfc --- /dev/null +++ b/app/models/badge.rb @@ -0,0 +1,51 @@ +class Badge < ActiveRecord::Base + # This structure sets the placeholders that the urls + # can have. This hash also sets which action to ask when + # the placeholder is found. + PLACEHOLDERS = { + 'project_path' => :full_path, + 'project_id' => :id, + 'default_branch' => :default_branch, + 'commit_sha' => ->(project) { project.commit&.sha } + }.freeze + + # This regex is built dynamically using the keys from the PLACEHOLDER struct. + # So, we can easily add new placeholder just by modifying the PLACEHOLDER hash. + # This regex will build the new PLACEHOLDER_REGEX with the new information + PLACEHOLDERS_REGEX = /(#{PLACEHOLDERS.keys.join('|')})/.freeze + + default_scope { order_created_at_asc } + + scope :order_created_at_asc, -> { reorder(created_at: :asc) } + + validates :link_url, :image_url, url_placeholder: { protocols: %w(http https), placeholder_regex: PLACEHOLDERS_REGEX } + validates :type, presence: true + + def rendered_link_url(project = nil) + build_rendered_url(link_url, project) + end + + def rendered_image_url(project = nil) + build_rendered_url(image_url, project) + end + + private + + def build_rendered_url(url, project = nil) + return url unless valid? && project + + Gitlab::StringPlaceholderReplacer.replace_string_placeholders(url, PLACEHOLDERS_REGEX) do |arg| + replace_placeholder_action(PLACEHOLDERS[arg], project) + end + end + + # The action param represents the :symbol or Proc to call in order + # to retrieve the return value from the project. + # This method checks if it is a Proc and use the call method, and if it is + # a symbol just send the action + def replace_placeholder_action(action, project) + return unless project + + action.is_a?(Proc) ? action.call(project) : project.public_send(action) # rubocop:disable GitlabSecurity/PublicSend + end +end diff --git a/app/models/badges/group_badge.rb b/app/models/badges/group_badge.rb new file mode 100644 index 00000000000..f4b2bdecdcc --- /dev/null +++ b/app/models/badges/group_badge.rb @@ -0,0 +1,5 @@ +class GroupBadge < Badge + belongs_to :group + + validates :group, presence: true +end diff --git a/app/models/badges/project_badge.rb b/app/models/badges/project_badge.rb new file mode 100644 index 00000000000..3945b376052 --- /dev/null +++ b/app/models/badges/project_badge.rb @@ -0,0 +1,15 @@ +class ProjectBadge < Badge + belongs_to :project + + validates :project, presence: true + + def rendered_link_url(project = nil) + project ||= self.project + super + end + + def rendered_image_url(project = nil) + project ||= self.project + super + end +end diff --git a/app/models/group.rb b/app/models/group.rb index 75bf013ecd2..201505c3d3c 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -31,6 +31,8 @@ class Group < Namespace has_many :uploads, as: :model, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent + has_many :badges, class_name: 'GroupBadge' + accepts_nested_attributes_for :variables, allow_destroy: true validate :visibility_level_allowed_by_projects diff --git a/app/models/project.rb b/app/models/project.rb index 5b1f8b2658b..a11b1e4f554 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -221,6 +221,8 @@ class Project < ActiveRecord::Base has_one :auto_devops, class_name: 'ProjectAutoDevops' has_many :custom_attributes, class_name: 'ProjectCustomAttribute' + has_many :project_badges, class_name: 'ProjectBadge' + accepts_nested_attributes_for :variables, allow_destroy: true accepts_nested_attributes_for :project_feature, update_only: true accepts_nested_attributes_for :import_data @@ -1766,6 +1768,17 @@ class Project < ActiveRecord::Base .set(import_jid, StuckImportJobsWorker::IMPORT_JOBS_EXPIRATION) end + def badges + return project_badges unless group + + group_badges_rel = GroupBadge.where(group: group.self_and_ancestors) + + union = Gitlab::SQL::Union.new([project_badges.select(:id), + group_badges_rel.select(:id)]) + + Badge.where("id IN (#{union.to_sql})") # rubocop:disable GitlabSecurity/SqlInjection + end + private def storage -- cgit v1.2.1 From 36a0f6aaa3d3999d743fc11a39a9f8dd2f127d70 Mon Sep 17 00:00:00 2001 From: Felipe Artur Date: Mon, 5 Mar 2018 16:32:51 -0300 Subject: Use host URL to build JIRA remote link icon --- app/models/project_services/jira_service.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'app/models') diff --git a/app/models/project_services/jira_service.rb b/app/models/project_services/jira_service.rb index 436a870b0c4..e5035c81df0 100644 --- a/app/models/project_services/jira_service.rb +++ b/app/models/project_services/jira_service.rb @@ -1,5 +1,7 @@ class JiraService < IssueTrackerService include Gitlab::Routing + include ApplicationHelper + include ActionView::Helpers::AssetUrlHelper validates :url, url: true, presence: true, if: :activated? validates :api_url, url: true, allow_blank: true @@ -268,7 +270,9 @@ class JiraService < IssueTrackerService url: url, title: title, status: status, - icon: { title: 'GitLab', url16x16: 'https://gitlab.com/favicon.ico' } + icon: { + title: 'GitLab', url16x16: asset_url('favicon.ico', host: gitlab_config.url) + } } } end -- cgit v1.2.1 From 7237ed59ac90148945efebf9624949c80c1298a4 Mon Sep 17 00:00:00 2001 From: Mayra Cabrera Date: Tue, 6 Mar 2018 08:26:50 +0000 Subject: Resolve "Enable privileged mode for Runner installed on Kubernetes" --- app/models/clusters/applications/runner.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/clusters/applications/runner.rb b/app/models/clusters/applications/runner.rb index 7adf1663c35..16efe90fa27 100644 --- a/app/models/clusters/applications/runner.rb +++ b/app/models/clusters/applications/runner.rb @@ -56,12 +56,13 @@ module Clusters def specification { "gitlabUrl" => gitlab_url, - "runnerToken" => ensure_runner.token + "runnerToken" => ensure_runner.token, + "runners" => { "privileged" => privileged } } end def content_values - specification.merge(YAML.load_file(chart_values_file)) + YAML.load_file(chart_values_file).deep_merge!(specification) end end end -- cgit v1.2.1 From b5ec6097b75819626083726d1073978d11aba7e3 Mon Sep 17 00:00:00 2001 From: Andrew Newdigate Date: Tue, 6 Mar 2018 13:32:48 +0000 Subject: Fix Gitaly n+1 in NetworkController#show --- app/models/network/commit.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'app/models') diff --git a/app/models/network/commit.rb b/app/models/network/commit.rb index 9357e55b419..22d48c9e661 100644 --- a/app/models/network/commit.rb +++ b/app/models/network/commit.rb @@ -24,12 +24,7 @@ module Network end def parents(map) - @commit.parents.map do |p| - if map.include?(p.id) - map[p.id] - end - end - .compact + map.values_at(*@commit.parent_ids).compact end end end -- cgit v1.2.1