-
Notifications
You must be signed in to change notification settings - Fork 438
/
bs_request.rb
1294 lines (1067 loc) · 43.9 KB
/
bs_request.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'xmlhash'
include MaintenanceHelper
# rubocop:disable Metrics/ClassLength
class BsRequest < ApplicationRecord
include BsRequest::Errors
MAX_DESCRIPTION_LENGTH_ALLOWED = 64_000
SEARCHABLE_FIELDS = [
'bs_requests.creator',
'bs_requests.priority',
'bs_request_actions.target_project',
'bs_request_actions.target_package',
'bs_request_actions.source_project',
'bs_request_actions.source_package',
'bs_request_actions.type'
].freeze
FINAL_REQUEST_STATES = %i[accepted declined superseded revoked].freeze
VALID_REQUEST_STATES = %i[new deleted declined accepted review revoked superseded].freeze
OBSOLETE_STATES = %i[declined superseded revoked].freeze
ACTION_NOTIFY_LIMIT = 50
scope :to_accept_by_time, -> { where(state: %w[new review]).where(accept_at: ...Time.now) }
# Scopes for collections
scope :with_actions, -> { joins(:bs_request_actions).distinct.order(priority: :asc, id: :desc) }
scope :with_involved_projects, ->(project_ids) { where(bs_request_actions: { target_project_id: project_ids }) }
scope :with_involved_packages, ->(package_ids) { where(bs_request_actions: { target_package_id: package_ids }) }
scope :with_source_subprojects, ->(project_name) { where('bs_request_actions.source_project like ?', project_name) }
scope :with_target_subprojects, ->(project_name) { where('bs_request_actions.target_project like ?', project_name) }
scope :with_types, lambda { |types|
includes(:bs_request_actions).where(bs_request_actions: { type: types }).distinct.order(priority: :asc, id: :desc)
}
scope :from_source_project, ->(source_project) { where(bs_request_actions: { source_project: source_project }) }
scope :in_ids, ->(ids) { where(id: ids) }
scope :not_creator, ->(login) { where.not(creator: login) }
# Searching capabilities using dataTable (1.9)
scope :do_search, lambda { |search|
includes(:bs_request_actions)
.references(:bs_request_actions)
.where(
[
SEARCHABLE_FIELDS.map { |field| "#{field} like ?" }.join(' or '), ["%#{search}%"] * SEARCHABLE_FIELDS.length
].flatten
)
}
scope :with_actions_and_reviews, -> { joins(:bs_request_actions).left_outer_joins(:reviews).distinct.order(priority: :asc, id: :desc) }
scope :with_submit_requests, -> { joins(:bs_request_actions).where(bs_request_actions: { type: 'submit' }) }
scope :by_user_reviews, ->(user_ids) { where(reviews: { user: user_ids }) }
scope :by_project_reviews, ->(project_ids) { where(reviews: { project: project_ids }) }
scope :by_package_reviews, ->(package_ids) { where(reviews: { package: package_ids }) }
scope :by_group_reviews, ->(group_ids) { where(reviews: { group: group_ids }) }
scope :obsolete, -> { where(state: OBSOLETE_STATES) }
scope :with_target_project, lambda { |target_project|
includes(:bs_request_actions).where('bs_request_actions.target_project': target_project)
}
scope :with_open_reviews_for, lambda { |review_attributes|
where(state: 'review', id: Review.where(review_attributes).where(state: 'new').select(:bs_request_id))
.includes(:reviews)
}
scope :with_action_type, ->(action_type) { joins(:bs_request_actions).where(bs_request_actions: { type: action_type }).distinct }
has_many :bs_request_actions, dependent: :destroy
has_many :reviews, dependent: :delete_all
has_many :comments, as: :commentable, dependent: :destroy
has_one :comment_lock, as: :commentable, dependent: :destroy
has_many :request_history_elements, -> { order(:created_at) }, class_name: 'HistoryElement::Request', foreign_key: :op_object_id
has_many :review_history_elements, through: :reviews, source: :history_elements
has_many :status_reports, as: :checkable, class_name: 'Status::Report', dependent: :destroy
has_many :target_project_objects, through: :bs_request_actions
belongs_to :staging_project, class_name: 'Project', optional: true
has_one :request_exclusion, class_name: 'Staging::RequestExclusion', dependent: :destroy
has_many :not_accepted_reviews, -> { where.not(state: :accepted) }, class_name: 'Review'
has_many :notifications, as: :notifiable, dependent: :delete_all
has_many :watched_items, as: :watchable, dependent: :destroy
has_many :reports, as: :reportable, dependent: :nullify
has_many :labels, as: :labelable
accepts_nested_attributes_for :labels, allow_destroy: true
validates :state, inclusion: { in: VALID_REQUEST_STATES }
validates :creator, presence: true
validate :check_supersede_state
validate :check_creator, on: %i[create save!]
validates :comment, length: { maximum: 65_535 }
validates :description, length: { maximum: MAX_DESCRIPTION_LENGTH_ALLOWED }
validates :number, uniqueness: true
validates_associated :bs_request_actions, message: ->(_, record) { record[:value].map { |r| r.errors.full_messages }.flatten.to_sentence }
before_validation :sanitize!, if: :sanitize?, on: :create
before_save :accept_staged_request
before_save :assign_number
after_create :notify
before_update :send_state_change
after_save :update_cache
after_save { PopulateToSphinxJob.perform_later(id: id, model_name: :bs_request) }
accepts_nested_attributes_for :bs_request_actions
def self.delayed_auto_accept
to_accept_by_time.each do |request|
BsRequestAutoAcceptJob.perform_later(request.id)
end
end
def self.list(opts)
# All types means don't pass 'type'
opts.delete(:types) if [opts[:types]].flatten.include?('all')
# Do not allow a full collection to avoid server load
raise 'This call requires at least one filter, either by user, project or package' if %i[project user package].all? { |filter| opts[filter].blank? }
roles = opts[:roles] || []
states = opts[:states] || []
# it's wiser to split the queries
if opts[:project] && roles.empty? && (states.empty? || states.include?('review'))
(BsRequest::FindFor::Query.new(opts.merge(roles: ['reviewer'])).all +
BsRequest::FindFor::Query.new(opts.merge(roles: %w[target source])).all).uniq
else
BsRequest::FindFor::Query.new(opts).all.uniq
end
end
def self.list_numbers(opts)
list(opts).pluck(:number)
end
def self.new_from_xml(xml)
hashed = Xmlhash.parse(xml)
raise SaveError, 'Failed parsing the request xml' unless hashed
new_from_hash(hashed)
end
def self.new_from_hash(hashed)
if hashed['id']
theid = hashed.delete('id') { raise 'not found' }
theid = Integer(theid)
else
theid = nil
end
# we will set it our own according to the user
hashed.delete('creator')
if hashed['submit'] && hashed['type'] == 'submit'
# old style, convert to new style on the fly
hashed.delete('type')
hashed['action'] = hashed.delete('submit')
hashed['action']['type'] = 'submit'
end
request = nil
BsRequest.transaction do
request = BsRequest.new
request.number = theid if theid
actions = hashed.delete('action')
actions = [actions] if actions.is_a?(Hash)
request.priority = hashed.delete('priority') || 'moderate'
state = hashed.delete('state') || Xmlhash::XMLHash.new('name' => 'new')
request.state = state.delete('name') || 'new'
request.state = :declined if request.state.to_s == 'rejected'
request.state = :accepted if request.state.to_s == 'accept'
request.state = request.state.to_sym
request.comment = state.value('comment')
state.delete('comment')
request.commenter = state.delete('who')
unless request.commenter
raise 'no one logged in and no user in request' unless User.session
request.commenter = User.session!.login
end
# to be overwritten if we find history
request.creator = request.commenter
if actions
actions.each do |ac|
a = BsRequestAction.new_from_xml_hash(ac)
request.bs_request_actions << a
a.bs_request = request
end
end
state.delete('created')
str = state.delete('when')
request.updated_when = Time.zone.parse(str) if str
str = state.delete('superseded_by') || ''
request.superseded_by = Integer(str) if str.present?
str = state.delete('approver')
request.approver = str if str.present?
raise ArgumentError, "too much information #{state.inspect}" if state.present?
request.description = hashed.value('description')
hashed.delete('description')
str = hashed.value('accept_at')
request.accept_at = Time.parse(str) if str
hashed.delete('accept_at')
raise SaveError, 'Auto accept time is in the past' if request.accept_at && request.accept_at < Time.now
# we do not support to import history anymore on purpose
# would be all fake, but means also history gets lost when
# updating from OBS 2.3 or older.
hashed.delete('history')
reviews = hashed.delete('review')
reviews = [reviews] if reviews.is_a?(Hash)
if reviews
reviews.each do |r|
request.reviews << Review.new_from_xml_hash(r)
end
end
raise ArgumentError, "too much information #{hashed.inspect}" if hashed.present?
request.updated_at ||= Time.now
end
request
end
# [DEPRECATED] TODO: there is only one instance of the @not_full_diff variable in the request scope which is using this method.
# Once request_workflow_redesign beta is rolled out, let's drop this method
# TODO: refactor this method as soon as the request_show_redesign feature is rolled out.
# Now it expects an array of action hashes we'll never display more than one action at a time.
def self.truncated_diffs?(actions)
submit_requests = actions.select { |action| action[:type] == :submit && action[:sourcediff] }
submit_requests.any? do |action|
action[:sourcediff].any? { |sourcediff| sourcediff_has_shown_attribute?(sourcediff) }
end
end
def self.sourcediff_has_shown_attribute?(sourcediff)
if sourcediff && sourcediff['files']
# the 'shown' attribute is only set if the backend truncated the diff
sourcediff['files'].any? { |file| file[1]['diff'].try(:[], 'shown') }
else
false
end
end
private_class_method :sourcediff_has_shown_attribute?
# Currently only used by staging projects for the obs factories and
# customized for that.
def as_json(*)
super(except: %i[state comment commenter]).tap do |request_hash|
request_hash['superseded_by_id'] = superseded_by if has_attribute?(:superseded_by)
request_hash['state'] = state.to_s if has_attribute?(:state)
request_hash['request_type'] = bs_request_actions.first.type
request_hash['package'] = bs_request_actions.first.target_package
request_hash['project'] = bs_request_actions.first.target_project
end
end
def history_elements
HistoryElement::Base.where(id: request_history_elements.pluck(:id) + review_history_elements.pluck(:id)).order(:created_at)
end
def set_add_revision
@addrevision = true
end
def set_ignore_build_state
@ignore_build_state = true
end
def set_ignore_delegate
@ignore_delegate = true
end
def sanitize?
!@skip_sanitize
end
def skip_sanitize
@skip_sanitize = true
end
def check_creator
errors.add(:creator, 'No creator defined') unless creator
# Allow admins to create requests for deleted or inactive users
return if User.admin_session?
user = User.not_deleted.find_by(login: creator)
# FIXME: We should run the authorization on controller level
raise APIError unless User.possibly_nobody.is_admin? || User.possibly_nobody == user
errors.add(:creator, "Invalid creator specified #{creator}") unless user
return if user.is_active?
errors.add(:creator, "Login #{user.login} is not an active user")
end
def assign_number
return if number
# to assign a unique and steady incremental number.
# Using MySQL auto-increment mechanism is not working on clusters.
BsRequest.transaction do
request_counter = BsRequestCounter.lock(true).first_or_create
self.number = request_counter.counter
request_counter.increment(:counter)
request_counter.save!
end
end
def check_supersede_state
errors.add(:superseded_by, 'Superseded_by should be set') if state == :superseded && (!superseded_by.is_a?(Numeric) || superseded_by <= 0)
return unless superseded_by && state != :superseded
errors.add(:superseded_by, 'Superseded_by should not be set')
end
def updated_when
self[:updated_when] || self[:updated_at]
end
def superseding
BsRequest.where(superseded_by: number)
end
def first_target_package
bs_request_actions.first.target_package
end
def state
self[:state].to_sym
end
def to_axml(opts = {})
if opts[:withfullhistory]
Rails.cache.fetch("xml_bs_request_fullhistory_#{cache_key_with_version}") do
render_xml(withfullhistory: 1)
end
elsif opts[:withhistory]
Rails.cache.fetch("xml_bs_request_history_#{cache_key_with_version}") do
render_xml(withhistory: 1)
end
else
Rails.cache.fetch("xml_bs_request_#{cache_key_with_version}") do
render_xml
end
end
end
def to_axml_id
# FIXME: naming it axml is nonsense if it's just a string
"<request id='#{number}'/>\n"
end
def to_param
number
end
def render_xml(opts = {})
builder = Nokogiri::XML::Builder.new
builder.request(id: number, creator: creator) do |r|
bs_request_actions.includes([:bs_request_action_accept_info]).find_each do |action|
action.render_xml(r)
end
r.priority(priority) unless priority == 'moderate'
# state element
attributes = { name: state, who: commenter, when: updated_when.strftime('%Y-%m-%dT%H:%M:%S'), created: created_at.strftime('%Y-%m-%dT%H:%M:%S') }
attributes[:superseded_by] = superseded_by if superseded_by
attributes[:approver] = approver if approver
r.state(attributes) do |s|
comment = self.comment
comment ||= ''
s.comment!(comment)
end
reviews.each do |review|
review.render_xml(r)
end
if opts[:withfullhistory] || opts[:withhistory]
attributes = { who: creator, when: created_at.strftime('%Y-%m-%dT%H:%M:%S') }
builder.history(attributes) do
# request description is on purpose the comment in history:
builder.description!('Request created')
builder.comment!(description) if description.present?
end
end
if opts[:withfullhistory]
history_elements.each do |history|
# we do ignore the review history here on purpose to stay compatible
history.render_xml(r)
end
elsif opts[:withhistory]
request_history_elements.each do |history|
# we do ignore the review history here on purpose to stay compatible
history.render_xml(r)
end
end
r.accept_at(accept_at) unless accept_at.nil?
r.description(description) unless description.nil?
end
builder.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION |
Nokogiri::XML::Node::SaveOptions::FORMAT)
end
def is_reviewer?(user)
return false if reviews.blank?
reviews.each do |r|
if r.by_user
return true if user.login == r.by_user
elsif r.by_group
return true if user.is_in_group?(r.by_group)
elsif r.by_project
if r.by_package
pkg = Package.find_by_project_and_name(r.by_project, r.by_package)
return true if pkg && user.can_modify?(pkg)
else
prj = Project.find_by_name(r.by_project)
return true if prj && user.can_modify?(prj)
end
end
end
false
end
def obsolete_reviews(opts)
return false unless opts[:by_user] || opts[:by_group] || opts[:by_project] || opts[:by_package]
reviews.each do |review|
next unless review.reviewable_by?(opts)
logger.debug "Obsoleting review #{review.id}"
review.state = :obsoleted
review.save
history = HistoryElement::ReviewObsoleted
history.create(review: review, comment: 'reviewer got removed', user_id: User.session!.id)
# Maybe this will turn the request into an approved state?
next unless state == :review && reviews.where(state: 'new').none?
self.state = :new
save
history = HistoryElement::RequestAllReviewsApproved
history.create(request: self, comment: opts[:comment], user_id: User.session!.id)
end
end
def permission_check_change_review!(params)
checker = BsRequestPermissionCheck.new(self, params)
checker.cmd_changereviewstate_permissions
end
def permission_check_setincident!(incident)
checker = BsRequestPermissionCheck.new(self, incident: incident)
checker.cmd_setincident_permissions
end
def permission_check_setpriority!
checker = BsRequestPermissionCheck.new(self, {})
checker.cmd_setpriority_permissions
end
def permission_check_addreview!
# allow request creator to add further reviewers
checker = BsRequestPermissionCheck.new(self, {})
checker.cmd_addreview_permissions(creator == User.session!.login || is_reviewer?(User.session!))
end
def permission_check_change_state!(opts)
checker = BsRequestPermissionCheck.new(self, opts)
checker.cmd_changestate_permissions
# check target write permissions
return unless opts[:newstate] == 'accepted'
check_bs_request_actions!(skip_source: true)
end
def permission_check_change_state(opts)
begin
permission_check_change_state!(opts)
rescue PostRequestNoPermission
return false
end
true
end
def changestate_accepted(opts)
# all maintenance_incident actions go into the same incident project
incident_project = nil
bs_request_actions.each do |action|
next unless action.is_maintenance_incident?
target_project = Project.get_by_name(action.target_project)
next unless target_project.is_maintenance?
source_project = Project.find_by_name(action.source_project)
# create incident if it is a maintenance project
incident_project ||= MaintenanceIncident.build_maintenance_incident(target_project, self, no_access: source_project.nil?).project
opts[:check_for_patchinfo] = true
raise MultipleMaintenanceIncidents, 'This request handles different maintenance incidents, this is not allowed !' unless incident_project.name.start_with?(target_project.name)
action.target_project = incident_project.name
action.save!
end
# We have permission to change all requests inside, now execute
bs_request_actions.each do |action|
action.execute_accept(opts)
end
# now do per request cleanup
bs_request_actions.each do |action|
action.per_request_cleanup(opts)
end
end
def changestate_revoked(opts)
bs_request_actions.where(type: 'maintenance_release').find_each do |action|
# unlock incident project in the soft way
prj = Project.get_by_name(action.source_project)
if prj.is_locked?
prj.unlock_by_request(self)
elsif !opts.key?(:keep_packages_locked)
pkg = Package.get_by_project_and_name(action.source_project, action.source_package)
pkg.unlock_by_request(self) if pkg.is_locked?
end
end
end
def change_state(opts)
with_lock do
permission_check_change_state!(opts)
changestate_revoked(opts) if opts[:newstate] == 'revoked'
changestate_accepted(opts) if opts[:newstate] == 'accepted'
state = opts[:newstate].to_sym
bs_request_actions.each do |a|
# "inform" the actions
a.request_changes_state(state)
end
self.state = state
self.commenter = User.session!.login
self.comment = opts[:comment]
self.superseded_by = opts[:superseded_by]
# check for not accepted reviews on re-open
if %i[new review].include?(state)
reviews.each do |review|
next unless review.state != :accepted
# FIXME3.0 review history?
review.state = :new
review.save!
self.state = :review
end
end
save!
params = { request: self, comment: opts[:comment], user_id: User.session!.id }
case opts[:newstate]
when 'accepted'
history = HistoryElement::RequestAccepted
when 'declined'
history = HistoryElement::RequestDeclined
when 'revoked'
history = HistoryElement::RequestRevoked
when 'superseded'
history = HistoryElement::RequestSuperseded
params[:description_extension] = superseded_by.to_s
when 'review', 'new'
history = HistoryElement::RequestReopened
when 'deleted'
history = HistoryElement::RequestDeleted
else
raise "Unhandled state #{opts[:newstate]} for history"
end
history.create(params)
end
end
def assignreview(opts = {})
raise InvalidStateError, 'request is not in review state' unless state == :review || state == :new
reviewer = User.find_by_login!(opts[:reviewer])
Review.transaction do
# check if user is a reviewer already
user_review = reviews.where(by_user: reviewer.login).last
if opts[:revert]
_assignreview_update_reviews(reviewer, opts)
raise Review::NotFoundError unless user_review
raise InvalidStateError, 'review is not in new state' unless user_review.state == :new
raise Review::NotFoundError, 'Not an assigned review' unless HistoryElement::ReviewAssigned.where(op_object_id: user_review.id).last
user_review.destroy
elsif user_review
review_comment = _assignreview_update_reviews(reviewer, opts)
user_review.state = :new
user_review.save!
HistoryElement::ReviewReopened.create(review: user_review, comment: review_comment, user: User.session!)
else
review = reviews.create(by_user: reviewer.login, creator: User.session!.login, state: :new)
review_comment = _assignreview_update_reviews(reviewer, opts, review)
HistoryElement::ReviewAssigned.create(review: review, comment: review_comment, user: User.session!)
end
save!
end
end
def approve(opts)
raise InvalidStateError, "already approved by #{approver}" if approver
approval_handling(User.session!, opts)
end
def cancelapproval(opts)
raise InvalidStateError, 'request is not approved' unless approver
approval_handling(nil, opts)
end
def calculate_state_from_reviews
return :declined if reviews.declined.exists?
if reviews.all?(&:accepted?)
:new
else
:review
end
end
def find_review_for_opts(opts)
matching_reviews = reviews.order(id: :desc).select { |review| review.reviewable_by?(opts) }
# prefer not yet accepted review
matching_reviews.find { |review| review.state != :accepted } || matching_reviews.first
end
def supersede_request(history_arguments, superseded_opt)
self.state = :superseded
self.superseded_by = superseded_opt
history_arguments[:description_extension] = superseded_by.to_s
save!
HistoryElement::RequestSuperseded.create(history_arguments)
end
def change_review_state(new_review_state, opts = {})
with_lock do
new_review_state = new_review_state.to_sym
raise InvalidStateError, 'request is not in a changeable state (new, review or declined)' unless state == :review || (state.in?(%i[new declined]) && new_review_state == :new)
check_if_valid_review!(opts)
raise InvalidStateError, "review state must be new, accepted, declined or superseded, was #{new_review_state}" unless new_review_state.in?(%i[new accepted declined superseded])
old_request_state = state
review = find_review_for_opts(opts)
raise Review::NotFoundError unless review
next unless review.change_state(new_review_state, opts[:comment] || '')
history_parameters = { request: self, comment: opts[:comment], user_id: User.session!.id }
next supersede_request(history_parameters, opts[:superseded_by]) if new_review_state == :superseded
new_request_state = calculate_state_from_reviews
next if new_request_state == old_request_state
self.comment = review.reason
self.state = new_request_state
self.commenter = User.session!.login
case new_request_state
when :new
self.comment = 'All reviewers accepted request'
save!
Event::RequestReviewsDone.create(event_parameters)
HistoryElement::RequestAllReviewsApproved.create(history_parameters)
# pre-approved requests can be processed
BsRequestAutoAcceptJob.perform_later(id) if approver
when :review
save!
when :declined
HistoryElement::RequestDeclined.create(history_parameters)
save!
end
end
end
def check_if_valid_review!(opts)
return if opts[:by_user] || opts[:by_group] || opts[:by_project]
raise InvalidReview
end
def addreview(opts)
with_lock do
permission_check_addreview!
check_if_valid_review!(opts)
self.state = 'review'
self.commenter = User.session!.login
self.comment = opts[:comment] if opts[:comment]
newreview = create_new_review(opts)
save!
history_params = {
request: self,
user_id: User.session!.id,
description_extension: newreview.id.to_s
}
history_params[:comment] = opts[:comment] if opts[:comment]
HistoryElement::RequestReviewAdded.create(history_params)
newreview.create_event(event_parameters)
end
end
def setpriority(opts)
permission_check_setpriority!
raise SaveError, "Illegal priority '#{opts[:priority]}'" unless opts[:priority].in?(%w[low moderate important critical])
p = { request: self, user_id: User.session!.id, description_extension: "#{priority} => #{opts[:priority]}" }
p[:comment] = opts[:comment] if opts[:comment]
self.priority = opts[:priority]
save!
HistoryElement::RequestPriorityChange.create(p)
end
def setincident(incident)
permission_check_setincident!(incident)
touched = false
# all maintenance_incident actions go into the same incident project
p = { request: self, user_id: User.session!.id }
bs_request_actions.where(type: 'maintenance_incident').find_each do |action|
tprj = Project.get_by_name(action.target_project)
# use an existing incident
if tprj.is_maintenance?
tprj = Project.get_by_name("#{action.target_project}:#{incident}")
action.target_project = tprj.name
action.save!
touched = true
p[:description_extension] = tprj.name
end
end
return unless touched
save!
HistoryElement::RequestSetIncident.create(p)
end
def send_state_change
return unless state_changed?
# new->review && review->new are not worth an event - it's just spam
return unless conclusive?
options = event_parameters
# measure duration unless superseding a final state, like revoked -> superseded
options[:duration] = (updated_at - created_at).to_i if FINAL_REQUEST_STATES.exclude?(state_was.to_sym) && FINAL_REQUEST_STATES.include?(state)
Event::RequestStatechange.create(options)
end
def accept_staged_request
return if staging_project_id.nil? || state.to_sym != :accepted
accepted_package = bs_request_actions.map(&:target_package)
staging_project.packages.where(name: accepted_package).destroy_all
self.staging_project_id = nil
end
def event_parameters
params = { id: id,
number: number,
description: description,
state: state,
when: updated_when.strftime('%Y-%m-%dT%H:%M:%S'),
comment: comment,
author: creator,
namespace: namespace }
params[:oldstate] = state_was if state_changed?
params[:who] = commenter if commenter.present?
# Use a nested data structure to support multiple actions in one request
params[:actions] = []
bs_request_actions[0..ACTION_NOTIFY_LIMIT].each do |a|
params[:actions] << a.notify_params
end
params
end
def namespace
maintained_request? ? target_project_name : target_project_name.split(':').first
end
def maintained_request?
maintenance_project = Project.get_maintenance_project
return false unless maintenance_project
maintenance_project.maintained_project_names.include?(target_project_name)
end
def target_project_name
bs_request_actions&.first&.target_project.to_s
end
# It is considered an "incident request" if it has at least one maintenance_incident action
def maintenance_incident_request?
bs_request_actions.where(type: 'maintenance_incident').any?
end
# It is considered a "release request" if it has at least one maintenance_release action
def maintenance_release_request?
bs_request_actions.where(type: 'maintenance_release').any?
end
def auto_accept
# do not run for processed requests. Ignoring review on purpose since this
# must also work when people do not react anymore
return unless state == :new || state == :review
# use approve mechanic in case you want to wait for reviews
return if approver && state == :review
return unless accept_at || approver
with_lock do
if accept_at
auto_accept_user = User.find_by!(login: creator)
elsif approver
auto_accept_user = User.find_by!(login: approver)
end
auto_accept_user.run_as do
raise 'Request lacks definition of owner for auto accept' unless User.session!
begin
change_state(newstate: 'accepted', comment: 'Auto accept')
rescue BsRequest::Errors::UnderEmbargo
# not yet free to release, postponing it to the embargo date
BsRequestAutoAcceptJob.set(wait_until: embargo_date).perform_later(id)
rescue BsRequestPermissionCheck::NotExistingTarget
change_state(newstate: 'revoked', comment: 'Target disappeared')
rescue PostRequestNoPermission
change_state(newstate: 'revoked', comment: 'Permission problem')
rescue APIError => e
logger.info("Failed to accept BsRequest #{number} with #{auto_accept_user.login}. #{e.class.name}: #{e}")
change_state(newstate: 'declined', comment: 'Unhandled error during accept, contact your admin.')
end
end
end
end
# Check if 'user' is maintainer in _all_ request sources:
def is_source_maintainer?(user)
bs_request_actions.all? { |action| action.is_source_maintainer?(user) }
end
# Check if 'user' is maintainer in _all_ request targets:
def is_target_maintainer?(user)
bs_request_actions.all? { |action| action.is_target_maintainer?(user) }
end
def sanitize!
# apply default values, expand and do permission checks
self.creator ||= User.session!.login
self.commenter ||= User.session!.login
# FIXME: Move permission checks to controller level
raise SaveError, 'Admin permissions required to set request creator to foreign user' unless self.creator == User.session!.login || User.admin_session?
raise SaveError, 'Admin permissions required to set request commenter to foreign user' unless self.commenter == User.session!.login || User.admin_session?
# ensure correct initial values, no matter what has been sent to us
self.state = :new
# expand release and submit request targets if not specified
expand_targets
check_bs_request_actions!
check_uniq_actions!
# Autoapproval? Is the creator allowed to accept it?
permission_check_change_state!(newstate: 'accepted') if accept_at
apply_default_reviewers
end
def set_accept_at!(time = nil)
# Approve a request to be accepted when the reviews finished
permission_check_change_state!(newstate: 'accepted')
self.accept_at = time || Time.now
save!
end
def notify
notify = event_parameters
Event::RequestCreate.create(notify)
reviews.each do |review|
review.create_event(notify)
end
end
# [DEPRECATED] TODO: drop this after request_workflow_redesign beta is rolled_out
def webui_actions(opts = {})
actions = []
action_id = opts.delete(:action_id)
xml = bs_request_actions.find_by(id: action_id) if action_id
if xml
actions << action_details(opts, xml: xml)
else
bs_request_actions.each do |action|
actions << action_details(opts, xml: action)
end
end
actions
end
def expand_targets
newactions = []
oldactions = []
bs_request_actions.each do |action|
new_action = action.expand_targets(@ignore_build_state.present?, @ignore_delegate.present?)
next if new_action.nil?
oldactions << action
newactions.concat(new_action)
end
# will become an empty request
raise MissingAction if newactions.empty? && oldactions.size == bs_request_actions.size
oldactions.each { |a| bs_request_actions.destroy(a) }
newactions.each { |a| bs_request_actions << a }
end
def forward_to(project:, package: nil, options: {})
new_request = BsRequest.new(description: options[:description])
BsRequest.transaction do
bs_request_actions.where(type: 'submit').find_each do |action|
rev = Directory.hashed(project: action.target_project, package: action.target_package)['rev']
opts = { source_project: action.target_project,
source_package: action.target_package,
source_rev: rev,
target_project: project,
target_package: package,
type: action.type }
new_request.bs_request_actions.build(opts)
new_request.save!
end
end
new_request
end
def required_checks
target_project_objects.pluck(:required_checks).flatten.uniq
end
def staged_request?
!staging_project_id.nil?
end
def can_be_reopened?
(reviews.accepted.size + reviews.opened.size + reviews.declined.size).positive? &&
# Declined is not really a final state, since the request can always be reopened...
(BsRequest::FINAL_REQUEST_STATES.exclude?(state) || state == :declined)
end
# Collects the embargo_date from all actions and returns...
# - the newest one
# - nil if there are no actions with embargo date