Class: WatchDoge::Notification::GitlabRepo

Inherits:
Base
  • Object
show all
Defined in:
lib/watchdoge/notification/gitlab_repo.rb

Instance Method Summary collapse

Methods inherited from Base

#msg_size, #push

Constructor Details

#initialize(opt) ⇒ GitlabRepo

Returns a new instance of GitlabRepo.


13
14
15
16
17
18
19
20
21
22
# File 'lib/watchdoge/notification/gitlab_repo.rb', line 13

def initialize opt
  super

  @host = opt[:host] || ENV['CI_API_V4_URL']
  @project_id = opt[:project_id] || ENV['CI_PROJECT_ID']
  @source_branch = opt[:source_branch] || ENV['CI_COMMIT_REF_NAME']
  @commit_short_sha = opt[:commit_short_sha] || ENV['CI_COMMIT_SHORT_SHA']

  @private_token = opt[:private_token]
end

Instance Method Details

#flushObject

Raises:

  • (RuntimeError)

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
# File 'lib/watchdoge/notification/gitlab_repo.rb', line 24

def flush
  return if @message_queue.empty?

  meesages_to_flush = @message_queue
  @message_queue = []

  target_id = get_latest_request_iid || get_commit_id

  raise RuntimeError, "Can't find any target to pushing message" if target_id.nil?

  thread = disscussion_thread do |thread_context|
    meesages_to_flush.each do |message|
      case message
      when String
        post_discussion target_id, message: message
      when ChunkyPNG::Image
        upload_link = upload_image message
        link = md_img_link upload_link

        post_discussion target_id, message: link
      when WatchDoge::PixelTest
        filename = message.option[:filename]

        before = md_img_link upload_image(message.before)
        after = md_img_link upload_image(message.after)
        diff = md_img_link upload_image(message.diff)

        thread_context << markdown_table(before, after, diff, filename)
      end
    end

    thread_context
  end

  post_discussion target_id, message: thread
end

#get_commit_idObject


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/watchdoge/notification/gitlab_repo.rb', line 111

def get_commit_id
  @discussion_target = :commit

  uri = project_uri "/repository/commits/#{@commit_short_sha}"

  req = Net::HTTP::Get.new uri
  req.add_field("PRIVATE-TOKEN", @private_token)

  res = Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == "https")) do |http|
    http.request(req).body
  end

  res = JSON.parse res

  res['id']
end

#get_latest_request_iidObject


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/watchdoge/notification/gitlab_repo.rb', line 92

def get_latest_request_iid
  @discussion_target = :merge_request

  uri = project_uri "/merge_requests?source_branch=#{@source_branch}&view=simple&state=opened"

  req = Net::HTTP::Get.new uri
  req.add_field("PRIVATE-TOKEN", @private_token)

  res = Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == "https")) do |http|
    http.request(req).body
  end

  res = JSON.parse res

  return nil if res.empty?

  res[0]['iid']
end

#post_discussion(target_iid, message:) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/watchdoge/notification/gitlab_repo.rb', line 61

def post_discussion target_iid, message:
  uri =
    case @discussion_target
    when :merge_request
      project_uri("/merge_requests/#{target_iid}/discussions")
    when :commit
      project_uri("/repository/commits/#{target_iid}/discussions")
    end

  Net::HTTP.post uri,
                 { body: message }.to_json,
                 "Content-Type": "application/json",
                 "PRIVATE-TOKEN": @private_token
end

#upload_image(image) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/watchdoge/notification/gitlab_repo.rb', line 76

def upload_image image
  uri = project_uri "/uploads"

  req = Net::HTTP::Post::Multipart.new uri.path, {
    file: UploadIO.new(StringIO.new(image.to_blob), 'image/png', 'image.png')
  }

  req.add_field("PRIVATE-TOKEN", @private_token)

  res = Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == "https")) do |http|
    res = http.request(req).body
  end

  JSON.parse(res)['url']
end