Skip to content

Commit

Permalink
feat(AllowedTags): Adds allowed_tags option
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo committed Jun 8, 2023
1 parent 744b12e commit d0d7caf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions lib/git_ops/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 30 additions & 7 deletions lib/mix/tasks/git_ops.release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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} =
Expand All @@ -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
Expand Down Expand Up @@ -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?)
Expand All @@ -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]
Expand Down
15 changes: 15 additions & 0 deletions test/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d0d7caf

Please sign in to comment.