diff --git a/csharp/Platform.Bot/Program.cs b/csharp/Platform.Bot/Program.cs index 521a6b95..4d72cf4d 100644 --- a/csharp/Platform.Bot/Program.cs +++ b/csharp/Platform.Bot/Program.cs @@ -95,7 +95,7 @@ private static async Task Main(string[] args) var dbContext = new FileStorage(databaseFilePath?.FullName ?? new TemporaryFile().Filename); Console.WriteLine($"Bot has been started. {Environment.NewLine}Press CTRL+C to close"); var githubStorage = new GitHubStorage(githubUserName, githubApiToken, githubApplicationName); - 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)); + var issueTracker = new IssueTracker(githubStorage, new WelcomeMessageTrigger(githubStorage, dbContext), 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)); var pullRequenstTracker = new PullRequestTracker(githubStorage, new MergeDependabotBumpsTrigger(githubStorage)); var timestampTracker = new DateTimeTracker(githubStorage, new CreateAndSaveOrganizationRepositoriesMigrationTrigger(githubStorage, dbContext, Path.Combine(Directory.GetCurrentDirectory(), "/github-migrations"))); var cancellation = new CancellationTokenSource(); diff --git a/csharp/Platform.Bot/Triggers/WelcomeMessageTrigger.cs b/csharp/Platform.Bot/Triggers/WelcomeMessageTrigger.cs new file mode 100644 index 00000000..a8fd295c --- /dev/null +++ b/csharp/Platform.Bot/Triggers/WelcomeMessageTrigger.cs @@ -0,0 +1,88 @@ +using System.Threading.Tasks; +using Interfaces; +using Octokit; +using Storage.Local; +using Storage.Remote.GitHub; + +namespace Platform.Bot.Triggers +{ + using TContext = Issue; + /// + /// + /// Represents the welcome message trigger for new users. + /// + /// + /// + /// + internal class WelcomeMessageTrigger : ITrigger + { + private readonly GitHubStorage _storage; + private readonly FileStorage _fileStorage; + + /// + /// + /// Initializes a new instance. + /// + /// + /// + /// + /// A GitHub storage. + /// + /// + /// + /// A file storage. + /// + /// + public WelcomeMessageTrigger(GitHubStorage storage, FileStorage fileStorage) + { + this._storage = storage; + this._fileStorage = fileStorage; + } + + /// + /// + /// Actions the context. + /// + /// + /// + /// + /// The context. + /// + /// + public async Task Action(TContext context) + { + var welcomeMessage = @"👋 Welcome to the platform! + +Thank you for creating your first issue. Here are some things you can do to get started: + +🌐 **Set up your languages list**: Let us know which programming languages you're interested in by mentioning them in your issues or profile. + +🔗 **Set up your GitHub link**: Make sure your GitHub profile is properly linked and accessible. + +❓ **Need help?**: Use the `help` command or create an issue with ""help"" in the title to get assistance with available commands and features. + +We're glad to have you here! Feel free to explore and don't hesitate to ask questions."; + + await _storage.CreateIssueComment(context.Repository.Id, context.Number, welcomeMessage); + + // Mark user as welcomed + _fileStorage.MarkUserAsWelcomed(context.User.Login); + } + + /// + /// + /// Determines whether this instance condition. + /// + /// + /// + /// + /// The context. + /// + /// + /// + /// The bool + /// + /// + public async Task Condition(TContext context) => _fileStorage.IsNewUser(context.User.Login); + } +} \ No newline at end of file diff --git a/csharp/Storage/LocalStorage/FileStorage.cs b/csharp/Storage/LocalStorage/FileStorage.cs index aa68fd6f..98495070 100644 --- a/csharp/Storage/LocalStorage/FileStorage.cs +++ b/csharp/Storage/LocalStorage/FileStorage.cs @@ -329,6 +329,50 @@ public List GetFilesFromSet(string set) // public void SetLastGithubMigrationTimeStamp() + /// + /// + /// Checks if a user has been welcomed before. + /// + /// + /// + /// + /// The username to check. + /// + /// + /// + /// True if user is new (not welcomed), false if already welcomed + /// + /// + public bool IsNewUser(string username) + { + var userMarker = CreateString($"welcomed_user:{username}"); + try + { + var existingUser = _synchronizedLinks.SearchOrDefault(userMarker, _synchronizedLinks.Constants.Itself); + return existingUser == _synchronizedLinks.Constants.Null; + } + catch + { + return true; // Assume new user if there's any error + } + } + + /// + /// + /// Marks a user as welcomed. + /// + /// + /// + /// + /// The username to mark as welcomed. + /// + /// + public void MarkUserAsWelcomed(string username) + { + var userMarker = CreateString($"welcomed_user:{username}"); + _synchronizedLinks.GetOrCreate(userMarker, _synchronizedLinks.Constants.Itself); + } + protected override void Dispose(bool manual, bool wasDisposed) { _disposableLinks.Dispose();