Skip to content

Commit

Permalink
Added TeamCity Connector
Browse files Browse the repository at this point in the history
  • Loading branch information
leandromonaco committed Jan 23, 2023
1 parent bfc2b55 commit 04c31bf
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private void AddAuthenticantionHeader(string key, AuthenticationType authType)

public async Task<string> GetAsync(string requestUri)
{
_httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
var result = await _httpClient.GetAsync(requestUri);
return await result.Content.ReadAsStringAsync();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Threading.Tasks;
using System.Globalization;

namespace IntegrationConnectors.TeamCity.Converters
{
public class DateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
CultureInfo provider = CultureInfo.InvariantCulture;
var str = reader.GetString();
str = str.Replace("+0000", string.Empty);
var date = DateTime.ParseExact(str, "yyyyMMddTHHmmss", provider);
return TimeZone.CurrentTimeZone.ToLocalTime(date);
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\IntegrationConnectors.Common\IntegrationConnectors.Common.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace IntegrationConnectors.TeamCity.Model
{
public class TeamCityBuild
{
public int Id { get; set; }
public string BuildTypeId { get; set; }
public string Number { get; set; }
public string Status { get; set; }
public string State { get; set; }
public string BranchName { get; set; }
public bool DefaultBranch { get; set; }
public string Href { get; set; }
public string WebUrl { get; set; }
public DateTime FinishOnAgentDate { get; set; }
public bool Customized { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace IntegrationConnectors.TeamCity.Model
{
public class TeamCityBuilds
{
public int Count { get; set; }
public string Href { get; set; }
[JsonPropertyName("build")]
public TeamCityBuild[] Builds { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace IntegrationConnectors.TeamCity.Model
{
public class TeamCityCodeChange
{
public int Id { get; set; }
public string Version { get; set; }
public string Username { get; set; }
public string Date { get; set; }
public string Href { get; set; }
public string WebUrl { get; set; }
public string Comment { get; set; }
public TeamCityCodeChangeFiles Files { get; set; }
public TeamCityCodeChangeVcsrootinstance VcsRootInstance { get; set; }
public TeamCityCodeChangeCommiter Commiter { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace IntegrationConnectors.TeamCity.Model
{
public class TeamCityCodeChangeCommiter
{
public string VcsUsername { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace IntegrationConnectors.TeamCity.Model
{
public class TeamCityCodeChangeFiles
{
public int Count { get; set; }
public object[] File { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace IntegrationConnectors.TeamCity.Model
{
public class TeamCityCodeChangeVcsrootinstance
{
public string Id { get; set; }
public string Vcsrootid { get; set; }
public string Name { get; set; }
public string Href { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace IntegrationConnectors.TeamCity.Model
{
public class TeamCityCodeChanges
{
[JsonPropertyName("change")]
public TeamCityCodeChange[] Changes { get; set; }
public string Href { get; set; }
public int Count { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Text.Json;
using IntegrationConnectors.Common;
using IntegrationConnectors.TeamCity.Converters;
using IntegrationConnectors.TeamCity.Model;

namespace IntegrationConnectors.JIRA
{

public class TeamCityConnector : HttpConnector
{
public TeamCityConnector(string baseUrl, string apiKey, AuthenticationType authType) : base(baseUrl, apiKey, authType)
{
}

public async Task<List<TeamCityCodeChange>> GetBuildChanges(int buildId)
{
JsonSerializerOptions options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
options.Converters.Add(new DateTimeConverter());


var response = await GetAsync($"{_url}/app/rest/changes?locator=build:(id:{buildId})");
var teamcityCodeChanges = JsonSerializer.Deserialize<TeamCityCodeChanges>(response, options);
return teamcityCodeChanges?.Changes.ToList();
}

public async Task<List<TeamCityBuild>> GetBuilds()
{
JsonSerializerOptions options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
options.Converters.Add(new DateTimeConverter());


var response = await GetAsync($"{_url}/app/rest/builds?locator=count:100,start:0");
var teamcityBuilds = JsonSerializer.Deserialize<TeamCityBuilds>(response, options);
return teamcityBuilds?.Builds.ToList();
}

public async Task<TeamCityCodeChange> GetChange(int changeId)
{
JsonSerializerOptions options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
options.Converters.Add(new DateTimeConverter());

var response = await GetAsync($"{_url}/app/rest/changes/{changeId}");
var teamCityCodeChange = JsonSerializer.Deserialize<TeamCityCodeChange>(response, options);
return teamCityCodeChange;
}
}
}
10 changes: 10 additions & 0 deletions Tools/ConnectionDropOutTester/ConnectionDropOutTester.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
23 changes: 23 additions & 0 deletions Tools/ConnectionDropOutTester/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Net.NetworkInformation;

for (int i = 0; i < 200; i++)
{
PingServer("google.com");
Thread.Sleep(1000);
}

void PingServer(string hostName)
{
try
{
var pinger = new Ping();
pinger.Send(hostName);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")} PING: {hostName} OK");
}
catch (Exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")} PING: {hostName} Failed");
}
}
19 changes: 19 additions & 0 deletions Tools/Tools.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfluenceKB", "src\Conflue
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntegrationConnectors.JIRA", "..\IntegrationConnectors\src\IntegrationConnectors.JIRA\IntegrationConnectors.JIRA.csproj", "{94B2ACBB-2E5E-45C2-96F6-48913DD85087}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConnectionDropOutTester", "ConnectionDropOutTester\ConnectionDropOutTester.csproj", "{7B4A28A7-2A77-43CD-947C-64C6D67D669F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntegrationConnectors.TeamCity", "..\IntegrationConnectors\src\IntegrationConnectors.TeamCity\IntegrationConnectors.TeamCity.csproj", "{AF002909-64E4-4B0E-8483-4FA120D697E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TeamCityHelper", "src\TeamCityHelper\TeamCityHelper.csproj", "{9CB92EB8-F87F-4502-B848-3C44073F3EA0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -79,6 +85,18 @@ Global
{94B2ACBB-2E5E-45C2-96F6-48913DD85087}.Debug|Any CPU.Build.0 = Debug|Any CPU
{94B2ACBB-2E5E-45C2-96F6-48913DD85087}.Release|Any CPU.ActiveCfg = Release|Any CPU
{94B2ACBB-2E5E-45C2-96F6-48913DD85087}.Release|Any CPU.Build.0 = Release|Any CPU
{7B4A28A7-2A77-43CD-947C-64C6D67D669F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7B4A28A7-2A77-43CD-947C-64C6D67D669F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B4A28A7-2A77-43CD-947C-64C6D67D669F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7B4A28A7-2A77-43CD-947C-64C6D67D669F}.Release|Any CPU.Build.0 = Release|Any CPU
{AF002909-64E4-4B0E-8483-4FA120D697E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AF002909-64E4-4B0E-8483-4FA120D697E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF002909-64E4-4B0E-8483-4FA120D697E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF002909-64E4-4B0E-8483-4FA120D697E8}.Release|Any CPU.Build.0 = Release|Any CPU
{9CB92EB8-F87F-4502-B848-3C44073F3EA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9CB92EB8-F87F-4502-B848-3C44073F3EA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9CB92EB8-F87F-4502-B848-3C44073F3EA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9CB92EB8-F87F-4502-B848-3C44073F3EA0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -88,6 +106,7 @@ Global
{A71D8EA2-D026-472F-A851-CE4F6FCB7683} = {2A54E36A-9C75-4A5E-BE49-F6609643AF3A}
{6410BDA1-C8E2-4E69-8C95-57D4109772D7} = {2A54E36A-9C75-4A5E-BE49-F6609643AF3A}
{94B2ACBB-2E5E-45C2-96F6-48913DD85087} = {2A54E36A-9C75-4A5E-BE49-F6609643AF3A}
{AF002909-64E4-4B0E-8483-4FA120D697E8} = {2A54E36A-9C75-4A5E-BE49-F6609643AF3A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5B7415CA-4F0A-426D-8452-EDF306CA90E3}
Expand Down

0 comments on commit 04c31bf

Please sign in to comment.