Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harness approval step #123

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions app/lib/bk/compat/parsers/harness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_relative '../translator'
require_relative '../pipeline'
require_relative '../pipeline/step'
require_relative 'harness/steps/run'
require_relative 'harness/steps'

module BK
module Compat
Expand Down Expand Up @@ -39,17 +39,11 @@ def initialize(text, options = {})
end

def parse
# map stages to groups
grp = BK::Compat::GroupStep.new(
label: @config['pipeline']['name'],
key: @config['pipeline']['identifier']
Pipeline.new(
steps: @config['pipeline']['stages'].map do |stage|
parse_stage(**stage['stage'].transform_keys(&:to_sym))
end
)

grp.steps = @config['pipeline']['stages'].map do |stage|
parse_stage(**stage['stage'].transform_keys(&:to_sym))
end

Pipeline.new(steps: [simplify_group(grp)])
end

private
Expand All @@ -62,30 +56,30 @@ def default_step(*, **)

def register_translators!
@translator = BK::Compat::StepTranslator.new(default: method(:default_step))
@translator.register(
BK::Compat::HarnessSteps::Run.new
)
@translator.register(BK::Compat::HarnessSteps::Translator.new)
end

def simplify_group(group)
# If there ended up being only 1 stage, skip the group and just
# pull the steps out.
if group.steps.length == 1
group.steps.map! { |step| step.conditional = group.conditional }
group = groups.steps.first
step = group.steps.first
step.conditional = group.conditional
group = step
end
group
end

def parse_stage(name:, identifier:, spec:, **_rest)
BK::Compat::CommandStep.new(
grp = BK::Compat::GroupStep.new(
label: name,
key: identifier
).tap do |cmd|
spec.dig('execution', 'steps').map do |step|
cmd << @translator.translate_step(**step['step'].transform_keys(&:to_sym))
key: identifier,
steps: spec.dig('execution', 'steps').map do |step|
@translator.translate_step(**step['step'].transform_keys(&:to_sym))
end
end
)

simplify_group(grp)
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions app/lib/bk/compat/parsers/harness/steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require_relative 'steps/approval'
require_relative 'steps/run'

module BK
module Compat
module HarnessSteps
# Implementation of native step translation
class Translator
VALID_TYPES = %w[harnessapproval run].freeze

def matcher(type:, **_rest)
VALID_TYPES.include?(type.downcase)
end

def translator(type:, **)
send("translate_#{type.downcase.gsub('-', '_')}", **)
end
end
end
end
end
25 changes: 25 additions & 0 deletions app/lib/bk/compat/parsers/harness/steps/approval.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require_relative '../../../pipeline/step'

module BK
module Compat
module HarnessSteps
# Implementation of native step translation
class Translator
private

def translate_harnessapproval(name:, identifier:, spec:, **_rest)
BK::Compat::BlockStep.new(
label: name,
key: identifier,
prompt: spec.fetch('approvalMessage', nil),
fields: spec.fetch('approverInputs', []).map do |e|
{ 'key' => e['name'], 'text' => "Default #{e['defaultValue']}" }
end
)
end
end
end
end
end
8 changes: 2 additions & 6 deletions app/lib/bk/compat/parsers/harness/steps/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ module BK
module Compat
module HarnessSteps
# Implementation of native step translation
class Run
def matcher(type:, **)
type == 'Run'
end

def translator(name:, identifier:, spec:, **_rest)
class Translator
def translate_run(name:, identifier:, spec:, **_rest)
BK::Compat::CommandStep.new(
label: name,
key: identifier,
Expand Down
8 changes: 6 additions & 2 deletions app/lib/bk/compat/pipeline/step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ def instantiate

# simple block step
class BlockStep
attr_accessor :conditional, :depends_on, :key, :prompt
attr_accessor :conditional, :depends_on, :fields, :label, :key, :prompt

def initialize(key:, conditional: nil, depends_on: [], prompt: nil)
def initialize(key:, conditional: nil, depends_on: [], fields: [], label: nil, prompt: nil)
@key = key
@depends_on = depends_on
@conditional = conditional
@prompt = prompt
@fields = fields
@label = label
end

def <<(_obj)
Expand All @@ -35,9 +37,11 @@ def <<(_obj)
def to_h
{ block: @key, key: @key }.tap do |h|
# rename conditional to if (a reserved word as an attribute or instance variable is complicated)
h[:label] = @label unless @label.nil?
h[:depends_on] = @depends_on unless @depends_on.empty?
h[:if] = @conditional unless @conditional.nil?
h[:prompt] = @prompt unless @prompt.nil?
h[:fields] = @fields unless Array(@fields).empty?
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
steps:
- block: approval
key: approval
label: Approval
prompt: |-
Please review the following information
and approve the pipeline progression
fields:
- key: myvar
text: Default myvalue
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
---
steps:
- group: gha-test
key: ghatest
- group: stage1
key: stage1
steps:
- commands:
- ' # step [] {:type=>"Background", :name=>"postgres-dependency", :identifier=>"postgresdependency",
:spec=>{"connectorRef"=>"myDockerHubConnector", "image"=>"postgres:10.8", "shell"=>"Sh",
"envVariables"=>{"POSTGRES_USER"=>"postgres", "POSTGRES_PASSWORD"=>"<+secrets.getValue(\"DbPasswordSecret\")>",
"POSTGRES_DB"=>"postgres"}}} not implemented yet :('
- echo "this runs on openjdk"
label: stage1
key: stage1
- commands:
- |-
echo "pipeline var:" <+pipeline.variables.pipelinevar1>
echo "project level var:" <+variable.proj_var>
echo "secret example :" <+secrets.getValue("DbPasswordSecret")>
label: stage2
key: stage2
- commands:
- echo "Testing on <+matrix.testparam>"
label: matrix stage
key: matrix_stage
- echo "this runs on openjdk"
label: Run_1
key: Run_1
- commands:
- |-
echo "pipeline var:" <+pipeline.variables.pipelinevar1>
echo "project level var:" <+variable.proj_var>
echo "secret example :" <+secrets.getValue("DbPasswordSecret")>
label: Run_2
key: Run_2
- commands:
- echo "Testing on <+matrix.testparam>"
label: Run_3
key: Run_3
31 changes: 31 additions & 0 deletions app/spec/lib/bk/compat/harness/examples/approval.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
pipeline:
name: approval-test
identifier: ghatest
stages:
- stage:
name: Manual Stage
identifier: Manual_Stage
description: ""
type: Approval
spec:
execution:
steps:
- step:
name: Approval
identifier: approval
type: HarnessApproval
timeout: 1d
spec:
approvalMessage: |-
Please review the following information
and approve the pipeline progression
includePipelineExecutionHistory: true
approvers:
minimumCount: 1
disallowPipelineExecutor: false
userGroups:
- docs
approverInputs:
- name: myvar
defaultValue: myvalue
failureStrategies: []