-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatabase.rb
170 lines (135 loc) · 4.05 KB
/
database.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
require 'active_record'
require 'logger'
class Database
def initialize
init_database
@logger = Logger.new('../see_through.log')
end
class User < ActiveRecord::Base
belongs_to :commentors
end
class PullRequest < ActiveRecord::Base
has_many :commentors
end
class Commentor < ActiveRecord::Base
belongs_to :pull_request
has_one :users
end
class Repository < ActiveRecord::Base
end
class DailyReport < ActiveRecord::Base
end
def create_pull_request (pull_request_data, repo, pr)
PullRequest.create(
:repo => repo,
:title => pull_request_data[:title],
:pr_id => pull_request_data[:number],
:author => pull_request_data[:user][:login],
:merged => pr.merged,
:mergeable => pr.mergeable,
:mergeable_state => pr.mergeable_state,
:state => pull_request_data[:state],
# :pr_commentors => pull_request_data[:commentors].to_a.join(', '),
:committer => pull_request_data[:committer].to_a.join(', '),
:labels => pr.head.label,
:pr_create_time => pull_request_data[:created_at],
:pr_update_time => pull_request_data[:updated_at],
:added_to_database => Time.new,
)
# commentors = pull_request_data[:commentors].to_a
# build_list_of_commentors commentors
@logger.info("Pull request##{pull_request_data[:number]} was added to database!")
end
def create_new_user (user)
User.create(
:user_login => user.login,
:user_email => user.email,
:notify_at => user.tz_shift,
:git_hub_id => user.id,
:slack_id => user.slack_id,
:enable => false,
)
end
def create_daily_report (user_login, sent_time)
DailyReport.create(
:user_name => user_login,
:sent_at => sent_time,
)
end
def create_repository (repository)
Repository.create(
:repo => repository
)
end
def update_daily_report_date (user_login, sent_time)
dr = DailyReport.where(user_name: user_login).first
dr.update(:sent_at => sent_time)
end
def update_pull_request_state (pull_request, state)
pull_request.update(state: state)
end
def update_pr_migration_conflict(pr_id, has_migration_conflict)
pr = get_pull_request_by_id(pr_id.to_i)
pr.update(has_migration_conflict: has_migration_conflict)
end
def update_pr_diff_sha(number, diff_sha, diff_updated)
PullRequest.where(pr_id: number).first.update({
:diff_sha => diff_sha,
:diff_updated => diff_updated
})
end
def update_pr_notified_at(id, notified_at)
PullRequest.where(pr_id: id).first.update(notified_at: notified_at)
end
# Getters
def get_daily_report_state (user_login)
DailyReport.where(user_name: user_login).first
end
def get_daily_reports_state
DailyReport.all
end
def get_repo_pr_by_state (repo, state)
PullRequest.where(repo: repo, state: state)
end
def get_pull_requests_by_repo (repo)
PullRequest.where(repo: repo)
end
def get_all_repositories
Repository.all
end
def get_repo_prs_with_migration_conflict(repo)
PullRequest.where(repo: repo, state: 'open', has_migration_conflict: true)
end
def get_repo_pr_by_mergeable (repo, state)
PullRequest.where(repo: repo, mergeable: state)
end
def get_pull_requests_by_mergeable (state)
PullRequest.where(mergeable: state)
end
def get_pull_requests_by_id (id)
PullRequest.where(pr_id: id)
end
def get_all_pull_requests
PullRequest.all
end
def get_pull_requests_by_state (state)
PullRequest.all.where(state: state)
end
def get_pull_request_by_id (pull_request_id)
PullRequest.find_by(pr_id: pull_request_id)
end
def get_recipients
User.all.where(enable: true)
end
def get_user_by_login (login)
User.where(user_login: login).take
end
def init_database
ActiveRecord::Base.logger = Logger.new(File.open('../database.log', 'w'))
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => '../db/data.db'
)
ActiveRecord::Schema.new.migrations_paths
end
end