This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathgen_notif.py
executable file
·184 lines (150 loc) · 5.97 KB
/
gen_notif.py
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
#!/bin/python
"""
Does rudimentary JSON diffing between two git versions of the manifest.json file,
and on new mod versions prints a GHA output for a discord webhook message's JSON.
"""
# pylint: disable=redefined-outer-name,broad-except
import json
import datetime
from os import environ
from copy import deepcopy
from typing import Any
import util
REF_BASE = util.exec_shell(f"git rev-parse {environ.get('REF_BASE') or 'HEAD^1'}")
REF_NEW = util.exec_shell(f"git rev-parse {environ.get('REF_NEW') or 'HEAD'}")
# The JSON manifests.
OLD_MANIFEST: dict[str, Any] = json.loads(util.exec_shell(f"git show {REF_BASE}:manifest.json"))
NEW_MANIFEST: dict[str, Any] = json.loads(util.exec_shell(f"git show {REF_NEW}:manifest.json"))
EMBEDS = []
BASE_EMBED: dict[str, Any] = {
"footer": {
"icon_url": "https://avatars.githubusercontent.com/u/101987083?s=200&v=4"
},
"timestamp": datetime.datetime.utcnow().isoformat(),
"color": "000000",
"fields": []
}
def mod_to_embed(mod: dict[str, Any]) -> dict[str, Any]:
"""
Create discord embed JSON from a mod and it's first release
"""
embed: dict[str, Any] = deepcopy(BASE_EMBED)
embed['title'] = "[" + mod['name'] + "/" + str(mod["versions"][0]["id"]) + "]"
embed['description'] = mod['description']
embed['footer']['text'] = f"{mod['guid']}"
if 'color' in mod:
embed['color'] = int(mod['color'], 16)
if 'releaseUrl' in mod['versions'][0]:
embed['url'] = mod['versions'][0]['releaseUrl']
embed['fields'].append({
"name": "Category",
"value": mod['category'],
"inline": True
})
if len(mod['authors']) > 0:
author_names = iter(mod['authors'])
first_author_name = next(author_names)
embed['author'] = {
"name": first_author_name
}
if 'url' in mod['authors'][first_author_name]:
embed['author']['url'] = mod['authors'][first_author_name]['url']
if 'iconUrl' in mod['authors'][first_author_name]:
embed['author']['icon_url'] = mod['authors'][first_author_name]['iconUrl']
additional_authors: list[str] = []
for author_name in author_names:
if 'url' in mod['authors'][author_name]:
author_link = f"[{author_name}]({mod['authors'][author_name]['url']})"
additional_authors.append(author_link)
else:
additional_authors.append(author_name)
if len(additional_authors) > 0:
embed['fields'].append({
"name": "Additional authors",
"value": ", ".join(additional_authors),
"inline": True
})
flags: list[str] = []
if 'flags' in mod:
flags.extend(mod['flags'])
if 'flags' in mod['versions'][0]:
flags.extend(mod['versions'][0]['flags'])
if len(flags) > 0:
embed['fields'].append({
"name": "Flags",
"value": "`" + "`, `".join(flags) + "`",
"inline": True
})
if 'tags' in mod:
embed['fields'].append({
"name": "Tags",
"value": ", ".join(mod['tags']),
"inline": True
})
links: list[str] = []
if 'website' in mod:
links.append("[Website](" + mod['website'] + ")")
if 'sourceLocation' in mod:
links.append("[Source](" + mod['sourceLocation'] + ")")
if len(links) > 0:
embed['fields'].append({
"name": "URL(s)",
"value": ", ".join(links),
"inline": True
})
if 'conflicts' in mod['versions'][0]:
conflicts: [str] = []
for conflict_guid in mod['versions'][0]['conflicts']:
conflict_version = mod['versions'][0]['conflicts'][conflict_guid]['version']
conflicts.append(f"`{conflict_guid}`: {conflict_version}")
if len(conflicts) > 0:
embed['fields'].append({
"name": "Conflicts",
"value": "- " + "\n- ".join(conflicts),
})
if 'dependencies' in mod['versions'][0]:
dependencies: [str] = []
for dep_guid in mod['versions'][0]['dependencies']:
dep_version = mod['versions'][0]['dependencies'][dep_guid]['version']
dependencies.append(f"`{dep_guid}`: {dep_version}")
if len(dependencies) > 0:
embed['fields'].append({
"name": "Dependencies",
"value": "- " + "\n- ".join(dependencies),
})
if 'changelog' in mod['versions'][0]:
embed['fields'].append({
"name": "Changelog",
"value": mod['versions'][0]['changelog'],
})
return embed
# Iterate over all the (new) mods
for mod_guid in NEW_MANIFEST["mods"]:
mod: dict[str, Any] = NEW_MANIFEST["mods"][mod_guid]
# Maps the versions into filtered & validated ones
mod["versions"] = util.map_mod_versions(mod["versions"], mod_guid)
# Transfer only mods that should be shown to grouped_mods
if util.should_show_mod(mod):
# Sort the mod's versions
mod["versions"].sort(reverse=True, key=lambda version: version["id"])
IS_NEW_MOD = True
if mod_guid in OLD_MANIFEST["mods"]:
old_versions = OLD_MANIFEST["mods"][mod_guid]['versions']
old_versions = util.map_mod_versions(old_versions, mod_guid)
for version in old_versions:
if version['id'] == mod["versions"][0]['id']:
IS_NEW_MOD = False
break
if IS_NEW_MOD:
mod["guid"] = mod_guid
EMBEDS.append(mod_to_embed(mod))
if len(EMBEDS) > 0:
DISCORD_JSON = {
"content": "<@&1079268851952406571>",
"embeds": EMBEDS,
"allowed_mentions": { "roles": ["1079268851952406571"] },
"username": "NMG mod verifications",
"avatar_url": "https://avatars.githubusercontent.com/u/101987083",
"attachments": []
}
print("::set-output name=JSON::" + json.dumps(DISCORD_JSON).replace("\\","\\\\").replace("'", "'\\''"))