Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
Fix #11 and fix #16
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérémie Bertrand committed Mar 6, 2015
1 parent 7e2e004 commit d06d36b
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,23 @@ public void Format_ExampleA_Output_Html()
public void Format_ExampleA_Output_Markdown()
{
// act
var resultHtml = SemanticReleaseNotesFormatter.Format(GetExempleAReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown });
var resultMarkdown = SemanticReleaseNotesFormatter.Format(GetExempleAReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown });

// assert
Assert.Equal(ExampleAMarkdown, resultHtml.Trim());
Assert.Equal(ExampleAMarkdown, resultMarkdown.Trim());
}

[Fact]
public void Format_ExampleA_Output_Markdown_TextWriter()
{
// arrange
var resultHtml = new StringBuilder();
var resultMarkdown = new StringBuilder();

// act
SemanticReleaseNotesFormatter.Format(new StringWriter(resultHtml), GetExempleAReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown });
SemanticReleaseNotesFormatter.Format(new StringWriter(resultMarkdown), GetExempleAReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown });

// assert
Assert.Equal(ExampleAMarkdown, resultHtml.ToString().Trim());
Assert.Equal(ExampleAMarkdown, resultMarkdown.ToString().Trim());
}

[Fact]
Expand All @@ -94,10 +94,10 @@ public void Format_ExampleA_Output_Html_GroupBy_Categories()
public void Format_ExampleA_Output_Markdown_GroupBy_Categories()
{
// act
var resultHtml = SemanticReleaseNotesFormatter.Format(GetExempleAReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown, GroupBy = GroupBy.Categories });
var resultMarkdown = SemanticReleaseNotesFormatter.Format(GetExempleAReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown, GroupBy = GroupBy.Categories });

// assert
Assert.Equal(ExampleAMarkdownCategories, resultHtml.Trim());
Assert.Equal(ExampleAMarkdownCategories, resultMarkdown.Trim());
}

[Fact]
Expand All @@ -114,10 +114,10 @@ public void Format_ExampleB_Output_Html()
public void Format_ExampleB_Output_Markdown()
{
// act
var resultHtml = SemanticReleaseNotesFormatter.Format(GetExempleBReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown });
var resultMarkdown = SemanticReleaseNotesFormatter.Format(GetExempleBReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown });

// assert
Assert.Equal(ExampleBMarkdown, resultHtml.Trim());
Assert.Equal(ExampleBMarkdown, resultMarkdown.Trim());
}

[Fact]
Expand All @@ -134,10 +134,10 @@ public void Format_ExampleA_Output_Html_Custom_LiquidTemplate()
public void Format_ExampleA_Output_Markdown_Custom_LiquidTemplate()
{
// act
var resultHtml = SemanticReleaseNotesFormatter.Format(GetExempleAReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown, LiquidTemplate = CustomLiquidTemplate });
var resultMarkdown = SemanticReleaseNotesFormatter.Format(GetExempleAReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown, LiquidTemplate = CustomLiquidTemplate });

// assert
Assert.Equal(CustomLiquidTemplateMarkdown, resultHtml.Trim());
Assert.Equal(CustomLiquidTemplateMarkdown, resultMarkdown.Trim());
}

[Fact]
Expand All @@ -154,10 +154,10 @@ public void Format_ExampleC_Output_Html()
public void Format_ExampleC_Output_Markdown()
{
// act
var resultHtml = SemanticReleaseNotesFormatter.Format(GetExempleCReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown });
var resultMarkdown = SemanticReleaseNotesFormatter.Format(GetExempleCReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown });

// assert
Assert.Equal(ExampleCMarkdown, resultHtml.Trim());
Assert.Equal(ExampleCMarkdown, resultMarkdown.Trim());
}

[Fact]
Expand All @@ -174,10 +174,10 @@ public void Format_ExampleC_Output_Html_GroupBy_Categories()
public void Format_ExampleC_Output_Markdown_GroupBy_Categories()
{
// act
var resultHtml = SemanticReleaseNotesFormatter.Format(GetExempleCReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown, GroupBy = GroupBy.Categories });
var resultMarkdown = SemanticReleaseNotesFormatter.Format(GetExempleCReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown, GroupBy = GroupBy.Categories });

// assert
Assert.Equal(ExampleCMarkdownCategories, resultHtml.Trim());
Assert.Equal(ExampleCMarkdownCategories, resultMarkdown.Trim());
}

[Fact]
Expand All @@ -195,10 +195,10 @@ public void Format_ExampleD_Output_Html()
public void Format_ExampleD_Output_Markdown()
{
// act
var resultHtml = SemanticReleaseNotesFormatter.Format(GetExempleDReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown });
var resultMarkdown = SemanticReleaseNotesFormatter.Format(GetExempleDReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown });

// assert
Assert.Equal(ExampleDMarkdown, resultHtml.Trim());
Assert.Equal(ExampleDMarkdown, resultMarkdown.Trim());
}

[Fact]
Expand All @@ -215,10 +215,30 @@ public void Format_ExampleA_Output_Html_PluralizeCategoriesTitle()
public void Format_ExampleA_Output_Markdown_PluralizeCategoriesTitle()
{
// act
var resultHtml = SemanticReleaseNotesFormatter.Format(GetExempleABisReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown, GroupBy = GroupBy.Categories, PluralizeCategoriesTitle = true });
var resultMarkdown = SemanticReleaseNotesFormatter.Format(GetExempleABisReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown, GroupBy = GroupBy.Categories, PluralizeCategoriesTitle = true });

// assert
Assert.Equal(ExampleABisMarkdownCategories, resultMarkdown.Trim());
}

[Fact]
public void Format_SyntaxMetadataCommits_Output_Html()
{
// act
var resultHtml = SemanticReleaseNotesFormatter.Format(GetSyntaxMetadataCommitsReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Html, GroupBy = GroupBy.Categories, PluralizeCategoriesTitle = true });

// assert
Assert.Equal(SyntaxMetadataCommitsHtml, resultHtml.Trim());
}

[Fact]
public void Format_SyntaxMetadataCommits_Output_Markdown()
{
// act
var resultMarkdown = SemanticReleaseNotesFormatter.Format(GetSyntaxMetadataCommitsReleaseNotes(), new SemanticReleaseNotesConverterSettings { OutputFormat = OutputFormat.Markdown, GroupBy = GroupBy.Categories, PluralizeCategoriesTitle = true });

// assert
Assert.Equal(ExampleABisMarkdownCategories, resultHtml.Trim());
Assert.Equal(SyntaxMetadataCommitsMarkdown, resultMarkdown.Trim());
}

private ReleaseNotes GetExempleAReleaseNotes()
Expand Down Expand Up @@ -316,6 +336,18 @@ private ReleaseNotes GetExempleABisReleaseNotes()
};
}

private ReleaseNotes GetSyntaxMetadataCommitsReleaseNotes()
{
return new ReleaseNotes
{
Metadata = new List<Metadata>
{
new Metadata { Name = "Commits", Value = "56af25a...d3fead4" },
new Metadata { Name = "Commits", Value = "[56af25a...d3fead4](https://github.com/Glimpse/Semantic-Release-Notes/compare/56af25a...d3fead4)" }
}
};
}

private const string ExampleAHtml = @"<html>
<body>
<p>Incremental release designed to provide an update to some of the core plugins.</p>
Expand Down Expand Up @@ -559,5 +591,15 @@ private ReleaseNotes GetExempleABisReleaseNotes()
# Plugin
1. {Changed} *Timeline*: Comes with an additional grid view to show the same data.
1. {Fix} *Ajax*: Fix that crashed poll in Chrome and IE due to log/trace statement. [i1234](http://getglimpse.com)";

private const string SyntaxMetadataCommitsMarkdown = @"Commits: 56af25a...d3fead4
Commits: [56af25a...d3fead4](https://github.com/Glimpse/Semantic-Release-Notes/compare/56af25a...d3fead4)";

private const string SyntaxMetadataCommitsHtml = @"<html>
<body>
<p>Commits: 56af25a...d3fead4</p>
<p>Commits: <a href=""https://github.com/Glimpse/Semantic-Release-Notes/compare/56af25a...d3fead4"">56af25a...d3fead4</a></p>
</body>
</html>";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,10 @@ public void Parse_Real_NVika()
var releaseNote = SemanticReleaseNotesParser.Parse(NVikaReleaseNotes);

// assert
Assert.Equal(@"
Assert.Equal(1, releaseNote.Metadata.Count);
Assert.Equal("Commits", releaseNote.Metadata[0].Name);
Assert.Equal("[19556f025b...0203ea9a43](https://github.com/laedit/vika/compare/19556f025b...0203ea9a43)", releaseNote.Metadata[0].Value);

Commits: [19556f025b...0203ea9a43](https://github.com/laedit/vika/compare/19556f025b...0203ea9a43)", releaseNote.Summary);
Assert.Equal(3, releaseNote.Items.Count);

Assert.Equal("Enhancement", releaseNote.Items[0].Categories[0]);
Expand Down Expand Up @@ -327,6 +328,21 @@ public void Parse_Example_A_WithOnlyLF()
Assert.Equal("Fix", releaseNote.Items[3].Categories[0]);
}

[Fact]
public void Parse_Syntax_Metadata_Commits()
{
// act
var releaseNote = SemanticReleaseNotesParser.Parse(Syntax_Metadata_Commits);

// assert
Assert.Equal(2, releaseNote.Metadata.Count);

Assert.Equal("Commits", releaseNote.Metadata[0].Name);
Assert.Equal("56af25a...d3fead4", releaseNote.Metadata[0].Value);
Assert.Equal("Commits", releaseNote.Metadata[1].Name);
Assert.Equal("[56af25a...d3fead4](https://github.com/Glimpse/Semantic-Release-Notes/compare/56af25a...d3fead4)", releaseNote.Metadata[1].Value);
}

private TextReader GetTextReader(string input)
{
return new StringReader(input);
Expand Down Expand Up @@ -426,5 +442,8 @@ This description is specific to plugin section.
Commits: [19556f025b...0203ea9a43](https://github.com/laedit/vika/compare/19556f025b...0203ea9a43)
";

private const string Syntax_Metadata_Commits = @"56af25a...d3fead4
Commits: [56af25a...d3fead4](https://github.com/Glimpse/Semantic-Release-Notes/compare/56af25a...d3fead4)";
}
}
20 changes: 20 additions & 0 deletions SemanticReleaseNotesParser.Core/Metadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using DotLiquid;

namespace SemanticReleaseNotesParser.Core
{
/// <summary>
/// Metadata of a release note
/// </summary>
public sealed class Metadata : Drop
{
/// <summary>
/// Name of the metadata
/// </summary>
public string Name { get; set; }

/// <summary>
/// Value of the metadata
/// </summary>
public string Value { get; set; }
}
}
14 changes: 14 additions & 0 deletions SemanticReleaseNotesParser.Core/Parser/MetadataDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Text.RegularExpressions;

namespace SemanticReleaseNotesParser.Core
{
internal sealed class MetadataDefinition
{
public string Name { get; set; }

public Regex Regex { get; set; }

public Func<Match, string> GetValue { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Humanizer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
Expand All @@ -17,6 +18,15 @@ internal static class SemanticReleaseNotesParser
private static readonly Regex CategoryRegex = new Regex(@"\+([\w-]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex SummaryRegex = new Regex(@"^[a-zA-Z0-9]", RegexOptions.Compiled | RegexOptions.IgnoreCase);

private static readonly List<MetadataDefinition> MetadataDefinitions = new List<MetadataDefinition>
{
{ new MetadataDefinition {
Name = "Commits",
Regex = new Regex(@"^(?:commits:)?[ ]*(?:([0-9a-f]{5,40}\.{3}[0-9a-f]{5,40})|(\[[0-9a-f]{5,40}\.{3}[0-9a-f]{5,40}\]\(https?:\/\/\S+\)))$", RegexOptions.Compiled | RegexOptions.IgnoreCase),
GetValue = match => match.Groups[1].Success ? match.Groups[1].Value : match.Groups[2].Value
} }
};

/// <summary>
/// Parse a release notes from a stream
/// </summary>
Expand Down Expand Up @@ -63,6 +73,11 @@ public static ReleaseNotes Parse(string rawReleaseNotes, SemanticReleaseNotesCon
matched = ProcessItem(rawLine, releaseNotes);
}

if (!matched)
{
matched = ProcessMetadata(rawLine, releaseNotes);
}

if (!matched)
{
string nextInput = string.Empty;
Expand All @@ -77,6 +92,20 @@ public static ReleaseNotes Parse(string rawReleaseNotes, SemanticReleaseNotesCon
return releaseNotes;
}

private static bool ProcessMetadata(string input, ReleaseNotes releaseNotes)
{
foreach (var metadataDefinition in MetadataDefinitions)
{
var match = metadataDefinition.Regex.Match(input);
if (match.Success)
{
releaseNotes.Metadata.Add(new Metadata { Name = metadataDefinition.Name, Value = metadataDefinition.GetValue(match) });
return true;
}
}
return false;
}

private static void ProcessPrimary(string input, ReleaseNotes releaseNotes, string nextInput)
{
input = input.Trim();
Expand Down
8 changes: 7 additions & 1 deletion SemanticReleaseNotesParser.Core/ReleaseNotes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ public sealed class ReleaseNotes : Drop
/// </summary>
public List<Item> Items { get; set; }

/// <summary>
/// Metadata of the release notes
/// </summary>
public List<Metadata> Metadata { get; set; }

/// <summary>
/// Instantiates a new ReleaseNotes
/// </summary>
public ReleaseNotes()
{
Sections = new List<Section>();
Items = new List<Item>();
Metadata = new List<Metadata>();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{{ release_notes.summary }}
{%- if items_without_categories.size > 0 -%}
{%- if items_without_categories != empty -%}

{%- endif -%}
{%- for item in items_without_categories -%}
{% if item.priority > 0 -%} {{ item.priority }}. {%- else -%} - {%- endif %} {{ item.summary }} {%- if item.task_id %} [{{ item.task_id }}]({{ item.task_link }}) {%- endif -%}

{%- endfor -%}
{%- if categories.size > 0 -%}
{%- if categories != empty -%}

{%- endif -%}
{%- for category in categories -%}
Expand All @@ -16,4 +16,8 @@

{%- endfor -%}

{%- endfor -%}
{%- for metadata in release_notes.metadata -%}
{{ metadata.name }}: {{ metadata.value }}

{%- endfor -%}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{{ release_notes.summary }}
{%- if release_notes.items.size > 0 -%}
{%- if release_notes.items != empty -%}

{%- endif -%}
{%- for item in release_notes.items -%}
{% if item.priority > 0 -%} {{ item.priority }}. {%- else -%} - {%- endif %} {% if item.categories != empty -%}{{ lcb }}{%- for category in item.categories -%}{%- if forloop.first == false -%}, {% endif -%}{{ category }}{%- endfor -%}{{ rcb }} {% endif %}{{ item.summary }} {%- if item.task_id %} [{{ item.task_id }}]({{ item.task_link }}) {%- endif -%}

{%- endfor -%}
{%- if release_notes.sections.size > 0 -%}
{%- if release_notes.sections != empty -%}

{%- endif -%}
{%- for section in release_notes.sections -%}
Expand All @@ -16,4 +16,8 @@

{%- endfor -%}

{%- endfor -%}
{%- for metadata in release_notes.metadata -%}
{{ metadata.name }}: {{ metadata.value }}

{%- endfor -%}
Loading

0 comments on commit d06d36b

Please sign in to comment.