Skip to content

Commit

Permalink
Extract sync instance method (#2070)
Browse files Browse the repository at this point in the history
Lots of the stuff going on inside the Subject.sync method is acting on
an instance and not doing things at the class level. This commit extracts
a Subject#sync instance method to make things more object oriented and
to allow us to start extracting the logic out of this method. This
should hopefully make things easier to understand what's going on.
  • Loading branch information
AndyStabler authored and andrew committed Oct 25, 2019
1 parent f74c44d commit 8ef8d9b
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 108 deletions.
29 changes: 16 additions & 13 deletions app/models/subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ def sync_involved_users
involved_user_ids.each { |user_id| SyncNotificationsWorker.perform_in(1.minutes, user_id) }
end

def self.sync(remote_subject)
subject = Subject.find_or_create_by(url: remote_subject['url'])

def sync(remote_subject)
# webhook payloads don't always have 'repository' info
if remote_subject['repository']
full_name = remote_subject['repository']['full_name']
Expand All @@ -56,31 +54,36 @@ def self.sync(remote_subject)
full_name = extract_full_name(remote_subject['url'])
end

comment_count = remote_subject['comments'] || remote_subject.fetch('commit', {})['comment_count']
comment_count = subject.comment_count if comment_count.nil?
updated_comment_count = remote_subject['comments'] || remote_subject.fetch('commit', {})['comment_count']
updated_comment_count = comment_count if updated_comment_count.nil?

subject.update({
update({
repository_full_name: full_name,
github_id: remote_subject['id'],
state: remote_subject['merged_at'].present? ? 'merged' : remote_subject['state'],
author: remote_subject.fetch('user', {})['login'],
html_url: remote_subject['html_url'],
created_at: remote_subject['created_at'] || Time.current,
updated_at: remote_subject['updated_at'] || Time.current,
comment_count: comment_count,
comment_count: updated_comment_count,
assignees: ":#{Array(remote_subject['assignees'].try(:map) {|a| a['login'] }).join(':')}:",
locked: remote_subject['locked'],
sha: remote_subject.fetch('head', {})['sha'],
body: remote_subject['body'].try(:gsub, "\u0000", ''),
draft: remote_subject['draft']
})

return unless subject.persisted?
return unless persisted?

subject.update_labels(remote_subject['labels']) if remote_subject['labels'].present?
subject.update_comments if Octobox.include_comments? && (subject.has_comments? || subject.pull_request?)
subject.update_status
subject.sync_involved_users if (subject.saved_changes.keys & subject.notifiable_fields).any?
update_labels(remote_subject['labels']) if remote_subject['labels'].present?
update_comments if Octobox.include_comments? && (has_comments? || pull_request?)
update_status
sync_involved_users if (saved_changes.keys & notifiable_fields).any?
end

def self.sync(remote_subject)
subject = Subject.find_or_create_by(url: remote_subject['url'])
subject.sync(remote_subject)
end

def self.sync_status(sha, repository_full_name)
Expand Down Expand Up @@ -243,7 +246,7 @@ def github_client
end
end

def self.extract_full_name(url)
def extract_full_name(url)
url.match(/\/repos\/([\w.-]+\/[\w.-]+)\//)[1]
end

Expand Down

0 comments on commit 8ef8d9b

Please sign in to comment.