forked from djmaze/stargazr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifier.rb
63 lines (57 loc) · 1.99 KB
/
notifier.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
$:.unshift '.'
require 'app'
Octokit::Client.module_eval do
def starred_by(user, options = {})
path = "users/#{user}/starred"
paginate path, options
end
end
# Set up caching
stack = Faraday::Builder.new do |builder|
# Enable log when debugging
#builder.response :logger
# Do not use Memcache at the moment (should not be needed as we want to refresh every time / day)
#builder.use Faraday::HttpCache, store: :mem_cache_store, store_options: ['localhost:11211']
builder.use Octokit::Response::RaiseError
builder.adapter Faraday.default_adapter
end
Octokit.middleware = stack
# Login
Octokit.configure do |c|
c.client_id = ENV['GITHUB_CLIENT_ID']
c.client_secret = ENV['GITHUB_CLIENT_SECRET']
c.auto_paginate = true
end
# Iterate through users
UsersCollection.all.each do |user|
puts "User #{user.username}"
# Grab the data
repositories_with_new_tags = Octokit.starred_by(user.username).collect do |repository_data|
repository = RepositoriesCollection.find_or_initialize_by_full_name repository_data.full_name
unless repository.fresh?
puts "Updating #{repository_data.full_name}"
repository.update
RepositoriesCollection.save(repository)
end
repository if repository.latest_tag_date.present? && repository.new_tag_since?(user.last_checked_at)
end.compact
now = Time.now
if repositories_with_new_tags.any?
vars = OpenStruct.new(repositories: repositories_with_new_tags, user: user)
Slim::Engine.set_default_options :pretty => true
text = Slim::Template.new('app/views/notification_mailer/user_notification.html.slim', {}).render(vars)
mail = Mail.new do
from ENV['FROM_EMAIL']
header['Reply-To'] = ENV['REPLY_TO_EMAIL'] || ENV['FROM_EMAIL']
to user.email
subject 'New releases in your starred repositories'
content_type 'text/html; charset=UTF-8'
body text
end
puts mail.to_s
mail.deliver
user.last_notified_at = now
end
user.last_checked_at = now
UsersCollection.replace user
end