Skip to content

Commit

Permalink
More clean up and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
IEvangelist committed Nov 26, 2023
1 parent 58f6c32 commit 84cd743
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 69 deletions.
9 changes: 3 additions & 6 deletions src/ProfanityFilter.Action/ActionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ private async Task HandleIssueCommentAsync(long issueCommentId, string? label)
// We don't apply labels to issue comments, discard it...
_ = label;

var clientId = Guid.NewGuid().ToString();
core.StartGroup(
$"Evaluating issue comment id #{issueCommentId} for profanity (Client mutation: {clientId})");
$"Evaluating issue comment id #{issueCommentId} for profanity");

try
{
Expand Down Expand Up @@ -157,9 +156,8 @@ private async Task HandleIssueCommentAsync(long issueCommentId, string? label)

private async Task HandleIssueAsync(long issueNumber, string? label)
{
var clientId = Guid.NewGuid().ToString();
core.StartGroup(
$"Evaluating issue #{issueNumber} for profanity (Client mutation: {clientId})");
$"Evaluating issue #{issueNumber} for profanity");

try
{
Expand Down Expand Up @@ -199,9 +197,8 @@ private async Task HandleIssueAsync(long issueNumber, string? label)

private async Task HandlePullRequestAsync(long pullRequestNumber, string? label)
{
var clientId = Guid.NewGuid().ToString();
core.StartGroup(
$"Evaluating pull request #{pullRequestNumber} for profanity (Client mutation: {clientId})");
$"Evaluating pull request #{pullRequestNumber} for profanity");

try
{
Expand Down
146 changes: 112 additions & 34 deletions src/ProfanityFilter.Action/Clients/GitHubRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,151 @@

namespace ProfanityFilter.Action.Clients;

internal sealed class GitHubRestClient(GitHubClient client, RepoConfig config)
internal sealed class GitHubRestClient(GitHubClient client, ICoreService core, RepoConfig config)
{
public async ValueTask<Issue> GetIssueAsync(int issueNumber)
{
var issue = await client.Issue.Get(
config.Owner, config.Repo, issueNumber);
try
{
core.Info(
$"Attempting to get issue #{issueNumber} in {config.Owner}/{config.Repo}.");

return issue;
var issue = await client.Issue.Get(
config.Owner, config.Repo, issueNumber);

return issue;
}
catch (Exception ex)
{
core.Error(ex.ToString());

throw;
}
}

public async ValueTask UpdateIssueAsync(int number, IssueUpdate input)
{
await client.Issue.Update(
config.Owner, config.Repo, number, input);
try
{
core.Info(
$"Attempting to update issue #{number} in {config.Owner}/{config.Repo}.");

await client.Issue.Update(
config.Owner, config.Repo, number, input);

await client.Reaction.Issue.Create(
config.Owner, config.Repo, number, new NewReaction(ReactionType.Confused));
}
catch (Exception ex)
{
core.Error(ex.ToString());

await client.Reaction.Issue.Create(
config.Owner, config.Repo, number, new NewReaction(ReactionType.Confused));
throw;
}
}

public async ValueTask<PullRequest> GetPullRequestAsync(int pullRequestNumber)
{
var pullRequest = await client.PullRequest.Get(
config.Owner, config.Repo, pullRequestNumber);
try
{
core.Info(
$"Attempting to get pull request #{pullRequestNumber} in {config.Owner}/{config.Repo}.");

var pullRequest = await client.PullRequest.Get(
config.Owner, config.Repo, pullRequestNumber);

return pullRequest;
}
catch (Exception ex)
{
core.Error(ex.ToString());

return pullRequest;
throw;
}
}

public async ValueTask UpdatePullRequestAsync(int number, PullRequestUpdate input, string? label)
{
await client.PullRequest.Update(
config.Owner, config.Repo, number, input);

if (label is not null)
try
{
// Add a existingLabel to the pull request
await client.Issue.Labels.AddToIssue(
config.Owner, config.Repo, number, [label]);
core.Info(
$"Attempting to update pull request #{number} in {config.Owner}/{config.Repo} with label: {label}.");

await client.PullRequest.Update(
config.Owner, config.Repo, number, input);

if (label is not null)
{
// Add a existingLabel to the pull request
await client.Issue.Labels.AddToIssue(
config.Owner, config.Repo, number, [label]);
}

await client.Reaction.Issue.Create(
config.Owner, config.Repo, number, new NewReaction(ReactionType.Confused));
}
catch (Exception ex)
{
core.Error(ex.ToString());

await client.Reaction.Issue.Create(
config.Owner, config.Repo, number, new NewReaction(ReactionType.Confused));
throw;
}
}

public async ValueTask<RestIssueComment> GetIssueCommentAsync(long issueCommentId) =>
await client.Issue.Comment.Get(config.Owner, config.Repo, (int)issueCommentId);

public async ValueTask UpdateIssueCommentAsync(long issueCommentId, string updatedComment)
public async ValueTask<RestIssueComment> GetIssueCommentAsync(long issueCommentId)
{
await client.Issue.Comment.Update(
config.Owner, config.Repo, (int)issueCommentId, updatedComment);
try
{
core.Info(
$"Attempting to get issue comment id {issueCommentId} in {config.Owner}/{config.Repo}.");

return await client.Issue.Comment.Get(
config.Owner, config.Repo, (int)issueCommentId);
}
catch (Exception ex)
{
core.Error(ex.ToString());

// Add a reaction to the issue comment
await client.Reaction.IssueComment.Create(
config.Owner, config.Repo, (int)issueCommentId, new NewReaction(ReactionType.Confused));
throw;
}
}

public async ValueTask<IReadOnlyList<Label>> GetIssueLabelsAsync(int issueNumber)
public async ValueTask UpdateIssueCommentAsync(long issueCommentId, string updatedComment)
{
var labels = await client.Issue.Labels.GetAllForIssue(
config.Owner, config.Repo, issueNumber);
try
{
core.Info(
$"Attempting to update issue comment id {issueCommentId} in {config.Owner}/{config.Repo}.");

await client.Issue.Comment.Update(
config.Owner, config.Repo, (int)issueCommentId, updatedComment);

// Add a reaction to the issue comment
await client.Reaction.IssueComment.Create(
config.Owner, config.Repo, (int)issueCommentId, new NewReaction(ReactionType.Confused));
}
catch (Exception ex)
{
core.Error(ex.ToString());

return labels;
throw;
}
}

public async ValueTask<string> GetOrCreateDefaultLabelAsync()
{
try
{
core.StartGroup(
$"Attempting to get all labels in {config.Owner}/{config.Repo}.");

var labels = await client.Issue.Labels.GetAllForRepository(
config.Owner, config.Repo);

foreach (var existingLabel in labels ?? [])
{
core.Info($"{existingLabel.Name} {existingLabel.Color}");

if (existingLabel is { Name: DefaultLabel.Name, Color: DefaultLabel.Color })
{
return existingLabel.Name;
Expand All @@ -90,9 +162,15 @@ public async ValueTask<string> GetOrCreateDefaultLabelAsync()

return label.Name;
}
catch
catch (Exception ex)
{
core.Error(ex.ToString());

return DefaultLabel.Name;
}
finally
{
core.EndGroup();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,38 @@ namespace ProfanityFilter.Action.Extensions;

internal static class ServiceCollectionExtensions
{
internal static IServiceCollection AddActionProcessorServices(
this IServiceCollection services) =>
services.AddSingleton<ActionProcessor>()
.AddProfanityFilter()
.AddOctokitServices();

private static IServiceCollection AddProfanityFilter(
this IServiceCollection services)
internal static IServiceCollection AddActionProcessorServices(this IServiceCollection services)
{
services.AddProfanityFilterServices();
services.AddSingleton<ActionProcessor>();

return services;
}
services.AddProfanityFilterServices();

private static IServiceCollection AddOctokitServices(
this IServiceCollection services)
{
services.AddGitHubActionsCore();

static RepoConfig GetRepositoryConfiguration(IServiceProvider provider)
services.AddGitHubClientServices();

services.AddSingleton(static provider =>
{
var core = provider.GetRequiredService<ICoreService>();

var context = Context.Current;
var repository = context.Repo;
try
{
core.StartGroup("Initializing context");

var token = core.GetInput("token");
var context = provider.GetRequiredService<Context>();

return (repository.Owner, repository.Repo, token);
}
var repository = context.Repo;

services.AddSingleton<GitHubRestClient>(static provider =>
{
var config = GetRepositoryConfiguration(provider);
var config = (repository.Owner, repository.Repo);

core.Info($"Repository: {config.Owner}/{config.Repo}");

return new(GitHub.Client, config);
return new GitHubRestClient(GitHub.Client, core, config);
}
finally
{
core.EndGroup();
}
});

return services;
Expand Down
4 changes: 2 additions & 2 deletions src/ProfanityFilter.Action/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
global using Actions.Core.Extensions;
global using Actions.Core.Services;

global using Actions.Octokit.Extensions;
global using Actions.Octokit;

global using Microsoft.Extensions.DependencyInjection;
Expand All @@ -17,15 +18,14 @@
global using ProfanityFilter.Action.Clients;
global using ProfanityFilter.Action.Extensions;
global using ProfanityFilter.Action.Models;

global using ProfanityFilter.Services;
global using ProfanityFilter.Services.Extensions;

global using Env = System.Environment;

global using RestIssueComment = Octokit.IssueComment;

global using RepoConfig = (string Owner, string Repo, string Token);
global using RepoConfig = (string Owner, string Repo);

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(
assemblyName: "ProfanityFilter.Action.Tests")]
5 changes: 2 additions & 3 deletions src/ProfanityFilter.Action/ProfanityFilter.Action.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="GitHub.Actions.Core" Version="8.0.1" />
<PackageReference Include="GitHub.Actions.Octokit" Version="8.0.1" />
<PackageReference Include="Octokit" Version="9.0.0" />
<PackageReference Include="GitHub.Actions.Core" Version="8.0.2" />
<PackageReference Include="GitHub.Actions.Octokit" Version="8.0.2" />
<PackageReference Include="Microsoft.NET.Build.Containers" Version="8.0.100" />
</ItemGroup>

Expand Down

0 comments on commit 84cd743

Please sign in to comment.