Created
June 28, 2020 16:18
-
-
Save smaglio81/6b3d07b334731aa2a401e042a551cbde to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Net.Http.Formatting; | |
using System.Text; | |
using System.Text.Json; | |
using System.Threading.Tasks; | |
using Microsoft.TeamFoundation.SourceControl.WebApi; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Serialization; | |
using JsonSerializer = System.Text.Json.JsonSerializer; | |
namespace YourProject | |
{ | |
public class JiraDevInfoHttpClient : IJiraDevInfoHttpClient | |
{ | |
private readonly HttpClient _client; | |
private readonly IUpdateSequenceIdManager _idManager; | |
public JiraDevInfoHttpClient( | |
HttpClient client, | |
IUpdateSequenceIdManager idManager | |
) | |
{ | |
_client = client; | |
_idManager = idManager; | |
_client.SetApiBaseUri(AtlassianConstants.YourInstanceCloudId, "devinfo"); | |
} | |
private JiraDevInfoBulkRequest BuildBulkRequest(JiraRepository repository) | |
{ | |
var bulkRequest = new JiraDevInfoBulkRequest() | |
{ | |
Repositories = new List<JiraRepository>() { repository }, | |
PreventTransitions = true, | |
ProviderMetadata = AtlassianConstants.AzDProviderMetadata | |
}; | |
return bulkRequest; | |
} | |
public async Task<JiraDevInfoResponse> BulkUpdateAsync( | |
JiraRepository repository | |
) { | |
var updateSequenceId = await _idManager.NextAsync(); | |
repository.UpdateSequenceId = updateSequenceId; | |
foreach (var c in repository.Commits) | |
{ | |
c.UpdateSequenceId = updateSequenceId; | |
} | |
foreach (var b in repository.Branches) | |
{ | |
b.UpdateSequenceId = updateSequenceId; | |
} | |
foreach (var pr in repository.PullRequests) | |
{ | |
pr.UpdateSequenceId = updateSequenceId; | |
} | |
var bulkRequest = BuildBulkRequest(repository); | |
var content = bulkRequest.ToJsonContent(); | |
var response = await _client.PostAsync("bulk", content); | |
var result = await response.DeserializeAsync<JiraDevInfoResponse>(); | |
return result; | |
} | |
} | |
public static class HttpClientExtensions | |
{ | |
public static void SetApiBaseUri(this HttpClient client, string cloudId, string entityType) | |
{ | |
client.BaseAddress = ApiBaseUri(cloudId, entityType); | |
} | |
public static Uri ApiBaseUri(string cloudId, string entityType) | |
{ | |
// example: | |
// https://api.atlassian.com/jira/devinfo/0.1/cloud/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/bulk | |
var baseUrl = string.Format("{0}jira/{1}/0.1/cloud/{2}/", | |
AtlassianConstants.AtlassianApiRootUrl, | |
entityType, | |
cloudId | |
); | |
var baseUri = new Uri(baseUrl); | |
return baseUri; | |
} | |
} | |
public static class JiraOAuthHttpClientExtensions | |
{ | |
public static readonly JsonSerializerOptions SerializerOptions = | |
new JsonSerializerOptions() {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}; | |
public static HttpContent ToJsonContent<T>(this T value) | |
{ | |
var json = JsonSerializer.Serialize(value, SerializerOptions); | |
var content = new StringContent(json, Encoding.UTF8, "application/json"); | |
return content; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment