Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log error output when a git command fails #4124

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions app/models/git_repository.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

require 'tempfile'

# Responsible for all git knowledge of a repo
# Caches a local mirror (not a full checkout) and creates a workspace when deploying
class GitRepository
Expand Down Expand Up @@ -184,13 +187,16 @@ def instance_cache(key)
# success: stdout as string
# error: nil
def capture_stdout(*command, dir: repo_cache_dir)
success, output = Samson::CommandExecutor.execute(
*command,
whitelist_env: ['HOME', 'PATH'],
timeout: 30.minutes,
err: '/dev/null',
dir: dir
)
output.strip if success
Tempfile.create('git-stderr') do |error_file|
success, output = Samson::CommandExecutor.execute(
*command,
whitelist_env: ['HOME', 'PATH'],
timeout: 30.minutes,
err: error_file.path,
dir: dir
)
Rails.logger.error("Failed to run command #{command}: #{error_file.read}") unless success
output.strip if success
end
end
end
6 changes: 6 additions & 0 deletions test/models/git_repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ def call
repository.commit_from_ref('NOT A VALID REF').must_be_nil
end

it 'logs an error if ref does not exist' do
create_repo_with_tags
Rails.logger.expects(:error).with { |message| message.must_match(/^Failed to run command/) }
repository.commit_from_ref('NOT A VALID REF')
end

it 'returns the commit of a branch' do
create_repo_with_an_additional_branch('my_branch')
repository.commit_from_ref('my_branch').must_match /^[0-9a-f]{40}$/
Expand Down
Loading