Class: Kennel::GithubReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/kennel/github_reporter.rb

Constant Summary collapse

MAX_COMMENT_SIZE =
65536
TRUNCATED_MSG =

finish the code block so it look nice

"\n```\n... (truncated)"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, git_sha) ⇒ GithubReporter

Returns a new instance of GithubReporter.



15
16
17
18
19
20
21
22
# File 'lib/kennel/github_reporter.rb', line 15

def initialize(token, git_sha)
  @token = token
  @git_sha = git_sha
  @repo_part = ENV["GITHUB_REPOSITORY"] || begin
    origin = ENV["PROJECT_REPOSITORY"] || Utils.capture_sh("git remote -v").split("\n").first
    origin[%r{github\.com[:/](\S+?)(\.git|$)}, 1] || raise("no origin found in #{origin}")
  end
end

Class Method Details

.report(token, &block) ⇒ Object



9
10
11
12
# File 'lib/kennel/github_reporter.rb', line 9

def report(token, &block)
  return yield unless token
  new(token, Utils.capture_sh("git rev-parse HEAD").strip).report(&block)
end

Instance Method Details

#comment(body) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/kennel/github_reporter.rb', line 34

def comment(body)
  # truncate to maximum allowed comment size for github to avoid 422
  if body.bytesize > MAX_COMMENT_SIZE
    body = body.byteslice(0, MAX_COMMENT_SIZE - TRUNCATED_MSG.bytesize) + TRUNCATED_MSG
  end

  post "commits/#{@git_sha}/comments", body: body
end

#report(&block) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/kennel/github_reporter.rb', line 24

def report(&block)
  output = Utils.strip_shell_control(Utils.tee_output(&block).strip)
rescue StandardError
  output = "Error:\n#{$ERROR_INFO.message}"
  raise
ensure
  comment "```\n#{output || "Error"}\n```"
end