Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
torbacz authored Aug 23, 2024
2 parents 019bba1 + f4be2be commit daecf44
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 38 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: CI

on:
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
2 changes: 2 additions & 0 deletions DependencyUpdated.Core/Config/AzureDevOpsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class AzureDevOpsConfig : IValidatableObject
public string? Project { get; set; }

public string? Repository { get; set; }

public int? WorkItemId { get; set; }

public string TargetBranchName { get; set; } = "dev";

Expand Down
97 changes: 61 additions & 36 deletions DependencyUpdated.Repositories.AzureDevOps/AzureDevOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,48 +93,73 @@ public async Task SubmitPullRequest(IReadOnlyCollection<UpdateResult> updates, s
$"https://dev.azure.com/{configValue.Organization}/{configValue.Project}/_apis/git/repositories/{configValue.Repository}/pullrequests?api-version=6.0";

logger.Information("Creating new PR");
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.UTF8.GetBytes($":{configValue.PAT}")));
var pr = new PullRequest(sourceBranch, targetBranch, prTitile, prDescription);
using var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.UTF8.GetBytes($":{configValue.PAT}")));
var pr = new PullRequest(sourceBranch, targetBranch, prTitile, prDescription);

var jsonString = JsonSerializer.Serialize(pr);
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
var jsonString = JsonSerializer.Serialize(pr);
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");

var result = await client.PostAsync(baseUrl, content);
result.EnsureSuccessStatusCode();
var result = await client.PostAsync(baseUrl, content);
result.EnsureSuccessStatusCode();

if (result.StatusCode == HttpStatusCode.NonAuthoritativeInformation)
{
throw new Exception("Invalid PAT token provided");
}
if (result.StatusCode == HttpStatusCode.NonAuthoritativeInformation)
{
throw new Exception("Invalid PAT token provided");
}

var response = await result.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
var responseObject = JsonSerializer.Deserialize<PullRequestResponse>(response, options);
if (responseObject is null)
{
throw new Exception("Missing response from API");
}
var response = await result.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
var responseObject = JsonSerializer.Deserialize<PullRequestResponse>(response, options);
if (responseObject is null)
{
throw new Exception("Missing response from API");
}

logger.Information("New PR created {Id}", responseObject.PullRequestId);
if (configValue.AutoComplete)
logger.Information("New PR created {Id}", responseObject.PullRequestId);
if (configValue.AutoComplete)
{
logger.Information("Setting autocomplete for PR {Id}", responseObject.PullRequestId);
baseUrl =
$"https://dev.azure.com/{configValue.Organization}/{configValue.Project}/_apis/git/repositories/{configValue.Repository}/pullrequests/{responseObject.PullRequestId}?api-version=6.0";
var autoComplete = new PullRequestUpdate(responseObject.CreatedBy,
new GitPullRequestCompletionOptions(true, false, GitPullRequestMergeStrategy.Squash));
jsonString = JsonSerializer.Serialize(autoComplete);
content = new StringContent(jsonString, Encoding.UTF8, "application/json");
result = await client.PatchAsync(baseUrl, content);
result.EnsureSuccessStatusCode();
}

if (configValue.WorkItemId.HasValue)
{
logger.Information("Setting work item {ConfigValueWorkItemId} relation to {PullRequestId}",
configValue.WorkItemId.Value, responseObject.PullRequestId);
var workItemUpdateUrl = new Uri(
$"https://dev.azure.com/{configValue.Organization}/{configValue.Project}/_apis/wit/workitems/{configValue.WorkItemId.Value}?api-version=6.0");
var patchValue = new[]
{
logger.Information("Setting autocomplete for PR {Id}", responseObject.PullRequestId);
baseUrl =
$"https://dev.azure.com/{configValue.Organization}/{configValue.Project}/_apis/git/repositories/{configValue.Repository}/pullrequests/{responseObject.PullRequestId}?api-version=6.0";
var autoComplete = new PullRequestUpdate(responseObject.CreatedBy,
new GitPullRequestCompletionOptions(true, false, GitPullRequestMergeStrategy.Squash));
jsonString = JsonSerializer.Serialize(autoComplete);
content = new StringContent(jsonString, Encoding.UTF8, "application/json");
result = await client.PatchAsync(baseUrl, content);
result.EnsureSuccessStatusCode();
}
new
{
op = "add",
path = "/relations/-",
value = new
{
rel = "ArtifactLink",
url = responseObject.ArtifactId,
attributes = new { name = "Pull Request" }
}
}
};

jsonString = JsonSerializer.Serialize(patchValue);
content = new StringContent(jsonString, Encoding.UTF8, "application/json-patch+json");
result = await client.PatchAsync(workItemUpdateUrl, content);
result.EnsureSuccessStatusCode();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace DependencyUpdated.Repositories.AzureDevOps.Dto;

public record PullRequestResponse(int PullRequestId, User CreatedBy);
public record PullRequestResponse(int PullRequestId, User CreatedBy, string Url, string ArtifactId);
3 changes: 2 additions & 1 deletion DependencyUpdated/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"PAT": "",
"Organization": "",
"Project": "",
"Repository": ""
"Repository": "",
"WorkItemId": null
},
"PullRequestType": "AzureDevOps",
"Projects": [
Expand Down

0 comments on commit daecf44

Please sign in to comment.