Class: Hubstats::Comment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/hubstats/comment.rb

Class Method Summary collapse

Class Method Details

.create_or_update(github_comment) ⇒ Object

Public - Makes a new comment based on a GitHub webhook occurrence. Assigns the user and the PR.

github_comment - the information from Github about the comment



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/hubstats/comment.rb', line 32

def self.create_or_update(github_comment)
  github_comment = github_comment.to_h.with_indifferent_access if github_comment.respond_to? :to_h

  unless github_comment[:user]
    Rails.logger.warn "Found comment with no user, ignoring. GitHub comment ID: #{github_comment[:id]}"
    return nil
  end

  user = Hubstats::User.create_or_update(github_comment[:user])
  github_comment[:user_id] = user.id
  
  if github_comment[:pull_number]
    pull_request = Hubstats::PullRequest.belonging_to_repo(github_comment[:repo_id]).where(number: github_comment[:pull_number]).first
    if pull_request
      github_comment[:pull_request_id] = pull_request.id
    end
  end

  comment_data = github_comment.slice(*Hubstats::Comment.column_names.map(&:to_sym))

  comment = where(:id => comment_data[:id]).first_or_create(comment_data)
  return comment if comment.update_attributes(comment_data)
  Rails.logger.warn comment.errors.inspect
end

.record_timestampsObject



4
# File 'app/models/hubstats/comment.rb', line 4

def self.record_timestamps; false; end