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

Public - Filters all of the pull requests between start_date and end_date that are the given state (passed as part of params) that belong to the params’ users and repos.

params - the params that are passed in (likely from the URL) start_date - the start of the date range end_date - the end of the data range

Returns - the PRs that fit all of the below sql queries



96
97
98
99
100
101
# File 'app/models/hubstats/pull_request.rb', line 96

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])
   .belonging_to_teams(params[:teams])
end

.create_or_update(github_pull) ⇒ Object

Public - Makes a new pull request from a GitHub webhook. Finds user_id and repo_id based on users and repos that are already in the Hubstats database. Updates the user_id of a deploy if the pull request has been merged in a deploy. If the user who makes the PR has a team, then it will update the team_id of the PR to match the team the user belongs to.

github_pull - the info that is from Github about the new or updated pull request

Returns - the pull request



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/hubstats/pull_request.rb', line 46

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)

  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

  if user.team && user.team.id
    pull_data[:team_id] = user.team.id
  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

Public - Finds all of the PRs with the current state, and then filters to ones that have been updated in between the start_date and end_date.

start_date - the start of the date range end_date - the end of the data range state - the current state (open, closed, or all) of the PRs

Returns - the PRs that are within the date range and that are the state



111
112
113
# File 'app/models/hubstats/pull_request.rb', line 111

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

Public - Groups the PRs by either username or repo name, based on the string passed in.

group - string that is the designated grouping

Returns - the data grouped



138
139
140
141
142
143
144
145
146
# File 'app/models/hubstats/pull_request.rb', line 138

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

.record_timestampsObject



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

def self.record_timestamps; false; end

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

Public - Finds all of the PRs that have the current state, then filters to PRs that were updated within the start_date and end_date. Lastly, it orders all of them based on the given order.

start_date - the start of the date range end_date - the end of the data range state - the current state (open, closed, or all) of the PRs order - the order (descending or ascending) that the PRs should be ordered in

Returns - the PRs that are within the date range and that are the state ordered



124
125
126
127
128
129
130
131
# File 'app/models/hubstats/pull_request.rb', line 124

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

.update_teams_in_pulls(days) ⇒ Object

Public - Takes days, a number, and updates all pull requests’ team_ids that were created since that many days ago

days - the amount of days ago that we wish to grab the number of pull requests since

Returns - nothing



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

def self.update_teams_in_pulls(days)
  pulls = created_since(days)
  pulls.each {|pull| pull.assign_team_from_user}
  puts "Finished updating the teams of past pull requests"
end

Instance Method Details

#add_labels(labels) ⇒ Object

Public - Adds any labels to the labels database if the labels passed in aren’t already there.

labels - the labels to be added to the db

Returns - the new labels



163
164
165
166
# File 'app/models/hubstats/pull_request.rb', line 163

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

#assign_team_from_userObject

Public - Assigns a team to a pull request based on the user’s team

Returns - nothing



151
152
153
154
155
156
# File 'app/models/hubstats/pull_request.rb', line 151

def assign_team_from_user
  user = Hubstats::User.find(self.user_id)
  if user.team && user.team.id
    self.update_column(:team_id, user.team.id)
  end
end