Skip to content

Commit

Permalink
feat(AllowedTags): Add config allow_untagged? to tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo committed Jun 8, 2023
1 parent ac39bb9 commit 8b299d1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/git_ops/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions lib/mix/tasks/git_ops.release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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} =
Expand All @@ -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
Expand Down Expand Up @@ -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?)

_ ->
Expand All @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion test/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

0 comments on commit 8b299d1

Please sign in to comment.