From d0d7cafab6af71fce182d9465375473a5f98df51 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 8 Jun 2023 20:11:24 +0000 Subject: [PATCH 1/4] feat(AllowedTags): Adds allowed_tags option --- README.md | 4 ++++ lib/git_ops/config.ex | 2 ++ lib/mix/tasks/git_ops.release.ex | 37 ++++++++++++++++++++++++++------ test/config_test.exs | 15 +++++++++++++ 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 872e08f..a1e3981 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,10 @@ config :git_ops, header: "Important Changes" ] ], + # Only add commits to the changelog that has the "backend" tag + allowed_tags: [ + "backend" + ] # Instructs the tool to manage your mix version in your `mix.exs` file # See below for more information manage_mix_version?: true, diff --git a/lib/git_ops/config.ex b/lib/git_ops/config.ex index 9706d59..df21708 100644 --- a/lib/git_ops/config.ex +++ b/lib/git_ops/config.ex @@ -89,6 +89,8 @@ defmodule GitOps.Config do end) end + def allowed_tags, do: Application.get_env(:git_ops, :allowed_tags) || :any + def prefix, do: Application.get_env(:git_ops, :version_tag_prefix) || "" defp truthy?(nil), do: false diff --git a/lib/mix/tasks/git_ops.release.ex b/lib/mix/tasks/git_ops.release.ex index d713ef1..5ea6e7b 100644 --- a/lib/mix/tasks/git_ops.release.ex +++ b/lib/mix/tasks/git_ops.release.ex @@ -90,6 +90,7 @@ defmodule Mix.Tasks.GitOps.Release do prefix = Config.prefix() config_types = Config.types() + allowed_tags = Config.allowed_tags() from_rc? = Version.parse!(current_version).pre != [] {commit_messages_for_version, commit_messages_for_changelog} = @@ -98,9 +99,10 @@ defmodule Mix.Tasks.GitOps.Release do log_for_version? = !opts[:initial] commits_for_version = - parse_commits(commit_messages_for_version, config_types, log_for_version?) + parse_commits(commit_messages_for_version, config_types, allowed_tags, log_for_version?) - commits_for_changelog = parse_commits(commit_messages_for_changelog, config_types, false) + commits_for_changelog = + parse_commits(commit_messages_for_changelog, config_types, allowed_tags, false) prefixed_new_version = if opts[:initial] do @@ -259,14 +261,16 @@ defmodule Mix.Tasks.GitOps.Release do end end - defp parse_commits(messages, config_types, log?) do - Enum.flat_map(messages, &parse_commit(&1, config_types, log?)) + defp parse_commits(messages, config_types, allowed_tags, log?) do + Enum.flat_map(messages, &parse_commit(&1, config_types, allowed_tags, log?)) end - defp parse_commit(text, config_types, log?) do + defp parse_commit(text, config_types, allowed_tags, log?) do case Commit.parse(text) do {:ok, commits} -> - commits_with_type(config_types, commits, text, log?) + commits + |> commits_with_allowed_tags(allowed_tags) + |> commits_with_type(config_types, text, log?) _ -> error_if_log("Unparseable commit: #{text}", log?) @@ -275,7 +279,26 @@ defmodule Mix.Tasks.GitOps.Release do end end - defp commits_with_type(config_types, commits, text, log?) do + defp commits_with_allowed_tags(commits, :any), do: commits + + defp commits_with_allowed_tags(commits, allowed_tags) do + case Enum.find(commits, fn %{type: type} -> type == "TAGS" end) do + nil -> + # If the commits don't have a tag, we allow it to go through by default + commits + + commit -> + tags = commit.message |> String.split(",", trim: true) |> Enum.map(&String.trim/1) + + if Enum.any?(tags, fn tag -> tag in allowed_tags end) do + commits + else + [] + end + end + end + + defp commits_with_type(commits, config_types, text, log?) do Enum.flat_map(commits, fn commit -> if Map.has_key?(config_types, String.downcase(commit.type)) do [commit] diff --git a/test/config_test.exs b/test/config_test.exs index 576efea..40e33f0 100644 --- a/test/config_test.exs +++ b/test/config_test.exs @@ -10,6 +10,7 @@ defmodule GitOps.Test.ConfigTest do Application.put_env(:git_ops, :changelog_file, "CUSTOM_CHANGELOG.md") Application.put_env(:git_ops, :manage_readme_version, true) Application.put_env(:git_ops, :types, custom: [header: "Custom"], docs: [hidden?: false]) + Application.put_env(:git_ops, :allowed_tags, ["tag_1", "tag_2"]) Application.put_env(:git_ops, :version_tag_prefix, "v") end @@ -91,6 +92,20 @@ defmodule GitOps.Test.ConfigTest do assert types["custom"][:header] == "Custom" end + test "Allowed tags configuration returns correcly" do + allowed_tags = Config.allowed_tags() + + assert allowed_tags == ["tag_1", "tag_2"] + end + + test "Allowed tags without being set in configuration returns :any" do + Application.delete_env(:git_ops, :allowed_tags) + + allowed_tags = Config.allowed_tags() + + assert allowed_tags == :any + end + test "custom prefixes returns correctly" do assert Config.prefix() == "v" end From 4d348fa6e66cf3b60dd6606caf2c72e7f611b138 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 8 Jun 2023 20:32:59 +0000 Subject: [PATCH 2/4] refactor(AllowedTags): Small tests refactoring --- test/config_test.exs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/test/config_test.exs b/test/config_test.exs index 40e33f0..de96a2d 100644 --- a/test/config_test.exs +++ b/test/config_test.exs @@ -93,17 +93,13 @@ defmodule GitOps.Test.ConfigTest do end test "Allowed tags configuration returns correcly" do - allowed_tags = Config.allowed_tags() - - assert allowed_tags == ["tag_1", "tag_2"] + assert Config.allowed_tags() == ["tag_1", "tag_2"] end - test "Allowed tags without being set in configuration returns :any" do - Application.delete_env(:git_ops, :allowed_tags) - - allowed_tags = Config.allowed_tags() + test "Allowed tags configuration returns :any if not set" do + Application.delete_env(:git_ops, :tags) - assert allowed_tags == :any + assert Config.allowed_tags() == :any end test "custom prefixes returns correctly" do From ac39bb99d27986a24638f2a47cd95a75b9f6153c Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 8 Jun 2023 20:34:34 +0000 Subject: [PATCH 3/4] refactor(AllowedTags): Changed the way we store the config Now it stores it as `tags: [allowed: []]` instead of `allowed_tags: []`` --- README.md | 6 +++--- lib/git_ops/config.ex | 2 +- test/config_test.exs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a1e3981..44aae96 100644 --- a/README.md +++ b/README.md @@ -65,9 +65,9 @@ config :git_ops, header: "Important Changes" ] ], - # Only add commits to the changelog that has the "backend" tag - allowed_tags: [ - "backend" + tags: [ + # Only add commits to the changelog that has the "backend" tag + allowed: ["backend"], ] # Instructs the tool to manage your mix version in your `mix.exs` file # See below for more information diff --git a/lib/git_ops/config.ex b/lib/git_ops/config.ex index df21708..6870c3d 100644 --- a/lib/git_ops/config.ex +++ b/lib/git_ops/config.ex @@ -89,7 +89,7 @@ defmodule GitOps.Config do end) end - def allowed_tags, do: Application.get_env(:git_ops, :allowed_tags) || :any + def allowed_tags, do: :git_ops |> Application.get_env(:tags, []) |> Keyword.get(:allowed, :any) def prefix, do: Application.get_env(:git_ops, :version_tag_prefix) || "" diff --git a/test/config_test.exs b/test/config_test.exs index de96a2d..7bcd903 100644 --- a/test/config_test.exs +++ b/test/config_test.exs @@ -10,7 +10,7 @@ defmodule GitOps.Test.ConfigTest do Application.put_env(:git_ops, :changelog_file, "CUSTOM_CHANGELOG.md") Application.put_env(:git_ops, :manage_readme_version, true) Application.put_env(:git_ops, :types, custom: [header: "Custom"], docs: [hidden?: false]) - Application.put_env(:git_ops, :allowed_tags, ["tag_1", "tag_2"]) + Application.put_env(:git_ops, :tags, [allowed: ["tag_1", "tag_2"]]) Application.put_env(:git_ops, :version_tag_prefix, "v") end From 8b299d161b2590e3ca3fdb26410c511697ee4a22 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Thu, 8 Jun 2023 20:38:20 +0000 Subject: [PATCH 4/4] feat(AllowedTags): Add config allow_untagged? to tags --- README.md | 2 ++ lib/git_ops/config.ex | 3 +++ lib/mix/tasks/git_ops.release.ex | 20 ++++++++++---------- test/config_test.exs | 12 +++++++++++- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 44aae96..b35af4e 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,8 @@ config :git_ops, tags: [ # Only add commits to the changelog that has the "backend" tag allowed: ["backend"], + # Filter out or not commits that don't contain tags + allow_untagged?: true ] # Instructs the tool to manage your mix version in your `mix.exs` file # See below for more information diff --git a/lib/git_ops/config.ex b/lib/git_ops/config.ex index 6870c3d..a4bc663 100644 --- a/lib/git_ops/config.ex +++ b/lib/git_ops/config.ex @@ -91,6 +91,9 @@ defmodule GitOps.Config do def allowed_tags, do: :git_ops |> Application.get_env(:tags, []) |> Keyword.get(:allowed, :any) + def allow_untagged?, + do: :git_ops |> Application.get_env(:tags, []) |> Keyword.get(:allow_untagged?, true) + def prefix, do: Application.get_env(:git_ops, :version_tag_prefix) || "" defp truthy?(nil), do: false diff --git a/lib/mix/tasks/git_ops.release.ex b/lib/mix/tasks/git_ops.release.ex index 5ea6e7b..25ef1a0 100644 --- a/lib/mix/tasks/git_ops.release.ex +++ b/lib/mix/tasks/git_ops.release.ex @@ -91,6 +91,7 @@ defmodule Mix.Tasks.GitOps.Release do config_types = Config.types() allowed_tags = Config.allowed_tags() + allow_untagged? = Config.allow_untagged?() from_rc? = Version.parse!(current_version).pre != [] {commit_messages_for_version, commit_messages_for_changelog} = @@ -99,10 +100,10 @@ defmodule Mix.Tasks.GitOps.Release do log_for_version? = !opts[:initial] commits_for_version = - parse_commits(commit_messages_for_version, config_types, allowed_tags, log_for_version?) + parse_commits(commit_messages_for_version, config_types, allowed_tags, allow_untagged?, log_for_version?) commits_for_changelog = - parse_commits(commit_messages_for_changelog, config_types, allowed_tags, false) + parse_commits(commit_messages_for_changelog, config_types, allowed_tags, allow_untagged?, false) prefixed_new_version = if opts[:initial] do @@ -261,15 +262,15 @@ defmodule Mix.Tasks.GitOps.Release do end end - defp parse_commits(messages, config_types, allowed_tags, log?) do - Enum.flat_map(messages, &parse_commit(&1, config_types, allowed_tags, log?)) + defp parse_commits(messages, config_types, allowed_tags, allow_untagged?, log?) do + Enum.flat_map(messages, &parse_commit(&1, config_types, allowed_tags, allow_untagged?, log?)) end - defp parse_commit(text, config_types, allowed_tags, log?) do + defp parse_commit(text, config_types, allowed_tags, allow_untagged?, log?) do case Commit.parse(text) do {:ok, commits} -> commits - |> commits_with_allowed_tags(allowed_tags) + |> commits_with_allowed_tags(allowed_tags, allow_untagged?) |> commits_with_type(config_types, text, log?) _ -> @@ -279,13 +280,12 @@ defmodule Mix.Tasks.GitOps.Release do end end - defp commits_with_allowed_tags(commits, :any), do: commits + defp commits_with_allowed_tags(commits, :any, _), do: commits - defp commits_with_allowed_tags(commits, allowed_tags) do + defp commits_with_allowed_tags(commits, allowed_tags, allow_untagged?) do case Enum.find(commits, fn %{type: type} -> type == "TAGS" end) do nil -> - # If the commits don't have a tag, we allow it to go through by default - commits + if allow_untagged?, do: commits, else: [] commit -> tags = commit.message |> String.split(",", trim: true) |> Enum.map(&String.trim/1) diff --git a/test/config_test.exs b/test/config_test.exs index 7bcd903..e370524 100644 --- a/test/config_test.exs +++ b/test/config_test.exs @@ -10,7 +10,7 @@ defmodule GitOps.Test.ConfigTest do Application.put_env(:git_ops, :changelog_file, "CUSTOM_CHANGELOG.md") Application.put_env(:git_ops, :manage_readme_version, true) Application.put_env(:git_ops, :types, custom: [header: "Custom"], docs: [hidden?: false]) - Application.put_env(:git_ops, :tags, [allowed: ["tag_1", "tag_2"]]) + Application.put_env(:git_ops, :tags, [allowed: ["tag_1", "tag_2"], allow_untagged?: false]) Application.put_env(:git_ops, :version_tag_prefix, "v") end @@ -102,6 +102,16 @@ defmodule GitOps.Test.ConfigTest do assert Config.allowed_tags() == :any end + test "Allow untagged? configuration returns correcly" do + assert Config.allow_untagged?() == false + end + + test "Allow untagged? configuration returns true if not set" do + Application.delete_env(:git_ops, :tags) + + assert Config.allow_untagged?() == true + end + test "custom prefixes returns correctly" do assert Config.prefix() == "v" end