-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionActivator.cs
More file actions
83 lines (70 loc) · 2.44 KB
/
Copy pathActionActivator.cs
File metadata and controls
83 lines (70 loc) · 2.44 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
#:property Nullable=disable
#:property PublishAot=false
using System.Net;
using System.Text.Json;
Conf _conf = Deserialize<Conf>(GetEnvValue("ActionActivator_Conf".ToUpper()));
HttpClient _scClient = new HttpClient();
Console.WriteLine("ActionActivator开始运行...");
foreach (Activator act in _conf.Acts)
{
string owner = act.User;
Console.WriteLine($"共 {_conf.Acts.Length} 个账号,正在运行{owner}...");
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", owner);
client.DefaultRequestHeaders.Add("Authorization", $"token {act.Token}");
foreach (Repo repo in act.Repos)
{
string badInfo = null;
try
{
var httpResponseMessage = await client.PutAsync($"https://api.github.com/repos/{owner}/{repo.Name}/actions/workflows/{repo.WorkflowFileName}/enable", null);
if (httpResponseMessage.StatusCode != HttpStatusCode.NoContent)
{//请求失败
badInfo = $"请求失败. code: {httpResponseMessage.StatusCode}, msg: {await httpResponseMessage.Content.ReadAsStringAsync()}";
}
}
catch (Exception ex)
{
badInfo = $"ex: {ex.Message}";
}
await Notify($"{repo.Name}...{badInfo ?? "ok"}", badInfo != null);
}
}
Console.WriteLine("ActionActivator运行完毕");
async Task Notify(string msg, bool isFailed = false)
{
Console.WriteLine(msg);
if (_conf.ScType == "Always" || (isFailed && _conf.ScType == "Failed"))
{
await _scClient?.GetAsync($"https://sctapi.ftqq.com/{_conf.ScKey}.send?title=ActionActivator-{(isFailed ? "bad" : "ok")}&desp={msg}");
}
}
string GetEnvValue(string key) => Environment.GetEnvironmentVariable(key);
public partial class Program
{
static readonly JsonSerializerOptions _options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
ReadCommentHandling = JsonCommentHandling.Skip
};
static T Deserialize<T>(string json) => JsonSerializer.Deserialize<T>(json, _options);
}
#region Conf
class Conf
{
public string ScKey { get; set; }
public string ScType { get; set; }
public Activator[] Acts { get; set; }
}
class Activator
{
public string User { get; set; }
public string Token { get; set; }
public Repo[] Repos { get; set; }
}
class Repo
{
public string Name { get; set; }
public string WorkflowFileName { get; set; } = "main.yml";
}
#endregion