Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit 1c7b22c

Browse files
authored
Fix CODEBUILD_SOURCE_VERSION matching (#102)
According to AWS CodeBuild User Guide, CODEBUILD_SOURCE_VERSION depends on the source repository. When build is triggered by a webhook pull request event, the value is pull_request number(pr/xxx) otherwise the value is other than pull request number(commit id, branch name or tag name). In the latter case, an error raises like below. ``` /root/caches/vendor/bundle/ruby/2.5.0/gems/codecov-0.2.11/lib/codecov.rb:171 :in `build_params': undefined method `[]' for nil:NilClass (NoMethodError) ```
1 parent 10a110d commit 1c7b22c

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

lib/codecov.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,10 @@ def build_params(ci)
168168
params[:commit] = ENV['CODEBUILD_RESOLVED_SOURCE_VERSION']
169169
params[:job] = ENV['CODEBUILD_BUILD_ID']
170170
params[:slug] = ENV['CODEBUILD_SOURCE_REPO_URL'].match(/.*github.com\/(?<slug>.*).git/)['slug']
171-
params[:pr] = ENV['CODEBUILD_SOURCE_VERSION'].match(/pr\/(?<pr>.*)/)['pr'] if ENV['CODEBUILD_SOURCE_VERSION']
171+
params[:pr] = if ENV['CODEBUILD_SOURCE_VERSION']
172+
matched = ENV['CODEBUILD_SOURCE_VERSION'].match(%r{pr/(?<pr>.*)})
173+
matched.nil? ? ENV['CODEBUILD_SOURCE_VERSION'] : matched['pr']
174+
end
172175
when CODESHIP
173176
# https://www.codeship.io/documentation/continuous-integration/set-environment-variables/
174177
params[:service] = 'codeship'

test/test_codecov.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,27 @@ def test_codebuild
639639
assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
640640
end
641641

642+
def test_codebuild_source_version_is_other_than_pr_number
643+
ENV['CODEBUILD_CI'] = 'true'
644+
ENV['CODEBUILD_BUILD_ID'] = 'codebuild-project:458dq3q8-7354-4513-8702-ea7b9c81efb3'
645+
ENV['CODEBUILD_RESOLVED_SOURCE_VERSION'] = 'd653b934ed59c1a785cc1cc79d08c9aaa4eba73b'
646+
ENV['CODEBUILD_WEBHOOK_HEAD_REF'] = 'refs/heads/master'
647+
ENV['CODEBUILD_SOURCE_VERSION'] = 'git-commit-hash-12345'
648+
ENV['CODEBUILD_SOURCE_REPO_URL'] = 'https://github.com/owner/repo.git'
649+
ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
650+
651+
result = upload
652+
653+
assert_equal('codebuild', result['params'][:service])
654+
assert_equal('d653b934ed59c1a785cc1cc79d08c9aaa4eba73b', result['params'][:commit])
655+
assert_equal('codebuild-project:458dq3q8-7354-4513-8702-ea7b9c81efb3', result['params'][:build])
656+
assert_equal('git-commit-hash-12345', result['params'][:pr])
657+
assert_equal('owner/repo', result['params'][:slug])
658+
assert_equal('master', result['params'][:branch])
659+
assert_equal('git-commit-hash-12345', result['params'][:pr])
660+
assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
661+
end
662+
642663
def test_filenames_are_shortened_correctly
643664
ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'
644665

0 commit comments

Comments
 (0)