Commit 0a1b4ba2 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'remove-thread-var' into 'master'

Remove thread variables

Lets get rid of thread variables. It produces additional complexity and weird stack trace.

Also part of !1133

- - -

Also it fixes issue/merge_request close and reopen bug via API when no dashboard event and comment was created.
parents f451a697 f6ee55aa
......@@ -4,7 +4,6 @@ class ApplicationController < ActionController::Base
before_filter :authenticate_user!
before_filter :reject_blocked!
before_filter :check_password_expiration
around_filter :set_current_user_for_thread
before_filter :add_abilities
before_filter :ldap_security_check
before_filter :dev_tools if Rails.env == 'development'
......@@ -53,15 +52,6 @@ class ApplicationController < ActionController::Base
end
end
def set_current_user_for_thread
Thread.current[:current_user] = current_user
begin
yield
ensure
Thread.current[:current_user] = nil
end
end
def abilities
@abilities ||= Six.new
end
......
......@@ -37,7 +37,7 @@ class Projects::MilestonesController < Projects::ApplicationController
end
def create
@milestone = @project.milestones.new(params[:milestone])
@milestone = Milestones::CreateService.new(project, current_user, params[:milestone]).execute
if @milestone.save
redirect_to project_milestone_path(@project, @milestone)
......@@ -47,7 +47,7 @@ class Projects::MilestonesController < Projects::ApplicationController
end
def update
@milestone.update_attributes(params[:milestone])
@milestone = Milestones::UpdateService.new(project, current_user, params[:milestone]).execute(milestone)
respond_to do |format|
format.js
......
......@@ -10,12 +10,4 @@ class BaseObserver < ActiveRecord::Observer
def log_info message
Gitlab::AppLogger.info message
end
def current_user
Thread.current[:current_user]
end
def current_commit
Thread.current[:current_commit]
end
end
class MilestoneObserver < BaseObserver
def after_create(milestone)
event_service.open_milestone(milestone, current_user)
end
def after_close(milestone, transition)
event_service.close_milestone(milestone, current_user)
end
def after_reopen(milestone, transition)
event_service.reopen_milestone(milestone, current_user)
end
end
......@@ -5,7 +5,7 @@ class NoteObserver < BaseObserver
# Skip system notes, like status changes and cross-references.
# Skip wall notes to prevent spamming of dashboard
if note.noteable_type.present? && !note.system
event_service.leave_note(note, current_user)
event_service.leave_note(note, note.author)
end
unless note.system?
......@@ -18,6 +18,6 @@ class NoteObserver < BaseObserver
end
def after_update(note)
note.notice_added_references(note.project, current_user)
note.notice_added_references(note.project, note.author)
end
end
module Issues
class UpdateService < Issues::BaseService
def execute(issue)
state = params.delete('state_event')
state = params.delete('state_event') || params.delete(:state_event)
case state
when 'reopen'
......
......@@ -10,7 +10,7 @@ module MergeRequests
params.delete(:source_project_id)
params.delete(:target_project_id)
state = params.delete('state_event')
state = params.delete('state_event') || params.delete(:state_event)
case state
when 'reopen'
......
module Milestones
class BaseService < ::BaseService
end
end
module Milestones
class CloseService < Milestones::BaseService
def execute(milestone)
if milestone.close
event_service.close_milestone(milestone, current_user)
end
milestone
end
end
end
module Milestones
class CreateService < Milestones::BaseService
def execute
milestone = project.milestones.new(params)
if milestone.save
event_service.open_milestone(milestone, current_user)
end
milestone
end
end
end
module Milestones
class ReopenService < Milestones::BaseService
def execute(milestone)
if milestone.activate
event_service.reopen_milestone(milestone, current_user)
end
milestone
end
end
end
module Milestones
class UpdateService < Milestones::BaseService
def execute(milestone)
state = params.delete('state_event') || params.delete(:state_event)
case state
when 'activate'
Milestones::ReopenService.new(project, current_user, {}).execute(milestone)
when 'close'
Milestones::CloseService.new(project, current_user, {}).execute(milestone)
end
if params.present?
milestone.update_attributes(params)
end
milestone
end
end
end
......@@ -19,8 +19,7 @@ module Gitlab
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
config.active_record.observers = :milestone_observer,
:project_activity_cache_observer,
config.active_record.observers = :project_activity_cache_observer,
:note_observer,
:project_observer,
:system_hook_observer,
......
......@@ -36,16 +36,6 @@ module API
end
end
def set_current_user_for_thread
Thread.current[:current_user] = current_user
begin
yield
ensure
Thread.current[:current_user] = nil
end
end
def user_project
@project ||= find_project(params[:id])
@project || not_found!
......
......@@ -184,21 +184,18 @@ module API
# POST /projects/:id/merge_request/:merge_request_id/comments
#
post ":id/merge_request/:merge_request_id/comments" do
set_current_user_for_thread do
required_attributes! [:note]
required_attributes! [:note]
merge_request = user_project.merge_requests.find(params[:merge_request_id])
note = merge_request.notes.new(note: params[:note], project_id: user_project.id)
note.author = current_user
merge_request = user_project.merge_requests.find(params[:merge_request_id])
note = merge_request.notes.new(note: params[:note], project_id: user_project.id)
note.author = current_user
if note.save
present note, with: Entities::MRNote
else
not_found!
end
if note.save
present note, with: Entities::MRNote
else
not_found!
end
end
end
end
end
......@@ -40,17 +40,15 @@ module API
# Example Request:
# POST /projects/:id/milestones
post ":id/milestones" do
set_current_user_for_thread do
authorize! :admin_milestone, user_project
required_attributes! [:title]
authorize! :admin_milestone, user_project
required_attributes! [:title]
attrs = attributes_for_keys [:title, :description, :due_date]
milestone = ::Milestones::CreateService.new(user_project, current_user, attrs).execute
attrs = attributes_for_keys [:title, :description, :due_date]
@milestone = user_project.milestones.new attrs
if @milestone.save
present @milestone, with: Entities::Milestone
else
not_found!
end
if milestone.valid?
present milestone, with: Entities::Milestone
else
not_found!
end
end
......@@ -66,16 +64,15 @@ module API
# Example Request:
# PUT /projects/:id/milestones/:milestone_id
put ":id/milestones/:milestone_id" do
set_current_user_for_thread do
authorize! :admin_milestone, user_project
authorize! :admin_milestone, user_project
attrs = attributes_for_keys [:title, :description, :due_date, :state_event]
milestone = user_project.milestones.find(params[:milestone_id])
milestone = ::Milestones::UpdateService.new(user_project, current_user, attrs).execute(milestone)
@milestone = user_project.milestones.find(params[:milestone_id])
attrs = attributes_for_keys [:title, :description, :due_date, :state_event]
if @milestone.update_attributes attrs
present @milestone, with: Entities::Milestone
else
not_found!
end
if milestone.valid?
present milestone, with: Entities::Milestone
else
not_found!
end
end
end
......
......@@ -41,19 +41,17 @@ module API
# Example Request:
# POST /projects/:id/notes
post ":id/notes" do
set_current_user_for_thread do
required_attributes! [:body]
required_attributes! [:body]
@note = user_project.notes.new(note: params[:body])
@note.author = current_user
@note = user_project.notes.new(note: params[:body])
@note.author = current_user
if @note.save
present @note, with: Entities::Note
else
# :note is exposed as :body, but :note is set on error
bad_request!(:note) if @note.errors[:note].any?
not_found!
end
if @note.save
present @note, with: Entities::Note
else
# :note is exposed as :body, but :note is set on error
bad_request!(:note) if @note.errors[:note].any?
not_found!
end
end
......@@ -99,19 +97,17 @@ module API
# POST /projects/:id/issues/:noteable_id/notes
# POST /projects/:id/snippets/:noteable_id/notes
post ":id/#{noteables_str}/:#{noteable_id_str}/notes" do
set_current_user_for_thread do
required_attributes! [:body]
required_attributes! [:body]
@noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
@note = @noteable.notes.new(note: params[:body])
@note.author = current_user
@note.project = user_project
@noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
@note = @noteable.notes.new(note: params[:body])
@note.author = current_user
@note.project = user_project
if @note.save
present @note, with: Entities::Note
else
not_found!
end
if @note.save
present @note, with: Entities::Note
else
not_found!
end
end
end
......
......@@ -9,12 +9,7 @@ module Gitlab
end
def self.by_user(user)
begin
Thread.current[:current_user] = user
yield
ensure
Thread.current[:current_user] = nil
end
yield
end
def self.mute_mailer
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment