-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow overrides of some GitHub defined variables
- Loading branch information
1 parent
b3feb3f
commit 3849225
Showing
7 changed files
with
133 additions
and
13 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
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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module GitlabPipelineAction | ||
module Helper | ||
module Env | ||
module_function | ||
|
||
def fetch(*args, default: nil, allow_blank: false) | ||
args.each do |key| | ||
value = ENV.fetch(key, nil) | ||
|
||
return value if !value.nil? && (!value.strip.empty? || allow_blank) | ||
end | ||
|
||
default | ||
end | ||
end | ||
end | ||
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
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
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
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,58 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe GitlabPipelineAction::Helper::Env do | ||
stub_env('TEST_A', 'value_a') | ||
stub_env('TEST_B', 'value_b') | ||
stub_env('TEST_C', 'value_c') | ||
stub_env('TEST_D', '') | ||
|
||
context 'when used with a single argument' do | ||
it 'returns the env value' do | ||
expect(described_class.fetch('TEST_A')).to eq('value_a') | ||
end | ||
|
||
it 'returns the env value if default is given' do | ||
expect(described_class.fetch('TEST_A', default: 'value_default')).to eq('value_a') | ||
end | ||
|
||
it 'returns nil if env var does not exist' do | ||
expect(described_class.fetch('BLUB')).to be_nil | ||
end | ||
|
||
it 'returns default if env var does not exist' do | ||
expect(described_class.fetch('BLUB', default: 'value_default')).to eq('value_default') | ||
end | ||
|
||
it 'returns the default if only a blank value exists' do | ||
expect(described_class.fetch('TEST_D', default: 'value_default')).to eq('value_default') | ||
end | ||
|
||
it 'returns the blank value if allow_blank is set' do | ||
expect(described_class.fetch('TEST_D', default: 'value_default', allow_blank: true)).to eq('') | ||
end | ||
end | ||
|
||
context 'when used with multiple arguments' do | ||
it 'returns the first env var' do | ||
expect(described_class.fetch('TEST_A', 'TEST_B', 'TEST_C')).to eq('value_a') | ||
end | ||
|
||
it 'returns the first env var that exists' do | ||
expect(described_class.fetch('BLUB', 'TEST_B', 'TEST_C')).to eq('value_b') | ||
end | ||
|
||
it 'returns the first env var that exists when default is given' do | ||
expect(described_class.fetch('BLUB', 'TEST_B', 'TEST_C', default: 'value_default')).to eq('value_b') | ||
end | ||
|
||
it 'returns nil if no env var exists' do | ||
expect(described_class.fetch('BLUB', 'BLAB', 'BLUB_BLAB')).to be_nil | ||
end | ||
|
||
it 'returns default if no env var exists' do | ||
expect(described_class.fetch('BLUB', 'BLAB', 'BLUB_BLAB', default: 'value_default')).to eq('value_default') | ||
end | ||
end | ||
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,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe GitlabPipelineAction::Step::PrepareContext do | ||
let(:context) { GitlabPipelineAction::Context.new } | ||
let(:step) { described_class.new(context) } | ||
|
||
before do | ||
step.execute | ||
end | ||
|
||
it 'takes data from default variables', :aggregate_failures do | ||
expect(context.gh_sha).to eq('master') | ||
expect(context.gh_ref).to eq('refs/heads/master') | ||
expect(context.gl_branch_name).to eq('glpa/master') | ||
end | ||
|
||
context 'when ref variables are overridden' do | ||
stub_env('INPUT_OVERRIDE_GITHUB_SHA', '123') | ||
stub_env('INPUT_OVERRIDE_GITHUB_REF', 'refs/heads/blub') | ||
stub_env('INPUT_OVERRIDE_GITHUB_REF_NAME', 'blub') | ||
|
||
it 'takes data from the override', :aggregate_failures do | ||
expect(context.gh_sha).to eq('123') | ||
expect(context.gh_ref).to eq('refs/heads/blub') | ||
expect(context.gl_branch_name).to eq('glpa/blub') | ||
end | ||
end | ||
end |