Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update structure to use DI, service and repositories #19

Merged
merged 1 commit into from
Jul 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions GbUtil.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionIt
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitBucket.Data", "src\GitBucket.Data\GitBucket.Data.csproj", "{FD4C2D34-F249-4E34-81C2-0D7F23D6D338}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitBucket.Service", "src\GitBucket.Service\GitBucket.Service.csproj", "{13A179C3-0F83-4083-A77A-8CC622522C0C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -55,13 +59,39 @@ Global
{E57AD6A1-4008-43F6-AE2A-7C23C790E184}.Release|x64.Build.0 = Release|Any CPU
{E57AD6A1-4008-43F6-AE2A-7C23C790E184}.Release|x86.ActiveCfg = Release|Any CPU
{E57AD6A1-4008-43F6-AE2A-7C23C790E184}.Release|x86.Build.0 = Release|Any CPU
{FD4C2D34-F249-4E34-81C2-0D7F23D6D338}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FD4C2D34-F249-4E34-81C2-0D7F23D6D338}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD4C2D34-F249-4E34-81C2-0D7F23D6D338}.Debug|x64.ActiveCfg = Debug|Any CPU
{FD4C2D34-F249-4E34-81C2-0D7F23D6D338}.Debug|x64.Build.0 = Debug|Any CPU
{FD4C2D34-F249-4E34-81C2-0D7F23D6D338}.Debug|x86.ActiveCfg = Debug|Any CPU
{FD4C2D34-F249-4E34-81C2-0D7F23D6D338}.Debug|x86.Build.0 = Debug|Any CPU
{FD4C2D34-F249-4E34-81C2-0D7F23D6D338}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD4C2D34-F249-4E34-81C2-0D7F23D6D338}.Release|Any CPU.Build.0 = Release|Any CPU
{FD4C2D34-F249-4E34-81C2-0D7F23D6D338}.Release|x64.ActiveCfg = Release|Any CPU
{FD4C2D34-F249-4E34-81C2-0D7F23D6D338}.Release|x64.Build.0 = Release|Any CPU
{FD4C2D34-F249-4E34-81C2-0D7F23D6D338}.Release|x86.ActiveCfg = Release|Any CPU
{FD4C2D34-F249-4E34-81C2-0D7F23D6D338}.Release|x86.Build.0 = Release|Any CPU
{13A179C3-0F83-4083-A77A-8CC622522C0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{13A179C3-0F83-4083-A77A-8CC622522C0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{13A179C3-0F83-4083-A77A-8CC622522C0C}.Debug|x64.ActiveCfg = Debug|Any CPU
{13A179C3-0F83-4083-A77A-8CC622522C0C}.Debug|x64.Build.0 = Debug|Any CPU
{13A179C3-0F83-4083-A77A-8CC622522C0C}.Debug|x86.ActiveCfg = Debug|Any CPU
{13A179C3-0F83-4083-A77A-8CC622522C0C}.Debug|x86.Build.0 = Debug|Any CPU
{13A179C3-0F83-4083-A77A-8CC622522C0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{13A179C3-0F83-4083-A77A-8CC622522C0C}.Release|Any CPU.Build.0 = Release|Any CPU
{13A179C3-0F83-4083-A77A-8CC622522C0C}.Release|x64.ActiveCfg = Release|Any CPU
{13A179C3-0F83-4083-A77A-8CC622522C0C}.Release|x64.Build.0 = Release|Any CPU
{13A179C3-0F83-4083-A77A-8CC622522C0C}.Release|x86.ActiveCfg = Release|Any CPU
{13A179C3-0F83-4083-A77A-8CC622522C0C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D69CF50A-D832-4D24-8239-E38638843A98} = {803B2494-938D-4027-BDEE-E9126627BA74}
{E57AD6A1-4008-43F6-AE2A-7C23C790E184} = {803B2494-938D-4027-BDEE-E9126627BA74}
{FD4C2D34-F249-4E34-81C2-0D7F23D6D338} = {803B2494-938D-4027-BDEE-E9126627BA74}
{13A179C3-0F83-4083-A77A-8CC622522C0C} = {803B2494-938D-4027-BDEE-E9126627BA74}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {20260553-8DF6-48CB-8D62-0EFEEBC7C3AE}
Expand Down
13 changes: 0 additions & 13 deletions src/GbUtil/Extensions/IQueryableExtensions.cs

This file was deleted.

86 changes: 43 additions & 43 deletions src/GbUtil/GbUtil.csproj
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<Description>.NET Core Global tool for GitBucket utilities.</Description>
<LangVersion>7.3</LangVersion>
<VersionPrefix>0.2.0</VersionPrefix>
<ToolCommandName>gbutil</ToolCommandName>
<Authors>SIkebe</Authors>
<Product>gitbucket-utility</Product>
<Copyright>MIT</Copyright>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseUrl>https://github.com/SIkebe/gitbucket-utility/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/SIkebe/gitbucket-utility/blob/master/README.md</PackageProjectUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<DebugType>embedded</DebugType>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IsPackable>true</IsPackable>
<PackAsTool>true</PackAsTool>
<PackageOutputPath>../../packages</PackageOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.1.1" />
<PackageReference Include="octokit" Version="0.30.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GitBucket.Core\GitBucket.Core.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<Description>.NET Core Global tool for GitBucket utilities.</Description>
<LangVersion>7.3</LangVersion>
<VersionPrefix>0.2.0</VersionPrefix>
<ToolCommandName>gbutil</ToolCommandName>
<Authors>SIkebe</Authors>
<Product>gitbucket-utility</Product>
<Copyright>MIT</Copyright>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseUrl>https://github.com/SIkebe/gitbucket-utility/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/SIkebe/gitbucket-utility/blob/master/README.md</PackageProjectUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<DebugType>embedded</DebugType>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IsPackable>true</IsPackable>
<PackAsTool>true</PackAsTool>
<PackageOutputPath>../../packages</PackageOutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.2.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GitBucket.Core\GitBucket.Core.csproj" />
<ProjectReference Include="..\GitBucket.Data\GitBucket.Data.csproj" />
<ProjectReference Include="..\GitBucket.Service\GitBucket.Service.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
145 changes: 24 additions & 121 deletions src/GbUtil/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,155 +2,58 @@
using System.IO;
using System.Linq;
using CommandLine;
using GbUtil.Extensions;
using GitBucket.Core;
using GitBucket.Data.Repositories;
using GitBucket.Service;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace GbUtil
{
class Program
{
public static IConfiguration Configuration { get; set; }
private static IConfiguration Configuration { get; set; }

static void Main(string[] args)
{
try
{
var builder = new ConfigurationBuilder()
Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true)
.AddEnvironmentVariables();

Configuration = builder.Build();
.AddEnvironmentVariables()
.Build();

var connectionString = Configuration.GetConnectionString("GitBucketConnection");
if (string.IsNullOrEmpty(connectionString))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("PostgreSQL ConnectionString is not configured. Add \"ConnectionStrings: GitBucketConnection\" environment variable.");
Console.ResetColor();
return;
}

Parser.Default.ParseArguments<ReleaseOptions>(args)
.WithParsed(options => OutputReleaseNotes(options, connectionString));
var serviceProvider = new ServiceCollection()
.AddScoped<DbContext>(_ => new GitBucketDbContext(connectionString))
.AddTransient<IReleaseNoteService, ReleaseNoteService>()
.AddTransient<IssueRepositoryBase, IssueRepository>()
.AddTransient<LabelRepositoryBase, LabelRepository>()
.BuildServiceProvider();

using (var scope = serviceProvider.CreateScope())
{
var releaseNoteService = scope.ServiceProvider.GetRequiredService<IReleaseNoteService>();
Parser.Default.ParseArguments<ReleaseOptions>(args)
.WithParsed(options => releaseNoteService.OutputReleaseNotes(options));
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.ToString());
Console.ResetColor();
}
}

private static int OutputReleaseNotes(ReleaseOptions option, string connectionString)
{
using (var context = new GitBucketDbContext(connectionString))
{
var issues = context.Issue
.Where(i => i.UserName == option.Owner)
.Where(i => i.RepositoryName == option.Repository)
.Where(i => i.Milestone.Title == option.MileStone)
.WhereIf(string.Equals(nameof(Target.Issues), option.Target, StringComparison.OrdinalIgnoreCase),
i => !i.PullRequest)
.WhereIf(!string.Equals(nameof(Target.Issues), option.Target, StringComparison.OrdinalIgnoreCase),
i => i.PullRequest)
.Include(i => i.Milestone)
.Include(i => i.Priority)
.ToList();

var closedTargets = option.Target.ToLowerInvariant();
if (issues.Count == 0)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"There are no {closedTargets} related to \"{option.MileStone}\".");
Console.ResetColor();
return 1;
}

if (issues.Any(i => !i.Closed))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"There are unclosed {closedTargets} in \"{option.MileStone}\".");
Console.Write("Do you want to continue?([Y]es/[N]o): ");
Console.ResetColor();

string yesOrNo = Console.ReadLine();

if (!string.Equals(yesOrNo, "y", StringComparison.OrdinalIgnoreCase)
&& !string.Equals(yesOrNo, "yes", StringComparison.OrdinalIgnoreCase))
{
return 1;
}

Console.WriteLine("");
}

var issueLabels = context.IssueLabel
.Where(l => l.UserName == option.Owner)
.Where(l => l.RepositoryName == option.Repository)
.Where(l => issues.Select(i => i.IssueId).Contains(l.IssueId));

if (issues.Any(i => !issueLabels.Select(l => l.IssueId).Contains(i.IssueId)))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"There are issues which have no labels in \"{option.MileStone}\".");
Console.ResetColor();
return 1;
}

var labels = context.Label
.Where(l => l.UserName == option.Owner)
.Where(l => l.RepositoryName == option.Repository)
.Where(l => issueLabels.Select(i => i.LabelId).Contains(l.LabelId));

var highestPriority = issues
.OrderBy(i => i.Priority.Ordering)
.First()
.Priority.PriorityName;

Console.WriteLine($"As part of this release we had {issues.Count} {closedTargets} closed.");
Console.WriteLine($"The highest priority among them is \"{highestPriority}\".");
Console.WriteLine("");
foreach (var label in labels)
{
Console.WriteLine($"### {label.LabelName.ConvertFirstCharToUpper()}");

var ids = issueLabels
.Where(l => l.LabelId == label.LabelId)
.Select(i => i.IssueId)
.OrderBy(i => i);

foreach (var issueId in ids)
{
var issue = issues.Where(i => i.IssueId == issueId).Single();
Console.WriteLine($"* {issue.Title} #{issue.IssueId}");
}

Console.WriteLine("");
}

return 0;
}
}
}

[Verb("release", HelpText = "Output a release note")]
public class ReleaseOptions
{
[Option('o', "owner", Required = true, HelpText = "The owner name of the repository.")]
public string Owner { get; set; }

[Option('r', "repository", Required = true, HelpText = "The repository name.")]
public string Repository { get; set; }

[Option('m', "milestone", Required = true, HelpText = "The milestone to publish a release note.")]
public string MileStone { get; set; }

[Option('t', "target", Required = false, HelpText = "The options to publish a release note based on issues or pull requests.")]
public string Target { get; set; } = nameof(GbUtil.Target.Issues);
}

public enum Target
{
Issues = 0,
PullRequests
}
}
1 change: 1 addition & 0 deletions src/GitBucket.Core/GitBucket.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.1.1" />
</ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions src/GitBucket.Core/ReleaseNoteTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace GitBucket.Core
{
public enum ReleaseNoteTarget
{
Issues = 0,
PullRequests
}
}
20 changes: 20 additions & 0 deletions src/GitBucket.Core/ReleaseOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using CommandLine;

namespace GitBucket.Core
{
[Verb("release", HelpText = "Output a release note")]
public class ReleaseOptions
{
[Option('o', "owner", Required = true, HelpText = "The owner name of the repository.")]
public string Owner { get; set; }

[Option('r', "repository", Required = true, HelpText = "The repository name.")]
public string Repository { get; set; }

[Option('m', "milestone", Required = true, HelpText = "The milestone to publish a release note.")]
public string MileStone { get; set; }

[Option('t', "target", Required = false, HelpText = "The options to publish a release note based on issues or pull requests.")]
public string Target { get; set; } = nameof(GitBucket.Core.ReleaseNoteTarget.Issues);
}
}
10 changes: 10 additions & 0 deletions src/GitBucket.Data/Extensions/IQueryableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Linq.Expressions;

namespace System.Linq
{
public static class IQueryableExtensions
{
public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, bool condition, Expression<Func<TSource, bool>> predicate)
=> condition ? source.Where(predicate) : source;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Linq;

namespace GbUtil.Extensions
namespace System
{
public static class StringExtensions
{
Expand Down
Loading