aireview 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +46 -0
- data/README.md +215 -79
- data/config/defaults.yml +6 -7
- data/lib/aireview/candidate_checker.rb +0 -1
- data/lib/aireview/cli.rb +91 -63
- data/lib/aireview/config.rb +42 -4
- data/lib/aireview/config_fallbacks.rb +8 -11
- data/lib/aireview/config_layers.rb +6 -4
- data/lib/aireview/config_loader.rb +31 -23
- data/lib/aireview/context_budget.rb +6 -2
- data/lib/aireview/context_builder.rb +1 -0
- data/lib/aireview/dry_run_report.rb +3 -0
- data/lib/aireview/github_client.rb +284 -0
- data/lib/aireview/jev_stage.rb +19 -2
- data/lib/aireview/llm_client.rb +40 -14
- data/lib/aireview/llm_router.rb +69 -90
- data/lib/aireview/model_candidate.rb +6 -3
- data/lib/aireview/model_pool.rb +6 -0
- data/lib/aireview/model_state.rb +5 -17
- data/lib/aireview/mr_parser.rb +44 -6
- data/lib/aireview/platform.rb +99 -0
- data/lib/aireview/publisher.rb +8 -7
- data/lib/aireview/review_comment_formatter.rb +158 -0
- data/lib/aireview/review_marker.rb +5 -1
- data/lib/aireview/review_pipeline.rb +24 -13
- data/lib/aireview/review_renderer.rb +33 -12
- data/lib/aireview/reviewer.rb +4 -5
- data/lib/aireview/stage_chains.rb +4 -0
- data/lib/aireview/version.rb +1 -1
- data/lib/aireview.rb +2 -1
- metadata +13 -14
data/lib/aireview/llm_router.rb
CHANGED
|
@@ -1,25 +1,22 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
|
-
require 'set'
|
|
3
2
|
require_relative 'errors'
|
|
4
3
|
require_relative 'llm_failure'
|
|
5
4
|
require_relative 'model_state'
|
|
6
5
|
|
|
7
6
|
module Aireview
|
|
8
|
-
# Walks the models
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
# expired. Two limits: the time budget of the run and the number of
|
|
14
|
-
# requests sent per model per stage (ModelState).
|
|
7
|
+
# Walks the models of a stage. An overloaded model goes into quarantine and
|
|
8
|
+
# is skipped until it expires; once the chain has been walked, the walk goes
|
|
9
|
+
# round again over the models whose quarantine has expired. Two limits: the
|
|
10
|
+
# time budget of the run and the number of requests sent per model per stage
|
|
11
|
+
# (ModelState).
|
|
15
12
|
class LlmRouter # rubocop:disable Metrics/ClassLength
|
|
16
|
-
Route = Struct.new(:candidate, :candidate_index, :key,
|
|
13
|
+
Route = Struct.new(:candidate, :candidate_index, :key, keyword_init: true) do
|
|
17
14
|
def fallback?
|
|
18
15
|
candidate_index.positive?
|
|
19
16
|
end
|
|
20
17
|
|
|
21
18
|
def to_s
|
|
22
|
-
|
|
19
|
+
candidate.to_s
|
|
23
20
|
end
|
|
24
21
|
end
|
|
25
22
|
|
|
@@ -33,8 +30,6 @@ module Aireview
|
|
|
33
30
|
end
|
|
34
31
|
|
|
35
32
|
Slot = Struct.new(:candidate, :index)
|
|
36
|
-
# The key the next model of the same provider continues from.
|
|
37
|
-
Carry = Struct.new(:key_source, :key_index)
|
|
38
33
|
Delay = Struct.new(:seconds, :source)
|
|
39
34
|
|
|
40
35
|
SWITCH_HINT = 'Try again later or switch model via --generate-model/--critique-model.'
|
|
@@ -74,30 +69,27 @@ module Aireview
|
|
|
74
69
|
|
|
75
70
|
# The block receives a route and a request timeout, makes the request and
|
|
76
71
|
# returns the answer. An error raised by the block is classified, then
|
|
77
|
-
# comes a retry, another
|
|
78
|
-
#
|
|
79
|
-
#
|
|
80
|
-
#
|
|
72
|
+
# comes a retry, another model, or ApiError once the routes are
|
|
73
|
+
# exhausted. pinned — only the model that answered last in this stage
|
|
74
|
+
# (the JSON repair): its failure is RouteExhaustedError, and the stage
|
|
75
|
+
# restarts on another model.
|
|
81
76
|
def call(stage:, request_chars:, pinned: false, &request)
|
|
82
77
|
@deadline ||= now + @config.llm_time_budget
|
|
83
78
|
visits = []
|
|
84
79
|
noted = Set.new
|
|
85
|
-
carry = nil
|
|
86
80
|
loop do
|
|
87
81
|
slots = available_slots(stage, request_chars, pinned, visits, noted)
|
|
88
82
|
slot = slots.empty? ? nil : ready_slot(stage, slots, visits)
|
|
89
83
|
raise exhausted_error(stage, visits, pinned) unless slot
|
|
90
84
|
|
|
91
|
-
status, value = try_candidate(stage, slot, visits,
|
|
85
|
+
status, value = try_candidate(stage, slot, visits, &request)
|
|
92
86
|
return value if status == :ok
|
|
93
|
-
|
|
94
|
-
carry = value
|
|
95
87
|
end
|
|
96
88
|
end
|
|
97
89
|
|
|
98
|
-
# Stages answered by
|
|
99
|
-
def
|
|
100
|
-
@used.
|
|
90
|
+
# Stages answered by any model, for the report.
|
|
91
|
+
def models
|
|
92
|
+
@used.transform_values { |route| route.candidate.display_name }
|
|
101
93
|
end
|
|
102
94
|
|
|
103
95
|
# The model that answered last in the stage and its place in the chain.
|
|
@@ -162,7 +154,7 @@ module Aireview
|
|
|
162
154
|
end
|
|
163
155
|
|
|
164
156
|
def bare_route(candidate, index)
|
|
165
|
-
Route.new(candidate: candidate, candidate_index: index
|
|
157
|
+
Route.new(candidate: candidate, candidate_index: index)
|
|
166
158
|
end
|
|
167
159
|
|
|
168
160
|
def skip_reason(stage, candidate, request_chars)
|
|
@@ -179,7 +171,7 @@ module Aireview
|
|
|
179
171
|
ensure_time_left!(stage, bare_route(slots.first.candidate, slots.first.index), visits)
|
|
180
172
|
moment = now
|
|
181
173
|
ready = slots.find { |slot| state(slot.candidate).quarantine_left(moment) <= 0 }
|
|
182
|
-
return ready if ready
|
|
174
|
+
return stronger_critic(stage, slots, ready, moment) || ready if ready
|
|
183
175
|
|
|
184
176
|
slot = slots.min_by { |item| state(item.candidate).quarantine_left(moment) }
|
|
185
177
|
wait = state(slot.candidate).quarantine_left(moment)
|
|
@@ -195,48 +187,50 @@ module Aireview
|
|
|
195
187
|
slot
|
|
196
188
|
end
|
|
197
189
|
|
|
198
|
-
#
|
|
199
|
-
#
|
|
200
|
-
#
|
|
201
|
-
#
|
|
202
|
-
#
|
|
203
|
-
#
|
|
204
|
-
#
|
|
205
|
-
#
|
|
206
|
-
def
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
#
|
|
224
|
-
#
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
key_index: key_index, key_count: keys.size)
|
|
231
|
-
end
|
|
232
|
-
routes.rotate(start_key(stage, slot, carry))
|
|
190
|
+
# A critique by the model that answered in Generate is the last resort
|
|
191
|
+
# of a ranked pool. When a model the critique chain puts before it is
|
|
192
|
+
# only quarantined and its release fits into the budget, wait for it:
|
|
193
|
+
# otherwise an overload in Generate turns the critique into a self-check
|
|
194
|
+
# (Generate and Critique on one model). Only the chain's own preference
|
|
195
|
+
# counts, so an explicit critique.start stays first; own chains and
|
|
196
|
+
# rank: any are not ranked and keep their order. Models out until the
|
|
197
|
+
# end of the run are not in slots. nil — nobody to wait for.
|
|
198
|
+
def stronger_critic(stage, slots, ready, moment)
|
|
199
|
+
generate = self_critique(stage, ready)
|
|
200
|
+
return nil unless generate
|
|
201
|
+
|
|
202
|
+
stronger = slots.select { |item| item.index < ready.index }
|
|
203
|
+
slot = stronger.min_by { |item| state(item.candidate).quarantine_left(moment) }
|
|
204
|
+
wait = slot && state(slot.candidate).quarantine_left(moment)
|
|
205
|
+
# The wait is optional: a full request must still fit after it, or a
|
|
206
|
+
# critique by the available model beats a failed review.
|
|
207
|
+
return nil if wait.nil? || wait + @config.llm_timeout.to_f > remaining_time
|
|
208
|
+
|
|
209
|
+
@logger.warn(format('LLM critique: waiting %<wait>.0fs for %<model>s instead of critiquing with %<same>s, ' \
|
|
210
|
+
'the model that answered in generate', wait: wait, model: slot.candidate, same: generate))
|
|
211
|
+
pause(wait)
|
|
212
|
+
slot
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# The Generate model when it is the only one ready for a ranked
|
|
216
|
+
# Critique; else nil.
|
|
217
|
+
def self_critique(stage, ready)
|
|
218
|
+
generate = @used['generate']&.candidate
|
|
219
|
+
return nil unless stage == 'critique' && generate&.same_model?(ready.candidate)
|
|
220
|
+
|
|
221
|
+
generate if @routing.ranked_critique?(generate)
|
|
233
222
|
end
|
|
234
223
|
|
|
235
|
-
|
|
236
|
-
|
|
224
|
+
# Every visit either sends a request or excludes the model: otherwise the
|
|
225
|
+
# walk round the pool would never stop.
|
|
226
|
+
def try_candidate(stage, slot, visits, &)
|
|
227
|
+
key = @config.candidate_api_keys(slot.candidate).first
|
|
228
|
+
route = Route.new(candidate: slot.candidate, candidate_index: slot.index, key: key)
|
|
229
|
+
log_switch(stage, route, visits)
|
|
230
|
+
status, response = try_route(stage, route, visits, &)
|
|
231
|
+
return [:ok, remember(stage, route, response)] if status == :ok
|
|
237
232
|
|
|
238
|
-
|
|
239
|
-
cursor_candidate == slot.index ? cursor_key : 0
|
|
233
|
+
[:next_model, nil]
|
|
240
234
|
end
|
|
241
235
|
|
|
242
236
|
# A visit to a route: requests until an answer, a retry or a refusal. try
|
|
@@ -272,7 +266,7 @@ module Aireview
|
|
|
272
266
|
if decision == :retry
|
|
273
267
|
return :retry if requests_left?(stage, route) && waited_before_retry?(stage, route, delay)
|
|
274
268
|
|
|
275
|
-
decision =
|
|
269
|
+
decision = :next_model
|
|
276
270
|
end
|
|
277
271
|
record_failure(stage, route, kind, decision, error)
|
|
278
272
|
visits << Visit.new(route: route, kind: kind, tries: try, error: error)
|
|
@@ -286,29 +280,23 @@ module Aireview
|
|
|
286
280
|
false
|
|
287
281
|
end
|
|
288
282
|
|
|
289
|
-
# :retry with a delay, :
|
|
283
|
+
# :retry with a delay, :next_model or :fail.
|
|
290
284
|
def decide(kind, try, error)
|
|
291
285
|
case kind
|
|
292
286
|
when :fatal then [:fail]
|
|
293
|
-
when :unavailable then [:next_model]
|
|
294
|
-
when :
|
|
295
|
-
when :rate_limit then try == 1 ? [:retry, rate_limit_delay(error)] : [:next_key]
|
|
287
|
+
when :unavailable, :daily_quota then [:next_model]
|
|
288
|
+
when :rate_limit then try == 1 ? [:retry, rate_limit_delay(error)] : [:next_model]
|
|
296
289
|
else try == 1 ? [:retry, short_delay] : [:next_model]
|
|
297
290
|
end
|
|
298
291
|
end
|
|
299
292
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
# A daily quota marks the key of the model, a missing model marks the
|
|
305
|
-
# model for the whole run, an overload or a timeout quarantines it, a
|
|
306
|
-
# per-minute limit quarantines it for the provider's hint (the next key
|
|
307
|
-
# is tried at once; the quarantine only affects the next visit).
|
|
293
|
+
# A daily quota or a missing model excludes the model for the whole run,
|
|
294
|
+
# an overload or a timeout quarantines it, a per-minute limit quarantines
|
|
295
|
+
# it for the provider's hint.
|
|
308
296
|
def record_failure(stage, route, kind, decision, error)
|
|
309
297
|
model = state(route.candidate)
|
|
310
298
|
case kind
|
|
311
|
-
when :daily_quota then model.
|
|
299
|
+
when :daily_quota then model.exclude('daily quota exhausted earlier in this run')
|
|
312
300
|
when :unavailable then model.exclude('model unavailable earlier in this run')
|
|
313
301
|
when :rate_limit then quarantine(stage, route, rate_limit_delay(error).seconds)
|
|
314
302
|
when :overloaded, :timeout then quarantine(stage, route, @config.overloaded_quarantine) if decision == :next_model
|
|
@@ -368,18 +356,9 @@ module Aireview
|
|
|
368
356
|
[@config.llm_timeout.to_f, remaining_time].min
|
|
369
357
|
end
|
|
370
358
|
|
|
371
|
-
|
|
372
|
-
return false unless state(route.candidate).key_exhausted?(route.key_index)
|
|
373
|
-
|
|
374
|
-
@logger.info("LLM #{stage}: skipping #{route}, daily quota exhausted earlier in this run")
|
|
375
|
-
visits << Visit.new(route: route, note: 'daily quota exhausted earlier in this run')
|
|
376
|
-
true
|
|
377
|
-
end
|
|
378
|
-
|
|
379
|
-
# A model that answered is not overloaded, whichever key answered: a
|
|
380
|
-
# quarantine set through another key of the same model is lifted.
|
|
359
|
+
# A model that answered is not overloaded: its quarantine is lifted.
|
|
381
360
|
def remember(stage, route, response)
|
|
382
|
-
@cursor[stage] =
|
|
361
|
+
@cursor[stage] = route.candidate_index
|
|
383
362
|
@used[stage] = route
|
|
384
363
|
state(route.candidate).lift_quarantine
|
|
385
364
|
response
|
|
@@ -396,7 +375,7 @@ module Aireview
|
|
|
396
375
|
# The next request of the stage (the JSON repair, for instance) starts
|
|
397
376
|
# from the model that answered; the rest stay in reserve after it.
|
|
398
377
|
def ordered_chain(stage)
|
|
399
|
-
start_candidate
|
|
378
|
+
start_candidate = @cursor.fetch(stage, 0)
|
|
400
379
|
chain_for(stage).each_with_index.to_a.rotate(start_candidate)
|
|
401
380
|
end
|
|
402
381
|
|
|
@@ -14,16 +14,19 @@ module Aireview
|
|
|
14
14
|
# The address is part of the name, scheme included: two servers with the
|
|
15
15
|
# same model (http and https ones too) are two models for quarantine,
|
|
16
16
|
# reserves and the review key.
|
|
17
|
+
def display_name
|
|
18
|
+
"#{provider}/#{model}"
|
|
19
|
+
end
|
|
20
|
+
|
|
17
21
|
def to_s
|
|
18
|
-
|
|
19
|
-
api_base ? "#{name}@#{api_base.chomp('/')}" : name
|
|
22
|
+
api_base ? "#{display_name}@#{api_base.chomp('/')}" : display_name
|
|
20
23
|
end
|
|
21
24
|
|
|
22
25
|
# Every way a start or a CLI override may name this model: bare,
|
|
23
26
|
# "provider/name", or the full name with the address. One list for the
|
|
24
27
|
# config check and for picking the model, so they cannot disagree.
|
|
25
28
|
def names
|
|
26
|
-
[model.to_s,
|
|
29
|
+
[model.to_s, display_name, to_s].uniq
|
|
27
30
|
end
|
|
28
31
|
|
|
29
32
|
def same_model?(other)
|
data/lib/aireview/model_pool.rb
CHANGED
|
@@ -122,6 +122,12 @@ module Aireview
|
|
|
122
122
|
chain(stage).first
|
|
123
123
|
end
|
|
124
124
|
|
|
125
|
+
# Critique is ranked against the model that answered in Generate: both
|
|
126
|
+
# stages walk the pool and the rank is not_below_generate.
|
|
127
|
+
def ranked_critique?(after)
|
|
128
|
+
rank_applies?(after)
|
|
129
|
+
end
|
|
130
|
+
|
|
125
131
|
def weaker?(critique_candidate, generate_candidate)
|
|
126
132
|
return false unless pool_member?(critique_candidate) && pool_member?(generate_candidate)
|
|
127
133
|
|
data/lib/aireview/model_state.rb
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
|
-
require 'set'
|
|
3
2
|
|
|
4
3
|
module Aireview
|
|
5
4
|
# What the router learned about a model during the run. Three things with
|
|
6
5
|
# different lifetimes: sent requests are counted per stage, a quarantine
|
|
7
6
|
# lasts until a moment in time, an exclusion lasts until the end of the
|
|
8
|
-
# run (the provider has no such model,
|
|
9
|
-
# end of the stage (an invalid result).
|
|
10
|
-
# are remembered separately: a quota is a property of "key + model", a
|
|
11
|
-
# quarantine a property of the model.
|
|
7
|
+
# run (the provider has no such model, daily quota is exhausted) or the
|
|
8
|
+
# end of the stage (an invalid result).
|
|
12
9
|
class ModelState
|
|
13
10
|
# Every request sent, the short retry and the JSON repair included,
|
|
14
|
-
# spends an attempt regardless of its outcome;
|
|
15
|
-
#
|
|
11
|
+
# spends an attempt regardless of its outcome; a quarantine does not
|
|
12
|
+
# reset it.
|
|
16
13
|
MAX_REQUESTS_PER_STAGE = 3
|
|
17
14
|
|
|
18
15
|
attr_reader :excluded_reason
|
|
@@ -23,7 +20,6 @@ module Aireview
|
|
|
23
20
|
@quarantined_until = nil
|
|
24
21
|
@excluded_reason = nil
|
|
25
22
|
@stage_exclusions = {}
|
|
26
|
-
@exhausted_keys = Set.new
|
|
27
23
|
end
|
|
28
24
|
|
|
29
25
|
def sent(stage)
|
|
@@ -42,7 +38,7 @@ module Aireview
|
|
|
42
38
|
@quarantined_until = until_time
|
|
43
39
|
end
|
|
44
40
|
|
|
45
|
-
# A model that answered is not overloaded
|
|
41
|
+
# A model that answered is not overloaded.
|
|
46
42
|
def lift_quarantine
|
|
47
43
|
@quarantined_until = nil
|
|
48
44
|
end
|
|
@@ -61,14 +57,6 @@ module Aireview
|
|
|
61
57
|
@stage_exclusions[stage.to_s] = reason
|
|
62
58
|
end
|
|
63
59
|
|
|
64
|
-
def exhaust_key(key_index)
|
|
65
|
-
@exhausted_keys << key_index
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def key_exhausted?(key_index)
|
|
69
|
-
@exhausted_keys.include?(key_index)
|
|
70
|
-
end
|
|
71
|
-
|
|
72
60
|
# Why the model cannot be tried in the stage; nil — it can (the
|
|
73
61
|
# quarantine is checked separately: it is a wait, not a ban).
|
|
74
62
|
def skip_reason(stage)
|
data/lib/aireview/mr_parser.rb
CHANGED
|
@@ -3,28 +3,66 @@ require 'uri'
|
|
|
3
3
|
|
|
4
4
|
module Aireview
|
|
5
5
|
class MrParser
|
|
6
|
-
Result = Struct.new(:url, :base_url, :project_path, :project_id, :iid, keyword_init: true)
|
|
6
|
+
Result = Struct.new(:platform, :url, :base_url, :project_path, :project_id, :iid, keyword_init: true)
|
|
7
7
|
|
|
8
8
|
MR_PATH = %r{\A/(?<project>.+)/-/merge_requests/(?<iid>\d+)\z}
|
|
9
|
+
# github.com and GitHub Enterprise share the path; the PR page tabs and a
|
|
10
|
+
# trailing slash point at the same pull request.
|
|
11
|
+
PR_PATH = %r{\A/(?<owner>[^/]+)/(?<repo>[^/]+)/pull/(?<number>\d+)(?:/(?:files|commits|checks))?/?\z}
|
|
12
|
+
# What GitHub allows in owner and repository names. Checked here, so that
|
|
13
|
+
# the "owner/repo" id goes into API paths as is, and a malformed URL
|
|
14
|
+
# fails as a URL error instead of a 404.
|
|
15
|
+
GITHUB_NAME = /\A[A-Za-z0-9_.-]+\z/
|
|
9
16
|
|
|
10
17
|
def self.parse(url)
|
|
11
18
|
uri = URI.parse(url.to_s)
|
|
12
19
|
raise ParseError, 'Merge request URL must include http:// or https://' unless uri.is_a?(URI::HTTP)
|
|
13
20
|
|
|
21
|
+
gitlab(url, uri) || github(url, uri) || raise(ParseError, "Unsupported merge request URL: #{url}")
|
|
22
|
+
rescue URI::InvalidURIError => e
|
|
23
|
+
raise ParseError, "Invalid merge request URL: #{e.message}"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.gitlab(url, uri)
|
|
14
27
|
match = MR_PATH.match(uri.path)
|
|
15
|
-
|
|
28
|
+
return unless match
|
|
16
29
|
|
|
17
30
|
project_path = match[:project]
|
|
18
|
-
|
|
19
31
|
Result.new(
|
|
32
|
+
platform: :gitlab,
|
|
20
33
|
url: url,
|
|
21
|
-
base_url:
|
|
34
|
+
base_url: base_url(uri),
|
|
22
35
|
project_path: project_path,
|
|
23
36
|
project_id: URI.encode_www_form_component(project_path),
|
|
24
37
|
iid: match[:iid].to_i
|
|
25
38
|
)
|
|
26
|
-
rescue URI::InvalidURIError => e
|
|
27
|
-
raise ParseError, "Invalid merge request URL: #{e.message}"
|
|
28
39
|
end
|
|
40
|
+
|
|
41
|
+
# GitHub takes the repository as two path segments (repos/owner/repo),
|
|
42
|
+
# not one encoded segment like a GitLab project id.
|
|
43
|
+
def self.github(url, uri)
|
|
44
|
+
match = PR_PATH.match(uri.path)
|
|
45
|
+
return unless match
|
|
46
|
+
|
|
47
|
+
owner, repo = match.values_at(:owner, :repo)
|
|
48
|
+
unless [owner, repo].all? { |name| GITHUB_NAME.match?(name) }
|
|
49
|
+
raise ParseError, "Unsupported pull request URL: #{url}"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
Result.new(
|
|
53
|
+
platform: :github,
|
|
54
|
+
url: url,
|
|
55
|
+
base_url: base_url(uri),
|
|
56
|
+
project_path: "#{owner}/#{repo}",
|
|
57
|
+
project_id: "#{owner}/#{repo}",
|
|
58
|
+
iid: match[:number].to_i
|
|
59
|
+
)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def self.base_url(uri)
|
|
63
|
+
"#{uri.scheme}://#{uri.host}#{":#{uri.port}" if uri.port && ![80, 443].include?(uri.port)}"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private_class_method :gitlab, :github, :base_url
|
|
29
67
|
end
|
|
30
68
|
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require_relative 'errors'
|
|
3
|
+
require_relative 'utils'
|
|
4
|
+
require_relative 'gitlab_client'
|
|
5
|
+
require_relative 'github_client'
|
|
6
|
+
|
|
7
|
+
module Aireview
|
|
8
|
+
# The only place that knows which platform a review target belongs to:
|
|
9
|
+
# which client to build, how to tell a CI retry from a new push and how to
|
|
10
|
+
# name the target in messages. No requests and no response handling here —
|
|
11
|
+
# those live in the clients.
|
|
12
|
+
module Platform
|
|
13
|
+
PLATFORMS = %i[gitlab github].freeze
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
def client(config:, target:, logger:)
|
|
18
|
+
case platform(target)
|
|
19
|
+
when :gitlab
|
|
20
|
+
GitlabClient.new(
|
|
21
|
+
base_url: config.gitlab_url || target.base_url,
|
|
22
|
+
token: config.require_gitlab_token!,
|
|
23
|
+
logger: logger
|
|
24
|
+
)
|
|
25
|
+
when :github
|
|
26
|
+
GithubClient.new(
|
|
27
|
+
api_url: config.github_api_url || GithubClient.api_url_for(target.base_url),
|
|
28
|
+
token: config.require_github_token!,
|
|
29
|
+
review_author: config.github_review_author,
|
|
30
|
+
logger: logger
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# A Retry of the GitLab job, not a new pipeline: see
|
|
36
|
+
# GitlabClient#retried_job?. GitHub numbers the attempts of a workflow
|
|
37
|
+
# run itself: "Re-run jobs" makes attempt 2. Outside CI nothing is a retry.
|
|
38
|
+
def retried_run?(client:, target:, env:)
|
|
39
|
+
case platform(target)
|
|
40
|
+
when :gitlab
|
|
41
|
+
project_id, job_id = gitlab_job(env)
|
|
42
|
+
project_id ? client.retried_job?(project_id, job_id) : false
|
|
43
|
+
when :github
|
|
44
|
+
github_actions?(env) && env['GITHUB_RUN_ATTEMPT'].to_i > 1
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def update_hint(target:, env:)
|
|
49
|
+
case platform(target)
|
|
50
|
+
when :gitlab
|
|
51
|
+
gitlab_job(env) ? 'retry the job to update' : 'use --force to review again'
|
|
52
|
+
when :github
|
|
53
|
+
github_actions?(env) ? 're-run the workflow to update' : 'use --force to review again'
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def label(target)
|
|
58
|
+
case platform(target)
|
|
59
|
+
when :gitlab then "MR #{target.project_path}!#{target.iid}"
|
|
60
|
+
when :github then "PR #{target.project_path}##{target.iid}"
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def name(target)
|
|
65
|
+
case platform(target)
|
|
66
|
+
when :gitlab then 'GitLab'
|
|
67
|
+
when :github then 'GitHub'
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def noun(target)
|
|
72
|
+
case platform(target)
|
|
73
|
+
when :gitlab then 'merge request'
|
|
74
|
+
when :github then 'pull request'
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def platform(target)
|
|
79
|
+
platform = target.platform
|
|
80
|
+
raise ArgumentError, "Unsupported platform: #{platform.inspect}" unless PLATFORMS.include?(platform)
|
|
81
|
+
|
|
82
|
+
platform
|
|
83
|
+
end
|
|
84
|
+
private_class_method :platform
|
|
85
|
+
|
|
86
|
+
def gitlab_job(env)
|
|
87
|
+
project_id, job_id = env.values_at('CI_PROJECT_ID', 'CI_JOB_ID')
|
|
88
|
+
return if Aireview::Utils.blank?(project_id) || Aireview::Utils.blank?(job_id)
|
|
89
|
+
|
|
90
|
+
[project_id, job_id]
|
|
91
|
+
end
|
|
92
|
+
private_class_method :gitlab_job
|
|
93
|
+
|
|
94
|
+
def github_actions?(env)
|
|
95
|
+
env['GITHUB_ACTIONS'] == 'true'
|
|
96
|
+
end
|
|
97
|
+
private_class_method :github_actions?
|
|
98
|
+
end
|
|
99
|
+
end
|
data/lib/aireview/publisher.rb
CHANGED
|
@@ -5,8 +5,9 @@ module Aireview
|
|
|
5
5
|
class Publisher
|
|
6
6
|
PREFIX = '**aireview review**'
|
|
7
7
|
|
|
8
|
-
def initialize(
|
|
9
|
-
@
|
|
8
|
+
def initialize(client:, platform_name: 'GitLab', logger: Logger.new($stderr))
|
|
9
|
+
@client = client
|
|
10
|
+
@platform_name = platform_name
|
|
10
11
|
@logger = logger
|
|
11
12
|
end
|
|
12
13
|
|
|
@@ -17,7 +18,7 @@ module Aireview
|
|
|
17
18
|
author_id = current_user_id
|
|
18
19
|
legacy = nil
|
|
19
20
|
|
|
20
|
-
@
|
|
21
|
+
@client.fetch_merge_request_notes(project_id, iid).each do |note|
|
|
21
22
|
next if note['system']
|
|
22
23
|
next if note.dig('author', 'id') != author_id
|
|
23
24
|
|
|
@@ -35,9 +36,9 @@ module Aireview
|
|
|
35
36
|
|
|
36
37
|
if existing
|
|
37
38
|
@logger.info("Updating review note #{existing[:id]}")
|
|
38
|
-
@
|
|
39
|
+
@client.update_merge_request_note(project_id, iid, existing[:id], body)
|
|
39
40
|
else
|
|
40
|
-
@
|
|
41
|
+
@client.post_merge_request_note(project_id, iid, body)
|
|
41
42
|
end
|
|
42
43
|
end
|
|
43
44
|
|
|
@@ -55,8 +56,8 @@ module Aireview
|
|
|
55
56
|
def current_user_id
|
|
56
57
|
return @current_user_id if defined?(@current_user_id)
|
|
57
58
|
|
|
58
|
-
id = @
|
|
59
|
-
raise ApiError,
|
|
59
|
+
id = @client.fetch_current_user['id']
|
|
60
|
+
raise ApiError, "#{@platform_name} did not return the current user id" if Aireview::Utils.blank?(id)
|
|
60
61
|
|
|
61
62
|
@current_user_id = id
|
|
62
63
|
end
|