Skip to content

Commit

Permalink
added support for appveyor pull request head commit (#12)
Browse files Browse the repository at this point in the history
* added support for appveyor pull request head commit

* Removed BuildTools

* Fixed bug in GitProcessGitDataResolver that added extra quotes

* Moved to single ILogger instead of logger factory

* Added logging of appveyor environment variables

* Updated to get the commit id and branch from appveyor env vars

* Added run-at timestamp

* Fixed test when running in appveyor
  • Loading branch information
abe545 authored Apr 12, 2018
1 parent a3e197b commit 54000e9
Show file tree
Hide file tree
Showing 17 changed files with 142 additions and 117 deletions.
24 changes: 0 additions & 24 deletions BuildTools/dupfinder.xslt

This file was deleted.

42 changes: 0 additions & 42 deletions BuildTools/resharperReport.xslt

This file was deleted.

9 changes: 9 additions & 0 deletions src/dotnet-coveralls/Adapters/DateTimeOffsetProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Dotnet.Coveralls.Adapters
{
public class DateTimeOffsetProvider : IDateTimeOffsetProvider
{
public DateTimeOffset UtcNow => DateTimeOffset.UtcNow;
}
}
9 changes: 9 additions & 0 deletions src/dotnet-coveralls/Adapters/IDateTimeOffsetProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Dotnet.Coveralls.Adapters
{
public interface IDateTimeOffsetProvider
{
DateTimeOffset UtcNow { get; }
}
}
3 changes: 3 additions & 0 deletions src/dotnet-coveralls/CommandLine/CoverallsOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public class CoverallsOptions
[NamedArgument("verbose", Action = ParseAction.StoreTrue, Description = "When set, will display additional log information")]
public bool Verbose { get; set; }

[NamedArgument("run-at", Description = "The time the job was run at. If not set, will pass UtcNow.")]
public string RunAt { get; set; }

public static string Usage => new AutomaticHelpGenerator<CoverallsOptions>().GetHelp(new CliParser<CoverallsOptions>(new CoverallsOptions()).Config);
public static CoverallsOptions Parse(string[] args)
{
Expand Down
1 change: 1 addition & 0 deletions src/dotnet-coveralls/Data/CoverallsData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public sealed class CoverallsData
public string ServiceNumber { get; set; }
public string ServiceBranch { get; set; }
public string ServicePullRequest { get; set; }
public string RunAt { get; set; }
public bool? Parallel { get; set; }
public CoverageFile[] SourceFiles { get; set; }
public GitData Git { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion src/dotnet-coveralls/Di.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ public static Scope Setup(string[] args)
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

container.Register(() => options);
container.Register(() => new LoggerFactory().AddConsole(options.Verbose ? LogLevel.Debug : LogLevel.Information));
container.Register(() => new LoggerFactory().AddConsole(options.Verbose ? LogLevel.Debug : LogLevel.Information).CreateLogger("dotnet-coveralls"));

container.Register<IFileWriter, FileWriter>();
container.Register<IFileProvider>(() => new UnrestrictedFileProvider(Environment.CurrentDirectory));
container.Register<IOutputFileWriter, OutputFileWriter>();
container.Register<IEnvironmentVariables, EnvironmentVariables>();
container.Register<IDateTimeOffsetProvider, DateTimeOffsetProvider>();
container.Register<IProcessExecutor, ProcessExecutor>();

container.Register<CoverallsPublisher>();
Expand Down
74 changes: 39 additions & 35 deletions src/dotnet-coveralls/Git/GitProcessGitDataResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,70 @@ namespace Dotnet.Coveralls.Git
public class GitProcessGitDataResolver : IGitDataResolver
{
private readonly IProcessExecutor processExecutor;
private readonly ILogger<GitProcessGitDataResolver> logger;

public static class GitArgs
{
public const string ID = "log -1 --pretty=format:'%H'";
public const string AUTHOR_EMAIL = "log -1 --pretty=format:'%ae'";
public const string AUTHOR_NAME = "log -1 --pretty=format:'%aN'";
public const string COMMITTER_EMAIL = "log -1 --pretty=format:'%ce'";
public const string COMMITTER_NAME = "log -1 --pretty=format:'%cN'";
public const string MESSAGE = "log -1 --pretty=format:'%s'";
public const string ID = "log -1 --pretty=format:%H";
public const string AUTHOR_EMAIL = "log -1 --pretty=format:%ae";
public const string AUTHOR_NAME = "log -1 --pretty=format:%aN";
public const string COMMITTER_EMAIL = "log -1 --pretty=format:%ce";
public const string COMMITTER_NAME = "log -1 --pretty=format:%cN";
public const string MESSAGE = "log -1 --pretty=format:%s";
public const string BRANCH = "rev-parse --abbrev-ref HEAD";
public const string REMOTES = "remote -v";
}

public GitProcessGitDataResolver(IProcessExecutor processExecutor, ILoggerFactory loggerFactory)
public GitProcessGitDataResolver(IProcessExecutor processExecutor)
{
this.processExecutor = processExecutor;
this.logger = loggerFactory.CreateLogger<GitProcessGitDataResolver>();
}

public bool CanProvideData => true;

public async Task<GitData> CreateGitData()
{
var gitData = new GitData { Head = new GitHead() };

gitData.Head.Id = await Git(GitArgs.ID);
gitData.Head.AuthorName = await Git(GitArgs.AUTHOR_NAME);
gitData.Head.AuthorEmail = await Git(GitArgs.AUTHOR_EMAIL);
gitData.Head.CommitterName = await Git(GitArgs.COMMITTER_NAME);
gitData.Head.CommitterEmail = await Git(GitArgs.COMMITTER_EMAIL);
gitData.Head.Message = await Git(GitArgs.MESSAGE);
gitData.Branch = await Git(GitArgs.BRANCH);
return new GitData
{
Head = new GitHead
{
Id = await Git(GitArgs.ID),
AuthorName = await Git(GitArgs.AUTHOR_NAME),
AuthorEmail = await Git(GitArgs.AUTHOR_EMAIL),
CommitterName = await Git(GitArgs.COMMITTER_NAME),
CommitterEmail = await Git(GitArgs.COMMITTER_EMAIL),
Message = await Git(GitArgs.MESSAGE),
},
Branch = await Git(GitArgs.BRANCH),
Remotes = await ParseRemotes()
};

var remotes = (await Git(GitArgs.REMOTES))?.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
if (remotes?.Length > 0)
async Task<GitRemote[]> ParseRemotes()
{
var splits = new[] { '\t', ' ' };
gitData.Remotes = remotes
.Select(r =>
{
var parts = r.Split(splits, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2) return null;
return new GitRemote { Name = parts[0], Url = parts[1] };
})
.Where(r => r != null)
.Distinct()
.ToArray();
}
var remotes = (await Git(GitArgs.REMOTES))?.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
if (remotes?.Length > 0)
{
var splits = new[] { '\t', ' ' };
return remotes
.Select(r =>
{
var parts = r.Split(splits, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2) return null;
return new GitRemote { Name = parts[0], Url = parts[1] };
})
.Where(r => r != null)
.Distinct()
.ToArray();
}

return gitData;
return null;
}

async Task<string> Git(string arguments)
{
logger.LogDebug($"git {arguments}");
var psi = new ProcessStartInfo("git", arguments);
var (stdOut, stdErr, exit) = await processExecutor.Execute(psi);
if (exit > 0)
{
if (!string.IsNullOrWhiteSpace(stdErr)) logger.LogWarning(stdErr);
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/dotnet-coveralls/Io/OutputFileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public OutputFileWriter(
CoverallsOptions options,
IFileWriter fileWriter,
IFileProvider fileProvider,
ILoggerFactory loggerFactory)
ILogger logger)
{
this.options = options;
this.fileWriter = fileWriter;
this.fileProvider = fileProvider;
this.logger = loggerFactory.CreateLogger<OutputFileWriter>();
this.logger = logger;
}

public async Task WriteCoverageOutput(string text)
Expand Down
17 changes: 12 additions & 5 deletions src/dotnet-coveralls/Io/ProcessExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace Dotnet.Coveralls.Io
{
public class ProcessExecutor : IProcessExecutor
{
private readonly ILogger<ProcessExecutor> Logger;
private readonly ILogger logger;

public ProcessExecutor(ILoggerFactory loggerFactory)
public ProcessExecutor(ILogger logger)
{
Logger = loggerFactory.CreateLogger<ProcessExecutor>();
this.logger = logger;
}

public async Task<(string StandardOut, string StandardErr, int ReturnCode)> Execute(ProcessStartInfo processStartInfo)
Expand All @@ -23,18 +23,25 @@ public ProcessExecutor(ILoggerFactory loggerFactory)
var stdOut = new StringBuilder();
var stdErr = new StringBuilder();

logger.LogDebug($"{processStartInfo.FileName} {processStartInfo.Arguments}");
using (var process = Process.Start(processStartInfo))
{
process.OutputDataReceived += (_, e) =>
{
stdOut.AppendLine(e.Data);
Logger.LogDebug(e.Data);
if (!string.IsNullOrWhiteSpace(e.Data))
{
logger.LogDebug(e.Data);
}
};

process.ErrorDataReceived += (_, e) =>
{
stdErr.AppendLine(e.Data);
Logger.LogDebug(e.Data);
if (!string.IsNullOrWhiteSpace(e.Data))
{
logger.LogWarning(e.Data);
}
};

process.BeginOutputReadLine();
Expand Down
19 changes: 17 additions & 2 deletions src/dotnet-coveralls/Publishing/AppVeyorProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;

namespace Dotnet.Coveralls.Git
{
Expand All @@ -23,11 +24,23 @@ public static class AppVeyor
public const string JOB_ID = "APPVEYOR_JOB_ID";
public const string BUILD_VERSION = "APPVEYOR_BUILD_VERSION";
public const string PR_NUMBER = "APPVEYOR_PULL_REQUEST_NUMBER";
public const string PR_COMMIT_ID = "APPVEYOR_PULL_REQUEST_HEAD_COMMIT";
}

public AppVeyorProvider(IEnvironmentVariables variables)
public AppVeyorProvider(IEnvironmentVariables variables, ILogger logger)
{
this.variables = variables;

var vars = System.Environment
.GetEnvironmentVariables()
.Keys
.Cast<string>()
.Where(k => k.StartsWith(nameof(AppVeyor), System.StringComparison.InvariantCultureIgnoreCase));

foreach (var v in vars)
{
logger.LogDebug($"{v}: {variables.GetEnvironmentVariable(v)}");
}
}

public bool CanProvideData => bool.TryParse(variables.GetEnvironmentVariable(nameof(AppVeyor).ToUpper()), out var value) && value;
Expand All @@ -37,15 +50,17 @@ public AppVeyorProvider(IEnvironmentVariables variables)
{
Head = new GitHead
{
Id = variables.GetEnvironmentVariable(AppVeyor.PR_COMMIT_ID),
CommitterName = variables.GetEnvironmentVariable(AppVeyor.COMMIT_AUTHOR),
CommitterEmail = variables.GetEnvironmentVariable(AppVeyor.COMMIT_EMAIL),
Message = variables.GetEnvironmentVariable(AppVeyor.COMMIT_MESSAGE)
},
Branch = variables.GetEnvironmentVariable(AppVeyor.COMMIT_BRANCH),
};

public Task<CoverallsData> ProvideCoverallsData() => Task.FromResult(new CoverallsData
{
CommitSha = variables.GetEnvironmentVariable(AppVeyor.COMMIT_ID),
CommitSha = variables.GetEnvironmentVariable(AppVeyor.PR_COMMIT_ID).NullIfEmpty() ?? variables.GetEnvironmentVariable(AppVeyor.COMMIT_ID),
ServiceBranch = variables.GetEnvironmentVariable(AppVeyor.COMMIT_BRANCH),
ServiceName = nameof(AppVeyor).ToLower(),
ServiceJobId = variables.GetEnvironmentVariable(AppVeyor.JOB_ID),
Expand Down
1 change: 1 addition & 0 deletions src/dotnet-coveralls/Publishing/CommandLineProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public Task<CoverallsData> ProvideCoverallsData() => Task.FromResult(new Coveral
ServiceBranch = options.CommitBranch,
ServiceBuildUrl = options.BuildUrl,
Parallel = options.Parallel ? (bool?)true : null,
RunAt = options.RunAt,
});

public Task<GitData> CreateGitData() => Task.FromResult(
Expand Down
1 change: 1 addition & 0 deletions src/dotnet-coveralls/Publishing/CoverallsDataBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ CoverallsData CombineData(CoverallsData accum, CoverallsData toAdd) =>
ServiceName = accum.ServiceName.NullIfEmpty() ?? toAdd.ServiceName.NullIfEmpty(),
ServiceNumber = accum.ServiceNumber.NullIfEmpty() ?? toAdd.ServiceNumber.NullIfEmpty(),
ServiceBuildUrl = accum.ServiceBuildUrl.NullIfEmpty() ?? toAdd.ServiceBuildUrl.NullIfEmpty(),
RunAt = accum.RunAt.NullIfEmpty() ?? toAdd.RunAt.NullIfEmpty(),
};
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/dotnet-coveralls/Publishing/CoverallsPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CoverallsPublisher
private readonly IOutputFileWriter fileWriter;
private readonly IFileProvider fileProvider;
private readonly IEnvironmentVariables environmentVariables;
private readonly ILogger<CoverallsPublisher> logger;
private readonly ILogger logger;
private const string CoverallsEndpoint = "https://coveralls.io/api/v1/jobs";

public CoverallsPublisher(
Expand All @@ -31,14 +31,14 @@ public CoverallsPublisher(
IOutputFileWriter fileWriter,
IFileProvider fileProvider,
IEnvironmentVariables environmentVariables,
ILoggerFactory loggerFactory)
ILogger logger)
{
this.options = options;
this.coverallsDataBuilder = coverallsDataBuilder;
this.fileWriter = fileWriter;
this.fileProvider = fileProvider;
this.environmentVariables = environmentVariables;
this.logger = loggerFactory.CreateLogger<CoverallsPublisher>();
this.logger = logger;
}

public async Task<int> Publish()
Expand Down
Loading

0 comments on commit 54000e9

Please sign in to comment.