forked from coveralls-net/coveralls.net
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
104 lines (91 loc) · 3.36 KB
/
Program.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
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using CommandLine;
using Coveralls.Net.Properties;
using Newtonsoft.Json;
namespace Coveralls.Net
{
internal class Program
{
internal static void Main(string[] args)
{
try
{
var options = Parser.Default.ParseArguments<CommandLineOptions>(args).Value;
Run(options);
}
catch (Exception e)
{
Console.WriteLine(Resources.GenericError);
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
Environment.Exit(1);
}
}
internal static void Run(CommandLineOptions options)
{
if (options.DebugMode)
{
Console.WriteLine(Resources.RepoTokenDebug, Environment.GetEnvironmentVariable("COVERALLS_REPO_TOKEN"));
}
using (var coveralls = new CoverallsBootstrap(options))
{
coveralls.FileSystem = new LocalFileSystem();
if (!coveralls.CoverageFiles.Any())
{
Console.WriteLine(Resources.NoCoverageFilesErrorMessage);
return;
}
if (coveralls.RepoToken.IsBlank())
{
Console.WriteLine(Resources.BlankTokenErrorMessage);
if (coveralls.ServiceName == "appveyor")
{
Console.Write(Resources.AppVeyorBlankToken);
}
return;
}
foreach (var file in coveralls.CoverageFiles)
{
file.Path = file.Path.ToRelativePath(Directory.GetCurrentDirectory());
}
var coverallsData = coveralls.GetData();
var json = JsonConvert.SerializeObject(coverallsData);
SendToCoveralls(json);
if (options.DebugMode)
{
Console.WriteLine(Resources.CoverallsJsonHeader, JsonPrettyPrint(json));
}
}
}
internal static bool SendToCoveralls(string json)
{
// Send to coveralls.io
HttpResponseMessage response;
using (var client = new HttpClient())
{
using (var formData = new MultipartFormDataContent())
{
formData.Add(new StringContent(json), "json_file", "coverage.json");
response = client.PostAsync(@"https://coveralls.io/api/v1/jobs", formData).Result;
}
}
if (!response.IsSuccessStatusCode)
{
var msg = string.Format("Error {0} sending to coveralls.io: {1}",
response.StatusCode,
response.ReasonPhrase);
msg += "\n - Error code 422 indicate a problem with your token. Use the --debug option for more details.";
Console.WriteLine(msg);
}
return response.IsSuccessStatusCode;
}
internal static string JsonPrettyPrint(string json)
{
dynamic parsedJson = JsonConvert.DeserializeObject(json);
return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
}
}
}