-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cloning from Azure DevOps provider
- Loading branch information
Showing
7 changed files
with
287 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
namespace GitMan.Clients | ||
{ | ||
internal class AzureClient | ||
{ | ||
private readonly AzureClientConfig _config; | ||
|
||
public AzureClient(AzureClientConfig config) | ||
{ | ||
_config = config; | ||
} | ||
|
||
private HttpClient GetClient() | ||
{ | ||
var client = new HttpClient(); | ||
|
||
var acceptHeader = new MediaTypeWithQualityHeaderValue("application/json"); | ||
client.DefaultRequestHeaders.Accept.Add(acceptHeader); | ||
|
||
var patBytes = Encoding.ASCII.GetBytes(":" + _config.PersonalAccessToken); | ||
var authParameter = Convert.ToBase64String(patBytes); | ||
var authHeader = new AuthenticationHeaderValue("Basic", authParameter); | ||
client.DefaultRequestHeaders.Authorization = authHeader; | ||
|
||
client.Timeout = TimeSpan.FromSeconds(30); | ||
|
||
return client; | ||
} | ||
|
||
private string MakeParameterString(KeyValuePair<string, string> parameter) | ||
{ | ||
var safeKey = Uri.EscapeUriString(parameter.Key); | ||
var safeValue = Uri.EscapeUriString(parameter.Value); | ||
var parameterString = $"{safeKey}={safeValue}"; | ||
return parameterString; | ||
} | ||
|
||
private string MakeQuery(Dictionary<string, string> queryParams) | ||
{ | ||
var pairs = queryParams.Select(MakeParameterString); | ||
var query = string.Join('&', pairs); | ||
return query; | ||
} | ||
|
||
private Uri BuildUri(string path) | ||
{ | ||
var emptyQueryParams = new Dictionary<string, string>(); | ||
var uri = BuildUri(path, emptyQueryParams); | ||
return uri; | ||
} | ||
|
||
private Uri BuildUri(string path, Dictionary<string, string> queryParams) | ||
{ | ||
var organization = Uri.EscapeUriString(_config.Organization); | ||
var project = Uri.EscapeUriString(_config.Project); | ||
var fullPath = $"{organization}/{project}/_apis/{path}"; | ||
|
||
queryParams["api-version"] = "5.0"; | ||
var query = MakeQuery(queryParams); | ||
|
||
var builder = new UriBuilder | ||
{ | ||
Host = "dev.azure.com", | ||
Scheme = "https", | ||
Query = query, | ||
Path = fullPath | ||
}; | ||
|
||
var uriString = builder.ToString(); | ||
var uri = new Uri(uriString); | ||
return uri; | ||
} | ||
|
||
private JsonDocument GetResponse(string path) | ||
{ | ||
var uri = BuildUri(path); | ||
|
||
using (var client = GetClient()) | ||
{ | ||
using (var response = client.GetAsync(uri).Result) | ||
{ | ||
var json = response.Content.ReadAsStringAsync().Result; | ||
var document = JsonDocument.Parse(json); | ||
return document; | ||
} | ||
} | ||
} | ||
|
||
public AzureRepository[] GetRepositories() | ||
{ | ||
var document = GetResponse("git/repositories"); | ||
var repositories = document.RootElement.GetProperty("value"); | ||
|
||
var count = repositories.GetArrayLength(); | ||
var azureRepos = new AzureRepository[count]; | ||
var index = 0; | ||
|
||
foreach (var repository in repositories.EnumerateArray()) | ||
{ | ||
var name = repository.GetProperty("name").GetString(); | ||
var remoteUrl = repository.GetProperty("remoteUrl").GetString(); | ||
var azureRepo = new AzureRepository(name, remoteUrl); | ||
azureRepos[index] = azureRepo; | ||
index++; | ||
} | ||
|
||
return azureRepos; | ||
} | ||
} | ||
} |
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 GitMan.Clients | ||
{ | ||
internal class AzureClientConfig | ||
{ | ||
public string Organization { get; set; } | ||
public string Project { get; set; } | ||
public string PersonalAccessToken { get; set; } | ||
} | ||
} |
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,14 @@ | ||
namespace GitMan.Clients | ||
{ | ||
public class AzureRepository | ||
{ | ||
public string Name { get; } | ||
public string RemoteUrl { get; } | ||
|
||
public AzureRepository(string name, string remoteUrl) | ||
{ | ||
Name = name; | ||
RemoteUrl = remoteUrl; | ||
} | ||
} | ||
} |
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,12 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace GitMan.Config | ||
{ | ||
internal class AzureProvider | ||
{ | ||
public string Organization { get; set; } | ||
public string Project { get; set; } | ||
public string PersonalAccessToken { get; set; } | ||
public Dictionary<string, string> DefaultConfig { get; set; } = new Dictionary<string, string>(); | ||
} | ||
} |
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
Oops, something went wrong.