Skip to content

Commit 75468d4

Browse files
konardclaude
andcommitted
Fix bot not deleting '+ ' messages after 6 hours
- Added GetIssueComments and DeleteIssueComment methods to GitHubStorage - Created DeletePlusMessagesTrigger to handle '+ ' comment deletion logic - Integrated new trigger into Program.cs issue tracker - Comments containing only '+ ' are deleted after 6 hours since last such comment - Added test documentation for the new feature Fixes #44 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 518fc33 commit 75468d4

File tree

4 files changed

+170
-1
lines changed

4 files changed

+170
-1
lines changed

csharp/Platform.Bot/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private static async Task<int> Main(string[] args)
9595
var dbContext = new FileStorage(databaseFilePath?.FullName ?? new TemporaryFile().Filename);
9696
Console.WriteLine($"Bot has been started. {Environment.NewLine}Press CTRL+C to close");
9797
var githubStorage = new GitHubStorage(githubUserName, githubApiToken, githubApplicationName);
98-
var issueTracker = new IssueTracker(githubStorage, new HelloWorldTrigger(githubStorage, dbContext, fileSetName), new OrganizationLastMonthActivityTrigger(githubStorage), new LastCommitActivityTrigger(githubStorage), new AdminAuthorIssueTriggerDecorator(new ProtectDefaultBranchTrigger(githubStorage), githubStorage), new AdminAuthorIssueTriggerDecorator(new ChangeOrganizationRepositoriesDefaultBranchTrigger(githubStorage, dbContext), githubStorage), new AdminAuthorIssueTriggerDecorator(new ChangeOrganizationPullRequestsBaseBranchTrigger(githubStorage, dbContext), githubStorage));
98+
var issueTracker = new IssueTracker(githubStorage, new HelloWorldTrigger(githubStorage, dbContext, fileSetName), new OrganizationLastMonthActivityTrigger(githubStorage), new LastCommitActivityTrigger(githubStorage), new AdminAuthorIssueTriggerDecorator(new ProtectDefaultBranchTrigger(githubStorage), githubStorage), new AdminAuthorIssueTriggerDecorator(new ChangeOrganizationRepositoriesDefaultBranchTrigger(githubStorage, dbContext), githubStorage), new AdminAuthorIssueTriggerDecorator(new ChangeOrganizationPullRequestsBaseBranchTrigger(githubStorage, dbContext), githubStorage), new DeletePlusMessagesTrigger(githubStorage));
9999
var pullRequenstTracker = new PullRequestTracker(githubStorage, new MergeDependabotBumpsTrigger(githubStorage));
100100
var timestampTracker = new DateTimeTracker(githubStorage, new CreateAndSaveOrganizationRepositoriesMigrationTrigger(githubStorage, dbContext, Path.Combine(Directory.GetCurrentDirectory(), "/github-migrations")));
101101
var cancellation = new CancellationTokenSource();
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Interfaces;
5+
using Octokit;
6+
using Storage.Remote.GitHub;
7+
8+
namespace Platform.Bot.Triggers
9+
{
10+
using TContext = Issue;
11+
12+
/// <summary>
13+
/// <para>
14+
/// Represents a trigger that deletes messages with "+ " as reply to other messages
15+
/// after 6 hours have passed since the last "+ " message.
16+
/// </para>
17+
/// <para></para>
18+
/// </summary>
19+
/// <seealso cref="ITrigger{TContext}"/>
20+
internal class DeletePlusMessagesTrigger : ITrigger<TContext>
21+
{
22+
private readonly GitHubStorage _storage;
23+
private readonly TimeSpan _deletionThreshold = TimeSpan.FromHours(6);
24+
25+
/// <summary>
26+
/// <para>
27+
/// Initializes a new <see cref="DeletePlusMessagesTrigger"/> instance.
28+
/// </para>
29+
/// <para></para>
30+
/// </summary>
31+
/// <param name="storage">
32+
/// <para>The GitHub storage instance.</para>
33+
/// <para></para>
34+
/// </param>
35+
public DeletePlusMessagesTrigger(GitHubStorage storage)
36+
{
37+
_storage = storage;
38+
}
39+
40+
/// <summary>
41+
/// <para>
42+
/// Determines whether this trigger should process the given issue.
43+
/// </para>
44+
/// <para></para>
45+
/// </summary>
46+
/// <param name="context">
47+
/// <para>The issue context.</para>
48+
/// <para></para>
49+
/// </param>
50+
/// <returns>
51+
/// <para>True if the issue should be processed, false otherwise.</para>
52+
/// <para></para>
53+
/// </returns>
54+
public async Task<bool> Condition(TContext context)
55+
{
56+
try
57+
{
58+
var comments = await _storage.GetIssueComments(context.Repository.Id, context.Number);
59+
var plusComments = comments.Where(c => c.Body.Trim() == "+ ").ToList();
60+
61+
if (!plusComments.Any())
62+
{
63+
return false;
64+
}
65+
66+
var lastPlusComment = plusComments.OrderByDescending(c => c.CreatedAt).First();
67+
var timeSinceLastPlus = DateTimeOffset.UtcNow - lastPlusComment.CreatedAt;
68+
69+
return timeSinceLastPlus >= _deletionThreshold;
70+
}
71+
catch (Exception ex)
72+
{
73+
Console.WriteLine($"Error checking condition for issue {context.Number}: {ex.Message}");
74+
return false;
75+
}
76+
}
77+
78+
/// <summary>
79+
/// <para>
80+
/// Deletes all "+ " messages from the issue if 6 hours have passed since the last one.
81+
/// </para>
82+
/// <para></para>
83+
/// </summary>
84+
/// <param name="context">
85+
/// <para>The issue context.</para>
86+
/// <para></para>
87+
/// </param>
88+
public async Task Action(TContext context)
89+
{
90+
try
91+
{
92+
var comments = await _storage.GetIssueComments(context.Repository.Id, context.Number);
93+
var plusComments = comments.Where(c => c.Body.Trim() == "+ ").ToList();
94+
95+
foreach (var comment in plusComments)
96+
{
97+
await _storage.DeleteIssueComment(context.Repository.Id, comment.Id);
98+
Console.WriteLine($"Deleted '+ ' comment {comment.Id} from issue {context.Number} in repository {context.Repository.FullName}");
99+
}
100+
101+
if (plusComments.Any())
102+
{
103+
Console.WriteLine($"Deleted {plusComments.Count} '+ ' comment(s) from issue {context.Number} in repository {context.Repository.FullName}");
104+
}
105+
}
106+
catch (Exception ex)
107+
{
108+
Console.WriteLine($"Error deleting '+ ' comments from issue {context.Number}: {ex.Message}");
109+
}
110+
}
111+
}
112+
}

csharp/Storage/RemoteStorage/GitHubStorage.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,16 @@ public Task<IssueComment> CreateIssueComment(long repositoryId, int issueNumber,
307307
return Client.Issue.Comment.Create(repositoryId, issueNumber, message);
308308
}
309309

310+
public Task<IReadOnlyList<IssueComment>> GetIssueComments(long repositoryId, int issueNumber)
311+
{
312+
return Client.Issue.Comment.GetAllForIssue(repositoryId, issueNumber);
313+
}
314+
315+
public Task DeleteIssueComment(long repositoryId, long commentId)
316+
{
317+
return Client.Issue.Comment.Delete(repositoryId, (int)commentId);
318+
}
319+
310320
#endregion
311321

312322
#region Branch
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Test Plan for Plus Message Deletion Feature
2+
3+
## Overview
4+
This document outlines how to test the new "+ " message deletion feature.
5+
6+
## Feature Description
7+
The bot now automatically deletes comments containing only "+ " (plus space) from GitHub issues after 6 hours have passed since the last such comment was posted.
8+
9+
## Implementation Details
10+
1. **GitHubStorage.cs**: Added methods to get and delete issue comments
11+
- `GetIssueComments(long repositoryId, int issueNumber)`
12+
- `DeleteIssueComment(long repositoryId, long commentId)`
13+
14+
2. **DeletePlusMessagesTrigger.cs**: New trigger that:
15+
- Checks for comments containing only "+ "
16+
- Verifies if 6 hours have passed since the last "+ " comment
17+
- Deletes all "+ " comments from the issue
18+
19+
3. **Program.cs**: Integrated the new trigger into the issue tracker
20+
21+
## Testing Steps
22+
23+
### Manual Testing (Recommended)
24+
1. Create a test issue in a repository the bot monitors
25+
2. Add several comments containing only "+ " with timestamps
26+
3. Wait for the bot's polling interval (typically 60 seconds)
27+
4. For comments older than 6 hours, verify they get deleted
28+
5. For comments newer than 6 hours, verify they remain
29+
30+
### Unit Testing Scenarios
31+
1. **No Plus Comments**: Issue with no "+ " comments - should not trigger
32+
2. **Recent Plus Comments**: Issue with "+ " comments less than 6 hours old - should not delete
33+
3. **Old Plus Comments**: Issue with "+ " comments older than 6 hours - should delete all
34+
4. **Mixed Comments**: Issue with both "+ " and regular comments - should only delete "+ " comments
35+
5. **Error Handling**: Test API failures and network issues
36+
37+
## Expected Behavior
38+
- Only comments containing exactly "+ " (plus followed by space) are deleted
39+
- Comments are only deleted if 6 hours have passed since the last "+ " comment
40+
- All "+ " comments in the issue are deleted at once, not just the old ones
41+
- Other comments remain untouched
42+
- Bot logs successful deletions to console
43+
44+
## Files Modified
45+
- `csharp/Storage/RemoteStorage/GitHubStorage.cs`: Added comment management methods
46+
- `csharp/Platform.Bot/Triggers/DeletePlusMessagesTrigger.cs`: New trigger implementation
47+
- `csharp/Platform.Bot/Program.cs`: Integrated new trigger

0 commit comments

Comments
 (0)