Skip to content

Commit 5171ee4

Browse files
konardclaude
andcommitted
Implement automated team invitation system for GitHub and Discord
- Add TeamInvitationTrigger to handle @bot invite @username requests in GitHub issues - Add GitHubStorage.InviteToOrganization() method for GitHub org invitations - Add DiscordService for creating temporary Discord invite links - Add OwnerKeeperApprovalTriggerDecorator to ensure only owners/admins can approve invitations - Update Program.cs to support Discord bot token, guild ID, and channel ID configuration - Add Discord.Net NuGet package dependency The bot now automatically processes team invitation requests when: 1. Issue contains "@bot invite @username" pattern 2. Issue author has admin or maintainer permissions 3. Bot invites user to GitHub organization 4. Bot creates Discord invite link if Discord is configured 5. Issue is automatically closed after successful invitation 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent fb1be75 commit 5171ee4

File tree

6 files changed

+256
-5
lines changed

6 files changed

+256
-5
lines changed

csharp/Platform.Bot/Platform.Bot.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="CommandLineParser" Version="2.9.1" />
11+
<PackageReference Include="Discord.Net" Version="3.15.3" />
1112
<PackageReference Include="Octokit" Version="7.0.1" />
1213
<PackageReference Include="Platform.Communication.Protocol.Lino" Version="0.4.0" />
1314
<PackageReference Include="Platform.Data.Doublets.Sequences" Version="0.1.1" />

csharp/Platform.Bot/Program.cs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Platform.Bot.Trackers;
1616
using Platform.Bot.Triggers;
1717
using Platform.Bot.Triggers.Decorators;
18+
using Platform.Bot.Services;
1819

1920
namespace Platform.Bot
2021
{
@@ -73,29 +74,67 @@ private static async Task<int> Main(string[] args)
7374
description: "Minimum interaction interval in seconds.",
7475
getDefaultValue: () => 60);
7576

77+
var discordTokenOption = new Option<string?>(
78+
name: "--discord-token",
79+
description: "Discord bot token (optional).");
80+
81+
var discordGuildIdOption = new Option<ulong?>(
82+
name: "--discord-guild-id",
83+
description: "Discord guild/server ID (optional).");
84+
85+
var discordChannelIdOption = new Option<ulong?>(
86+
name: "--discord-channel-id",
87+
description: "Discord channel ID for invites (optional).");
88+
7689
var rootCommand = new RootCommand("Sample app for System.CommandLine")
7790
{
7891
githubUserNameOption,
7992
githubApiTokenOption,
8093
githubApplicationNameOption,
8194
databaseFilePathOption,
8295
fileSetNameOption,
83-
minimumInteractionIntervalOption
96+
minimumInteractionIntervalOption,
97+
discordTokenOption,
98+
discordGuildIdOption,
99+
discordChannelIdOption
84100
};
85101

86-
rootCommand.SetHandler(async (githubUserName, githubApiToken, githubApplicationName, databaseFilePath, fileSetName, minimumInteractionInterval) =>
102+
rootCommand.SetHandler(async (context) =>
87103
{
104+
var githubUserName = context.ParseResult.GetValueForOption(githubUserNameOption)!;
105+
var githubApiToken = context.ParseResult.GetValueForOption(githubApiTokenOption)!;
106+
var githubApplicationName = context.ParseResult.GetValueForOption(githubApplicationNameOption)!;
107+
var databaseFilePath = context.ParseResult.GetValueForOption(databaseFilePathOption);
108+
var fileSetName = context.ParseResult.GetValueForOption(fileSetNameOption);
109+
var minimumInteractionInterval = context.ParseResult.GetValueForOption(minimumInteractionIntervalOption);
110+
var discordToken = context.ParseResult.GetValueForOption(discordTokenOption);
111+
var discordGuildId = context.ParseResult.GetValueForOption(discordGuildIdOption);
112+
var discordChannelId = context.ParseResult.GetValueForOption(discordChannelIdOption);
113+
88114
Debug.WriteLine($"Nickname: {githubUserName}");
89115
Debug.WriteLine($"GitHub API Token: {githubApiToken}");
90116
Debug.WriteLine($"Application Name: {githubApplicationName}");
91117
Debug.WriteLine($"Database File Path: {databaseFilePath?.FullName}");
92118
Debug.WriteLine($"File Set Name: {fileSetName}");
93119
Debug.WriteLine($"Minimum Interaction Interval: {minimumInteractionInterval} seconds");
120+
Debug.WriteLine($"Discord Token: {(string.IsNullOrEmpty(discordToken) ? "Not provided" : "Provided")}");
121+
Debug.WriteLine($"Discord Guild ID: {discordGuildId}");
122+
Debug.WriteLine($"Discord Channel ID: {discordChannelId}");
94123

95124
var dbContext = new FileStorage(databaseFilePath?.FullName ?? new TemporaryFile().Filename);
96125
Console.WriteLine($"Bot has been started. {Environment.NewLine}Press CTRL+C to close");
97126
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));
127+
128+
var discordService = !string.IsNullOrEmpty(discordToken) && discordGuildId.HasValue && discordChannelId.HasValue
129+
? new Platform.Bot.Services.DiscordService(discordToken, discordGuildId.Value, discordChannelId.Value)
130+
: null;
131+
132+
if (discordService != null)
133+
{
134+
await discordService.ConnectAsync();
135+
}
136+
137+
var issueTracker = new IssueTracker(githubStorage, new HelloWorldTrigger(githubStorage, dbContext, fileSetName ?? "HelloWorldSet"), 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 OwnerKeeperApprovalTriggerDecorator(new TeamInvitationTrigger(githubStorage, discordService!), githubStorage));
99138
var pullRequenstTracker = new PullRequestTracker(githubStorage, new MergeDependabotBumpsTrigger(githubStorage));
100139
var timestampTracker = new DateTimeTracker(githubStorage, new CreateAndSaveOrganizationRepositoriesMigrationTrigger(githubStorage, dbContext, Path.Combine(Directory.GetCurrentDirectory(), "/github-migrations")));
101140
var cancellation = new CancellationTokenSource();
@@ -113,8 +152,7 @@ private static async Task<int> Main(string[] args)
113152
Console.WriteLine(ex.ToStringWithAllInnerExceptions());
114153
}
115154
}
116-
},
117-
githubUserNameOption, githubApiTokenOption, githubApplicationNameOption, databaseFilePathOption, fileSetNameOption, minimumInteractionIntervalOption);
155+
});
118156

119157
return await rootCommand.InvokeAsync(args);
120158
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using Discord;
2+
using Discord.WebSocket;
3+
using System;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace Platform.Bot.Services
8+
{
9+
/// <summary>
10+
/// Service for Discord operations including creating invite links
11+
/// </summary>
12+
public class DiscordService
13+
{
14+
private readonly DiscordSocketClient _client;
15+
private readonly string? _token;
16+
private readonly ulong? _guildId;
17+
private readonly ulong? _channelId;
18+
private bool _isConnected = false;
19+
20+
public DiscordService(string? token = null, ulong? guildId = null, ulong? channelId = null)
21+
{
22+
_token = token;
23+
_guildId = guildId;
24+
_channelId = channelId;
25+
_client = new DiscordSocketClient();
26+
}
27+
28+
public async Task<bool> ConnectAsync()
29+
{
30+
if (string.IsNullOrEmpty(_token))
31+
return false;
32+
33+
try
34+
{
35+
await _client.LoginAsync(TokenType.Bot, _token);
36+
await _client.StartAsync();
37+
_isConnected = true;
38+
return true;
39+
}
40+
catch (Exception)
41+
{
42+
return false;
43+
}
44+
}
45+
46+
public async Task<string?> CreateInviteLink()
47+
{
48+
if (!_isConnected || !_guildId.HasValue)
49+
return null;
50+
51+
try
52+
{
53+
var guild = _client.GetGuild(_guildId.Value);
54+
if (guild == null)
55+
return null;
56+
57+
var channel = guild.DefaultChannel ?? guild.TextChannels.FirstOrDefault();
58+
if (channel == null)
59+
return null;
60+
61+
var invite = await channel.CreateInviteAsync(maxAge: (int)TimeSpan.FromDays(1).TotalSeconds, maxUses: 1, isTemporary: false, isUnique: true);
62+
return invite.Url;
63+
}
64+
catch (Exception)
65+
{
66+
return null;
67+
}
68+
}
69+
70+
public async Task DisconnectAsync()
71+
{
72+
if (_isConnected)
73+
{
74+
await _client.StopAsync();
75+
await _client.LogoutAsync();
76+
_isConnected = false;
77+
}
78+
}
79+
80+
public void Dispose()
81+
{
82+
_client?.Dispose();
83+
}
84+
}
85+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Threading.Tasks;
2+
using Interfaces;
3+
using Octokit;
4+
using Storage.Remote.GitHub;
5+
6+
namespace Platform.Bot.Triggers.Decorators
7+
{
8+
/// <summary>
9+
/// Decorator that ensures only organization owners or repository admins can approve team invitations
10+
/// </summary>
11+
public class OwnerKeeperApprovalTriggerDecorator : ITrigger<Issue>
12+
{
13+
private readonly ITrigger<Issue> _trigger;
14+
private readonly GitHubStorage _githubStorage;
15+
16+
public OwnerKeeperApprovalTriggerDecorator(ITrigger<Issue> trigger, GitHubStorage githubStorage)
17+
{
18+
_trigger = trigger;
19+
_githubStorage = githubStorage;
20+
}
21+
22+
public async Task<bool> Condition(Issue issue)
23+
{
24+
if (!await _trigger.Condition(issue))
25+
return false;
26+
27+
try
28+
{
29+
var issueAuthorLogin = issue.User.Login;
30+
31+
var organizationMembership = await _githubStorage.Client.Organization.Member.GetOrganizationMembership(_githubStorage.Owner, issueAuthorLogin);
32+
if (organizationMembership.Role.Value == MembershipRole.Admin)
33+
{
34+
return true;
35+
}
36+
37+
var repositoryPermission = await _githubStorage.Client.Repository.Collaborator.ReviewPermission(issue.Repository.Id, issueAuthorLogin);
38+
return repositoryPermission.Permission == "admin" || repositoryPermission.Permission == "maintain";
39+
}
40+
catch
41+
{
42+
return false;
43+
}
44+
}
45+
46+
public async Task Action(Issue issue)
47+
{
48+
await _trigger.Action(issue);
49+
}
50+
}
51+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System.Text.RegularExpressions;
2+
using System.Threading.Tasks;
3+
using Interfaces;
4+
using Octokit;
5+
using Storage.Remote.GitHub;
6+
using Platform.Bot.Services;
7+
8+
namespace Platform.Bot.Triggers
9+
{
10+
/// <summary>
11+
/// Handles team invitation requests via GitHub issues
12+
/// Automatically invites approved users to both GitHub organization and Discord server
13+
/// </summary>
14+
public class TeamInvitationTrigger : ITrigger<Issue>
15+
{
16+
private readonly GitHubStorage _githubStorage;
17+
private readonly DiscordService _discordService;
18+
private readonly Regex _invitationPattern = new(@"@bot\s+invite\s+@?(\w+)", RegexOptions.IgnoreCase);
19+
20+
public TeamInvitationTrigger(GitHubStorage githubStorage, DiscordService discordService)
21+
{
22+
_githubStorage = githubStorage;
23+
_discordService = discordService;
24+
}
25+
26+
public async Task<bool> Condition(Issue issue)
27+
{
28+
if (issue.State.Value != ItemState.Open)
29+
return false;
30+
31+
var match = _invitationPattern.Match(issue.Body ?? "");
32+
if (!match.Success)
33+
return false;
34+
35+
var issueAuthorPermission = await _githubStorage.Client.Repository.Collaborator.ReviewPermission(issue.Repository.Id, issue.User.Login);
36+
return issueAuthorPermission.Permission == "admin" || issueAuthorPermission.Permission == "maintain";
37+
}
38+
39+
public async Task Action(Issue issue)
40+
{
41+
var match = _invitationPattern.Match(issue.Body ?? "");
42+
if (!match.Success)
43+
return;
44+
45+
var usernameToInvite = match.Groups[1].Value;
46+
47+
try
48+
{
49+
await _githubStorage.InviteToOrganization(_githubStorage.Owner, usernameToInvite);
50+
await _githubStorage.CreateIssueComment(issue.Repository.Id, issue.Number,
51+
$"✅ Successfully sent GitHub organization invitation to @{usernameToInvite}");
52+
53+
if (_discordService != null)
54+
{
55+
var inviteLink = await _discordService.CreateInviteLink();
56+
if (!string.IsNullOrEmpty(inviteLink))
57+
{
58+
await _githubStorage.CreateIssueComment(issue.Repository.Id, issue.Number,
59+
$"🎮 Discord invite link for @{usernameToInvite}: {inviteLink}");
60+
}
61+
}
62+
63+
_githubStorage.CloseIssue(issue);
64+
}
65+
catch (System.Exception ex)
66+
{
67+
await _githubStorage.CreateIssueComment(issue.Repository.Id, issue.Number,
68+
$"❌ Failed to invite @{usernameToInvite}: {ex.Message}");
69+
}
70+
}
71+
}
72+
}

csharp/Storage/RemoteStorage/GitHubStorage.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@ public async Task<List<User>> GetAllOrganizationMembers(string organizationName)
392392
return allMembers;
393393
}
394394

395+
public async Task InviteToOrganization(string organizationName, string username)
396+
{
397+
await Client.Organization.Member.AddOrUpdateOrganizationMembership(organizationName, username, new OrganizationMembershipUpdate());
398+
}
395399

396400
#endregion
397401

0 commit comments

Comments
 (0)