-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement autoapprover + migrate to refit (#40)
* Migrate to refit - work in progress. * Approver * Api fixes
- Loading branch information
Showing
13 changed files
with
173 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/DependencyUpdated.Repositories.AzureDevOps/AzureApiHeaderHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using DependencyUpdated.Core.Config; | ||
using Microsoft.Extensions.Options; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
|
||
namespace DependencyUpdated.Repositories.AzureDevOps; | ||
|
||
internal sealed class AzureApiHeaderHandler(IOptions<UpdaterConfig> config) : DelegatingHandler | ||
{ | ||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
var approverPat = config.Value.AzureDevOps.ApproverPAT; | ||
if (request.RequestUri?.AbsolutePath.Contains(IAzureDevOpsClient.ReviewersResource) == true && | ||
!string.IsNullOrEmpty(approverPat)) | ||
{ | ||
request.Headers.Authorization ??= CreateToken(approverPat); | ||
} | ||
|
||
request.Headers.Authorization ??= CreateToken(config.Value.AzureDevOps.PAT!); | ||
var response = await base.SendAsync(request, cancellationToken); | ||
response.EnsureSuccessStatusCode(); | ||
return response; | ||
} | ||
|
||
private static AuthenticationHeaderValue CreateToken(string token) | ||
{ | ||
return new AuthenticationHeaderValue("Basic", | ||
Convert.ToBase64String(Encoding.UTF8.GetBytes($":{token}"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/DependencyUpdated.Repositories.AzureDevOps/Dto/ApproveBody.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace DependencyUpdated.Repositories.AzureDevOps.Dto; | ||
|
||
public record ApproveBody(int Vote) | ||
{ | ||
public static ApproveBody Approve() | ||
{ | ||
return new ApproveBody(10); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/DependencyUpdated.Repositories.AzureDevOps/Dto/WorkItemUpdateAttributes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace DependencyUpdated.Repositories.AzureDevOps.Dto; | ||
|
||
public sealed record WorkItemUpdateAttributes(string Name); |
3 changes: 3 additions & 0 deletions
3
src/DependencyUpdated.Repositories.AzureDevOps/Dto/WorkItemUpdatePatch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace DependencyUpdated.Repositories.AzureDevOps.Dto; | ||
|
||
public sealed record WorkItemUpdatePatch(string Op, string Path, WorkItemUpdateRelation Value); |
3 changes: 3 additions & 0 deletions
3
src/DependencyUpdated.Repositories.AzureDevOps/Dto/WorkItemUpdateRelation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace DependencyUpdated.Repositories.AzureDevOps.Dto; | ||
|
||
public sealed record WorkItemUpdateRelation(string Rel, string Url, WorkItemUpdateAttributes Attributes); |
25 changes: 25 additions & 0 deletions
25
src/DependencyUpdated.Repositories.AzureDevOps/IAzureDevOpsClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using DependencyUpdated.Repositories.AzureDevOps.Dto; | ||
using Refit; | ||
|
||
namespace DependencyUpdated.Repositories.AzureDevOps; | ||
|
||
public interface IAzureDevOpsClient | ||
{ | ||
const string ReviewersResource = "reviewers"; | ||
|
||
[Get("/_apis/git/repositories/{repository}/pullrequests?api-version=6.0")] | ||
Task<PullRequestArray> GetPullRequests(string repository); | ||
|
||
[Post("/_apis/git/repositories/{repository}/pullrequests?api-version=6.0")] | ||
Task<PullRequestResponse> CreatePullRequest(string repository, [Body] PullRequest pullRequestInfo); | ||
|
||
[Patch("/_apis/git/repositories/{repository}/pullrequests/{pullRequestId}?api-version=6.0")] | ||
Task<HttpResponseMessage> UpdatePullRequest(string repository, int pullRequestId, [Body] PullRequestUpdate pullRequestUpdate); | ||
|
||
[Patch("/_apis/wit/workitems/{workItemId}?api-version=6.0")] | ||
[Headers("Content-Type: application/json-patch+json")] | ||
Task<HttpResponseMessage> UpdateWorkItemRelation(int workItemId, [Body] WorkItemUpdatePatch[] patchValue); | ||
|
||
[Put("/_apis/git/repositories/{repository}/pullRequests/{pullRequestId}/" + ReviewersResource + "/{reviewerId}?api-version=6.0")] | ||
Task<HttpResponseMessage> Approve(string repository, int pullRequestId, string reviewerId, [Body] ApproveBody body); | ||
} |
33 changes: 33 additions & 0 deletions
33
src/DependencyUpdated.Repositories.AzureDevOps/LoggingHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Refit; | ||
using System.Diagnostics; | ||
using System.Net; | ||
|
||
namespace DependencyUpdated.Repositories.AzureDevOps; | ||
|
||
internal sealed class LoggingHandler : HttpClientHandler | ||
{ | ||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, | ||
CancellationToken cancellationToken) | ||
{ | ||
Debug.WriteLine("Request:"); | ||
Debug.WriteLine(request.ToString()); | ||
if (request.Content != null) | ||
{ | ||
Debug.WriteLine(await request.Content.ReadAsStringAsync(cancellationToken)); | ||
} | ||
|
||
Debug.WriteLine(string.Empty); | ||
|
||
var response = await base.SendAsync(request, cancellationToken); | ||
if (response.StatusCode == HttpStatusCode.NonAuthoritativeInformation) | ||
{ | ||
throw await ApiException.Create("Invalid PAT token", request, request.Method, response, new RefitSettings()); | ||
} | ||
|
||
Debug.WriteLine("Response:"); | ||
Debug.WriteLine(response.ToString()); | ||
Debug.WriteLine(await response.Content.ReadAsStringAsync(cancellationToken)); | ||
|
||
return response; | ||
} | ||
} |