forked from sigmike/peer4commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.rb
More file actions
157 lines (133 loc) · 4.17 KB
/
project.rb
File metadata and controls
157 lines (133 loc) · 4.17 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
class Project < ActiveRecord::Base
has_many :deposits # todo: only confirmed deposits that have amount > paid_out
has_many :tips
validates :full_name, uniqueness: true, presence: true
validates :github_id, uniqueness: true, presence: true
def update_github_info repo
self.github_id = repo.id
self.name = repo.name
self.full_name = repo.full_name
self.source_full_name = repo.source.full_name rescue ''
self.description = repo.description
self.watchers_count = repo.watchers_count
self.language = repo.language
self.save!
end
def github_url
"https://github.com/#{full_name}"
end
def source_github_url
"https://github.com/#{source_full_name}"
end
def new_commits
begin
commits = Timeout::timeout(90) do
client = Octokit::Client.new \
:client_id => CONFIG['github']['key'],
:client_secret => CONFIG['github']['secret'],
:per_page => 100
client.commits(full_name).
# Filter merge request
select{|c| !(c.commit.message =~ /^(Merge\s|auto\smerge)/)}.
# Filter fake emails
select{|c| c.commit.author.email =~ Devise::email_regexp }.
# Filter commited after t4c project creation
select{|c| c.commit.committer.date > self.deposits.first.created_at }.
to_a
end
rescue Octokit::BadGateway, Octokit::NotFound, Octokit::InternalServerError,
Errno::ETIMEDOUT, Net::ReadTimeout, Faraday::Error::ConnectionFailed => e
Rails.logger.info "Project ##{id}: #{e.class} happened"
rescue StandardError => e
Airbrake.notify(e)
end
sleep(1)
commits || []
end
def tip_commits
new_commits.each do |commit|
Project.transaction do
tip_for commit
update_attribute :last_commit, commit.sha
end
end
end
def tip_for commit
email = commit.commit.author.email
user = User.find_by email: email
if (next_tip_amount > 0) &&
Tip.find_by_commit(commit.sha).nil?
# create user
unless user
generated_password = Devise.friendly_token.first(8)
user = User.create({
email: email,
password: generated_password,
name: commit.commit.author.name,
nickname: (commit.author.login rescue nil)
})
end
if commit.author && commit.author.login
user.update nickname: commit.author.login
end
# create tip
tip = Tip.create({
project: self,
user: user,
amount: next_tip_amount,
commit: commit.sha
})
# notify user
if tip && user.bitcoin_address.blank? && !user.unsubscribed
if !user.notified_at || (user.notified_at < (Time.now - 30.days))
UserMailer.new_tip(user, tip).deliver
user.touch :notified_at
end
end
Rails.logger.info " Tip created #{tip.inspect}"
end
end
def available_amount
self.deposits.where("confirmations > 0").map(&:available_amount).sum - tips_paid_amount
end
def unconfirmed_amount
self.deposits.where(:confirmations => 0).where('created_at > ?', 7.days.ago).map(&:available_amount).sum
end
def tips_paid_amount
self.tips.non_refunded.sum(:amount)
end
def tips_paid_unclaimed_amount
self.tips.non_refunded.unclaimed.sum(:amount)
end
def next_tip_amount
(CONFIG["tip"]*available_amount).ceil
end
def update_cache
update available_amount_cache: available_amount
end
def self.update_cache
find_each do |project|
project.update_cache
end
end
def github_info
client = Octokit::Client.new \
:client_id => CONFIG['github']['key'],
:client_secret => CONFIG['github']['secret']
if github_id.present?
client.get("/repositories/#{github_id}")
else
client.repo(full_name)
end
end
def update_info
begin
update_github_info(github_info)
rescue Octokit::BadGateway, Octokit::NotFound, Octokit::InternalServerError,
Errno::ETIMEDOUT, Net::ReadTimeout, Faraday::Error::ConnectionFailed => e
Rails.logger.info "Project ##{id}: #{e.class} happened"
rescue StandardError => e
Airbrake.notify(e)
end
end
end