Skip to content

Commit

Permalink
Add user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss committed Mar 30, 2024
1 parent 1a83d11 commit 009995f
Showing 1 changed file with 7 additions and 5 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();
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 @@ -86,7 +91,6 @@ await response.Content.ReadAsStreamAsync(cancellationToken),
$"GitHub API '{requestUri}' returned an invalid response");
}

var bearerToken = _configuration.GetValue<string>("GithubBearerToken");
var title = titleProperty.GetString()!;
var commitSha = pullRequestModel.Payload.PullRequest.MergeCommitSha;
var statusUri = $"https://api.github.com/repos/{owner}/{repo}/statuses/{commitSha}";
Expand All @@ -99,8 +103,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 009995f

Please sign in to comment.