Class: Hubstats::PullRequest

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_filtered(params, start_date, end_date) ⇒ Object



63
64
65
66
67
# File 'app/models/hubstats/pull_request.rb', line 63

def self.all_filtered(params, start_date, end_date)
  filter_based_on_date_range(start_date, end_date, params[:state])
   .belonging_to_users(params[:users])
   .belonging_to_repos(params[:repos])
end

.create_or_update(github_pull) ⇒ Object



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

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

  user = Hubstats::User.create_or_update(github_pull[:user])
  github_pull[:user_id] = user.id

  github_pull[:repository][:updated_at] = github_pull[:updated_at]
  repo = Hubstats::Repo.create_or_update(github_pull[:repository])
  github_pull[:repo_id] = repo.id

  pull_data = github_pull.slice(*column_names.map(&:to_sym))

  pull = where(:id => pull_data[:id]).first_or_create(pull_data)

  # Updates the merged_by part of the pull request and the user_id of the deploy
  if github_pull[:merged_by] && github_pull[:merged_by][:id]
    pull_data[:merged_by] = github_pull[:merged_by][:id]
    deploy = Hubstats::Deploy.where(id: pull.deploy_id, user_id: nil).first
      if deploy
        deploy.user_id = pull_data[:merged_by]
        deploy.save!
      end
  end

  return pull if pull.update_attributes(pull_data)
  Rails.logger.warn pull.errors.inspect
end

.filter_based_on_date_range(start_date, end_date, state) ⇒ Object



69
70
71
# File 'app/models/hubstats/pull_request.rb', line 69

def self.filter_based_on_date_range(start_date, end_date, state)
  with_state(state).updated_in_date_range(start_date, end_date)
end

.group_by(group) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'app/models/hubstats/pull_request.rb', line 82

def self.group_by(group)
  if group == 'user'
    with_user_name.order("user_name ASC")
  elsif group == 'repo'
    with_repo_name.order("repo_name asc")
  else
    scoped
  end
end

.state_based_order(start_date, end_date, state, order) ⇒ Object



73
74
75
76
77
78
79
80
# File 'app/models/hubstats/pull_request.rb', line 73

def self.state_based_order(start_date, end_date, state, order)
  order = ["ASC","DESC"].detect{|order_type| order_type.to_s == order.to_s.upcase } || "DESC"
  if state == "closed"
    with_state(state).updated_in_date_range(start_date, end_date).order("hubstats_pull_requests.closed_at #{order}")
  else #state == "open"
    with_state(state).updated_in_date_range(start_date, end_date).order("hubstats_pull_requests.created_at #{order}")
  end
end

Instance Method Details

#add_labels(labels) ⇒ Object



58
59
60
61
# File 'app/models/hubstats/pull_request.rb', line 58

def add_labels(labels)
  labels.map!{|label| Hubstats::Label.first_or_create(label) }
  self.labels = labels
end