Skip to content

Commit

Permalink
Fix YAML syntax error
Browse files Browse the repository at this point in the history
  • Loading branch information
almirsarajcic committed Jun 28, 2024
1 parent d5b4fed commit 1b205dc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Dropped support for Elixir 1.11 and 1.12 (Credo requires version 1.13 at least).
- Removed `fast_yaml` and `libyaml` from `README.md`.
- Upgraded Elixir version to 1.17.1.
- Fixed YAML syntax error (`Nested mappings are not allowed in compact mappings`)

## 0.1.2 (2024-06-21)

Expand Down
16 changes: 13 additions & 3 deletions lib/github_workflows_generator/yml_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,31 @@ defmodule GithubWorkflowsGenerator.YmlEncoder do
defp to_yml(data, level, _indent?) do
cond do
!is_binary(data) ->
to_string(data)
value(data)

String.contains?(data, "\n") ->
values =
data
|> String.split("\n", trim: true)
|> Enum.map_join("\n", &(String.duplicate(" ", level) <> &1))
|> Enum.map_join("\n", &(String.duplicate(" ", level) <> value(&1)))

"|\n#{values}"

true ->
data
value(data)
end
end

defp value(value) when is_binary(value) do
if String.contains?(value, ": ") do
"'#{String.replace(value, "'", "''")}'"
else
value
end
end

defp value(value), do: to_string(value)

defp handle_keyword_list(data, level, indent?) do
data
|> Enum.map_reduce(indent?, fn {key, value}, indent? ->
Expand Down
31 changes: 31 additions & 0 deletions test/github_workflows_generator/yml_encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ defmodule GithubWorkflowsGenerator.YmlEncoderTest do
jobs: [
test_schedule: [
"runs-on": "ubuntu-latest",
services: [
db: [
image: "postgres:13",
ports: ["5432:5432"],
env: [POSTGRES_PASSWORD: "postgres"],
options:
"--health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5"
]
],
steps: [
[
name: "Not on Monday or Wednesday",
Expand All @@ -58,6 +67,16 @@ defmodule GithubWorkflowsGenerator.YmlEncoderTest do
"mix-${{ matrix.versions.runner-image }}-${{ matrix.versions.otp }}-${{ matrix.versions.elixir }}-${{ github.sha }}",
"restore-keys": "\nmix-${{ matrix.versions.runner-image }}"
]
],
[
name: "Create an environment",
run:
"read token_id token_secret webhook_secret < <(echo $(curl -X POST \"http://localhost/api/environments\" -H \"Content-Type: application/json; charset=utf-8\" -H \"Authorization: ApiKey ${{ secrets.API_KEY }}\" -d '{\"name\": \"pr-${{ github.event.number }}\"}' | jq -r '.token_id, .token_secret, .webhook_secret')) && echo \"TOKEN_ID=$token_id\" >> credentials && echo \"TOKEN_SECRET=$token_secret\" >> credentials && echo \"WEBHOOK_SECRET=$webhook_secret\" >> credentials && cat credentials >> $GITHUB_ENV\"\""
],
[
name: "Delete an environment",
run:
"curl -X DELETE \"http://localhost/api/environments/1\" -H \"Content-Type: application/json; charset=utf-8\" -H \"Authorization: ApiKey ${{ secrets.API_KEY }}\""
]
]
]
Expand All @@ -72,6 +91,14 @@ defmodule GithubWorkflowsGenerator.YmlEncoderTest do
jobs:
test_schedule:
runs-on: ubuntu-latest
services:
db:
image: postgres:13
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: postgres
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Not on Monday or Wednesday
if: github.event.schedule != '30 5 * * 1,3'
Expand All @@ -86,6 +113,10 @@ defmodule GithubWorkflowsGenerator.YmlEncoderTest do
key: mix-${{ matrix.versions.runner-image }}-${{ matrix.versions.otp }}-${{ matrix.versions.elixir }}-${{ github.sha }}
restore-keys: |
mix-${{ matrix.versions.runner-image }}
- name: Create an environment
run: 'read token_id token_secret webhook_secret < <(echo $(curl -X POST "http://localhost/api/environments" -H "Content-Type: application/json; charset=utf-8" -H "Authorization: ApiKey ${{ secrets.API_KEY }}" -d ''{"name": "pr-${{ github.event.number }}"}'' | jq -r ''.token_id, .token_secret, .webhook_secret'')) && echo "TOKEN_ID=$token_id" >> credentials && echo "TOKEN_SECRET=$token_secret" >> credentials && echo "WEBHOOK_SECRET=$webhook_secret" >> credentials && cat credentials >> $GITHUB_ENV""'
- name: Delete an environment
run: 'curl -X DELETE "http://localhost/api/environments/1" -H "Content-Type: application/json; charset=utf-8" -H "Authorization: ApiKey ${{ secrets.API_KEY }}"'
"""
end
end
Expand Down

0 comments on commit 1b205dc

Please sign in to comment.