This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
/
Codecov.cs
147 lines (129 loc) · 5.62 KB
/
Codecov.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Codecov.Coverage.EnviornmentVariables;
using Codecov.Coverage.Report;
using Codecov.Coverage.SourceCode;
using Codecov.Coverage.Tool;
using Codecov.Factories;
using Codecov.Program;
using Codecov.Services.VersionControlSystems;
using Codecov.Upload;
using Codecov.Url;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Serilog;
using Serilog.Sinks.MSBuild;
namespace Codecov.MSBuild
{
/// <summary>
/// MSBuild task for uploading coverage reports to Codecov.
/// </summary>
public class Codecov : Task, IEnviornmentVariablesOptions, IHostOptions, IQueryOptions, IReportOptions, IVersionControlSystemOptions
{
/// <summary>
/// Gets or sets the report files to upload.
/// </summary>
[Required]
public ITaskItem[] ReportFiles { get; set; }
/// <summary>
/// Gets or sets a value specifying the environment variables to be included with this build.
/// </summary>
public string[] EnvironmentVariables { get; set; } = new string[0];
/// <summary>
/// Gets or sets a value used when not in git/hg project to identify project root directory.
/// </summary>
public string RepoRoot { get; set; }
/// <summary>
/// Gets or sets a value specifying the target url for Enterprise customers.
/// </summary>
public string Url { get; set; }
/// <summary>
/// Gets or sets a property specifying the branch name.
/// </summary>
public string Branch { get; set; }
/// <summary>
/// Gets or sets a property specifying the build number.
/// </summary>
public string Build { get; set; }
/// <summary>
/// Gets or sets a property specifying the commit sha.
/// </summary>
public string Commit { get; set; }
/// <summary>
/// Gets or sets a value specifying the flag the upload to group coverage metrics. See https://docs.codecov.io/docs/flags
/// </summary>
public string Flags { get; set; }
/// <summary>
/// Gets or sets a value specifying the custom defined name of the upload. Visible in Codecov UI.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets a value specifying the pull request number.
/// </summary>
public string Pr { get; set; }
/// <summary>
/// Gets or sets a value specifying the owner/repo slug used instead of the private repo token in Enterprise.
/// </summary>
public string Slug { get; set; }
/// <summary>
/// Gets or sets a value specifying the git tag.
/// </summary>
public string Tag { get; set; }
/// <summary>
/// Gets or sets a value specifying the private repository token.
/// </summary>
public string Token { get; set; }
/// <summary>
/// Gets or sets the features to toggle on/off.
/// </summary>
public string[] Features { get; set; } = new string[0];
/// <summary>
/// Gets or sets a value disabling the upload of the file network.
/// </summary>
public bool DisableNetwork { get; set; }
/// <summary>
/// Gets or sets the URL at which the uploaded report can be viewed.
/// </summary>
[Output]
public string ReportUrl { get; set; }
/// <summary>
/// Execute the task.
/// </summary>
/// <returns><c>true</c> if the task executed successfully; otherwise, <c>false</c>.</returns>
public override bool Execute()
{
Serilog.Log.Logger = new LoggerConfiguration()
.Enrich.WithProperty(MSBuildProperties.Subcategory, nameof(Codecov))
.WriteTo.MSBuild(this)
.CreateLogger();
try
{
var reportFiles = ReportFiles.Select(e => new ReportFile(e.ItemSpec, File.ReadAllText(e.ItemSpec)));
var coverage = new Coverage(reportFiles);
var envVars = new EnviornmentVariables(this);
var continuousIntegrationServer = ContinuousIntegrationServerFactory.Create(envVars);
envVars.LoadEnviornmentVariables(continuousIntegrationServer);
var versionControlSystem = VersionControlSystemFactory.Create(this, new Terminal.Terminal());
var sourceCode = new SourceCode(versionControlSystem);
var yaml = new Yaml.Yaml(sourceCode);
var repositories = RepositoryFactory.Create(versionControlSystem, continuousIntegrationServer);
var url = new Url.Url(new Host(this, envVars), new Route(), new Query(this, repositories, continuousIntegrationServer, yaml, envVars));
var report = new Report(this, envVars, sourceCode, coverage);
var upload = new Uploads(url, report, Features);
var uploadFacade = new UploadFacade(continuousIntegrationServer, versionControlSystem, yaml, coverage, envVars, url, upload);
uploadFacade.LogContinuousIntegrationAndVersionControlSystem();
ReportUrl = uploadFacade.UploadReports();
return true;
}
catch (Exception exception)
{
Log.LogError("The Codecov MSBuild task failed.");
Log.LogErrorFromException(exception, showStackTrace: true);
return false;
}
}
IEnumerable<string> IEnviornmentVariablesOptions.Envs => EnvironmentVariables;
}
}