fbe 0.0.32 → 0.0.34
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -1
- data/lib/fbe/middleware/quota.rb +58 -0
- data/lib/fbe/middleware.rb +28 -0
- data/lib/fbe/octo.rb +7 -0
- data/lib/fbe.rb +1 -1
- data/test/fbe/middleware/test_quota.rb +81 -0
- data/test/fbe/test_octo.rb +27 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7c1fa90f4347201bc63959d8a5063250310b6186108e616ed3c4bb7e2459624
|
4
|
+
data.tar.gz: 8e4361583f7d0bff9f158dccbc08640d9bd26b9c6b1fecb754abb2a352ada780
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ae17407c882da0104d175cba88c71f2b3847dc0b83b993474c7b37cd1a64b8181cf64d368ca6d265aea62e0da43a37bb2755b336438eb73926f267a936f9fcc
|
7
|
+
data.tar.gz: c581e42133dd5dd788f492e4d86dfb4235ea41109e3f059e98cdf1556a922721c6e9a81bd9d0c41d7a668c7d2ddf1a0ec696211dc24af4c9bdeefecafdb8f4a4
|
data/README.md
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# FactBase Extended (FBE)
|
2
2
|
|
3
3
|
[![DevOps By Rultor.com](http://www.rultor.com/b/zerocracy/fbe)](http://www.rultor.com/p/zerocracy/fbe)
|
4
|
-
[![We recommend RubyMine](https://www.elegantobjects.org/rubymine.svg)](https://www.jetbrains.com/ruby/)
|
5
4
|
|
6
5
|
[![rake](https://github.com/zerocracy/fbe/actions/workflows/rake.yml/badge.svg)](https://github.com/zerocracy/fbe/actions/workflows/rake.yml)
|
7
6
|
[![PDD status](http://www.0pdd.com/svg?name=zerocracy/fbe)](http://www.0pdd.com/p?name=zerocracy/fbe)
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# MIT License
|
4
|
+
#
|
5
|
+
# Copyright (c) 2024 Zerocracy
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
|
25
|
+
require 'faraday'
|
26
|
+
|
27
|
+
# Faraday Middleware that monitors GitHub API rate limits.
|
28
|
+
class Fbe::Middleware::Quota < Faraday::Middleware
|
29
|
+
def initialize(app, logger: Loog::NULL, pause: 60, limit: 100, rate: 5)
|
30
|
+
super(app)
|
31
|
+
@limit = limit
|
32
|
+
@requests = 0
|
33
|
+
@app = app
|
34
|
+
@logger = logger
|
35
|
+
@pause = pause
|
36
|
+
@rate = rate
|
37
|
+
end
|
38
|
+
|
39
|
+
def call(env)
|
40
|
+
@requests += 1
|
41
|
+
response = @app.call(env)
|
42
|
+
if out_of_limit?(env)
|
43
|
+
@logger.info(
|
44
|
+
"Too much GitHub API quota consumed, pausing for #{@pause} seconds"
|
45
|
+
)
|
46
|
+
sleep(@pause)
|
47
|
+
@requests = 0
|
48
|
+
end
|
49
|
+
response
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def out_of_limit?(env)
|
55
|
+
remaining = env.response_headers['x-ratelimit-remaining'].to_i
|
56
|
+
(@requests % @limit).zero? && remaining < @rate
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# MIT License
|
4
|
+
#
|
5
|
+
# Copyright (c) 2024 Zerocracy
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
|
25
|
+
# The module.
|
26
|
+
module Fbe::Middleware
|
27
|
+
# empty
|
28
|
+
end
|
data/lib/fbe/octo.rb
CHANGED
@@ -30,6 +30,8 @@ require 'verbose'
|
|
30
30
|
require 'faraday/http_cache'
|
31
31
|
require 'faraday/retry'
|
32
32
|
require_relative '../fbe'
|
33
|
+
require_relative 'middleware'
|
34
|
+
require_relative 'middleware/quota'
|
33
35
|
|
34
36
|
def Fbe.octo(options: $options, global: $global, loog: $loog)
|
35
37
|
raise 'The $global is not set' if global.nil?
|
@@ -75,6 +77,11 @@ def Fbe.octo(options: $options, global: $global, loog: $loog)
|
|
75
77
|
methods: [:get],
|
76
78
|
backoff_factor: 2
|
77
79
|
)
|
80
|
+
builder.use(
|
81
|
+
Fbe::Middleware::Quota,
|
82
|
+
logger: loog,
|
83
|
+
pause: options.github_api_pause
|
84
|
+
)
|
78
85
|
builder.use(Faraday::HttpCache, serializer: Marshal, shared_cache: false, logger: Loog::NULL)
|
79
86
|
builder.use(Octokit::Response::RaiseError)
|
80
87
|
builder.use(Faraday::Response::Logger, Loog::NULL)
|
data/lib/fbe.rb
CHANGED
@@ -0,0 +1,81 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# MIT License
|
4
|
+
#
|
5
|
+
# Copyright (c) 2024 Zerocracy
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
|
25
|
+
require 'minitest/autorun'
|
26
|
+
require 'faraday'
|
27
|
+
require 'logger'
|
28
|
+
require_relative '../../../lib/fbe/middleware'
|
29
|
+
|
30
|
+
class QuotaTest < Minitest::Test
|
31
|
+
class FakeApp
|
32
|
+
def initialize
|
33
|
+
@calls = 0
|
34
|
+
end
|
35
|
+
|
36
|
+
def call(env)
|
37
|
+
@calls += 1
|
38
|
+
response_headers = {
|
39
|
+
'x-ratelimit-remaining' => (100 - @calls).to_s
|
40
|
+
}
|
41
|
+
env[:response_headers] = response_headers
|
42
|
+
env
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_quota_middleware_pauses_when_quota_low
|
47
|
+
loog = Loog::NULL
|
48
|
+
pause = 0
|
49
|
+
app = FakeApp.new
|
50
|
+
middleware = Fbe::Middleware::Quota.new(app, logger: loog, pause:)
|
51
|
+
start_time = Time.now
|
52
|
+
105.times do
|
53
|
+
env = Judges::Options.new(
|
54
|
+
'method' => :get,
|
55
|
+
'url' => 'http://example.com',
|
56
|
+
'request_headers' => {},
|
57
|
+
'response_headers' => {}
|
58
|
+
)
|
59
|
+
middleware.call(env)
|
60
|
+
end
|
61
|
+
assert_in_delta pause, Time.now - start_time, 0.4
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_quota_middleware_logs_when_quota_low
|
65
|
+
pause = 1
|
66
|
+
log_output = StringIO.new
|
67
|
+
loog = Logger.new(log_output)
|
68
|
+
app = FakeApp.new
|
69
|
+
middleware = Fbe::Middleware::Quota.new(app, logger: loog, pause:)
|
70
|
+
105.times do
|
71
|
+
env = Judges::Options.new(
|
72
|
+
'method' => :get,
|
73
|
+
'url' => 'http://example.com',
|
74
|
+
'request_headers' => {},
|
75
|
+
'response_headers' => {}
|
76
|
+
)
|
77
|
+
middleware.call(env)
|
78
|
+
end
|
79
|
+
assert_match(/Too much GitHub API quota/, log_output.string)
|
80
|
+
end
|
81
|
+
end
|
data/test/fbe/test_octo.rb
CHANGED
@@ -117,4 +117,31 @@ class TestOcto < Minitest::Test
|
|
117
117
|
assert_equal(1, o.commit_pulls('zerocracy/fbe', '0b7d0699bd744b62c0731064c2adaad0c58e1416').size)
|
118
118
|
assert_equal(0, o.commit_pulls('zerocracy/fbe', '16b3ea6b71c6e932ba7666c40ca846ecaa6d6f0d').size)
|
119
119
|
end
|
120
|
+
|
121
|
+
def test_pauses_when_quota_is_exceeded
|
122
|
+
WebMock.disable_net_connect!
|
123
|
+
global = {}
|
124
|
+
pause = 1
|
125
|
+
o = Fbe.octo(loog: Loog::NULL, global:, options: Judges::Options.new({ 'github_api_pause' => pause }))
|
126
|
+
limit = 100
|
127
|
+
start_time = Time.now
|
128
|
+
105.times do |i|
|
129
|
+
n = i + 1
|
130
|
+
user = "test#{n}"
|
131
|
+
limit = 100 if n > 100
|
132
|
+
stub_request(:get, "https://api.github.com/users/#{user}")
|
133
|
+
.then
|
134
|
+
.to_return(
|
135
|
+
status: 200, body: '{}',
|
136
|
+
headers: {
|
137
|
+
'x-ratelimit-remaining' => limit.to_s
|
138
|
+
}
|
139
|
+
)
|
140
|
+
.times(1)
|
141
|
+
o.user(user)
|
142
|
+
assert(!o.off_quota) if n > 100
|
143
|
+
limit -= 1
|
144
|
+
end
|
145
|
+
assert_in_delta(pause, Time.now - start_time, 0.4)
|
146
|
+
end
|
120
147
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fbe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.34
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yegor Bugayenko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-07-
|
11
|
+
date: 2024-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: backtrace
|
@@ -230,12 +230,15 @@ files:
|
|
230
230
|
- lib/fbe/if_absent.rb
|
231
231
|
- lib/fbe/issue.rb
|
232
232
|
- lib/fbe/iterate.rb
|
233
|
+
- lib/fbe/middleware.rb
|
234
|
+
- lib/fbe/middleware/quota.rb
|
233
235
|
- lib/fbe/octo.rb
|
234
236
|
- lib/fbe/sec.rb
|
235
237
|
- lib/fbe/unmask_repos.rb
|
236
238
|
- lib/fbe/who.rb
|
237
239
|
- renovate.json
|
238
240
|
- rules/basic.fe
|
241
|
+
- test/fbe/middleware/test_quota.rb
|
239
242
|
- test/fbe/test_award.rb
|
240
243
|
- test/fbe/test_conclude.rb
|
241
244
|
- test/fbe/test_fb.rb
|