Skip to content

Commit

Permalink
Refactor GitHubHelper to non-static class
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaslevesque committed Feb 3, 2022
1 parent 2df9561 commit 84427f9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 59 deletions.
71 changes: 27 additions & 44 deletions FakeItEasy.PrepareRelease/GitHubHelper.cs
Original file line number Diff line number Diff line change
@@ -1,91 +1,74 @@
namespace FakeItEasy.PrepareRelease;

using Octokit;
using static FakeItEasy.Tools.ReleaseHelpers;

internal static class GitHubHelper
internal class GitHubHelper
{
private const string RepoOwner = "FakeItEasy";
private static string? repoName;
private readonly IGitHubClient gitHubClient;
private readonly string repoOwner;
private readonly string repoName;

public static string RepoName
public GitHubHelper(IGitHubClient gitHubClient, string repoOwner, string repoName)
{
get
{
if (string.IsNullOrEmpty(repoName))
{
throw new InvalidOperationException($"{nameof(RepoName)} is not set");
}

return repoName;
}
set => repoName = value;
this.gitHubClient = gitHubClient;
this.repoOwner = repoOwner;
this.repoName = repoName;
}

public static bool IsPreRelease(string version)
{
return version.Contains('-', StringComparison.Ordinal);
}

public static async Task<Milestone> GetExistingMilestone(this IGitHubClient gitHubClient, string existingMilestoneTitle)
public async Task<Milestone> GetExistingMilestone(string existingMilestoneTitle)
{
Console.WriteLine($"Fetching milestone '{existingMilestoneTitle}'...");
var milestoneRequest = new MilestoneRequest { State = ItemStateFilter.Open };
var existingMilestone = (await gitHubClient.Issue.Milestone.GetAllForRepository(RepoOwner, RepoName, milestoneRequest))
var existingMilestone = (await this.gitHubClient.Issue.Milestone.GetAllForRepository(this.repoOwner, this.repoName, milestoneRequest))
.Single(milestone => milestone.Title == existingMilestoneTitle);
Console.WriteLine($"Fetched milestone '{existingMilestone.Title}'");
return existingMilestone;
}

public static async Task<IReadOnlyCollection<Release>> GetAllReleases(this IGitHubClient gitHubClient)
public async Task<IReadOnlyCollection<Release>> GetAllReleases()
{
Console.WriteLine("Fetching all GitHub releases...");
var allReleases = await gitHubClient.Repository.Release.GetAll(RepoOwner, RepoName);
var allReleases = await this.gitHubClient.Repository.Release.GetAll(this.repoOwner, this.repoName);
Console.WriteLine("Fetched all GitHub releases");
return allReleases;
}

public static async Task<IList<Issue>> GetIssuesInMilestone(this IGitHubClient gitHubClient, Milestone milestone)
public async Task<IList<Issue>> GetIssuesInMilestone(Milestone milestone)
{
Console.WriteLine($"Fetching issues in milestone '{milestone.Title}'...'");
var issueRequest = new RepositoryIssueRequest { Milestone = milestone.Number.ToString(), State = ItemStateFilter.All };
var issues = (await gitHubClient.Issue.GetAllForRepository(RepoOwner, RepoName, issueRequest)).ToList();
var issues = (await this.gitHubClient.Issue.GetAllForRepository(this.repoOwner, this.repoName, issueRequest)).ToList();
Console.WriteLine($"Fetched {issues.Count} issues in milestone '{milestone.Title}'");
return issues;
}

public static Issue GetExistingReleaseIssue(IList<Issue> issues, string existingReleaseName)
{
var issue = issues.Single(i => i.Title == $"Release {existingReleaseName}");
Console.WriteLine($"Found release issue #{issue.Number}: '{issue.Title}'");
return issue;
}

public static async Task RenameMilestone(this IGitHubClient gitHubClient, Milestone existingMilestone, string version)
public async Task RenameMilestone(Milestone existingMilestone, string version)
{
var milestoneUpdate = new MilestoneUpdate { Title = version };
Console.WriteLine($"Renaming milestone '{existingMilestone.Title}' to '{milestoneUpdate.Title}'...");
var updatedMilestone = await gitHubClient.Issue.Milestone.Update(RepoOwner, RepoName, existingMilestone.Number, milestoneUpdate);
var updatedMilestone = await this.gitHubClient.Issue.Milestone.Update(this.repoOwner, this.repoName, existingMilestone.Number, milestoneUpdate);
Console.WriteLine($"Renamed milestone '{existingMilestone.Title}' to '{updatedMilestone.Title}'");
}

public static async Task<Milestone> CreateNextMilestone(this IGitHubClient gitHubClient, string nextReleaseName)
public async Task<Milestone> CreateNextMilestone(string nextReleaseName)
{
var newMilestone = new NewMilestone(nextReleaseName);
Console.WriteLine($"Creating new milestone '{newMilestone.Title}'...");
var nextMilestone = await gitHubClient.Issue.Milestone.Create(RepoOwner, RepoName, newMilestone);
var nextMilestone = await this.gitHubClient.Issue.Milestone.Create(this.repoOwner, this.repoName, newMilestone);
Console.WriteLine($"Created new milestone '{nextMilestone.Title}'");
return nextMilestone;
}

public static async Task UpdateRelease(this IGitHubClient gitHubClient, Release existingRelease, string version)
public async Task UpdateRelease(Release existingRelease, string version)
{
var releaseUpdate = new ReleaseUpdate { Name = version, TagName = version, Prerelease = IsPreRelease(version) };
Console.WriteLine($"Renaming GitHub release '{existingRelease.Name}' to {releaseUpdate.Name}...");
var updatedRelease = await gitHubClient.Repository.Release.Edit(RepoOwner, RepoName, existingRelease.Id, releaseUpdate);
var updatedRelease = await this.gitHubClient.Repository.Release.Edit(this.repoOwner, this.repoName, existingRelease.Id, releaseUpdate);
Console.WriteLine($"Renamed GitHub release '{existingRelease.Name}' to {updatedRelease.Name}");
}

public static async Task CreateNextRelease(this IGitHubClient gitHubClient, string nextReleaseName)
public async Task CreateNextRelease(string nextReleaseName)
{
const string newReleaseBody = @"
### Changed
Expand All @@ -103,19 +86,19 @@ public static async Task CreateNextRelease(this IGitHubClient gitHubClient, stri

var newRelease = new NewRelease(nextReleaseName) { Draft = true, Name = nextReleaseName, Body = newReleaseBody.Trim() };
Console.WriteLine($"Creating new GitHub release '{newRelease.Name}'...");
var nextRelease = await gitHubClient.Repository.Release.Create(RepoOwner, RepoName, newRelease);
var nextRelease = await this.gitHubClient.Repository.Release.Create(this.repoOwner, this.repoName, newRelease);
Console.WriteLine($"Created new GitHub release '{nextRelease.Name}'");
}

public static async Task UpdateIssue(this IGitHubClient gitHubClient, Issue existingIssue, Milestone existingMilestone, string version)
public async Task UpdateIssue(Issue existingIssue, Milestone existingMilestone, string version)
{
var issueUpdate = new IssueUpdate { Title = $"Release {version}", Milestone = existingMilestone.Number };
Console.WriteLine($"Renaming release issue '{existingIssue.Title}' to '{issueUpdate.Title}'...");
var updatedIssue = await gitHubClient.Issue.Update(RepoOwner, RepoName, existingIssue.Number, issueUpdate);
var updatedIssue = await this.gitHubClient.Issue.Update(this.repoOwner, this.repoName, existingIssue.Number, issueUpdate);
Console.WriteLine($"Renamed release issue '{existingIssue.Title}' to '{updatedIssue.Title}'");
}

public static async Task CreateNextIssue(this IGitHubClient gitHubClient, Issue existingIssue, Milestone nextMilestone, string nextReleaseName)
public async Task CreateNextIssue(Issue existingIssue, Milestone nextMilestone, string nextReleaseName)
{
var newIssue = new NewIssue($"Release {nextReleaseName}")
{
Expand All @@ -128,7 +111,7 @@ public static async Task CreateNextIssue(this IGitHubClient gitHubClient, Issue
}

Console.WriteLine($"Creating new release issue '{newIssue.Title}'...");
var nextIssue = await gitHubClient.Issue.Create(RepoOwner, RepoName, newIssue);
var nextIssue = await this.gitHubClient.Issue.Create(this.repoOwner, this.repoName, newIssue);
Console.WriteLine($"Created new release issue #{nextIssue.Number}: '{newIssue.Title}'");
}
}
38 changes: 24 additions & 14 deletions FakeItEasy.PrepareRelease/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FakeItEasy.PrepareRelease;
using FakeItEasy.Tools;
using Octokit;
using static FakeItEasy.PrepareRelease.GitHubHelper;
using static FakeItEasy.Tools.ReleaseHelpers;

if (args.Length != 4 || (args[1] != "next" && args[1] != "fork"))
Expand All @@ -11,21 +11,24 @@
return;
}

RepoName = args[0];
const string repoOwner = "FakeItEasy";
var repoName = args[0];
var action = args[1];
var version = args[2];
var existingReleaseName = args[3];

var gitHubClient = GetAuthenticatedGitHubClient();
var existingMilestone = await gitHubClient.GetExistingMilestone(existingReleaseName);
var issuesInExistingMilestone = await gitHubClient.GetIssuesInMilestone(existingMilestone);
var gitHubHelper = new GitHubHelper(gitHubClient, repoOwner, repoName);

var existingMilestone = await gitHubHelper.GetExistingMilestone(existingReleaseName);
var issuesInExistingMilestone = await gitHubHelper.GetIssuesInMilestone(existingMilestone);
var existingReleaseIssue = GetExistingReleaseIssue(issuesInExistingMilestone, existingReleaseName);

if (action == "next")
{
var nextReleaseName = existingReleaseName;

var allReleases = await gitHubClient.GetAllReleases();
var allReleases = await gitHubHelper.GetAllReleases();
var existingRelease = allReleases.Single(release => release.Name == existingReleaseName && release.Draft);

var releasesForExistingMilestone = GetReleasesForExistingMilestone(allReleases, existingRelease, version);
Expand All @@ -46,22 +49,22 @@
}
else
{
await gitHubClient.RenameMilestone(existingMilestone, version);
nextMilestone = await gitHubClient.CreateNextMilestone(nextReleaseName);
await gitHubHelper.RenameMilestone(existingMilestone, version);
nextMilestone = await gitHubHelper.CreateNextMilestone(nextReleaseName);
}

await gitHubClient.UpdateRelease(existingRelease, version);
await gitHubClient.CreateNextRelease(nextReleaseName);
await gitHubClient.UpdateIssue(existingReleaseIssue, existingMilestone, version);
await gitHubClient.CreateNextIssue(existingReleaseIssue, nextMilestone, nextReleaseName);
await gitHubHelper.UpdateRelease(existingRelease, version);
await gitHubHelper.CreateNextRelease(nextReleaseName);
await gitHubHelper.UpdateIssue(existingReleaseIssue, existingMilestone, version);
await gitHubHelper.CreateNextIssue(existingReleaseIssue, nextMilestone, nextReleaseName);
}
else
{
var nextReleaseName = version;

var nextMilestone = await gitHubClient.CreateNextMilestone(nextReleaseName);
await gitHubClient.CreateNextRelease(nextReleaseName);
await gitHubClient.CreateNextIssue(existingReleaseIssue, nextMilestone, nextReleaseName);
var nextMilestone = await gitHubHelper.CreateNextMilestone(nextReleaseName);
await gitHubHelper.CreateNextRelease(nextReleaseName);
await gitHubHelper.CreateNextIssue(existingReleaseIssue, nextMilestone, nextReleaseName);
}

static List<Release> GetReleasesForExistingMilestone(IReadOnlyCollection<Release> allReleases, Release existingRelease, string version)
Expand All @@ -79,6 +82,13 @@ static GitHubClient GetAuthenticatedGitHubClient()
return new GitHubClient(new ProductHeaderValue("FakeItEasy-build-scripts")) { Credentials = credentials };
}

static Issue GetExistingReleaseIssue(IList<Issue> issues, string existingReleaseName)
{
var issue = issues.Single(i => i.Title == $"Release {existingReleaseName}");
Console.WriteLine($"Found release issue #{issue.Number}: '{issue.Title}'");
return issue;
}

static IList<Issue> ExcludeReleaseIssues(IList<Issue> issues, IEnumerable<Release> releases)
{
return issues.Where(issue => releases.All(release => $"Release {release.Name}" != issue.Title)).ToList();
Expand Down
7 changes: 6 additions & 1 deletion ReleaseHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace FakeItEasy.Tools;
using System.Text.RegularExpressions;
using Octokit;

public static class ReleaseHelpers
internal static class ReleaseHelpers
{
public static ICollection<int> GetIssueNumbersReferencedFromReleases(IEnumerable<Release> releases)
{
Expand All @@ -25,4 +25,9 @@ public static ICollection<int> GetIssueNumbersReferencedFromReleases(IEnumerable

return issuesReferencedFromRelease;
}

public static bool IsPreRelease(string version)
{
return version.Contains('-', StringComparison.Ordinal);
}
}

0 comments on commit 84427f9

Please sign in to comment.