Skip to content

Commit

Permalink
Merge pull request #1221 from Homebrew/include-error-annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
carlocab authored Sep 16, 2024
2 parents 4f3fe40 + 40e110a commit 451d938
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,17 @@ def annotation_location(name)
[@repository.glob("**/#{name}*").first, nil]
end

def truncate_output(output, max_kb:, max_lines:)
max_length_start = [output.length - (max_kb * 1024), 0].max
def truncate_output(output, max_kb:, max_lines:, context_lines:)
output_lines = output.lines
first_error_index = output_lines.find_index { |line| line.match?(/\b[Ee]rror:\s+/) }

output[max_length_start..].lines.last(max_lines).join
if first_error_index.blank?
max_length_start = [output.length - (max_kb * 1024), 0].max
output[max_length_start..].lines.last(max_lines).join
else
start = [first_error_index - context_lines, 0].max
output_lines[start..(start + max_lines - 1)].join[..(max_kb * 1024)]
end
end

def run(dry_run: false, fail_fast: false)
Expand Down Expand Up @@ -213,7 +220,7 @@ def run(dry_run: false, fail_fast: false)

# GitHub Actions has a 64KB maximum for annotiations. That's a bit
# too long so instead let's go for a maximum of 24KB or 256 lines.
annotation_output = truncate_output(@output, max_kb: 24, max_lines: 256)
annotation_output = truncate_output(@output, max_kb: 24, max_lines: 256, context_lines: 10)

annotation_title = "`#{command_trimmed}` failed on #{os_string}!"
file = path.to_s.delete_prefix("#{@repository}/")
Expand Down

0 comments on commit 451d938

Please sign in to comment.