mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-08-01 08:18:20 -04:00
Allows us to get rid of a ton of abstraction in SboxBuild, and gets rid of a lot of GitHub API plumbing. I feel like now that our workflows are more complex it's nicer to just use GitHub directly rather than having our own pipeline abstraction in SboxBuild. This way we get nicer logging and error reporting, which we can\t achieve through GitHub API.
76 lines
1.9 KiB
C#
76 lines
1.9 KiB
C#
using System.Text;
|
|
using System.Text.Json;
|
|
using static Facepunch.Constants;
|
|
|
|
namespace Facepunch.Steps;
|
|
|
|
internal class DiscordPostStep( string message, string author )
|
|
{
|
|
public string Message { get; } = message;
|
|
public string Author { get; } = author;
|
|
|
|
internal ExitCode Run()
|
|
{
|
|
try
|
|
{
|
|
// Run the async method synchronously
|
|
return PostToDiscordAsync().GetAwaiter().GetResult();
|
|
}
|
|
catch ( Exception ex )
|
|
{
|
|
Log.Error( $"Discord post failed with error: {ex}" );
|
|
return ExitCode.Failure;
|
|
}
|
|
}
|
|
|
|
private async Task<ExitCode> PostToDiscordAsync()
|
|
{
|
|
// Validate inputs
|
|
if ( string.IsNullOrWhiteSpace( Message ) )
|
|
{
|
|
Log.Warning( "Discord message was empty, skipping post" );
|
|
return ExitCode.Success; // Not treating this as a failure
|
|
}
|
|
|
|
var hook = Environment.GetEnvironmentVariable( "DISCORD_WEBHOOK" );
|
|
if ( string.IsNullOrEmpty( hook ) )
|
|
{
|
|
Log.Error( "DISCORD_WEBHOOK environment variable was empty" );
|
|
return ExitCode.Failure;
|
|
}
|
|
|
|
Log.Info( $"Posting to Discord webhook as {Author}" );
|
|
|
|
try
|
|
{
|
|
using ( var client = new HttpClient() )
|
|
{
|
|
var payload = new
|
|
{
|
|
content = Message.Substring( 0, Math.Min( Message.Length, 2000 ) ), // Discord message limit is 2000 characters
|
|
username = Author
|
|
};
|
|
|
|
// Serialize and post
|
|
using var content = new StringContent( JsonSerializer.Serialize( payload ), Encoding.UTF8, "application/json" );
|
|
var response = await client.PostAsync( hook, content );
|
|
|
|
if ( !response.IsSuccessStatusCode )
|
|
{
|
|
var responseString = await response.Content.ReadAsStringAsync();
|
|
Log.Error( $"Discord returned {response.StatusCode}: {responseString}" );
|
|
return ExitCode.Failure;
|
|
}
|
|
|
|
Log.Info( "Successfully posted to Discord" );
|
|
return ExitCode.Success;
|
|
}
|
|
}
|
|
catch ( Exception ex )
|
|
{
|
|
Log.Error( $"Discord post error: {ex}" );
|
|
return ExitCode.Failure;
|
|
}
|
|
}
|
|
}
|