-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
207 lines (188 loc) · 8.17 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using Mastonet;
using Octokit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
namespace MastodonGitHubBot;
public class Program
{
private static readonly string SettingsFilePath = Path.GetFullPath(Path.Join(AppContext.BaseDirectory, "appsettings.json"));
private static async Task Main()
{
using Log log = new();
try
{
await StartAsync(log);
}
catch (Exception e)
{
log.WriteFailure($"{e}: {e.Message}{Environment.NewLine}{e.StackTrace}");
throw;
}
}
private static async Task StartAsync(Log log)
{
Settings settings = Load(log);
HttpClient sharedHttpClient = new();
List<Publisher> publishers = new();
foreach (Server server in settings.Servers)
{
var publisher = await Publisher.CreateAsync(log, sharedHttpClient, settings, server);
publishers.Add(publisher);
}
log.WriteInfo($"Starting loop...");
while (true)
{
foreach (Publisher publisher in publishers)
{
await publisher.PublishAsync();
}
Flush(settings);
log.WriteInfo($"Sleeping for {settings.SleepSeconds} seconds...");
Thread.Sleep(TimeSpan.FromSeconds(settings.SleepSeconds));
}
}
private static void Flush(Settings settings)
{
JsonSerializerOptions options = new() { WriteIndented = true };
options.Converters.Add(new JsonStringEnumConverter());
string json = JsonSerializer.Serialize(settings, options);
File.WriteAllText(SettingsFilePath, json);
}
private static Settings Load(Log log)
{
ArgumentNullException.ThrowIfNull(log);
if (!File.Exists(SettingsFilePath))
{
throw new FileNotFoundException(SettingsFilePath);
}
log.WriteInfo($"Reading settings file: {SettingsFilePath}");
string json = File.ReadAllText(SettingsFilePath);
JsonSerializerOptions options = new() { WriteIndented = true };
options.Converters.Add(new JsonStringEnumConverter());
Settings settings = JsonSerializer.Deserialize<Settings>(json, options) ?? throw new JsonException($"Malformed {SettingsFilePath}.");
log.WriteInfo("Verifying that all settings values are filled out or prompting user if a value is missing...");
if (settings.SleepSeconds <= 0)
{
throw new ArgumentOutOfRangeException("The SleepSeconds value should be a positive number.", innerException: null);
}
log.WriteInfo("Debugging mode " + (settings.Debug ? "enabled. Won't" : "disabled. Will") + " publish to Mastodon.");
foreach (Server server in settings.Servers)
{
if (string.IsNullOrWhiteSpace(server.ServerName))
{
throw new JsonException("ServerName should not be empty.");
}
if (string.IsNullOrWhiteSpace(server.AppName))
{
server.AppName = AskString(server.ServerName, "name of the GitHub application reserved for the OAuth services. If you haven't created it, create it in https://github.com/settings/applications/new. After creating it, make sure to also store the client and the secret values");
}
if (string.IsNullOrWhiteSpace(server.GitHubClientId))
{
server.GitHubClientId = AskString(server.ServerName, "value of the GitHub OAuth application's ClientId. You should have gotten one after creating your application");
}
if (string.IsNullOrWhiteSpace(server.GitHubSecret))
{
server.GitHubSecret = AskString(server.ServerName, "value of the GitHub OAuth application's secret. You should have gotten one after creating your application");
}
if (string.IsNullOrWhiteSpace(server.GitHubUserName))
{
server.GitHubUserName = AskString(server.ServerName, "value of the GitHub account handle that will be used to execute requests");
}
if (string.IsNullOrWhiteSpace(server.GitHubRepoOrg))
{
server.GitHubRepoOrg = AskString(server.ServerName, "value of the GitHub org or user that owns the target repo");
}
if (string.IsNullOrWhiteSpace(server.GitHubRepoName))
{
server.GitHubRepoName = AskString(server.ServerName, "value of the target GitHub repo name");
}
if (string.IsNullOrWhiteSpace(server.MastodonServer))
{
server.MastodonServer = AskString(server.ServerName, "name of the Mastodon server that hosts your automated account");
}
if (string.IsNullOrWhiteSpace(server.GitHubAccessToken))
{
server.GitHubAccessToken = AskOptionalString(server.ServerName, "GitHub access token");
}
if (string.IsNullOrWhiteSpace(server.MastodonAccessToken))
{
server.GitHubAccessToken = AskOptionalString(server.ServerName, "Mastodon access token");
}
if (server.Visibility is < Mastonet.Visibility.Public or > Mastonet.Visibility.Direct)
{
throw new ArgumentOutOfRangeException($"{server.ServerName} - Visibility value is out of range: {server.Visibility}. Allowed numeric values are: 0 (Public), 1 (Unlisted), 2 (Private), 3 (Direct).", innerException: null);
}
}
return settings;
}
private static string AskString(string serverName, string description)
{
string? value = null;
while (string.IsNullOrWhiteSpace(value))
{
Console.Write($"{serverName} - Please provide the {description}: ");
value = Console.ReadLine();
if (string.IsNullOrWhiteSpace(value))
{
Console.WriteLine($"{serverName} - Incorrect value (null, empty or whitespace). Try again.");
}
else
{
string? answer = null;
while (string.IsNullOrWhiteSpace(answer))
{
Console.Write($"{serverName} - You typed [{value}]. Is this correct? [Y/N]: ");
answer = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(answer))
{
switch (answer)
{
case "n" or "N":
Console.WriteLine($"{serverName} - Ok, starting over.");
value = null;
break;
case "y" or "Y":
Console.WriteLine($"{serverName} - Will save [{value}] into the json file.");
break;
default:
Console.WriteLine($"{serverName} - Unexpected value. Asking again.");
answer = null;
break;
}
}
}
}
}
return value;
}
private static string AskOptionalString(string serverName, string description)
{
string value = string.Empty;
string? answer = null;
while (string.IsNullOrWhiteSpace(answer))
{
Console.Write($"{serverName} - Do you already have a {description}? [Y/N]: ");
answer = Console.ReadLine();
switch (answer)
{
case "n" or "N":
Console.WriteLine($"{serverName} - No problem. Will go through the authentication process.");
break;
case "y" or "Y":
value = AskString(serverName, description);
break;
default:
Console.WriteLine($"{serverName} - Unexpected value. Asking again.");
answer = null;
break;
}
}
return value;
}
}