Skip to content

Commit

Permalink
Added more input options, and add more details to audit summary.
Browse files Browse the repository at this point in the history
  • Loading branch information
IEvangelist committed Feb 2, 2024
1 parent dd23d29 commit cf9dcc5
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 13 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Potty Mouth: GitHub Action

> 🤬 Profane content filter
> This repository contains the source code for a profane content filter 🤬.
[![.NET](https://github.com/IEvangelist/profanity-filter/actions/workflows/dotnet.yml/badge.svg)](https://github.com/IEvangelist/profanity-filter/actions/workflows/dotnet.yml) [![Dogfood](https://github.com/IEvangelist/profanity-filter/actions/workflows/dogfood.yml/badge.svg)](https://github.com/IEvangelist/profanity-filter/actions/workflows/dogfood.yml)

Expand Down Expand Up @@ -54,7 +54,7 @@ jobs:
id: profanity-filter
with:
token: ${{ secrets.GITHUB_TOKEN }}
# See https://github.com/IEvangelist/profanity-filter?tab=readme-ov-file#-replacement-strategies
# See https://bit.ly/potty-mouth-replacement-strategies
replacement-strategy: Emoji # See Replacement strategy
```
Expand All @@ -69,7 +69,7 @@ If you already have an existing workflow that is triggered `on/issues|pull_reque
id: profanity-filter
with:
token: ${{ secrets.GITHUB_TOKEN }}
# See https://github.com/IEvangelist/profanity-filter?tab=readme-ov-file#-replacement-strategies
# See https://bit.ly/potty-mouth-replacement-strategies
replacement-strategy: FirstLetterThenAsterisk
```

Expand All @@ -87,6 +87,7 @@ The following table describes each input:
| `token` | The GitHub token used to update the issues or pull requests with.<br><br>Example, `${{ secrets.GITHUB_TOKEN }}`. | `true` |
| `replacement-strategy` | The type of replacement method to use when profane content is filtered. | `false` (default: `asterisk`) |
| `include-update-note` | A `boolean` value to indicate if the action should include a note in the issue or pull request body when profane content is replaced. | `false` (default: `true`) |
| `include-confused-reaction` | A `boolean` value to indicate if the action should react to the issue or pull request with the confused 😕 reaction. | `false` (default: `true`) |

### 😵 Replacement strategies

Expand Down Expand Up @@ -125,7 +126,7 @@ Consider the following automatically applied label to an issue that contains pro
When profane content is detected, the action will update the issue or pull request by:

- Replacing any found profane content with the configured replacement strategy.
- ~~Reacting to the issue or pull request with the [confused 😕 reaction](https://docs.github.com/rest/reactions/reactions).~~
- Reacting to the issue or pull request with the [confused 😕 reaction](https://docs.github.com/rest/reactions/reactions).
- Conditionally applying the `profane content 🤬` label if found in the repository.
- Reporting the profane content in the workflow summary as a detailed table.

Expand All @@ -137,7 +138,8 @@ flowchart TD
--> B[Contains Profane Content?]
B -->|YES| C(Apply Filter)
--> E(All swear words are filtered, for example sw**r)
--o F[Job Summary]
--> F(React to and label profane content)
--o G[Job Summary]
B --o|NO| D{{Stop}} ~~~A
```

Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ inputs:
'Whether to include a note in the issue or pull request body indicating that the content was filtered.'
required: false
default: 'true'
include-confused-reaction:
description:
'Whether to include a confused reaction on the issue or pull request when profane content is filtered.'
required: false
default: 'true'

runs:
using: 'docker'
Expand Down
1 change: 1 addition & 0 deletions src/ProfanityFilter.Action/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.

global using System.Diagnostics;
global using System.Diagnostics.CodeAnalysis;

global using Actions.Core.Extensions;
Expand Down
5 changes: 4 additions & 1 deletion src/ProfanityFilter.Action/ProfanityProcessor.Issue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ private async Task<FiltrationResult> HandleIssueAsync(

await client.UpdateIssueAsync((int)issueNumber, issueUpdate);

await client.AddReactionAsync(issueNumber, ReactionContent.Confused);
if (core.GetBoolInput("include-confused-reaction"))
{
await client.AddReactionAsync(issueNumber, ReactionContent.Confused);
}
}
}
finally
Expand Down
7 changes: 5 additions & 2 deletions src/ProfanityFilter.Action/ProfanityProcessor.IssueComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ private async Task<FiltrationResult> HandleIssueCommentAsync(

await client.UpdateIssueCommentAsync(issueCommentId, finalBodyUpdate);

var issueNumber = (int)_context!.Payload!.Issue!.Number;
await client.AddReactionAsync(issueNumber, ReactionContent.Confused);
if (core.GetBoolInput("include-confused-reaction"))
{
var issueNumber = (int)_context!.Payload!.Issue!.Number;
await client.AddReactionAsync(issueNumber, ReactionContent.Confused);
}
}
}
finally
Expand Down
7 changes: 5 additions & 2 deletions src/ProfanityFilter.Action/ProfanityProcessor.PullRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ private async Task<FiltrationResult> HandlePullRequestAsync(
await client.UpdatePullRequestAsync(
(int)pullRequestNumber, pullRequestUpdate, label?.Name);

await client.AddReactionAsync(
pullRequestNumber, ReactionContent.Confused);
if (core.GetBoolInput("include-confused-reaction"))
{
await client.AddReactionAsync(
pullRequestNumber, ReactionContent.Confused);
}
}
}
finally
Expand Down
9 changes: 7 additions & 2 deletions src/ProfanityFilter.Action/ProfanityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public async Task ProcessProfanityAsync()
var success = true;

Summary summary = new();
var startingTimestamp = Stopwatch.GetTimestamp();

try
{
Expand Down Expand Up @@ -58,7 +59,9 @@ public async Task ProcessProfanityAsync()

ContextSummaryPair contextSummaryPair = (context, summary);

await SummarizeAppliedFiltersAsync(result, contextSummaryPair);
var elapsedTime = Stopwatch.GetElapsedTime(startingTimestamp);

await SummarizeAppliedFiltersAsync(result, contextSummaryPair, elapsedTime);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -180,7 +183,7 @@ private async ValueTask<FilterResult> TryApplyFilterAsync(
}

private static async Task SummarizeAppliedFiltersAsync(
FiltrationResult result, ContextSummaryPair contextSummaryPair)
FiltrationResult result, ContextSummaryPair contextSummaryPair, TimeSpan elapsedTime)
{
if (result.IsFiltered == false)
{
Expand Down Expand Up @@ -210,6 +213,8 @@ private static async Task SummarizeAppliedFiltersAsync(
```{Env.NewLine}{Env.NewLine}
""");

summary.AddRawMarkdown($"> The _potty mouth_ profanity filter ran in {elapsedTime:g}.");

if (!summary.IsBufferEmpty)
{
await summary.WriteAsync(new() { Overwrite = true });
Expand Down
2 changes: 1 addition & 1 deletion src/ProfanityFilter.Services/Internals/MatchRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static class MatchRegistry
private static readonly ConcurrentDictionary<FilterParameters, List<string>> s_registry = new();

/// <summary>
/// Registers a given <paramref name="match"/> for the given <paramref name="parameters"/>.
/// Registers a given <paramref name="match"/> with the given <paramref name="parameters"/>.
/// </summary>
internal static void Register(FilterParameters parameters, Match match)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ public async Task FilterProfanityAsync_Returns_Expected_Result(string? input, st
}
}

[Fact]
public async Task FilterProfanityAsyncMultipleParameters_Returns_Valid_Results()
{
var input = "This is fucking bullshit!";

// Act
var titleResult = await _sut.FilterProfanityAsync(input,
new(ReplacementStrategy.Bleep, FilterTarget.Title));

var bodyResult = await _sut.FilterProfanityAsync(input,
new(ReplacementStrategy.Emoji, FilterTarget.Body));

// Assert
Assert.Equal("This is bleep bleep!", titleResult.FinalOutput);
Assert.True(titleResult.IsFiltered);
Assert.Equal(2, titleResult.Matches.Count);

Assert.NotEqual(input, bodyResult.FinalOutput);
Assert.True(bodyResult.IsFiltered);
Assert.Equal(2, bodyResult.Matches.Count);
}

[Fact]
public async Task FilterProfanityAsyncWithEmoji_Returns_Valid_Result()
{
Expand Down

0 comments on commit cf9dcc5

Please sign in to comment.