Skip to content

Commit

Permalink
add docker image and run url into replies
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticroentgen committed May 13, 2024
1 parent 82cc9bb commit b22f281
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Telegram.Bot.Exceptions;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using JsonDocument = System.Text.Json.JsonDocument;

namespace TelegramBuildBot;
Expand Down Expand Up @@ -120,10 +121,11 @@ static async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update,
{
string? repo = match.Groups[1].Value;
string branch = match.Groups[2].Value;
if (await TriggerGitHubWorkflow(_gitHubToken, repo, branch))
(bool triggerSuccess, string dockerImage, string runUrl) = await TriggerGitHubWorkflow(_gitHubToken, repo, branch);
if (triggerSuccess)
{
await SendResponse("Your build was triggered.");
Console.WriteLine($"Build triggered for {repo} - {branch}");
await SendResponse($"Your build was triggered. [View run on GitHub]({runUrl})\nDocker Image once run completed: `{dockerImage}`");
Console.WriteLine($"Build triggered for {repo}/{branch} [Run URL: {runUrl} | DockerImage: {dockerImage}]");
}
else
{
Expand All @@ -145,13 +147,14 @@ async Task SendResponse(string msg)
await botClient.SendTextMessageAsync(
chatId: chatId,
text: msg,
parseMode: ParseMode.MarkdownV2,
messageThreadId: isTopic ? message.MessageThreadId : null,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
}
}

private static async Task<bool> TriggerGitHubWorkflow(string gitHubToken, string? repo, string branch)
private static async Task<(bool IsSuccessStatusCode, string dockerImageUrl, string? runUrl)> TriggerGitHubWorkflow(string gitHubToken, string? repo, string branch)
{

// Register a runner with github
Expand All @@ -169,6 +172,8 @@ private static async Task<bool> TriggerGitHubWorkflow(string gitHubToken, string
}
};

bool isFork = false;

// Check if given repo is a direct map
if (!RepoToWorkflow.TryGetValue(repo.ToLower(), out string? workflowId))
{
Expand All @@ -183,31 +188,44 @@ private static async Task<bool> TriggerGitHubWorkflow(string gitHubToken, string
JsonDocument forkJson = System.Text.Json.JsonSerializer.Deserialize<JsonDocument>(forkContent);

// check if fork
bool isFork = forkJson.RootElement.GetProperty("fork").GetBoolean();
isFork = forkJson.RootElement.GetProperty("fork").GetBoolean();
if (!isFork)
{
// Not a fork - irgnore.
return false;
return (false, String.Empty, String.Empty);
}

string? parentRepo = forkJson.RootElement.GetProperty("parent").GetProperty("full_name").GetString();

// Check if we have a workflow for the parent
if (!RepoToWorkflow.TryGetValue(parentRepo.ToLower(), out workflowId))
{
return false;
return (false,String.Empty, String.Empty);
}

Console.WriteLine($"Found valid parent repo at {parentRepo}");
}
else
{
// Unable to grab repo meta - return error
return false;
return (false, String.Empty, String.Empty);
}

}

// Build tag url

// Grab the docker base from the workflow
string dockerBase = Regex.Match(workflowId, @"build-push-(.+)\.yml").Groups[1].Value;

string dockerImageUrl = $"192.168.45.152:80/dh/ethpandaops/{dockerBase}:{branch}";
if (isFork)
{
string forkUser = repo.Split('/')[0];
dockerImageUrl = $"192.168.45.152:80/dh/ethpandaops/{dockerBase}:{forkUser}-{branch}";
}

// Trigger job
var content = new StringContent(
JsonConvert.SerializeObject(requestData),
Encoding.UTF8,
Expand All @@ -216,7 +234,24 @@ private static async Task<bool> TriggerGitHubWorkflow(string gitHubToken, string
HttpResponseMessage response = await client.PostAsync(
$"https://api.github.com/repos/ethpandaops/eth-client-docker-image-builder/actions/workflows/{workflowId}/dispatches", content);

return response.IsSuccessStatusCode;
// Grab job url from GH

HttpResponseMessage runsResponse = await client.GetAsync(
$"https://api.github.com/repos/ethpandaops/eth-client-docker-image-builder/actions/runs");

string? runUrl = String.Empty;
if(response.IsSuccessStatusCode)
{
string runsContent = await runsResponse.Content.ReadAsStringAsync();
JsonDocument? responseObject = System.Text.Json.JsonSerializer.Deserialize<JsonDocument>(runsContent);
if (responseObject != null)
{
var firstRun = responseObject.RootElement.GetProperty("workflow_runs").EnumerateArray().FirstOrDefault();
runUrl = firstRun.GetProperty("html_url").GetString();
}
}

return (response.IsSuccessStatusCode, dockerImageUrl, runUrl);
}

static Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
Expand Down

0 comments on commit b22f281

Please sign in to comment.