module WatchDoge::Notification

Public Class Methods

flush() click to toggle source
# File lib/watchdoge/notification.rb, line 42
def flush
  raise_err = false

  sources do |klass|
    puts '[notification] flush'
    raise_err = true if klass.msg_size > 0
    klass.flush
  end

  if raise_err
    raise WatchDoge::RegressionError, 'Has detected style changed. Regression failed'
  end
end
push(message) click to toggle source
# File lib/watchdoge/notification.rb, line 35
def push message
  sources do |klass|
    puts "[notification] new msg push to #{klass}"
    klass.push message
  end
end

Private Class Methods

get_source_class(klass_sym) click to toggle source
# File lib/watchdoge/notification.rb, line 78
def get_source_class klass_sym
  klass = klass_sym.to_s.split('_').collect(&:capitalize).join

  "WatchDoge::Notification::#{klass}".constantize
end
sources() { |sources_map| ... } click to toggle source
# File lib/watchdoge/notification.rb, line 58
def sources
  @sources ||= WatchDoge.configuration.notifications

  @sources = {
    base: {}
  } if @sources.empty?

  @sources.each do |klass_sym, source_args|
    sources_map[klass_sym] ||=
      get_source_class(klass_sym).new source_args

    yield(sources_map[klass_sym])
  end
end
sources_map() click to toggle source
# File lib/watchdoge/notification.rb, line 73
def sources_map
  @sources_map ||= {}
  @sources_map
end

Public Instance Methods

get_commit_id() click to toggle source
# 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_iid() click to toggle source
# 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
upload_image(image) click to toggle source
# 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

Private Instance Methods

disscussion_thread() click to toggle source

disscussion_thread do |context|

# File lib/watchdoge/notification/gitlab_repo.rb, line 135
      def disscussion_thread
        <<~MARKDOWN
          #### Detail: #{ENV['CI_PIPELINE_URL']}
          #{yield("")}
        MARKDOWN
      end
markdown_table(before, after, diff, filename) click to toggle source
# File lib/watchdoge/notification/gitlab_repo.rb, line 142
      def markdown_table before, after, diff, filename
        <<~MARKDOWN
          <h5>#{filename}</h5>
          <table>
            <tr>
              <th>Reference</th>
              <th>Test</th>
              <th>Diff</th>
            </tr>
            <tr>
              <td>#{before}</td>
              <td>#{after}</td>
              <td>#{diff}</td>
            </tr>
          </table>
        MARKDOWN
      end
project_uri(path) click to toggle source
# File lib/watchdoge/notification/gitlab_repo.rb, line 160
def project_uri path
  URI(@host + "/projects/#{@project_id}#{path}")
end