Commit f197b528 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'refactor-complex-methods' into 'master'

Refactor complex methods

Make flog part of CI check which is not allowed to fail. I used high score (70) and refactored most complex method. In future releases we should lower acceptable score to something like 40..50

Part of #3444 

See merge request !1794
parents b9fedcb7 3c16fb93
...@@ -80,7 +80,6 @@ flog: ...@@ -80,7 +80,6 @@ flog:
tags: tags:
- ruby - ruby
- mysql - mysql
allow_failure: true
flay: flay:
script: script:
......
...@@ -108,19 +108,23 @@ module EventsHelper ...@@ -108,19 +108,23 @@ module EventsHelper
end end
end end
elsif event.push? elsif event.push?
if event.push_with_commits? && event.md_ref? push_event_feed_url(event)
if event.commits_count > 1 end
namespace_project_compare_url(event.project.namespace, event.project, end
from: event.commit_from, to:
event.commit_to) def push_event_feed_url(event)
else if event.push_with_commits? && event.md_ref?
namespace_project_commit_url(event.project.namespace, event.project, if event.commits_count > 1
id: event.commit_to) namespace_project_compare_url(event.project.namespace, event.project,
end from: event.commit_from, to:
event.commit_to)
else else
namespace_project_commits_url(event.project.namespace, event.project, namespace_project_commit_url(event.project.namespace, event.project,
event.ref_name) id: event.commit_to)
end end
else
namespace_project_commits_url(event.project.namespace, event.project,
event.ref_name)
end end
end end
......
...@@ -22,24 +22,27 @@ module Issues ...@@ -22,24 +22,27 @@ module Issues
issue, issue.labels - old_labels, old_labels - issue.labels) issue, issue.labels - old_labels, old_labels - issue.labels)
end end
if issue.previous_changes.include?('milestone_id') handle_changes(issue)
create_milestone_note(issue)
end
if issue.previous_changes.include?('assignee_id')
create_assignee_note(issue)
notification_service.reassigned_issue(issue, current_user)
end
if issue.previous_changes.include?('title')
create_title_change_note(issue, issue.previous_changes['title'].first)
end
issue.create_new_cross_references!(current_user) issue.create_new_cross_references!(current_user)
execute_hooks(issue, 'update') execute_hooks(issue, 'update')
end end
issue issue
end end
def handle_changes(issue)
if issue.previous_changes.include?('milestone_id')
create_milestone_note(issue)
end
if issue.previous_changes.include?('assignee_id')
create_assignee_note(issue)
notification_service.reassigned_issue(issue, current_user)
end
if issue.previous_changes.include?('title')
create_title_change_note(issue, issue.previous_changes['title'].first)
end
end
end end
end end
...@@ -35,35 +35,38 @@ module MergeRequests ...@@ -35,35 +35,38 @@ module MergeRequests
) )
end end
if merge_request.previous_changes.include?('target_branch') handle_changes(merge_request)
create_branch_change_note(merge_request, 'target', merge_request.create_new_cross_references!(current_user)
merge_request.previous_changes['target_branch'].first, execute_hooks(merge_request, 'update')
merge_request.target_branch) end
end
if merge_request.previous_changes.include?('milestone_id') merge_request
create_milestone_note(merge_request) end
end
if merge_request.previous_changes.include?('assignee_id') def handle_changes(merge_request)
create_assignee_note(merge_request) if merge_request.previous_changes.include?('target_branch')
notification_service.reassigned_merge_request(merge_request, current_user) create_branch_change_note(merge_request, 'target',
end merge_request.previous_changes['target_branch'].first,
merge_request.target_branch)
end
if merge_request.previous_changes.include?('title') if merge_request.previous_changes.include?('milestone_id')
create_title_change_note(merge_request, merge_request.previous_changes['title'].first) create_milestone_note(merge_request)
end end
if merge_request.previous_changes.include?('target_branch') || if merge_request.previous_changes.include?('assignee_id')
merge_request.previous_changes.include?('source_branch') create_assignee_note(merge_request)
merge_request.mark_as_unchecked notification_service.reassigned_merge_request(merge_request, current_user)
end end
merge_request.create_new_cross_references!(current_user) if merge_request.previous_changes.include?('title')
execute_hooks(merge_request, 'update') create_title_change_note(merge_request, merge_request.previous_changes['title'].first)
end end
merge_request if merge_request.previous_changes.include?('target_branch') ||
merge_request.previous_changes.include?('source_branch')
merge_request.mark_as_unchecked
end
end end
end end
end end
...@@ -33,17 +33,7 @@ class SystemHooksService ...@@ -33,17 +33,7 @@ class SystemHooksService
) )
end end
when Project when Project
owner = model.owner data.merge!(project_data(model))
data.merge!({
name: model.name,
path: model.path,
path_with_namespace: model.path_with_namespace,
project_id: model.id,
owner_name: owner.name,
owner_email: owner.respond_to?(:email) ? owner.email : "",
project_visibility: Project.visibility_levels.key(model.visibility_level_field).downcase
})
when User when User
data.merge!({ data.merge!({
name: model.name, name: model.name,
...@@ -51,16 +41,7 @@ class SystemHooksService ...@@ -51,16 +41,7 @@ class SystemHooksService
user_id: model.id user_id: model.id
}) })
when ProjectMember when ProjectMember
data.merge!({ data.merge!(project_member_data(model))
project_name: model.project.name,
project_path: model.project.path,
project_path_with_namespace: model.project.path_with_namespace,
project_id: model.project.id,
user_name: model.user.name,
user_email: model.user.email,
access_level: model.human_access,
project_visibility: Project.visibility_levels.key(model.project.visibility_level_field).downcase
})
when Group when Group
owner = model.owner owner = model.owner
...@@ -72,15 +53,7 @@ class SystemHooksService ...@@ -72,15 +53,7 @@ class SystemHooksService
owner_email: owner.respond_to?(:email) ? owner.email : nil, owner_email: owner.respond_to?(:email) ? owner.email : nil,
) )
when GroupMember when GroupMember
data.merge!( data.merge!(group_member_data(model))
group_name: model.group.name,
group_path: model.group.path,
group_id: model.group.id,
user_name: model.user.name,
user_email: model.user.email,
user_id: model.user.id,
group_access: model.human_access,
)
end end
end end
...@@ -96,4 +69,43 @@ class SystemHooksService ...@@ -96,4 +69,43 @@ class SystemHooksService
"#{model.class.name.downcase}_#{event.to_s}" "#{model.class.name.downcase}_#{event.to_s}"
end end
end end
def project_data(model)
owner = model.owner
{
name: model.name,
path: model.path,
path_with_namespace: model.path_with_namespace,
project_id: model.id,
owner_name: owner.name,
owner_email: owner.respond_to?(:email) ? owner.email : "",
project_visibility: Project.visibility_levels.key(model.visibility_level_field).downcase
}
end
def project_member_data(model)
{
project_name: model.project.name,
project_path: model.project.path,
project_path_with_namespace: model.project.path_with_namespace,
project_id: model.project.id,
user_name: model.user.name,
user_email: model.user.email,
access_level: model.human_access,
project_visibility: Project.visibility_levels.key(model.project.visibility_level_field).downcase
}
end
def group_member_data(model)
{
group_name: model.group.name,
group_path: model.group.path,
group_id: model.group.id,
user_name: model.user.name,
user_email: model.user.email,
user_id: model.user.id,
group_access: model.human_access,
}
end
end end
...@@ -30,7 +30,7 @@ module Gitlab ...@@ -30,7 +30,7 @@ module Gitlab
def user_map def user_map
@user_map ||= begin @user_map ||= begin
user_map = Hash.new do |hash, user| user_map = Hash.new do |hash, user|
# Replace ... by \.\.\., so `johnsm...@gmail.com` isn't autolinked. # Replace ... by \.\.\., so `johnsm...@gmail.com` isn't autolinked.
Client.mask_email(user).sub("...", "\\.\\.\\.") Client.mask_email(user).sub("...", "\\.\\.\\.")
end end
...@@ -76,18 +76,7 @@ module Gitlab ...@@ -76,18 +76,7 @@ module Gitlab
attachments = format_attachments(raw_issue["id"], 0, issue_comment["attachments"]) attachments = format_attachments(raw_issue["id"], 0, issue_comment["attachments"])
body = format_issue_body(author, date, content, attachments) body = format_issue_body(author, date, content, attachments)
labels = import_issue_labels(raw_issue)
labels = []
raw_issue["labels"].each do |label|
name = nice_label_name(label)
labels << name
unless @known_labels.include?(name)
create_label(name)
@known_labels << name
end
end
labels << nice_status_name(raw_issue["status"])
assignee_id = nil assignee_id = nil
if raw_issue.has_key?("owner") if raw_issue.has_key?("owner")
...@@ -110,6 +99,7 @@ module Gitlab ...@@ -110,6 +99,7 @@ module Gitlab
assignee_id: assignee_id, assignee_id: assignee_id,
state: raw_issue["state"] == "closed" ? "closed" : "opened" state: raw_issue["state"] == "closed" ? "closed" : "opened"
) )
issue.add_labels_by_names(labels) issue.add_labels_by_names(labels)
if issue.iid != raw_issue["id"] if issue.iid != raw_issue["id"]
...@@ -120,6 +110,23 @@ module Gitlab ...@@ -120,6 +110,23 @@ module Gitlab
end end
end end
def import_issue_labels(raw_issue)
labels = []
raw_issue["labels"].each do |label|
name = nice_label_name(label)
labels << name
unless @known_labels.include?(name)
create_label(name)
@known_labels << name
end
end
labels << nice_status_name(raw_issue["status"])
labels
end
def import_issue_comments(issue, comments) def import_issue_comments(issue, comments)
Note.transaction do Note.transaction do
while raw_comment = comments.shift while raw_comment = comments.shift
...@@ -172,7 +179,7 @@ module Gitlab ...@@ -172,7 +179,7 @@ module Gitlab
"#5cb85c" "#5cb85c"
when "Status: Started" when "Status: Started"
"#8e44ad" "#8e44ad"
when "Priority: Critical" when "Priority: Critical"
"#ffcfcf" "#ffcfcf"
when "Priority: High" when "Priority: High"
...@@ -181,7 +188,7 @@ module Gitlab ...@@ -181,7 +188,7 @@ module Gitlab
"#fff5cc" "#fff5cc"
when "Priority: Low" when "Priority: Low"
"#cfe9ff" "#cfe9ff"
when "Type: Defect" when "Type: Defect"
"#d9534f" "#d9534f"
when "Type: Enhancement" when "Type: Enhancement"
...@@ -249,8 +256,8 @@ module Gitlab ...@@ -249,8 +256,8 @@ module Gitlab
end end
if raw_updates.has_key?("cc") if raw_updates.has_key?("cc")
cc = raw_updates["cc"].map do |l| cc = raw_updates["cc"].map do |l|
deleted = l.start_with?("-") deleted = l.start_with?("-")
l = l[1..-1] if deleted l = l[1..-1] if deleted
l = user_map[l] l = user_map[l]
l = "~~#{l}~~" if deleted l = "~~#{l}~~" if deleted
...@@ -261,8 +268,8 @@ module Gitlab ...@@ -261,8 +268,8 @@ module Gitlab
end end
if raw_updates.has_key?("labels") if raw_updates.has_key?("labels")
labels = raw_updates["labels"].map do |l| labels = raw_updates["labels"].map do |l|
deleted = l.start_with?("-") deleted = l.start_with?("-")
l = l[1..-1] if deleted l = l[1..-1] if deleted
l = nice_label_name(l) l = nice_label_name(l)
l = "~~#{l}~~" if deleted l = "~~#{l}~~" if deleted
...@@ -278,45 +285,39 @@ module Gitlab ...@@ -278,45 +285,39 @@ module Gitlab
if raw_updates.has_key?("blockedOn") if raw_updates.has_key?("blockedOn")
blocked_ons = raw_updates["blockedOn"].map do |raw_blocked_on| blocked_ons = raw_updates["blockedOn"].map do |raw_blocked_on|
name, id = raw_blocked_on.split(":", 2) format_blocking_updates(raw_blocked_on)
deleted = name.start_with?("-")
name = name[1..-1] if deleted
text =
if name == project.import_source
"##{id}"
else
"#{project.namespace.path}/#{name}##{id}"
end
text = "~~#{text}~~" if deleted
text
end end
updates << "*Blocked on: #{blocked_ons.join(", ")}*" updates << "*Blocked on: #{blocked_ons.join(", ")}*"
end end
if raw_updates.has_key?("blocking") if raw_updates.has_key?("blocking")
blockings = raw_updates["blocking"].map do |raw_blocked_on| blockings = raw_updates["blocking"].map do |raw_blocked_on|
name, id = raw_blocked_on.split(":", 2) format_blocking_updates(raw_blocked_on)
deleted = name.start_with?("-")
name = name[1..-1] if deleted
text =
if name == project.import_source
"##{id}"
else
"#{project.namespace.path}/#{name}##{id}"
end
text = "~~#{text}~~" if deleted
text
end end
updates << "*Blocking: #{blockings.join(", ")}*" updates << "*Blocking: #{blockings.join(", ")}*"
end end
updates updates
end end
def format_blocking_updates(raw_blocked_on)
name, id = raw_blocked_on.split(":", 2)
deleted = name.start_with?("-")
name = name[1..-1] if deleted
text =
if name == project.import_source
"##{id}"
else
"#{project.namespace.path}/#{name}##{id}"
end
text = "~~#{text}~~" if deleted
text
end
def format_attachments(issue_id, comment_id, raw_attachments) def format_attachments(issue_id, comment_id, raw_attachments)
return [] unless raw_attachments return [] unless raw_attachments
...@@ -325,7 +326,7 @@ module Gitlab ...@@ -325,7 +326,7 @@ module Gitlab
filename = attachment["fileName"] filename = attachment["fileName"]
link = "https://storage.googleapis.com/google-code-attachments/#{@repo.name}/issue-#{issue_id}/comment-#{comment_id}/#{filename}" link = "https://storage.googleapis.com/google-code-attachments/#{@repo.name}/issue-#{issue_id}/comment-#{comment_id}/#{filename}"
text = "[#{filename}](#{link})" text = "[#{filename}](#{link})"
text = "!#{text}" if filename =~ /\.(png|jpg|jpeg|gif|bmp|tiff)\z/i text = "!#{text}" if filename =~ /\.(png|jpg|jpeg|gif|bmp|tiff)\z/i
text text
......
...@@ -11,48 +11,71 @@ module Gitlab ...@@ -11,48 +11,71 @@ module Gitlab
indexes.each do |index| indexes.each do |index|
first_line = diff_arr[index+1] first_line = diff_arr[index+1]
second_line = diff_arr[index+2] second_line = diff_arr[index+2]
max_length = [first_line.size, second_line.size].max
# Skip inline diff if empty line was replaced with content # Skip inline diff if empty line was replaced with content
next if first_line == "-\n" next if first_line == "-\n"
first_the_same_symbols = 0 first_token = find_first_token(first_line, second_line)
(0..max_length + 1).each do |i| apply_first_token(diff_arr, index, first_token)
first_the_same_symbols = i - 1
if first_line[i] != second_line[i] && i > 0 last_token = find_last_token(first_line, second_line, first_token)
break apply_last_token(diff_arr, index, last_token)
end end
end
diff_arr
end
def apply_first_token(diff_arr, index, first_token)
start = first_token + START
if first_token.empty?
# In case if we remove string of spaces in commit
diff_arr[index+1].sub!("-", "-" => "-#{START}")
diff_arr[index+2].sub!("+", "+" => "+#{START}")
else
diff_arr[index+1].sub!(first_token, first_token => start)
diff_arr[index+2].sub!(first_token, first_token => start)
end
end
first_token = first_line[0..first_the_same_symbols][1..-1] def apply_last_token(diff_arr, index, last_token)
start = first_token + START # This is tricky: escape backslashes so that `sub` doesn't interpret them
# as backreferences. Regexp.escape does NOT do the right thing.
replace_token = FINISH + last_token.gsub(/\\/, '\&\&')
diff_arr[index+1].sub!(/#{Regexp.escape(last_token)}$/, replace_token)
diff_arr[index+2].sub!(/#{Regexp.escape(last_token)}$/, replace_token)
end
def find_first_token(first_line, second_line)
max_length = [first_line.size, second_line.size].max
first_the_same_symbols = 0
(0..max_length + 1).each do |i|
first_the_same_symbols = i - 1
if first_token.empty? if first_line[i] != second_line[i] && i > 0
# In case if we remove string of spaces in commit break
diff_arr[index+1].sub!("-", "-" => "-#{START}")
diff_arr[index+2].sub!("+", "+" => "+#{START}")
else
diff_arr[index+1].sub!(first_token, first_token => start)
diff_arr[index+2].sub!(first_token, first_token => start)
end end
end
first_line[0..first_the_same_symbols][1..-1]
end
def find_last_token(first_line, second_line, first_token)
max_length = [first_line.size, second_line.size].max
last_the_same_symbols = 0
(1..max_length + 1).each do |i|
last_the_same_symbols = -i
shortest_line = second_line.size > first_line.size ? first_line : second_line
last_the_same_symbols = 0 if (first_line[-i] != second_line[-i]) || "#{first_token}#{START}".size == shortest_line[1..-i].size
(1..max_length + 1).each do |i| break
last_the_same_symbols = -i
shortest_line = second_line.size > first_line.size ? first_line : second_line
if ( first_line[-i] != second_line[-i] ) || "#{first_token}#{START}".size == shortest_line[1..-i].size
break
end
end end
last_the_same_symbols += 1
last_token = first_line[last_the_same_symbols..-1]
# This is tricky: escape backslashes so that `sub` doesn't interpret them
# as backreferences. Regexp.escape does NOT do the right thing.
replace_token = FINISH + last_token.gsub(/\\/, '\&\&')
diff_arr[index+1].sub!(/#{Regexp.escape(last_token)}$/, replace_token)
diff_arr[index+2].sub!(/#{Regexp.escape(last_token)}$/, replace_token)
end end
diff_arr
last_the_same_symbols += 1
first_line[last_the_same_symbols..-1]
end end
def _indexes_of_changed_lines(diff_arr) def _indexes_of_changed_lines(diff_arr)
......
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