Skip to content

Commit

Permalink
disable-s3 argument to use V2 of the API
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebro committed Oct 3, 2018
1 parent 9298abe commit 4934429
Show file tree
Hide file tree
Showing 21 changed files with 268 additions and 54 deletions.
4 changes: 4 additions & 0 deletions Source/Codecov.Tests/Codecov.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
Expand Down
18 changes: 16 additions & 2 deletions Source/Codecov.Tests/Url/RouteTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using Codecov.Url;
using FluentAssertions;
using Xunit;

namespace Codecov.Tests.Url
Expand All @@ -12,10 +13,23 @@ public void Should_Be_Version_Four()
var route = new Codecov.Url.Route();

// When
var getRoute = route.GetRoute;
var getRoute = route.GetRoute(ApiVersion.V4);

// Then
getRoute.Should().Be("upload/v4");
}

[Fact]
public void Should_Be_Version_Two()
{
// Given
var route = new Codecov.Url.Route();

// When
var getRoute = route.GetRoute(ApiVersion.V2);

// Then
getRoute.Should().Be("upload/v2");
}
}
}
27 changes: 23 additions & 4 deletions Source/Codecov.Tests/Url/UrlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,41 @@ namespace Codecov.Tests.Url
public class UrlTests
{
[Fact]
public void Should_Return_Url()
public void Should_Return_V4_Url()
{
// Given
var host = Substitute.For<IHost>();
host.GetHost.Returns("https://codecov.io");
var route = Substitute.For<IRoute>();
route.GetRoute.Returns("upload/v4");
route.GetRoute(ApiVersion.V4).Returns("upload/v4");
var query = Substitute.For<IQuery>();
query.GetQuery.Returns("branch=develop&commit=123");
var url = new Codecov.Url.Url(host, route, query);

// When
var getUrl = url.GetUrl;
var getUrl = url.GetUrl(ApiVersion.V4);

// Then
getUrl.Should().Be(new Uri("https://codecov.io/upload/v4?branch=develop&commit=123"));
}

[Fact]
public void Should_Return_V2_Url()
{
// Given
var host = Substitute.For<IHost>();
host.GetHost.Returns("https://codecov.io");
var route = Substitute.For<IRoute>();
route.GetRoute(ApiVersion.V2).Returns("upload/v2");
var query = Substitute.For<IQuery>();
query.GetQuery.Returns("branch=develop&commit=123");
var url = new Codecov.Url.Url(host, route, query);

// When
var getUrl = url.GetUrl(ApiVersion.V2);

// Then
getUrl.Should().Be(new Uri("https://codecov.io/upload/v2?branch=develop&commit=123"));
}
}
}
}
4 changes: 4 additions & 0 deletions Source/Codecov/Codecov.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
<RuntimeIdentifiers>win7-x64</RuntimeIdentifiers>
<AssemblyName>codecov</AssemblyName>
<Product>codecov-exe</Product>
Expand All @@ -15,6 +16,9 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702</NoWarn>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Scripts\**" />
<EmbeddedResource Remove="Scripts\**" />
Expand Down
2 changes: 1 addition & 1 deletion Source/Codecov/Factories/TerminalFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static IDictionary<TerminalName, ITerminal> Create()

foreach (var key in terminals.Keys)
{
if (!terminals[key].Exits)
if (!terminals[key].Exists)
{
terminals.Remove(key);
}
Expand Down
7 changes: 7 additions & 0 deletions Source/Codecov/Program/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public class CommandLineOptions : IVersionControlSystemOptions, ICoverageOptions
[Option("disable-network", HelpText = "Toggle functionalities. (1) --disable-network. Disable uploading the file network.")]
public bool DisableNetwork { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to use S3 or not.
/// </summary>
/// <value>A value indicating whether to upload to S3 or not.</value>
[Option("disable-s3", HelpText = "Disable uploading the reports directly to S3.")]
public bool DisableS3 { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to don't upload and dump to stdin.
/// </summary>
Expand Down
33 changes: 14 additions & 19 deletions Source/Codecov/Program/UploadFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public UploadFacade(CommandLineOptions commandLineOptions)

private ICoverage Coverage => new Coverage.Tool.Coverage(CommandLineCommandLineOptions);

private IUpload Upload => new Uploads(Url, Report, Terminals);
private IUpload Upload => new Uploads(Url, Report, Terminals, CommandLineCommandLineOptions.DisableS3);

private IUrl Url => new Url.Url(new Host(CommandLineCommandLineOptions), new Route(), new Query(CommandLineCommandLineOptions, Repositories, ContinuousIntegrationServer, Yaml));

Expand All @@ -49,16 +49,6 @@ public UploadFacade(CommandLineOptions commandLineOptions)

private IVersionControlSystem VersionControlSystem => VersionControlSystemFactory.Create(CommandLineCommandLineOptions, Terminals[TerminalName.Generic]);

private string DisplayUrl
{
get
{
var url = Url.GetUrl.ToString();
var regex = new Regex(@"token=\w{8}-\w{4}-\w{4}-\w{4}-\w{12}&");
return regex.Replace(url, string.Empty);
}
}

public void Uploader()
{
var ci = ContinuousIntegrationServer.GetType().Name;
Expand Down Expand Up @@ -107,24 +97,29 @@ public void Uploader()
if (CommandLineCommandLineOptions.Dump)
{
Log.Warning("Skipping upload and dumping contents.");
Log.Information($"url: {Url.GetUrl}");
Log.Information(Report.Reporter);
return;
}

Log.Information("Uploading Reports.");
Log.Information($"url: {Url.GetUrl.Scheme}://{Url.GetUrl.Authority}");
Log.Verboase($"api endpoint: {Url.GetUrl}");
Log.Information($"query: {DisplayUrl}");
Log.Information("Pinging Codecov");

var response = Upload.Uploader();
Log.Verboase($"response: {response}");
var splitResponse = response.Split('\n');
var s3 = new Uri(splitResponse[1]);
var reportUrl = splitResponse[0];
Log.Information($"Uploading to S3 {s3.Scheme}://{s3.Authority}");
Log.Information($"View reports at: {reportUrl}");

if (CommandLineCommandLineOptions.DisableS3)
{
var reportUrl = splitResponse[1];
Log.Information($"View reports at: {reportUrl}");
}
else
{
var s3 = new Uri(splitResponse[1]);
var reportUrl = splitResponse[0];
Log.Information($"Uploading to S3 {s3.Scheme}://{s3.Authority}");
Log.Information($"View reports at: {reportUrl}");
}
}
}
}
2 changes: 1 addition & 1 deletion Source/Codecov/Terminal/ITerminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
internal interface ITerminal
{
bool Exits { get; }
bool Exists { get; }

string Run(string command, string commandArguments);

Expand Down
2 changes: 1 addition & 1 deletion Source/Codecov/Terminal/PowerShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
internal class PowerShell : Terminal
{
public override bool Exits => !string.IsNullOrWhiteSpace(RunScript("$PSVersionTable.PSVersion"));
public override bool Exists => !string.IsNullOrWhiteSpace(RunScript("$PSVersionTable.PSVersion"));

public override string Run(string command, string commandArguments)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Codecov/Terminal/Terminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Codecov.Terminal
{
internal class Terminal : ITerminal
{
public virtual bool Exits => true;
public virtual bool Exists => true;

public string RunScript(string script)
{
Expand Down
51 changes: 51 additions & 0 deletions Source/Codecov/Upload/HttpWebRequestV2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;
using Codecov.Coverage.Report;
using Codecov.Logger;
using Codecov.Url;

namespace Codecov.Upload
{
internal class HttpWebRequestV2 : UploadV2
{
public HttpWebRequestV2(IUrl url, IReport report)
: base(url, report)
{
}

protected override string Post()
{
Log.Verboase("Trying to upload using HttpWebRequest.");

var postRequest = (System.Net.HttpWebRequest)WebRequest.Create(Uri);
postRequest.ContentType = "text/plain";
postRequest.Method = "POST";
postRequest.Headers["Content-Encoding"] = "gzip";
postRequest.Headers["X-Content-Encoding"] = "gzip";
postRequest.Headers["Accept"] = "text/plain";

using (var putStreamWriter = new GZipStream(postRequest.GetRequestStreamAsync().Result, CompressionLevel.Optimal))
{
var content = Encoding.UTF8.GetBytes(Report.Reporter);
putStreamWriter.Write(content, 0, content.Length);
}

var postResponse = (HttpWebResponse)postRequest.GetResponseAsync().Result;

if (postResponse.StatusCode != HttpStatusCode.OK)
{
Log.Verboase("Failed to upload the report.");
return string.Empty;
}

using (var postStreamReader = new StreamReader(postResponse.GetResponseStream()))
{
return postStreamReader.ReadToEnd();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace Codecov.Upload
{
internal class HttpWebRequest : Upload
internal class HttpWebRequestV4 : UploadV4
{
public HttpWebRequest(IUrl url, IReport report)
public HttpWebRequestV4(IUrl url, IReport report)
: base(url, report)
{
}
Expand All @@ -20,7 +20,7 @@ protected override string Post()
{
Log.Verboase("Trying to upload using HttpWebRequest.");

var postRequest = (System.Net.HttpWebRequest)WebRequest.Create(Url.GetUrl);
var postRequest = (System.Net.HttpWebRequest)WebRequest.Create(Uri);
postRequest.Method = "POST";
postRequest.Headers["X-Reduced-Redundancy"] = "false";
postRequest.Headers["X-Content-Type"] = "application/x-gzip";
Expand Down
63 changes: 63 additions & 0 deletions Source/Codecov/Upload/UploadV2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Codecov.Coverage.Report;
using Codecov.Logger;
using Codecov.Url;

namespace Codecov.Upload
{
internal abstract class UploadV2 : IUpload
{
private readonly Lazy<IReport> _report;

private readonly Lazy<IUrl> _url;

protected UploadV2(IUrl url, IReport report)
{
_url = new Lazy<IUrl>(() => url);
_report = new Lazy<IReport>(() => report);
}

protected IReport Report => _report.Value;

protected Uri Uri => _url.Value.GetUrl(ApiVersion.V2);

private string DisplayUrl
{
get
{
var url = Uri.ToString();
var regex = new Regex(@"token=\w{8}-\w{4}-\w{4}-\w{4}-\w{12}&");
return regex.Replace(url, string.Empty);
}
}

public string Uploader()
{
Log.Information($"url: {Uri.Scheme}://{Uri.Authority}");
Log.Verboase($"api endpoint: {Uri}");
Log.Information($"query: {DisplayUrl}");

try
{
var response = Post();
if (string.IsNullOrWhiteSpace(response))
{
Log.Verboase("Failed to ping codecov.");
return string.Empty;
}

return response;
}
catch (Exception ex)
{
Log.VerboaseException(ex);
return string.Empty;
}
}

protected abstract string Post();
}
}
Loading

0 comments on commit 4934429

Please sign in to comment.