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

Commit

Permalink
Fix CODEBUILD_SOURCE_VERSION matching (#102)
Browse files Browse the repository at this point in the history
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)
```
  • Loading branch information
neko314 authored Sep 15, 2020
1 parent 10a110d commit 1c7b22c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/codecov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ def build_params(ci)
params[:commit] = ENV['CODEBUILD_RESOLVED_SOURCE_VERSION']
params[:job] = ENV['CODEBUILD_BUILD_ID']
params[:slug] = ENV['CODEBUILD_SOURCE_REPO_URL'].match(/.*github.com\/(?<slug>.*).git/)['slug']
params[:pr] = ENV['CODEBUILD_SOURCE_VERSION'].match(/pr\/(?<pr>.*)/)['pr'] if ENV['CODEBUILD_SOURCE_VERSION']
params[:pr] = if ENV['CODEBUILD_SOURCE_VERSION']
matched = ENV['CODEBUILD_SOURCE_VERSION'].match(%r{pr/(?<pr>.*)})
matched.nil? ? ENV['CODEBUILD_SOURCE_VERSION'] : matched['pr']
end
when CODESHIP
# https://www.codeship.io/documentation/continuous-integration/set-environment-variables/
params[:service] = 'codeship'
Expand Down
21 changes: 21 additions & 0 deletions test/test_codecov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,27 @@ def test_codebuild
assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
end

def test_codebuild_source_version_is_other_than_pr_number
ENV['CODEBUILD_CI'] = 'true'
ENV['CODEBUILD_BUILD_ID'] = 'codebuild-project:458dq3q8-7354-4513-8702-ea7b9c81efb3'
ENV['CODEBUILD_RESOLVED_SOURCE_VERSION'] = 'd653b934ed59c1a785cc1cc79d08c9aaa4eba73b'
ENV['CODEBUILD_WEBHOOK_HEAD_REF'] = 'refs/heads/master'
ENV['CODEBUILD_SOURCE_VERSION'] = 'git-commit-hash-12345'
ENV['CODEBUILD_SOURCE_REPO_URL'] = 'https://github.com/owner/repo.git'
ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'

result = upload

assert_equal('codebuild', result['params'][:service])
assert_equal('d653b934ed59c1a785cc1cc79d08c9aaa4eba73b', result['params'][:commit])
assert_equal('codebuild-project:458dq3q8-7354-4513-8702-ea7b9c81efb3', result['params'][:build])
assert_equal('git-commit-hash-12345', result['params'][:pr])
assert_equal('owner/repo', result['params'][:slug])
assert_equal('master', result['params'][:branch])
assert_equal('git-commit-hash-12345', result['params'][:pr])
assert_equal('f881216b-b5c0-4eb1-8f21-b51887d1d506', result['params']['token'])
end

def test_filenames_are_shortened_correctly
ENV['CODECOV_TOKEN'] = 'f881216b-b5c0-4eb1-8f21-b51887d1d506'

Expand Down

0 comments on commit 1c7b22c

Please sign in to comment.