-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
142 lines (133 loc) · 4.72 KB
/
Copy pathProgram.cs
File metadata and controls
142 lines (133 loc) · 4.72 KB
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
using System;
using Console = Log73.Console;
using System.IO;
using System.Linq;
using System.Text.Json;
using Log73;
using Log73.ExtensionMethod;
using Log73.Extensions;
using Octokit;
// configure logging
Console.Configure.UseNewtonsoftJson();
Console.Options.UseAnsi = false;
Console.Options.LogLevel = LogLevel.Debug;
var github = new GitHubClient(new ProductHeaderValue("GitHubMilestoneSync", "v1.1"))
{
Credentials = new Credentials(Environment.GetEnvironmentVariable("GITHUB_TOKEN"))
};
foreach (var config in JsonSerializer.Deserialize<Config[]>(
await File.ReadAllTextAsync(Environment.GetEnvironmentVariable("CONFIG_FILE") ?? "config.json"),
new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
})!)
{
foreach (var repoName in config.Repositories)
{
var repo = await github.Repository.Get(repoName.Split('/')[0], repoName.Split('/')[1]);
$"Milestones for {repoName}".Dump();
var milestones = await github.Issue.Milestone.GetAllForRepository(repo.Id, new MilestoneRequest()
{
// Only gives us Open by default
State = ItemStateFilter.All
});
if (!config.ExcludeFromMilestoneSync?.Contains(repoName) ?? true)
{
foreach (var mile in config.Milestones)
{
var match = milestones.FirstOrDefault(m => m.Title == mile.Title);
if (match != null)
{
"Has".DumpDebug();
if (match.State.StringValue != mile.State ||
match.Description != mile.Description)
{
"Differs, updating.".Dump();
await github.Issue.Milestone.Update(repo.Id, match.Number, new MilestoneUpdate()
{
Description = mile.Description,
State = StateFromString(mile.State)
});
}
}
else
{
"Doesn't have, creating.".DumpDebug();
await github.Issue.Milestone.Create(repo.Id, new NewMilestone(mile.Title)
{
Description = mile.Description,
State = StateFromString(mile.State)
});
}
}
}
else
{
$"Repo {repoName} is excluded from milestone sync.".DumpDebug();
}
$"Labels for {repoName}".Dump();
var labels = await github.Issue.Labels.GetAllForRepository(repo.Id);
if (!config.ExcludeFromLabelSync?.Contains(repoName) ?? true)
{
foreach (var label in config.Labels)
{
var match = labels.FirstOrDefault(l => l.Name == label.Name);
if (match != null)
{
"Has".DumpDebug();
if (match.Color != label.Color ||
match.Description != label.Description)
{
"Differs, updating.".DumpDebug();
await github.Issue.Labels.Update(repo.Id, label.Name, new LabelUpdate(label.Name, label.Color)
{
Description = label.Description
});
}
}
else
{
"Doesn't have, creating.".DumpDebug();
await github.Issue.Labels.Create(repo.Id, new NewLabel(label.Name, label.Color)
{
Description = label.Description
});
}
}
}
else
{
$"Repo {repoName} is excluded from label sync.".DumpDebug();
}
}
}
"Finished.".Dump();
var rateLimit = github.GetLastApiInfo().RateLimit;
$"Remaining {rateLimit.Remaining} of {rateLimit.Limit} rate limit.".Dump();
static ItemState StateFromString(string str)
=> str.ToLower() switch
{
"open" => ItemState.Open,
"closed" => ItemState.Closed,
_ => throw new Exception("Invalid state!")
};
class Config
{
public string[] Repositories { get; set; }
public string[]? ExcludeFromLabelSync { get; set; }
public string[]? ExcludeFromMilestoneSync { get; set; }
public Milestone[] Milestones { get; set; }
public Label[] Labels { get; set; }
}
class Milestone
{
public string Title { get; set; }
public string State { get; set; }
public string Description { get; set; }
}
class Label
{
public string Name { get; set; }
public string Color { get; set; }
public string Description { get; set; }
}