Class: Fluent::Plugin::GithubActivities::Crawler
- Inherits:
-
Object
- Object
- Fluent::Plugin::GithubActivities::Crawler
show all
- Defined in:
- lib/fluent/plugin/github-activities/crawler.rb
Defined Under Namespace
Classes: EmptyRequestQueue
Constant Summary
collapse
- NO_INTERVAL =
0
- DEFAULT_INTERVAL =
1
- RELATED_USER_IMAGE_KEY =
"$github-activities-related-avatar"
- RELATED_ORGANIZATION_IMAGE_KEY =
"$github-activities-related-organization-logo"
- RELATED_EVENT =
"$github-activities-related-event"
- MERGE_COMMIT_MESSAGE_PATTERN =
/\AMerge pull request #\d+ from [^\/]+\/[^\/]+\n\n/
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ Crawler
Returns a new instance of Crawler.
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 44
def initialize(options={})
@users_manager = UsersManager.new(users: options[:watching_users],
pos_storage: options[:pos_storage])
@access_token = options[:access_token]
@watching_users = options[:watching_users] || []
@include_commits_from_pull_request = options[:include_commits_from_pull_request]
@include_foreign_commits = options[:include_foreign_commits]
@request_queue = options[:request_queue] || []
@default_interval = options[:default_interval] || DEFAULT_INTERVAL
@interval_for_next_request = @default_interval
@log = options[:log]
@running = true
end
|
Instance Attribute Details
#interval_for_next_request ⇒ Object
Returns the value of attribute interval_for_next_request.
42
43
44
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 42
def interval_for_next_request
@interval_for_next_request
end
|
#log ⇒ Object
Returns the value of attribute log.
42
43
44
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 42
def log
@log
end
|
#on_emit=(value) ⇒ Object
Sets the attribute on_emit
41
42
43
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 41
def on_emit=(value)
@on_emit = value
end
|
#request_queue ⇒ Object
Returns the value of attribute request_queue.
42
43
44
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 42
def request_queue
@request_queue
end
|
#running ⇒ Object
Returns the value of attribute running.
42
43
44
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 42
def running
@running
end
|
Instance Method Details
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 132
def (request)
= {}
if request[:previous_entity_tag]
["If-None-Match"] = request[:previous_entity_tag]
elsif request[:type] == TYPE_EVENTS
position = @users_manager.position_for(request[:user])
if position
entity_tag = position["entity_tag"]
["If-None-Match"] = entity_tag if entity_tag
end
end
end
|
#process_commit(commit, push_event) ⇒ Object
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 214
def process_commit(commit, push_event)
log.debug("GithubActivities::Crawler: processing commit #{commit["sha"]}")
user = commit["author"]["login"]
if user and (@include_foreign_commits or watching_user?(user))
commit[RELATED_USER_IMAGE_KEY] = push_event["actor"]["avatar_url"]
if push_event["org"]
commit[RELATED_ORGANIZATION_IMAGE_KEY] = push_event["org"]["avatar_url"]
end
commit[RELATED_EVENT] = push_event.reject {|k, _| k == "payload" }
emit("commit", commit)
end
commit_refs = push_event["payload"]["commits"]
target_commit_ref = commit_refs.find do |commit_ref|
commit_ref["sha"] == commit["sha"]
end
target_commit_ref["commit"] = commit if target_commit_ref
completely_fetched = commit_refs.all? do |commit_ref|
commit_ref["commit"]
end
emit("push", push_event) if completely_fetched
end
|
#process_create_event(event) ⇒ Object
302
303
304
305
306
307
308
309
310
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 302
def process_create_event(event)
payload = event["payload"]
case payload["ref_type"]
when "branch"
emit("branch", event)
when "tag"
emit("tag", event)
end
end
|
#process_issue_event(event) ⇒ Object
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 243
def process_issue_event(event)
payload = event["payload"]
case payload["action"]
when "opened"
emit("issue-open", event)
when "closed"
emit("issue-close", event)
when "reopened"
emit("issue-reopen", event)
when "assigned"
emit("issue-assign", event)
when "unassigned"
emit("issue-unassign", event)
when "labeled"
emit("issue-label", event)
when "unlabeled"
emit("issue-unlabel", event)
end
end
|
292
293
294
295
296
297
298
299
300
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 292
def (event)
payload = event["payload"]
if payload["issue"]["pull_request"]
emit("pull-request-comment", event)
else
emit("issue-comment", event)
end
end
|
#process_pull_request_event(event) ⇒ Object
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 263
def process_pull_request_event(event)
payload = event["payload"]
case payload["action"]
when "opened"
emit("pull-request", event)
when "closed"
if payload["pull_request"]["merged"]
emit("pull-request-merged", event)
else
emit("pull-request-cancelled", event)
end
when "reopened"
emit("pull-request-reopen", event)
end
end
|
#process_push_event(event) ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 197
def process_push_event(event)
return unless @running
payload = event["payload"]
commit_refs = payload["commits"]
if !@include_commits_from_pull_request and
push_event_from_merged_pull_request?(event)
return
end
commit_refs.reverse.each do |commit_ref|
@request_queue.push(type: TYPE_COMMIT,
uri: commit_ref["url"],
sha: commit_ref["sha"],
push: event)
end
end
|
#process_request ⇒ Object
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
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 64
def process_request
request = @request_queue.shift
log.debug("GithubActivities::Crawler: processing request: #{request.inspect}")
if request[:process_after] and
Time.now.to_i < request[:process_after]
@request_queue.push(request)
@interval_for_next_request = NO_INTERVAL
return false
end
uri = request_uri(request)
= (request)
log.debug("GithubActivities::Crawler: requesting to #{uri.inspect}")
response = http_get(uri, )
log.debug("GithubActivities::Crawler: response: #{response.inspect}")
case response
when Net::HTTPSuccess
log.trace("GithubActivities::Crawler: Net::HTTPSuccess / request type: #{request[:type]}")
body = JSON.parse(response.body)
case request[:type]
when TYPE_EVENTS
events = body
log.trace("GithubActivities::Crawler: events size: #{events.size}")
process_user_events(request[:user], events)
reserve_user_events(request[:user], previous_response: response)
@users_manager.save_position_for(request[:user], entity_tag: response["ETag"])
when TYPE_COMMIT
process_commit(body, request[:push])
end
when Net::HTTPNotModified
log.trace("GithubActivities::Crawler: Net::HTTPNotModified / request type: #{request[:type]}")
case request[:type]
when TYPE_EVENTS
reserve_user_events(request[:user],
previous_response: response,
previous_entity_tag: ["If-None-Match"])
end
@interval_for_next_request = @default_interval
return true
else
log.trace("GithubActivities::Crawler: UnknownType / request type: #{request[:type]}")
case request[:type]
when TYPE_COMMIT
fake_body = {
"sha" => request[:sha],
"author" => {},
}
process_commit(fake_body, request[:push])
end
end
@interval_for_next_request = @default_interval
return true
rescue StandardError => error
log.error(error.inspect)
end
|
#process_user_event(user, event) ⇒ Object
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 169
def process_user_event(user, event)
event[RELATED_USER_IMAGE_KEY] = event["actor"]["avatar_url"]
if event["org"]
event[RELATED_ORGANIZATION_IMAGE_KEY] = event["org"]["avatar_url"]
end
case event["type"]
when "PushEvent"
process_push_event(event)
when "CommitCommentEvent"
emit("commit-comment", event)
when "IssuesEvent"
process_issue_event(event)
when "IssueCommentEvent"
(event)
when "ForkEvent"
emit("fork", event)
when "PullRequestEvent"
process_pull_request_event(event)
when "CreateEvent"
process_create_event(event)
else
emit(event["type"], event)
end
rescue StandardError => error
log.fatal(error)
end
|
#process_user_events(user, events) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 151
def process_user_events(user, events)
last_event_timestamp = UsersManager::DEFAULT_LAST_EVENT_TIMESTAMP
position = @users_manager.position_for(user)
if position and position["last_event_timestamp"]
last_event_timestamp = position["last_event_timestamp"]
end
events = events.sort do |a, b|
b["created_at"] <=> a["created_at"]
end
events.each do |event|
timestamp = Time.parse(event["created_at"]).to_i
next if timestamp <= last_event_timestamp
process_user_event(user, event)
@users_manager.save_position_for(user, last_event_timestamp: timestamp)
end
end
|
#push_event_from_merged_pull_request?(event) ⇒ Boolean
281
282
283
284
285
286
287
288
289
290
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 281
def push_event_from_merged_pull_request?(event)
payload = event["payload"]
inserted_requests = []
commit_refs = payload["commits"]
if MERGE_COMMIT_MESSAGE_PATTERN =~ commit_refs.last["message"]
true
else
false
end
end
|
#request_uri(request) ⇒ Object
122
123
124
125
126
127
128
129
130
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 122
def request_uri(request)
uri = nil
case request[:type]
when TYPE_EVENTS
uri = user_activities(request[:user])
else
uri = request[:uri]
end
end
|
#reserve_user_events(user, options = {}) ⇒ Object
146
147
148
149
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 146
def reserve_user_events(user, options={})
request = @users_manager.new_events_request(user, options)
@request_queue.push(request) if @running
end
|
#stop ⇒ Object
312
313
314
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 312
def stop
@running = false
end
|
#watching_user?(user) ⇒ Boolean
239
240
241
|
# File 'lib/fluent/plugin/github-activities/crawler.rb', line 239
def watching_user?(user)
@watching_users.include?(user)
end
|