Skip to content

Commit

Permalink
Allow overrides of some GitHub defined variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Taucher2003 committed Nov 22, 2023
1 parent b3feb3f commit 3849225
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 13 deletions.
10 changes: 9 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ inputs:
GL_RUNNER_TOKEN:
description: 'A runner token, if a runner should be started in this action'
GL_API_TOKEN:
description: 'A token to check the pipeline status with. Required if the project is private'
description: 'A token to check the pipeline status with'
required: true
SHOW_JOB_LOGS:
description: 'Set if the job log should be shown in the action output'
default: 'none'

# Overrides of default variables
OVERRIDE_GITHUB_SHA:
description: 'Override the git sha given from GitHub'
OVERRIDE_GITHUB_REF:
description: 'Override the git ref given from GitHub. Should be in format "refs/<type>/<name>"'
OVERRIDE_GITHUB_REF_NAME:
description: 'Override the git ref name given from GitHub'
author: 'Taucher2003'
runs:
using: 'docker'
Expand Down
19 changes: 19 additions & 0 deletions lib/action/helper/env.rb
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
4 changes: 4 additions & 0 deletions lib/action/step/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def skip?
def github
GitlabPipelineAction::Helper::Github
end

def env
GitlabPipelineAction::Helper::Env
end
end
end
end
24 changes: 12 additions & 12 deletions lib/action/step/prepare_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ module GitlabPipelineAction
module Step
class PrepareContext < Base
def execute
context.gh_project = ENV.fetch('GITHUB_REPOSITORY', nil)
context.gh_sha = ENV.fetch('GITHUB_SHA', nil)
context.gh_ref = ENV.fetch('GITHUB_REF', nil)
context.gh_server_url = ENV.fetch('GITHUB_SERVER_URL', nil)
context.gh_project = env.fetch('GITHUB_REPOSITORY')
context.gh_sha = env.fetch('INPUT_OVERRIDE_GITHUB_SHA', 'GITHUB_SHA')
context.gh_ref = env.fetch('INPUT_OVERRIDE_GITHUB_REF', 'GITHUB_REF')
context.gh_server_url = env.fetch('GITHUB_SERVER_URL')

context.gl_branch_name = "glpa/#{ENV.fetch('GITHUB_REF_NAME', nil)}"
context.gl_branch_name = "glpa/#{env.fetch('INPUT_OVERRIDE_GITHUB_REF_NAME', 'GITHUB_REF_NAME')}"

context.gl_server_url = ENV.fetch('INPUT_GL_SERVER_URL', nil)
context.gl_server_url_for_runner = ENV.fetch(
context.gl_server_url = env.fetch('INPUT_GL_SERVER_URL')
context.gl_server_url_for_runner = env.fetch(
'INPUT_GL_SERVER_URL_FOR_RUNNER', # intentionally undocumented
context.gl_server_url
default: context.gl_server_url
)
context.gl_project_id = ENV.fetch('INPUT_GL_PROJECT_ID', nil)
context.gl_runner_token = ENV.fetch('INPUT_GL_RUNNER_TOKEN', nil)
context.gl_api_token = ENV.fetch('INPUT_GL_API_TOKEN', nil)
context.gl_project_id = env.fetch('INPUT_GL_PROJECT_ID')
context.gl_runner_token = env.fetch('INPUT_GL_RUNNER_TOKEN')
context.gl_api_token = env.fetch('INPUT_GL_API_TOKEN')
context.gl_pipeline_variables = ENV.select { |key| key.start_with?('GLPA_') }
.transform_keys { |key| key.delete_prefix('GLPA_') }

context.gl_show_job_logs = ENV.fetch('INPUT_SHOW_JOB_LOGS', nil)&.to_sym
context.gl_show_job_logs = env.fetch('INPUT_SHOW_JOB_LOGS')&.to_sym

context.git_path = "/tmp/repo/#{SecureRandom.hex}"

Expand Down
1 change: 1 addition & 0 deletions lib/gitlab_pipeline_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'securerandom'
require 'docker'

require 'action/helper/env'
require 'action/helper/github'

require 'action/step/base'
Expand Down
58 changes: 58 additions & 0 deletions spec/action/helper/env_spec.rb
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
30 changes: 30 additions & 0 deletions spec/action/step/prepare_context_spec.rb
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

0 comments on commit 3849225

Please sign in to comment.