Class: Aireview::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/aireview/publisher.rb

Constant Summary collapse

PREFIX =
'**aireview review**'

Instance Method Summary collapse

Constructor Details

#initialize(gitlab_client:, logger: Logger.new($stderr)) ⇒ Publisher

Returns a new instance of Publisher.



8
9
10
11
# File 'lib/aireview/publisher.rb', line 8

def initialize(gitlab_client:, logger: Logger.new($stderr))
  @gitlab_client = gitlab_client
  @logger = logger
end

Instance Method Details

#existing_review(project_id:, iid:) ⇒ Object

Our own review note: key: or nil. The marker alone is not enough — anyone can quote it, so the author is checked too. The old format without a marker is picked up only when no marked note exists.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aireview/publisher.rb', line 16

def existing_review(project_id:, iid:)
  author_id = current_user_id
  legacy = nil

  @gitlab_client.fetch_merge_request_notes(project_id, iid).each do |note|
    next if note['system']
    next if note.dig('author', 'id') != author_id

    key = ReviewMarker.extract(note['body'])
    return {id: note['id'], key: key} if key

    legacy ||= {id: note['id'], key: nil} if note['body'].to_s.start_with?(PREFIX)
  end

  legacy
end

#publish(project_id:, iid:, review_body:, key: nil, existing: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/aireview/publisher.rb', line 33

def publish(project_id:, iid:, review_body:, key: nil, existing: nil)
  body = compose(review_body, key)

  if existing
    @logger.info("Updating review note #{existing[:id]}")
    @gitlab_client.update_merge_request_note(project_id, iid, existing[:id], body)
  else
    @gitlab_client.post_merge_request_note(project_id, iid, body)
  end
end