-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.rb
49 lines (40 loc) · 1.13 KB
/
job.rb
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
require 'dotenv/load'
require 'postmark'
require './models'
require './client'
class Job
def self.perform
Alert.all.each do |alert|
current_sha = Client.head_sha(alert.owner, alert.repo, alert.branch)
commits = Client.compare(alert.owner, alert.repo, alert.last_sha, current_sha)
if commits.files.any?{|file| file.filename == alert.path }
alarm = Alarm.create(
alert_id: alert.id,
status: "unresolved",
first_sha: alert.last_sha,
second_sha: current_sha
)
send_notification(alarm)
end
alert.update(last_sha: current_sha)
end
end
def self.send_notification(alarm)
alert = alarm.alert
user = alert.user
if user.email
client = Postmark::ApiClient.new(ENV["POSTMARK_API_TOKEN"])
client.deliver(
from: '[email protected]',
to: user.email,
subject: 'Github Watcher - Notification',
text_body: "
#{alert.owner} | #{alert.repo}
#{alert.path} has changed
https://github.com/#{alert.owner}/#{alert.repo}/compare/#{alarm.first_sha}...#{alarm.second_sha}
"
)
end
end
end
Job.perform