Class: Hubstats::PullRequest
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Hubstats::PullRequest
- Defined in:
- app/models/hubstats/pull_request.rb
Class Method Summary collapse
-
.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.
-
.create_or_update(github_pull) ⇒ Object
Public - Makes a new pull request from a GitHub webhook.
-
.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.
-
.group_by(group) ⇒ Object
Public - Groups the PRs by either username or repo name, based on the string passed in.
- .record_timestamps ⇒ Object
-
.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.
-
.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.
Instance Method Summary collapse
-
#add_labels(labels) ⇒ Object
Public - Adds any labels to the labels database if the labels passed in aren’t already there.
-
#assign_team_from_user ⇒ Object
Public - Assigns a team to a pull request based on the user’s team.
-
#update_label(payload) ⇒ Object
Public - Adds/remove a label based on the the webhook action.
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
91 92 93 94 95 96 |
# File 'app/models/hubstats/pull_request.rb', line 91 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
41 42 43 44 45 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 |
# File 'app/models/hubstats/pull_request.rb', line 41 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
106 107 108 |
# File 'app/models/hubstats/pull_request.rb', line 106 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
133 134 135 136 137 138 139 140 141 |
# File 'app/models/hubstats/pull_request.rb', line 133 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 all end end |
.record_timestamps ⇒ Object
4 |
# File 'app/models/hubstats/pull_request.rb', line 4 def self.; 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
119 120 121 122 123 124 125 126 |
# File 'app/models/hubstats/pull_request.rb', line 119 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
77 78 79 80 81 |
# File 'app/models/hubstats/pull_request.rb', line 77 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
158 159 160 161 |
# File 'app/models/hubstats/pull_request.rb', line 158 def add_labels(labels) labels.map! { |label| Hubstats::Label.first_or_create(label) } self.labels = labels end |
#assign_team_from_user ⇒ Object
Public - Assigns a team to a pull request based on the user’s team
Returns - nothing
146 147 148 149 150 151 |
# File 'app/models/hubstats/pull_request.rb', line 146 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 |
#update_label(payload) ⇒ Object
Public - Adds/remove a label based on the the webhook action
166 167 168 169 170 171 172 173 174 175 |
# File 'app/models/hubstats/pull_request.rb', line 166 def update_label(payload) return unless payload[:label] label = Hubstats::Label.first_or_create(payload[:label]) if payload[:github_action] == 'labeled' labels << label elsif payload[:github_action] == 'unlabeled' labels.delete(label) end labels end |