Skip to content

Commit

Permalink
Merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss committed Mar 30, 2024
2 parents b561315 + dbfa9dc commit f97efe7
Showing 1 changed file with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http.Headers;
using System.Reflection;
using System.Text.Json;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -59,8 +60,11 @@ public async Task<IActionResult> OnPullRequestChanged(
{
return BadRequest($"Only public repositories from '{RepositoryOwner}' are supported!");
}

var bearerToken = _configuration.GetValue<string>("GithubBearerToken");
using var client = _clientFactory.CreateClient("Proxied");
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Testably", Assembly.GetExecutingAssembly().GetName().Version.ToString()));

Check warning on line 65 in Source/Testably.Server/Controllers/PullRequestStatusCheckController.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 65 in Source/Testably.Server/Controllers/PullRequestStatusCheckController.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", bearerToken);

var owner = pullRequestModel.Payload.Repository.Owner.Login;
var repo = pullRequestModel.Payload.Repository.Name;
Expand All @@ -71,8 +75,9 @@ public async Task<IActionResult> OnPullRequestChanged(

if (!response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
return StatusCode(StatusCodes.Status500InternalServerError,
$"GitHub API '{requestUri}' not available");
$"GitHub API '{requestUri}' not available: {responseContent}");
}

var jsonDocument = await JsonDocument.ParseAsync(
Expand All @@ -83,12 +88,19 @@ await response.Content.ReadAsStreamAsync(cancellationToken),
titleProperty.GetString() == null)
{
return StatusCode(StatusCodes.Status500InternalServerError,
$"GitHub API '{requestUri}' returned an invalid response");
$"GitHub API '{requestUri}' returned an invalid response (missing title).");
}

if (!jsonDocument.RootElement.TryGetProperty("head", out var headProperty) ||
!headProperty.TryGetProperty("sha", out var shaProperty) ||
shaProperty.GetString() == null)
{
return StatusCode(StatusCodes.Status500InternalServerError,
$"GitHub API '{requestUri}' returned an invalid response (missing head.sha).");
}

var bearerToken = _configuration.GetValue<string>("GithubBearerToken");
var title = titleProperty.GetString()!;
var commitSha = pullRequestModel.Payload.PullRequest.MergeCommitSha;
var commitSha = shaProperty.GetString();
var statusUri = $"https://api.github.com/repos/{owner}/{repo}/statuses/{commitSha}";
var hasValidTitle = ValidateTitle(title);
// https://docs.github.com/en/rest/commits/statuses?apiVersion=2022-11-28#create-a-commit-status
Expand All @@ -99,8 +111,6 @@ await response.Content.ReadAsStreamAsync(cancellationToken),
description = "The PR title must conform to the conventional commits guideline."
});
using var content = new StringContent(json);
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", bearerToken);
await client.PostAsync(statusUri, content, cancellationToken);
return NoContent();
}
Expand Down

0 comments on commit f97efe7

Please sign in to comment.