-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #30 Реализовал возможность задать шаблон сообщения в конфигурационном файле, поддержал замену в этом шаблоне подстрок {value.Title} и {value.Url} на информацию из issue / pull request. Добавил тесты, добавил базово документацию.
- Loading branch information
Showing
12 changed files
with
201 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Issueneter.Domain.Models; | ||
using Issueneter.Host.Composition; | ||
using Issueneter.Runner; | ||
using Issueneter.Telegram; | ||
using Issueneter.Telegram.Formatters; | ||
|
||
namespace Issueneter.Host.Modules; | ||
|
||
public static class ScanModule | ||
{ | ||
public static IServiceCollection AddScanModule(this IServiceCollection services, IHostEnvironment env, IConfigurationRoot configuration) => | ||
services | ||
.AddSingleton(GetIssueFormatter) | ||
.AddSingleton(GetPullRequestFormatter) | ||
.AddSingleton<ScanRunner>(); | ||
|
||
private static IMessageFormatter<Issue> GetIssueFormatter(IServiceProvider serviceProvider) | ||
{ | ||
var options = serviceProvider.GetRequiredOptions<TelegramOptions>(); | ||
|
||
if (options.IssueMessageTemplate is not null) | ||
return new ConfigurableMessageFormatter<Issue>(options.IssueMessageTemplate); | ||
|
||
return new IssueMessageFormatter(); | ||
} | ||
|
||
private static IMessageFormatter<PullRequest> GetPullRequestFormatter(IServiceProvider serviceProvider) | ||
{ | ||
var options = serviceProvider.GetRequiredOptions<TelegramOptions>(); | ||
|
||
if (options.PullRequestMessageTemplate is not null) | ||
return new ConfigurableMessageFormatter<PullRequest>(options.PullRequestMessageTemplate); | ||
|
||
return new PullRequestMessageFormatter(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Issueneter.Telegram/Formatters/ConfigurableMessageFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Issueneter.Annotation; | ||
using Issueneter.Mappings; | ||
|
||
namespace Issueneter.Telegram.Formatters; | ||
|
||
public class ConfigurableMessageFormatter<T> : IMessageFormatter<T> where T : IFilterable | ||
{ | ||
private readonly string _template; | ||
|
||
public ConfigurableMessageFormatter(string template) | ||
{ | ||
_template = template; | ||
} | ||
|
||
public string ToMessage(T entity) | ||
{ | ||
var result = _template; | ||
|
||
foreach (string property in ModelsInfo.AvailableScanSources[T.ScanType]) | ||
result = result.Replace($"{{value.{property}}}", entity.GetProperty(property)); | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using NUnit.Framework; |
37 changes: 37 additions & 0 deletions
37
Issueneter.Tests/IssueConfigurableMessageFormatterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Collections; | ||
using Issueneter.Domain.Models; | ||
using Issueneter.Domain.Utility; | ||
using Issueneter.Telegram.Formatters; | ||
|
||
namespace Issueneter.Tests | ||
{ | ||
public class IssueConfigurableMessageFormatterTests | ||
{ | ||
private static IEnumerable GetTestCases() | ||
{ | ||
var dummyRefValue = new Ref<List<TimelineEvent>>(() => Task.FromResult(new List<TimelineEvent>())); | ||
|
||
var issue = new Issue( | ||
"Issue title", | ||
"Issue author", | ||
"Url", | ||
IssueState.Opened, | ||
Array.Empty<string>(), | ||
dummyRefValue); | ||
|
||
yield return new TestCaseData("Message template", issue, "Message template"); | ||
yield return new TestCaseData("Issue {value.Title}", issue, $"Issue {issue.Title}"); | ||
yield return new TestCaseData("Issue {value.Url}", issue, $"Issue {issue.Url}"); | ||
} | ||
|
||
[TestCaseSource(nameof(GetTestCases))] | ||
public void Formatter_ReturnCorrectString(string template, Issue issue, string expected) | ||
{ | ||
var formatter = new ConfigurableMessageFormatter<Issue>(template); | ||
|
||
var actual = formatter.ToMessage(issue); | ||
|
||
Assert.That(actual, Is.EqualTo(expected)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" /> | ||
<PackageReference Include="NUnit" Version="3.13.3" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" /> | ||
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" /> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Issueneter.Telegram\Issueneter.Telegram.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
36 changes: 36 additions & 0 deletions
36
Issueneter.Tests/PullRequestConfigurableMessageFormatterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Collections; | ||
using Issueneter.Domain.Models; | ||
using Issueneter.Domain.Utility; | ||
using Issueneter.Telegram.Formatters; | ||
|
||
namespace Issueneter.Tests; | ||
|
||
public class PullRequestConfigurableMessageFormatterTests | ||
{ | ||
private static IEnumerable GetTestCases() | ||
{ | ||
var dummyRefValue = new Ref<List<TimelineEvent>>(() => Task.FromResult(new List<TimelineEvent>())); | ||
|
||
var issue = new PullRequest( | ||
"PR title", | ||
"PR author", | ||
"Url", | ||
PullRequestState.Opened, | ||
Array.Empty<string>(), | ||
dummyRefValue); | ||
|
||
yield return new TestCaseData("Message template", issue, "Message template"); | ||
yield return new TestCaseData("Pull request {value.Title}", issue, $"Pull request {issue.Title}"); | ||
yield return new TestCaseData("Pull request {value.Url}", issue, $"Pull request {issue.Url}"); | ||
} | ||
|
||
[TestCaseSource(nameof(GetTestCases))] | ||
public void Formatter_ReturnCorrectString(string template, PullRequest pullRequest, string expected) | ||
{ | ||
var formatter = new ConfigurableMessageFormatter<PullRequest>(template); | ||
|
||
var actual = formatter.ToMessage(pullRequest); | ||
|
||
Assert.That(actual, Is.EqualTo(expected)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Issueneter | ||
|
||
## Configuration | ||
|
||
### Message templates | ||
|
||
Для изменения текста сообщений нужно выставить шаблоны в конфигурационном файле: | ||
|
||
``` | ||
{ | ||
"TelegramOptions": { | ||
"IssueMessageTemplate": "изи [ишуя]({value.Url})", | ||
"PullRequestMessageTemplate": "[пр]({entity.Url})" | ||
} | ||
} | ||
``` | ||
|
||
Шаблоны поддерживают динамически вычисляемые поля. Для того, чтобы в шаблон вставить информацию из issue / pull request нужно указать `{value.*}`, где `*` - это название поля из issue / pull request. Список поддерживаемых динамических полей: | ||
|
||
- Title | ||
- Url |