-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathvotable.rb
More file actions
86 lines (75 loc) · 2.19 KB
/
Copy pathvotable.rb
File metadata and controls
86 lines (75 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# frozen_string_literal: true
#
# Methods for models that can be voted on
#
module Votable
extend ActiveSupport::Concern
def queue_vote_count
VoteCounterJob.perform_later self
end
def score
weights = {
yes: Settings.yes_weight,
no: Settings.no_weight,
block: Settings.block_weight,
}
interactions.all.inject(0) do |sum, i|
sum + (weights[i.last_vote.try(:to_sym)] || 0)
end
end
def passed?
score >= Settings.pass_threshold
end
def blocked?
score < Settings.block_threshold
end
def count_votes!
comments = github_comments
# Post instructions if they're not already there
if !instructions_posted?(comments) && !pr_closed?
post_instructions
end
# Count up all the votes
count_votes_in_comments(comments)
# Update the state flag
update_state!
# Set build statuses on github
unless closed?
set_vote_build_status
set_time_build_status
end
end
private
INSTRUCTION_HEADER = "<!-- votebot instructions -->"
def instructions_posted?(comments)
instructions_found = false
comments.each do |c|
if Regexp.new(INSTRUCTION_HEADER).match?(c.body)
instructions_found = true
end
end
instructions_found
end
def count_vote_in_comment(comment, time_of_last_commit)
# Skip instructions
return if Regexp.new(INSTRUCTION_HEADER).match?(comment.body)
# Ignore proposer and non-voters
user = User.find_or_create_by!(login: comment.user.login)
return if user == proposer || !user.can_vote?
# Store vote in an interaction record
interaction = interactions.find_or_create_by!(user: user)
interaction.set_last_vote_from_comment!(comment, time_of_last_commit)
end
def count_votes_in_comments(comments)
comments.each { |c| count_vote_in_comment(c, time_of_last_commit) }
end
def post_instructions
vars = {
site_url: ENV.fetch("SITE_URL"),
proposal_number: number,
repo: ENV.fetch("GITHUB_REPO"),
proposer: proposer.login,
}.merge(Settings)
github_add_comment [INSTRUCTION_HEADER, I18n.t("help.instruction_comment", vars)].join("\n")
end
end