From 1b205dc4a88a76d7b087384e7f223c8863c38e85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Almir=20Saraj=C4=8Di=C4=87?= Date: Fri, 28 Jun 2024 18:40:59 +0200 Subject: [PATCH] Fix YAML syntax error --- CHANGELOG.md | 1 + lib/github_workflows_generator/yml_encoder.ex | 16 ++++++++-- .../yml_encoder_test.exs | 31 +++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f983b1..21427a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/github_workflows_generator/yml_encoder.ex b/lib/github_workflows_generator/yml_encoder.ex index 02fa3ba..7a19d93 100644 --- a/lib/github_workflows_generator/yml_encoder.ex +++ b/lib/github_workflows_generator/yml_encoder.ex @@ -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? -> diff --git a/test/github_workflows_generator/yml_encoder_test.exs b/test/github_workflows_generator/yml_encoder_test.exs index 11a216f..71f1db9 100644 --- a/test/github_workflows_generator/yml_encoder_test.exs +++ b/test/github_workflows_generator/yml_encoder_test.exs @@ -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", @@ -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 }}\"" ] ] ] @@ -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' @@ -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