-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathactivity_checker.rb
131 lines (115 loc) · 4.15 KB
/
activity_checker.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
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
require 'rest-client'
require 'digest'
require 'time_difference'
require 'logger'
require_relative 'octokit_client'
require_relative 'main_controller'
require_relative 'config/config_reader'
require_relative 'slack_client'
class ActivityChecker
def initialize
@config_reader = Config_reader.new
@conf = @config_reader.get_activity_checker_configuration
@repos = @config_reader.get_repos
@controller = MainController.new
@client = OctokitClient.new
@slack_client = SlackClient.new
@logger = Logger.new('../see_through.log')
@token = ENV['SEE_THROUGH_TOKEN']
end
def check_activity
@logger.info('activity checker start')
@config_reader.get_users_from_config_yml.each do |user|
@controller.sync_user_with_config user
end
@repos.each do |repository|
current_time = Time.now.utc
repo = repository.repository_name
prs = @client.get_all_github_pr(repo)
prs.each do |pr|
pr_labels = get_pr_labels(repo, pr[:number])
has_label_for_ignoring = false
pr_labels.each do |label|
if @conf[:labels].include? label
has_label_for_ignoring = true
break
end
end
if has_label_for_ignoring
next
end
pr_github_diff_sha = get_pr_diff_sha(repo, pr)
db_pr = @controller.get_pr_by_id(pr.number).first
if !db_pr
@controller.create_or_update_pr(pr, repo)
db_pr = @controller.get_pr_by_id(pr.number).first
end
if !db_pr[:diff_sha]
pr_diff_updated = pr.updated_at
else
pr_diff_updated = current_time
end
if !db_pr[:diff_sha] or db_pr[:diff_sha] != pr_github_diff_sha
@controller.update_pr_diff_sha(db_pr.pr_id, pr_github_diff_sha, pr_diff_updated)
else
days_without_diff_update = TimeDifference.between(db_pr[:diff_updated], current_time).in_days.to_i
if days_without_diff_update >= @conf[:timeout]
if pr_need_notification?(db_pr, current_time, days_without_diff_update)
@controller.update_pr_notified_at(db_pr[:pr_id], current_time)
pr_notificants = @conf[:recipients].to_set
user = @controller.get_user_by_login(db_pr[:author])
if user and user[:enable]
pr_notificants << user[:slack_id]
end
pr_notificants.each do |receiver|
create_slack_notification_on_outdated_pr(repo, db_pr, days_without_diff_update, receiver)
end
end
end
end
end
end
@logger.info('activity checker end')
end
def pr_need_notification?(pr, current_time, days_inactive)
if !pr[:notified_at]
return true
end
notify = false
if days_inactive <= 30
if TimeDifference.between(pr[:notified_at], current_time).in_days.to_i >= 3
notify = true
end
elsif days_inactive > 30
if TimeDifference.between(pr[:notified_at], current_time).in_days.to_i >= 10
notify = true
end
end
notify
end
def get_pr_diff_sha(repo, pr)
diff = RestClient.get("https://#{@token}:x-oauth-basic@api.github.com/repos/#{repo}/pulls/#{pr.number}.diff",
{
:accept => 'application/vnd.github.diff'
})
Digest::SHA1.hexdigest(diff.body)
end
def get_pr_labels(repo, number)
pr_labels = CLIENT.issue(repo, number)[:labels]
labels = pr_labels.map {|label| label[:name]}
labels
end
def create_slack_notification_on_outdated_pr(repo, pr, days_without_update, recipient)
attachments = [{
fallback: "Outdated Pull Request",
title: "##{pr.pr_id} - #{pr.title}",
title_link: "https://github.com/#{repo}/pull/#{pr.pr_id}/",
text: "This pull request hasn't been updated for #{days_without_update} days.",
mrkdwn_in: [
"text",
"pretext"]
}]
@slack_client.send_message(attachments, recipient)
end
end
ActivityChecker.new.check_activity