Skip to content

Commit

Permalink
fixed #1050
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed Aug 25, 2023
1 parent 3f04975 commit db9e2ad
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ and this project adheres to

### Fixed

- Fixed issue where job names were being incorrectly hyphenated during
project.yaml export [#1050](https://github.com/OpenFn/Lightning/issues/1050)

## [v0.7.3] - 2023-08-15

### Added
Expand Down
69 changes: 37 additions & 32 deletions lib/lightning/export_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ defmodule Lightning.ExportUtils do
Workflows
}

defp job_to_treenode(job) do
job_name = job.name |> String.replace(" ", "-")
defp hyphenate(string) when is_binary(string) do
string |> String.replace(" ", "-")
end

defp hyphenate(other), do: other

defp job_to_treenode(job) do
%{
id: job.id,
name: job_name,
# The identifier here for our YAML reducer will be the hyphenated name
id: hyphenate(job.name),
name: job.name,
node_type: :job,
adaptor: job.adaptor,
body: job.body,
Expand All @@ -42,7 +47,7 @@ defmodule Lightning.ExportUtils do
defp edge_to_treenode(%{source_job_id: nil} = edge, triggers) do
edge = Repo.preload(edge, [:source_trigger, :target_job])
trigger_name = edge.source_trigger.type |> Atom.to_string()
target_name = edge.target_job.name |> String.replace(" ", "-")
target_name = edge.target_job.name |> hyphenate()

%{
name: "#{trigger_name}->#{target_name}",
Expand All @@ -55,8 +60,8 @@ defmodule Lightning.ExportUtils do

defp edge_to_treenode(%{source_trigger_id: nil} = edge, _unused_triggers) do
edge = Repo.preload(edge, [:source_job, :target_job])
source_job = edge.source_job.name |> String.replace(" ", "-")
target_job = edge.target_job.name |> String.replace(" ", "-")
source_job = edge.source_job.name |> hyphenate()
target_job = edge.target_job.name |> hyphenate()

%{
name: "#{source_job}->#{target_job}",
Expand All @@ -73,26 +78,6 @@ defmodule Lightning.ExportUtils do
trigger.name
end

defp handle_bitstring(k, v, i) do
case k do
:body ->
indented_expression =
String.split(v, "\n")
|> Enum.map_join("\n", fn line -> "#{i} #{line}" end)

"body: |\n#{indented_expression}"

:adaptor ->
"#{k}: '#{v}'"

:cron_expression ->
"#{k}: '#{v}'"

_ ->
"#{k}: #{v}"
end
end

defp pick_and_sort(map) do
ordering_map = %{
project: [:name, :description, :credentials, :globals, :workflows],
Expand Down Expand Up @@ -124,8 +109,28 @@ defmodule Lightning.ExportUtils do
)
end

defp handle_input(key, value, indentation) when is_bitstring(value) do
"#{indentation}#{handle_bitstring(key, value, indentation)}"
defp handle_binary(k, v, i) do
case k do
:body ->
indented_expression =
String.split(v, "\n")
|> Enum.map_join("\n", fn line -> "#{i} #{line}" end)

"body: |\n#{indented_expression}"

:adaptor ->
"#{k}: '#{v}'"

:cron_expression ->
"#{k}: '#{v}'"

_ ->
"#{k}: #{v}"
end
end

defp handle_input(key, value, indentation) when is_binary(value) do
"#{indentation}#{handle_binary(key, value, indentation)}"
end

defp handle_input(key, value, indentation) when is_number(value) do
Expand All @@ -141,11 +146,11 @@ defmodule Lightning.ExportUtils do
end

defp handle_input(key, value, indentation) when is_map(value) do
"#{indentation}#{key}:\n#{to_new_yaml(value, "#{indentation} ")}"
"#{indentation}#{hyphenate(key)}:\n#{to_new_yaml(value, "#{indentation} ")}"
end

defp handle_input(key, value, indentation) when is_list(value) do
"#{indentation}#{key}:\n#{Enum.map_join(value, "\n", fn map -> "#{indentation} #{map.name}:\n#{to_new_yaml(map, "#{indentation} ")}" end)}"
"#{indentation}#{hyphenate(key)}:\n#{Enum.map_join(value, "\n", fn map -> "#{indentation} #{hyphenate(map.name)}:\n#{to_new_yaml(map, "#{indentation} ")}" end)}"
end

defp to_new_yaml(map, indentation \\ "") do
Expand All @@ -172,7 +177,7 @@ defmodule Lightning.ExportUtils do
workflows_map =
Enum.reduce(workflows, %{}, fn workflow, acc ->
ytree = build_workflow_yaml_tree(workflow)
Map.put(acc, String.replace(workflow.name, " ", "-"), ytree)
Map.put(acc, hyphenate(workflow.name), ytree)
end)

%{
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/canonical_project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ workflows:
name: workflow 1
jobs:
webhook-job:
name: webhook-job
name: webhook job
adaptor: '@openfn/language-common@latest'
enabled: true
# credential:
Expand All @@ -16,7 +16,7 @@ workflows:
console.log('webhook job')
fn(state => state)
on-fail:
name: on-fail
name: on fail
adaptor: '@openfn/language-common@latest'
enabled: true
# credential:
Expand All @@ -25,7 +25,7 @@ workflows:
console.log('on fail')
fn(state => state)
on-success:
name: on-success
name: on success
adaptor: '@openfn/language-common@latest'
enabled: true
# credential:
Expand All @@ -52,15 +52,15 @@ workflows:
name: workflow 2
jobs:
some-cronjob:
name: some-cronjob
name: some cronjob
adaptor: '@openfn/language-common@latest'
enabled: true
# credential:
# globals:
body: |
console.log('hello!');
on-cron-failure:
name: on-cron-failure
name: on cron failure
adaptor: '@openfn/language-common@latest'
enabled: true
# credential:
Expand Down

0 comments on commit db9e2ad

Please sign in to comment.