From 3849225d0d7255fcdfe1aa98aefdb79e8562c2b1 Mon Sep 17 00:00:00 2001 From: Niklas van Schrick Date: Wed, 22 Nov 2023 20:23:23 +0100 Subject: [PATCH] Allow overrides of some GitHub defined variables --- action.yml | 10 +++- lib/action/helper/env.rb | 19 ++++++++ lib/action/step/base.rb | 4 ++ lib/action/step/prepare_context.rb | 24 +++++----- lib/gitlab_pipeline_action.rb | 1 + spec/action/helper/env_spec.rb | 58 ++++++++++++++++++++++++ spec/action/step/prepare_context_spec.rb | 30 ++++++++++++ 7 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 lib/action/helper/env.rb create mode 100644 spec/action/helper/env_spec.rb create mode 100644 spec/action/step/prepare_context_spec.rb diff --git a/action.yml b/action.yml index f5fac05..e8b082d 100644 --- a/action.yml +++ b/action.yml @@ -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//"' + OVERRIDE_GITHUB_REF_NAME: + description: 'Override the git ref name given from GitHub' author: 'Taucher2003' runs: using: 'docker' diff --git a/lib/action/helper/env.rb b/lib/action/helper/env.rb new file mode 100644 index 0000000..738d607 --- /dev/null +++ b/lib/action/helper/env.rb @@ -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 diff --git a/lib/action/step/base.rb b/lib/action/step/base.rb index f5a5e20..1331f73 100644 --- a/lib/action/step/base.rb +++ b/lib/action/step/base.rb @@ -18,6 +18,10 @@ def skip? def github GitlabPipelineAction::Helper::Github end + + def env + GitlabPipelineAction::Helper::Env + end end end end diff --git a/lib/action/step/prepare_context.rb b/lib/action/step/prepare_context.rb index 12f3c96..ff4ffb3 100644 --- a/lib/action/step/prepare_context.rb +++ b/lib/action/step/prepare_context.rb @@ -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}" diff --git a/lib/gitlab_pipeline_action.rb b/lib/gitlab_pipeline_action.rb index 2e680e2..8b48755 100644 --- a/lib/gitlab_pipeline_action.rb +++ b/lib/gitlab_pipeline_action.rb @@ -5,6 +5,7 @@ require 'securerandom' require 'docker' +require 'action/helper/env' require 'action/helper/github' require 'action/step/base' diff --git a/spec/action/helper/env_spec.rb b/spec/action/helper/env_spec.rb new file mode 100644 index 0000000..028513e --- /dev/null +++ b/spec/action/helper/env_spec.rb @@ -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 diff --git a/spec/action/step/prepare_context_spec.rb b/spec/action/step/prepare_context_spec.rb new file mode 100644 index 0000000..298b4be --- /dev/null +++ b/spec/action/step/prepare_context_spec.rb @@ -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