-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This class is responsible for taking the output from an external command and cleaning it up ready to be included in a GitHub comment.
- Loading branch information
1 parent
fcf7527
commit c98c8ff
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
require 'colorize' | ||
|
||
class CleanedOutput | ||
def initialize(output:) | ||
@output = output | ||
end | ||
|
||
def to_s | ||
cleaned_output[-64_000..-1] || cleaned_output | ||
end | ||
|
||
private | ||
|
||
def cleaned_output | ||
output.gsub(morph_api_key, 'REDACTED').uncolorize | ||
end | ||
|
||
def morph_api_key | ||
ERB::Util.url_encode(ENV['MORPH_API_KEY']) | ||
end | ||
|
||
attr_reader :output | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
require 'test_helper' | ||
|
||
describe 'CleanedOutput' do | ||
describe 'basic output' do | ||
subject { CleanedOutput.new(output: 'Hello, world') } | ||
|
||
it 'returns it untouched' do | ||
subject.to_s.must_equal 'Hello, world' | ||
end | ||
end | ||
|
||
describe 'output containing Morph API key' do | ||
before do | ||
@old_key = ENV['MORPH_API_KEY'] | ||
ENV['MORPH_API_KEY'] = 'test-morph-api-key' | ||
end | ||
|
||
after { ENV['MORPH_API_KEY'] = @old_key } | ||
|
||
subject { CleanedOutput.new(output: 'Key: test-morph-api-key') } | ||
|
||
it 'removes the key from the output' do | ||
subject.to_s.must_equal 'Key: REDACTED' | ||
end | ||
end | ||
|
||
describe 'with color escape codes in output' do | ||
let(:output) { '[0;31;49mTest[0m' } | ||
subject { CleanedOutput.new(output: output) } | ||
|
||
it 'strips escape codes' do | ||
subject.to_s.must_equal 'Test' | ||
end | ||
end | ||
|
||
describe 'large output' do | ||
let(:output) { 'x' * 100_000 } | ||
subject { CleanedOutput.new(output: output) } | ||
|
||
it 'only returns that last 64k' do | ||
subject.to_s.size.must_equal 64_000 | ||
end | ||
end | ||
end |