Skip to content

Commit

Permalink
Rescue format errors when parsing workflows yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
hellcp-work committed Feb 8, 2023
1 parent 05af6e2 commit a7ddae3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/api/app/models/token/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ class SCMTokenInvalid < APIError
class WorkflowsYamlNotParsable < APIError
setup 400
end

class WorkflowsYamlFormatError < APIError
setup 400
end
end
8 changes: 6 additions & 2 deletions src/api/app/services/workflows/yaml_to_workflows_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def call
def create_workflows
begin
parsed_workflows_yaml = YAML.safe_load(parse_workflows_file(@yaml_file))
rescue Psych::SyntaxError => e
rescue Psych::SyntaxError, Token::Errors::WorkflowsYamlFormatError => e
raise Token::Errors::WorkflowsYamlNotParsable, "Unable to parse #{@token.workflow_configuration_path}: #{e.message}"
end

Expand All @@ -47,7 +47,11 @@ def parse_workflows_file(file_path)

# Mapping the placeholder variables to their values from the webhook event payload
placeholder_variables = SUPPORTED_PLACEHOLDER_VARIABLES.zip([scm_organization_name, scm_repository_name, pr_number, commit_sha]).to_h
format(workflows_file_content, placeholder_variables)
begin
format(workflows_file_content, placeholder_variables)
rescue ArgumentError => e
raise Token::Errors::WorkflowsYamlFormatError, e.message
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,14 @@
expect { subject }.to raise_error(Token::Errors::WorkflowsYamlNotParsable)
end
end

context 'with invalid placeholder variables' do
let(:workflows_yml_file) { Rails.root.join('spec/support/files/unparsable_workflows_placeholders.yml').expand_path }
let(:payload) { github_extractor_payload }

it 'raises a user-friendly error' do
expect { subject }.to raise_error(Token::Errors::WorkflowsYamlNotParsable, 'Unable to parse .obs/workflows.yml: malformed format string - %S')
end
end
end
end
12 changes: 12 additions & 0 deletions src/api/spec/support/files/unparsable_workflows_placeholders.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
workflow123:
steps:
- branch_package:
source_project: "test-project:%SCM_ORGANIZATION_NAME"
source_package: "test-package:%u"
target_project: "test-target-project"
filters:
branches:
only:
- 'main'
- 'master'
- 'source'

0 comments on commit a7ddae3

Please sign in to comment.