Skip to content

Commit

Permalink
feat: support changelog customization options (#43)
Browse files Browse the repository at this point in the history
* feat: support changelog customization options

* test: add custom header test

* docs: add sample .versionize changelog configuration
  • Loading branch information
cabauman committed Jan 30, 2022
1 parent 5926a19 commit c27cf07
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 77 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Versionize
# Versionize

[![Coverage Status](https://coveralls.io/repos/saintedlama/versionize/badge.svg?branch=)](https://coveralls.io/r/saintedlama/versionize?branch=master)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
Expand Down Expand Up @@ -139,6 +139,36 @@ You can configure `versionize` either by creating a `.versionize` JSON file the
Any of the command line parameters accepted by `versionized` can be provided via configuration file leaving out any `-`. For example `skip-dirty` can be provided as `skipDirty` in the configuration file.
Changelog customization can only be done via a `.versionize` file. The following is an example configuration:
```json
{
"changelogAll": true,
"changelog": {
"header": "My Changelog",
"sections": [
{
"type": "feat",
"section": "✨ Features",
"hidden": false
},
{
"type": "fix",
"section": "🐛 Bug Fixes",
"hidden": true
},
{
"type": "perf",
"section": "🚀 Performance",
"hidden": false
}
]
}
}
```
Because `changelogAll` is true and the _fix_ section is hidden, fix commits will appear in the a section titled "Other".
## Developing
To get prettier test outputs run `dotnet test` with prettier test logger
Expand Down
280 changes: 235 additions & 45 deletions Versionize.Tests/Changelog/ChangelogBuilderTests.cs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Versionize.Tests/Versionize.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
44 changes: 24 additions & 20 deletions Versionize/Changelog/ChangelogBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,41 @@ namespace Versionize.Changelog
{
public class ChangelogBuilder
{
private const string Preamble = "# Change Log\n\nAll notable changes to this project will be documented in this file. See [versionize](https://github.com/saintedlama/versionize) for commit guidelines.\n";

private ChangelogBuilder(string file)
{
FilePath = file;
}

public string FilePath { get; }

public void Write(Version version, DateTimeOffset versionTime, IChangelogLinkBuilder linkBuilder, IEnumerable<ConventionalCommit> commits,
bool includeAllCommitsInChangelog = false)
public void Write(
Version version,
DateTimeOffset versionTime,
IChangelogLinkBuilder linkBuilder,
IEnumerable<ConventionalCommit> commits,
ChangelogOptions changelogOptions)
{
var versionTagLink = string.IsNullOrWhiteSpace(linkBuilder.BuildVersionTagLink(version)) ? version.ToString() : $"[{version}]({linkBuilder.BuildVersionTagLink(version)})";
var versionTagLink = string.IsNullOrWhiteSpace(linkBuilder.BuildVersionTagLink(version))
? version.ToString()
: $"[{version}]({linkBuilder.BuildVersionTagLink(version)})";

var markdown = $"<a name=\"{version}\"></a>";
markdown += "\n";
markdown += $"## {versionTagLink} ({versionTime.Year}-{versionTime.Month}-{versionTime.Day})";
markdown += "\n";
markdown += "\n";

var bugFixes = BuildBlock("Bug Fixes", linkBuilder, commits.Where(commit => commit.IsFix));

if (!string.IsNullOrWhiteSpace(bugFixes))
{
markdown += bugFixes;
markdown += "\n";
}

var features = BuildBlock("Features", linkBuilder, commits.Where(commit => commit.IsFeature));
var visibleChangelogSections = changelogOptions.Sections.Where(x => !x.Hidden);

if (!string.IsNullOrWhiteSpace(features))
foreach (var changelogSection in visibleChangelogSections)
{
markdown += features;
markdown += "\n";
var matchingCommits = commits.Where(commit => commit.Type == changelogSection.Type);
var buildBlock = BuildBlock(changelogSection.Section, linkBuilder, matchingCommits);
if (!string.IsNullOrWhiteSpace(buildBlock))
{
markdown += buildBlock;
markdown += "\n";
}
}

var breaking = BuildBlock("Breaking Changes", linkBuilder, commits.Where(commit => commit.IsBreakingChange));
Expand All @@ -53,9 +54,12 @@ private ChangelogBuilder(string file)
markdown += "\n";
}

if (includeAllCommitsInChangelog)
if (changelogOptions.IncludeAllCommits)
{
var other = BuildBlock("Other", linkBuilder, commits.Where(commit => !commit.IsFix && !commit.IsFeature && !commit.IsBreakingChange));
var other = BuildBlock(
"Other",
linkBuilder,
commits.Where(commit => !visibleChangelogSections.Any(x => x.Type == commit.Type) && !commit.IsBreakingChange));

if (!string.IsNullOrWhiteSpace(other))
{
Expand Down Expand Up @@ -83,7 +87,7 @@ private ChangelogBuilder(string file)
}
else
{
File.WriteAllText(FilePath, Preamble + "\n" + markdown);
File.WriteAllText(FilePath, changelogOptions.Header + "\n" + markdown);
}
}

Expand Down
29 changes: 29 additions & 0 deletions Versionize/ChangelogOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Versionize
{
// TODO: Consider creating a ChangelogConfigurationContract as a layer of abstraction.
public record class ChangelogOptions
{
private const string Preamble = "# Change Log\n\nAll notable changes to this project will be documented in this file. See [versionize](https://github.com/saintedlama/versionize) for commit guidelines.\n";
public static readonly ChangelogOptions Default = new ChangelogOptions
{
Header = Preamble,
IncludeAllCommits = false,
Sections = new ChangelogSection[]
{
new ChangelogSection { Type = "feat", Section = "Features", Hidden = false },
new ChangelogSection { Type = "fix", Section = "Bug Fixes", Hidden = false },
}
};

public string Header { get; set; }
// Ignoring this serialization option for the moment, since ConfigurationContract
// already has this option. It would be nice to remove that other option in favor
// of this one, but it would be a breaking change.
[JsonIgnore]
public bool IncludeAllCommits { get; set; }
public IEnumerable<ChangelogSection> Sections { get; set; }
}
}
9 changes: 9 additions & 0 deletions Versionize/ChangelogSection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Versionize
{
public class ChangelogSection
{
public string Type { get; set; }
public string Section { get; set; }
public bool Hidden { get; set; }
}
}
4 changes: 2 additions & 2 deletions Versionize/ConfigurationContract.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

using System;
using System;

namespace Versionize
{
Expand All @@ -13,5 +12,6 @@ public class ConfigurationContract
public bool? IgnoreInsignificantCommits { get; set; }
public bool? ChangelogAll { get; set; }
public String CommitSuffix { get; set; }
public ChangelogOptions Changelog { get; set; }
}
}
31 changes: 26 additions & 5 deletions Versionize/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Text.Json;
using McMaster.Extensions.CommandLineUtils;
Expand Down Expand Up @@ -57,9 +57,10 @@ public static int Main(string[] args)
SkipCommit = optionSkipCommit.HasValue(),
ReleaseAs = optionReleaseAs.Value(),
IgnoreInsignificantCommits = optionIgnoreInsignificant.HasValue(),
ChangelogAll = optionIncludeAllCommitsInChangelog.HasValue(),
CommitSuffix = optionCommitSuffix.Value(),
});
Changelog = ChangelogOptions.Default,
},
optionIncludeAllCommitsInChangelog.HasValue());
CommandLineUI.Verbosity = MergeBool(optionSilent.HasValue(), jsonFileConfig?.Silent) ? LogLevel.Silent : LogLevel.All;
Expand Down Expand Up @@ -103,17 +104,37 @@ private static ConfigurationContract FromJsonFile(string filePath)
}
}

private static VersionizeOptions MergeWithOptions(ConfigurationContract optionalConfiguration, VersionizeOptions configuration)
private static VersionizeOptions MergeWithOptions(
ConfigurationContract optionalConfiguration,
VersionizeOptions configuration,
bool changelogAll)
{
var includeAllCommits = MergeBool(changelogAll, optionalConfiguration?.ChangelogAll);
var changelog = MergeChangelogOptions(optionalConfiguration?.Changelog, configuration.Changelog);
changelog.IncludeAllCommits = includeAllCommits;
return new VersionizeOptions
{
DryRun = MergeBool(configuration.DryRun, optionalConfiguration?.DryRun),
SkipDirty = MergeBool(configuration.SkipDirty, optionalConfiguration?.SkipDirty),
SkipCommit = MergeBool(configuration.SkipCommit, optionalConfiguration?.SkipCommit),
ReleaseAs = configuration.ReleaseAs ?? optionalConfiguration?.ReleaseAs,
IgnoreInsignificantCommits = MergeBool(configuration.IgnoreInsignificantCommits, optionalConfiguration?.IgnoreInsignificantCommits),
ChangelogAll = MergeBool(configuration.ChangelogAll, optionalConfiguration?.ChangelogAll),
CommitSuffix = configuration.CommitSuffix ?? optionalConfiguration?.CommitSuffix,
Changelog = changelog,
};
}

private static ChangelogOptions MergeChangelogOptions(ChangelogOptions customOptions, ChangelogOptions defaultOptions)
{
if (customOptions == null)
{
return defaultOptions;
}

return new ChangelogOptions
{
Header = customOptions.Header ?? defaultOptions.Header,
Sections = customOptions.Sections ?? defaultOptions.Sections,
};
}

Expand Down
1 change: 1 addition & 0 deletions Versionize/Versionize.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<Version>1.10.0</Version>
<ToolCommandName>versionize</ToolCommandName>
<LangVersion>latest</LangVersion>
<PackAsTool>True</PackAsTool>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
Expand Down
6 changes: 4 additions & 2 deletions Versionize/VersionizeOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Versionize
{
Expand All @@ -9,7 +11,7 @@ public class VersionizeOptions
public bool SkipCommit { get; set; }
public String ReleaseAs { get; set; }
public bool IgnoreInsignificantCommits { get; set; }
public bool ChangelogAll { get; set; }
public String CommitSuffix { get; set; }
public ChangelogOptions Changelog { get; set; } = ChangelogOptions.Default;
}
}
4 changes: 2 additions & 2 deletions Versionize/WorkingCopy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Linq;
using LibGit2Sharp;
Expand Down Expand Up @@ -115,7 +115,7 @@ public Version Versionize(VersionizeOptions options)
if (!options.DryRun)
{
var changelogLinkBuilder = LinkBuilderFactory.CreateFor(repo);
changelog.Write(nextVersion, versionTime, changelogLinkBuilder, conventionalCommits, options.ChangelogAll);
changelog.Write(nextVersion, versionTime, changelogLinkBuilder, conventionalCommits, options.Changelog);
}

Step("updated CHANGELOG.md");
Expand Down

0 comments on commit c27cf07

Please sign in to comment.