-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
137 lines (112 loc) · 4.99 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
using LibGit2Sharp;
using RDHT_Backend.Models;
using System.Net;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text.Json;
namespace RDHT_Backend
{
internal class Program
{
#if RELEASE
static readonly string? PersonalToken = Environment.GetEnvironmentVariable("RDHT_TOKEN");
static readonly string? AuthUsername = Environment.GetEnvironmentVariable("RDHT_USER");
#endif
// https://stackoverflow.com/a/8714329
// git clone generates read only folders & files
static void ForceDeleteDirectory(string path)
{
var directory = new DirectoryInfo(path) { Attributes = FileAttributes.Normal };
foreach (var info in directory.GetFileSystemInfos("*", SearchOption.AllDirectories))
info.Attributes = FileAttributes.Normal;
directory.Delete(true);
}
static async Task<int> Main(string[] args)
{
Console.WriteLine($"RDHT-Backend {Assembly.GetExecutingAssembly().GetName().Version}");
#if RELEASE
if (string.IsNullOrEmpty(PersonalToken) || string.IsNullOrEmpty(AuthUsername))
{
Console.WriteLine("Please add the environment variables!");
return 1;
}
#endif
await Config.Fetch();
if (Directory.Exists(Globals.ClonePath))
{
Console.WriteLine("Deleting existing clone folder...");
ForceDeleteDirectory(Globals.ClonePath);
}
Console.WriteLine("Cloning...");
var gitPath = Repository.Clone($"http://github.com/{Globals.TrackerRepositoryPath}.git", Globals.ClonePath);
Console.WriteLine(gitPath);
var repo = new Repository(gitPath);
// get channel list
string channelsPath = Path.Combine(Globals.ClonePath, "Channels.json");
string channelsStr = await File.ReadAllTextAsync(channelsPath);
//string[] channelsList = new string[] { "ZIntegration", "ZCanary", "ZNext" }; // FOR TESTING
string[] channelsList = JsonSerializer.Deserialize<string[]>(channelsStr) ?? throw new Exception("Failed to deserialise channels list");
Queue<string> channels = new Queue<string>();
foreach (string channel in channelsList)
channels.Enqueue(channel);
Console.WriteLine("Starting!");
List<string> changed = new List<string>();
// start the workers
WorkerFactory factory = new WorkerFactory(channels, repo, changed);
List<Task> workers = new List<Task>();
for (int i = 1; i <= Config.Instance.WorkerCount; i++)
workers.Add(factory.Create());
// wait for workers to complete
Task.WaitAll(workers.ToArray());
// sort changed list
changed.Sort();
// delete channels removed from the list
foreach (var file in Directory.GetFiles(Globals.ClonePath, "*.txt"))
{
var fileName = Path.GetFileName(file);
var channel = Path.GetFileNameWithoutExtension(file);
if (!channelsList.Contains(channel))
{
Console.WriteLine($"Removing unused channel file `{file}` `{fileName}`");
changed.Add(channel);
File.Delete(file);
repo.Index.Remove(fileName);
}
}
#if RELEASE
try
{
var time = DateTimeOffset.Now;
var signature = new Signature("Roblox DeployHistory Bot", "rdhb@rdht.local", time);
var commit = repo.Commit($"{time.ToString("dd/MM/yyyy HH:mm:ss")} [{string.Join(", ", changed)}]", signature, signature);
Console.WriteLine("Committing!");
var remote = repo.Network.Remotes["origin"];
var options = new PushOptions
{
CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials { Username = AuthUsername, Password = PersonalToken }
};
var pushRefSpec = "refs/heads/main";
repo.Network.Push(remote, pushRefSpec, options);
}
catch (EmptyCommitException) // any better way?
{
Console.WriteLine("No changes");
}
#else
Console.WriteLine("Debug mode has committing disabled.");
Console.WriteLine("Changed:");
if (changed.Count == 0)
Console.WriteLine("Nothing!");
foreach (string channel in changed)
Console.WriteLine(channel);
Console.WriteLine();
Console.WriteLine("Repository index:");
if (repo.Index.Count == 0)
Console.WriteLine("Nothing... not supposed to be like that!");
foreach (var entry in repo.Index)
Console.WriteLine(entry.Path);
#endif
return 0;
}
}
}