Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor subject model #2070

Merged
merged 3 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about having an instance and a class method with the same name? We can change this to something else if we like I'm just wary of changing too much in one PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind that, I've done it a lot in the past

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