hungernetbot 1.0.dev1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hungernetbot/__init__.py +15 -0
- hungernetbot/__main__.py +4 -0
- hungernetbot/bot.py +126 -0
- hungernetbot/commands/__init__.py +11 -0
- hungernetbot/commands/general.py +42 -0
- hungernetbot/commands/github.py +117 -0
- hungernetbot/commands/posts.py +76 -0
- hungernetbot/commands/server.py +231 -0
- hungernetbot/config.py +81 -0
- hungernetbot/database.py +164 -0
- hungernetbot/embeds.py +31 -0
- hungernetbot/emojis.py +160 -0
- hungernetbot/github/__init__.py +3 -0
- hungernetbot/github/dispatcher.py +83 -0
- hungernetbot/github/events.py +246 -0
- hungernetbot/github/repo.py +16 -0
- hungernetbot/github/security.py +12 -0
- hungernetbot/github/server.py +59 -0
- hungernetbot/posts.py +253 -0
- hungernetbot/runner.py +15 -0
- hungernetbot/webhooks.py +126 -0
- hungernetbot-1.0.dev1.dist-info/METADATA +127 -0
- hungernetbot-1.0.dev1.dist-info/RECORD +27 -0
- hungernetbot-1.0.dev1.dist-info/WHEEL +5 -0
- hungernetbot-1.0.dev1.dist-info/entry_points.txt +2 -0
- hungernetbot-1.0.dev1.dist-info/licenses/LICENSE +674 -0
- hungernetbot-1.0.dev1.dist-info/top_level.txt +1 -0
hungernetbot/config.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass
|
|
7
|
+
class Settings:
|
|
8
|
+
discord_token: str
|
|
9
|
+
panel_url: str
|
|
10
|
+
panel_api_key: str
|
|
11
|
+
server_id: str
|
|
12
|
+
server_name: str
|
|
13
|
+
server_domain: str
|
|
14
|
+
server_port: int
|
|
15
|
+
server_staff_role_id: int
|
|
16
|
+
notification_channel_id: int
|
|
17
|
+
owner_id: int
|
|
18
|
+
bridge_url: str
|
|
19
|
+
bridge_token_id: str
|
|
20
|
+
bridge_token_secret: str
|
|
21
|
+
bridge_webhook_token: str
|
|
22
|
+
bridge_webhook_port: int
|
|
23
|
+
github_webhook_secret: str
|
|
24
|
+
webhook_host: str
|
|
25
|
+
github_webhook_port: int
|
|
26
|
+
database_path: Path
|
|
27
|
+
posts_directory: Path
|
|
28
|
+
dev_guild_ids: list[int]
|
|
29
|
+
|
|
30
|
+
@classmethod
|
|
31
|
+
def from_environment(cls):
|
|
32
|
+
required = {
|
|
33
|
+
'DISCORD_TOKEN': 'discord_token',
|
|
34
|
+
'PANEL_URL': 'panel_url',
|
|
35
|
+
'PANEL_API_KEY': 'panel_api_key',
|
|
36
|
+
'SERVER_ID': 'server_id',
|
|
37
|
+
'SERVER_STAFF_ROLE_ID': 'server_staff_role_id',
|
|
38
|
+
'NOTIFICATION_CHANNEL_ID': 'notification_channel_id',
|
|
39
|
+
'OWNER_ID': 'owner_id',
|
|
40
|
+
'BRIDGE_URL': 'bridge_url',
|
|
41
|
+
'BRIDGE_TOKEN_ID': 'bridge_token_id',
|
|
42
|
+
'BRIDGE_TOKEN_SECRET': 'bridge_token_secret',
|
|
43
|
+
'BRIDGE_WEBHOOK_TOKEN': 'bridge_webhook_token',
|
|
44
|
+
'GITHUB_WEBHOOK_SECRET': 'github_webhook_secret',
|
|
45
|
+
}
|
|
46
|
+
missing = [key for key in required if not os.environ.get(key)]
|
|
47
|
+
if missing:
|
|
48
|
+
names = ', '.join(missing)
|
|
49
|
+
raise ValueError(f'Missing required environment variables: {names}')
|
|
50
|
+
|
|
51
|
+
try:
|
|
52
|
+
dev_guild_ids = [
|
|
53
|
+
int(value.strip())
|
|
54
|
+
for value in os.environ.get('DEV_GUILD_IDS', '').split(',')
|
|
55
|
+
if value.strip()
|
|
56
|
+
]
|
|
57
|
+
values = {
|
|
58
|
+
name: os.environ[key]
|
|
59
|
+
for key, name in required.items()
|
|
60
|
+
}
|
|
61
|
+
values['server_staff_role_id'] = int(values['server_staff_role_id'])
|
|
62
|
+
values['notification_channel_id'] = int(values['notification_channel_id'])
|
|
63
|
+
values['owner_id'] = int(values['owner_id'])
|
|
64
|
+
values['server_name'] = os.environ.get('SERVER_NAME', 'Hunger SMP')
|
|
65
|
+
values['server_domain'] = os.environ.get('SERVER_DOMAIN', 'localhost')
|
|
66
|
+
values['server_port'] = int(os.environ.get('MINECRAFT_SERVER_PORT', '25565'))
|
|
67
|
+
values['bridge_webhook_port'] = int(os.environ.get('BRIDGE_WEBHOOK_PORT', '8081'))
|
|
68
|
+
values['webhook_host'] = os.environ.get('WEBHOOK_HOST', '0.0.0.0')
|
|
69
|
+
values['github_webhook_port'] = int(
|
|
70
|
+
os.environ.get('GITHUB_WEBHOOK_PORT', os.environ.get('SERVER_PORT', '8000'))
|
|
71
|
+
)
|
|
72
|
+
values['database_path'] = Path(os.environ.get('DATABASE_PATH', './data/hungernetbot.sqlite3'))
|
|
73
|
+
values['posts_directory'] = Path(os.environ.get('POSTS_DIRECTORY', './posts'))
|
|
74
|
+
values['dev_guild_ids'] = dev_guild_ids
|
|
75
|
+
except ValueError as exc:
|
|
76
|
+
raise ValueError(f'Invalid integer configuration value: {exc}') from exc
|
|
77
|
+
|
|
78
|
+
if values['github_webhook_port'] == values['bridge_webhook_port']:
|
|
79
|
+
raise ValueError('GITHUB_WEBHOOK_PORT and BRIDGE_WEBHOOK_PORT must be different.')
|
|
80
|
+
|
|
81
|
+
return cls(**values)
|
hungernetbot/database.py
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import aiosqlite
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
EVENT_CATEGORIES = ('issues', 'pulls', 'reviews', 'pushes', 'releases', 'ci')
|
|
7
|
+
|
|
8
|
+
_SCHEMA = '''
|
|
9
|
+
CREATE TABLE IF NOT EXISTS guilds (
|
|
10
|
+
guild_id INTEGER PRIMARY KEY,
|
|
11
|
+
default_channel_id INTEGER
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
CREATE TABLE IF NOT EXISTS linked_repos (
|
|
15
|
+
guild_id INTEGER NOT NULL,
|
|
16
|
+
owner TEXT NOT NULL,
|
|
17
|
+
name TEXT NOT NULL,
|
|
18
|
+
channel_id INTEGER,
|
|
19
|
+
PRIMARY KEY (guild_id, owner, name)
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
CREATE INDEX IF NOT EXISTS linked_repos_by_repository
|
|
23
|
+
ON linked_repos (owner, name);
|
|
24
|
+
|
|
25
|
+
CREATE TABLE IF NOT EXISTS channel_routes (
|
|
26
|
+
guild_id INTEGER NOT NULL,
|
|
27
|
+
owner TEXT NOT NULL,
|
|
28
|
+
name TEXT NOT NULL,
|
|
29
|
+
event_type TEXT NOT NULL,
|
|
30
|
+
channel_id INTEGER NOT NULL,
|
|
31
|
+
PRIMARY KEY (guild_id, owner, name, event_type)
|
|
32
|
+
);
|
|
33
|
+
'''
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass
|
|
37
|
+
class LinkedRepo:
|
|
38
|
+
guild_id: int
|
|
39
|
+
owner: str
|
|
40
|
+
name: str
|
|
41
|
+
channel_id: int
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def full_name(self):
|
|
45
|
+
return f'{self.owner}/{self.name}'
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class Database:
|
|
49
|
+
def __init__(self, path):
|
|
50
|
+
self.path = Path(path)
|
|
51
|
+
self.connection = None
|
|
52
|
+
|
|
53
|
+
async def connect(self):
|
|
54
|
+
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
55
|
+
self.connection = await aiosqlite.connect(self.path)
|
|
56
|
+
self.connection.row_factory = aiosqlite.Row
|
|
57
|
+
await self.connection.executescript(_SCHEMA)
|
|
58
|
+
await self.connection.commit()
|
|
59
|
+
|
|
60
|
+
async def close(self):
|
|
61
|
+
if self.connection is not None:
|
|
62
|
+
await self.connection.close()
|
|
63
|
+
self.connection = None
|
|
64
|
+
|
|
65
|
+
def _conn(self):
|
|
66
|
+
if self.connection is None:
|
|
67
|
+
raise RuntimeError('Database.connect() must be called first.')
|
|
68
|
+
return self.connection
|
|
69
|
+
|
|
70
|
+
async def link_repo(self, guild_id, owner, name, channel_id=None):
|
|
71
|
+
connection = self._conn()
|
|
72
|
+
await connection.execute(
|
|
73
|
+
'''
|
|
74
|
+
INSERT INTO guilds (guild_id, default_channel_id)
|
|
75
|
+
VALUES (?, ?)
|
|
76
|
+
ON CONFLICT(guild_id) DO UPDATE SET
|
|
77
|
+
default_channel_id = COALESCE(excluded.default_channel_id, guilds.default_channel_id)
|
|
78
|
+
''',
|
|
79
|
+
(guild_id, channel_id),
|
|
80
|
+
)
|
|
81
|
+
await connection.execute(
|
|
82
|
+
'''
|
|
83
|
+
INSERT INTO linked_repos (guild_id, owner, name, channel_id)
|
|
84
|
+
VALUES (?, ?, ?, ?)
|
|
85
|
+
ON CONFLICT(guild_id, owner, name) DO UPDATE SET
|
|
86
|
+
channel_id = COALESCE(excluded.channel_id, linked_repos.channel_id)
|
|
87
|
+
''',
|
|
88
|
+
(guild_id, owner.lower(), name.lower(), channel_id),
|
|
89
|
+
)
|
|
90
|
+
await connection.commit()
|
|
91
|
+
|
|
92
|
+
async def unlink_repo(self, guild_id, owner, name):
|
|
93
|
+
connection = self._conn()
|
|
94
|
+
cursor = await connection.execute(
|
|
95
|
+
'DELETE FROM linked_repos WHERE guild_id = ? AND owner = ? AND name = ?',
|
|
96
|
+
(guild_id, owner.lower(), name.lower()),
|
|
97
|
+
)
|
|
98
|
+
await connection.execute(
|
|
99
|
+
'DELETE FROM channel_routes WHERE guild_id = ? AND owner = ? AND name = ?',
|
|
100
|
+
(guild_id, owner.lower(), name.lower()),
|
|
101
|
+
)
|
|
102
|
+
await connection.commit()
|
|
103
|
+
return cursor.rowcount > 0
|
|
104
|
+
|
|
105
|
+
async def list_repos(self, guild_id):
|
|
106
|
+
async with self._conn().execute(
|
|
107
|
+
'SELECT guild_id, owner, name, channel_id FROM linked_repos '
|
|
108
|
+
'WHERE guild_id = ? ORDER BY owner, name',
|
|
109
|
+
(guild_id,),
|
|
110
|
+
) as cursor:
|
|
111
|
+
rows = await cursor.fetchall()
|
|
112
|
+
return [LinkedRepo(**dict(row)) for row in rows]
|
|
113
|
+
|
|
114
|
+
async def find_repo_links(self, owner, name):
|
|
115
|
+
async with self._conn().execute(
|
|
116
|
+
'SELECT guild_id, owner, name, channel_id FROM linked_repos '
|
|
117
|
+
'WHERE owner = ? AND name = ?',
|
|
118
|
+
(owner.lower(), name.lower()),
|
|
119
|
+
) as cursor:
|
|
120
|
+
rows = await cursor.fetchall()
|
|
121
|
+
return [LinkedRepo(**dict(row)) for row in rows]
|
|
122
|
+
|
|
123
|
+
async def set_channel_route(self, guild_id, owner, name, event_type, channel_id):
|
|
124
|
+
if event_type not in EVENT_CATEGORIES:
|
|
125
|
+
raise ValueError(f'Unknown GitHub event category: {event_type}')
|
|
126
|
+
connection = self._conn()
|
|
127
|
+
await connection.execute(
|
|
128
|
+
'''
|
|
129
|
+
INSERT INTO linked_repos (guild_id, owner, name)
|
|
130
|
+
VALUES (?, ?, ?)
|
|
131
|
+
ON CONFLICT(guild_id, owner, name) DO NOTHING
|
|
132
|
+
''',
|
|
133
|
+
(guild_id, owner.lower(), name.lower()),
|
|
134
|
+
)
|
|
135
|
+
await connection.execute(
|
|
136
|
+
'''
|
|
137
|
+
INSERT INTO channel_routes (guild_id, owner, name, event_type, channel_id)
|
|
138
|
+
VALUES (?, ?, ?, ?, ?)
|
|
139
|
+
ON CONFLICT(guild_id, owner, name, event_type) DO UPDATE SET
|
|
140
|
+
channel_id = excluded.channel_id
|
|
141
|
+
''',
|
|
142
|
+
(guild_id, owner.lower(), name.lower(), event_type, channel_id),
|
|
143
|
+
)
|
|
144
|
+
await connection.commit()
|
|
145
|
+
|
|
146
|
+
async def resolve_target_channel(self, link, event_type):
|
|
147
|
+
async with self._conn().execute(
|
|
148
|
+
'''
|
|
149
|
+
SELECT channel_id FROM channel_routes
|
|
150
|
+
WHERE guild_id = ? AND owner = ? AND name = ? AND event_type = ?
|
|
151
|
+
''',
|
|
152
|
+
(link.guild_id, link.owner, link.name, event_type),
|
|
153
|
+
) as cursor:
|
|
154
|
+
route = await cursor.fetchone()
|
|
155
|
+
if route is not None:
|
|
156
|
+
return route['channel_id']
|
|
157
|
+
if link.channel_id is not None:
|
|
158
|
+
return link.channel_id
|
|
159
|
+
async with self._conn().execute(
|
|
160
|
+
'SELECT default_channel_id FROM guilds WHERE guild_id = ?',
|
|
161
|
+
(link.guild_id,),
|
|
162
|
+
) as cursor:
|
|
163
|
+
guild = await cursor.fetchone()
|
|
164
|
+
return guild['default_channel_id'] if guild else None
|
hungernetbot/embeds.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import discord
|
|
2
|
+
from mapres import res
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
HUNGERNET_COLOR = 0xC82828
|
|
6
|
+
HUNGERNET_FOOTER = 'HungerNet · Automated system'
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def branded_embed(title, fields=None, description=None, color=HUNGERNET_COLOR):
|
|
10
|
+
embed = discord.Embed(title=res(title), description=description, color=color)
|
|
11
|
+
if fields:
|
|
12
|
+
for name, value in fields.items():
|
|
13
|
+
inline = False
|
|
14
|
+
if isinstance(value, tuple):
|
|
15
|
+
value, inline = value
|
|
16
|
+
if isinstance(value, str):
|
|
17
|
+
value = res(value)
|
|
18
|
+
embed.add_field(name=name, value=value or '\u200b', inline=inline)
|
|
19
|
+
embed.set_footer(text=HUNGERNET_FOOTER)
|
|
20
|
+
return embed
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def status_emoji(online):
|
|
24
|
+
return res(':status_online:') if online else res(':status_offline:')
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def truncate(value, limit):
|
|
28
|
+
value = str(value)
|
|
29
|
+
if len(value) <= limit:
|
|
30
|
+
return value
|
|
31
|
+
return value[:limit - 1] + '…'
|
hungernetbot/emojis.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
from mapres import datamap
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
@datamap.colons
|
|
5
|
+
class EmojiMap:
|
|
6
|
+
status_online: str = '<:power_green:1529935734193262793>'
|
|
7
|
+
status_offline: str = '<:power_red:1529935731613630575>'
|
|
8
|
+
status_warning: str = '<:triangle_alert_yellow:1529935783316685011>'
|
|
9
|
+
players: str = '<:users_blue:1529935809325830174>'
|
|
10
|
+
terminal: str = '<:terminal_blue:1529935761665953842>'
|
|
11
|
+
statistics: str = '<:stats_blue:1529935766808035339>'
|
|
12
|
+
start: str = '<:start_button:1529935725749862504>'
|
|
13
|
+
restart: str = '<:restart_button:1529935483893842075>'
|
|
14
|
+
stop: str = '<:stop_button:1529935758721286277>'
|
|
15
|
+
kill: str = '<:zap_blue:1529935832981569749>'
|
|
16
|
+
success: str = '<:check:1529941459006918697>'
|
|
17
|
+
failure: str = '<:x_red:1529935822344687787>'
|
|
18
|
+
issue: str = '<:info_red:1529935477359120474>'
|
|
19
|
+
pull_request: str = '<:git_pull_request:1541978867592339506>'
|
|
20
|
+
review: str = '<:git_review:1541978866099163216>'
|
|
21
|
+
push: str = '<:git_package:1541978852794830969>'
|
|
22
|
+
release: str = '<:git_rocket:1541978853910646894>'
|
|
23
|
+
workflow: str = '<:activity_blue:1529935527883702487>'
|
|
24
|
+
|
|
25
|
+
activity_blue: str = '<:activity_blue:1529935527883702487>'
|
|
26
|
+
activity_white: str = '<:activity_white:1529935530899538162>'
|
|
27
|
+
arrow_left_red: str = '<:arrow_left_red:1529935440877191379>'
|
|
28
|
+
arrow_right: str = '<:arrow_right:1529935538906337300>'
|
|
29
|
+
ban: str = '<:ban:1529935553372623130>'
|
|
30
|
+
bell: str = '<:bell:1529935557507944508>'
|
|
31
|
+
binary: str = '<:binary:1529935562251833446>'
|
|
32
|
+
blocks_red: str = '<:blocks_red:1529935585295335517>'
|
|
33
|
+
bolt_blue: str = '<:bolt_blue:1529941469639741693>'
|
|
34
|
+
bolt_white: str = '<:bolt_white:1529941461473431724>'
|
|
35
|
+
boxes_red: str = '<:boxes_red:1529935588185215048>'
|
|
36
|
+
check: str = '<:check:1529941459006918697>'
|
|
37
|
+
circle_alert: str = '<:circle_alert:1529941478015504546>'
|
|
38
|
+
circle_check: str = '<:circle_check:1529941481610023012>'
|
|
39
|
+
circle_down: str = '<:circle_down:1529935608133452018>'
|
|
40
|
+
circle_plus_red: str = '<:circle_plus_red:1529935616870187168>'
|
|
41
|
+
circle_power_green: str = '<:circle_power_green:1529935625657127103>'
|
|
42
|
+
circle_power_red: str = '<:circle_power_red:1529935619046772977>'
|
|
43
|
+
circle_power_yellow: str = '<:circle_power_yellow:1529941488891461683>'
|
|
44
|
+
circle_question_red: str = '<:circle_question_red:1529935631634141215>'
|
|
45
|
+
circle_question_yellow: str = '<:circle_question_yellow:1529935635144507503>'
|
|
46
|
+
circle_up: str = '<:circle_up:1529935614907256994>'
|
|
47
|
+
cloud_alert: str = '<:cloud_alert:1529935459134996571>'
|
|
48
|
+
cloud_alert_yellow: str = '<:cloud_alert_yellow:1529935652999790784>'
|
|
49
|
+
cloud_blue: str = '<:cloud_blue:1529935455280435320>'
|
|
50
|
+
cloud_check: str = '<:cloud_check:1529935655768162496>'
|
|
51
|
+
cloud_download: str = '<:cloud_download:1529935659303698572>'
|
|
52
|
+
cloud_green: str = '<:cloud_green:1529935645013967028>'
|
|
53
|
+
cloud_red: str = '<:cloud_red:1529935647865962596>'
|
|
54
|
+
cloud_sync: str = '<:cloud_sync:1529935661749243995>'
|
|
55
|
+
cloud_upload: str = '<:cloud_upload:1529935663959511050>'
|
|
56
|
+
codexml_blue: str = '<:codexml_blue:1529935461299126292>'
|
|
57
|
+
codexml_white: str = '<:codexml_white:1529935672327143424>'
|
|
58
|
+
code_blue: str = '<:code_blue:1529935670011887849>'
|
|
59
|
+
code_white: str = '<:code_white:1529935666614636636>'
|
|
60
|
+
clock_blue: str = '<:clock_blue:1529941496583684157>'
|
|
61
|
+
clock_white: str = '<:clock_white:1529941492876185842>'
|
|
62
|
+
connected_green: str = '<:connected_green:1529935811607265402>'
|
|
63
|
+
dashboard_red: str = '<:dashboard_red:1529935693311377700>'
|
|
64
|
+
down_red: str = '<:down_red:1529935592907997246>'
|
|
65
|
+
dot_red: str = '<:dot_red:1529935465384378559>'
|
|
66
|
+
edit: str = '<:edit:1529935492299362384>'
|
|
67
|
+
ethernet_green: str = '<:ethernet_green:1529935678744297522>'
|
|
68
|
+
file_question_red: str = '<:file_question_red:1529935473592635534>'
|
|
69
|
+
file_question_yellow: str = '<:file_question_yellow:1529935682125041724>'
|
|
70
|
+
flame: str = '<:flame:1529935685463572642>'
|
|
71
|
+
grid_red: str = '<:grid_red:1529935696423420045>'
|
|
72
|
+
home_buttom: str = '<:home_buttom:1529935688831864892>'
|
|
73
|
+
hungernet: str = '<:hungernet:1529225331742081104>'
|
|
74
|
+
info_red: str = '<:info_red:1529935477359120474>'
|
|
75
|
+
layers_red: str = '<:layers_red:1529935691155505283>'
|
|
76
|
+
left_red: str = '<:left_red:1529935595626037298>'
|
|
77
|
+
list_blue: str = '<:list_blue:1529935701981008055>'
|
|
78
|
+
logout: str = '<:logout:1529935482048348191>'
|
|
79
|
+
mail_red: str = '<:mail_red:1529935705973723196>'
|
|
80
|
+
network_green: str = '<:network_green:1529935711547953393>'
|
|
81
|
+
package_red: str = '<:package_red:1529935714492485632>'
|
|
82
|
+
power_green: str = '<:power_green:1529935734193262793>'
|
|
83
|
+
power_red: str = '<:power_red:1529935731613630575>'
|
|
84
|
+
puzzle_blue: str = '<:puzzle_blue:1529941501004611614>'
|
|
85
|
+
puzzle_green: str = '<:puzzle_green:1529941512820101200>'
|
|
86
|
+
puzzle_red: str = '<:puzzle_red:1529941506620653788>'
|
|
87
|
+
puzzle_yellow: str = '<:puzzle_yellow:1529941519056764928>'
|
|
88
|
+
restart_button: str = '<:restart_button:1529935483893842075>'
|
|
89
|
+
right_red: str = '<:right_red:1529935599497379880>'
|
|
90
|
+
schedule_check: str = '<:schedule_check:1529935451031343185>'
|
|
91
|
+
search_red: str = '<:search_red:1529935737401643168>'
|
|
92
|
+
server_blue: str = '<:server_blue:1529935748101574718>'
|
|
93
|
+
server_green: str = '<:server_green:1529935742418292858>'
|
|
94
|
+
server_red: str = '<:server_red:1529935739733934300>'
|
|
95
|
+
settings_red: str = '<:settings_red:1529935752308199474>'
|
|
96
|
+
share_red: str = '<:share_red:1529935754447556718>'
|
|
97
|
+
shield_alert: str = '<:shield_alert:1529941536287096852>'
|
|
98
|
+
shield_alert_red: str = '<:shield_alert_red:1529935490252279889>'
|
|
99
|
+
shield_check: str = '<:shield_check:1529941539957244126>'
|
|
100
|
+
shield_question: str = '<:shield_question:1529941542603722772>'
|
|
101
|
+
shield_x: str = '<:shield_x:1529941546370076784>'
|
|
102
|
+
signal_excellent: str = '<:signal_excellent:1529279696049537164>'
|
|
103
|
+
signal_fair: str = '<:signal_fair:1529279700109754379>'
|
|
104
|
+
signal_good: str = '<:signal_good:1529279697244913724>'
|
|
105
|
+
signal_poor: str = '<:signal_poor:1529279698914508870>'
|
|
106
|
+
signal_red: str = '<:signal_red:1529935486338990220>'
|
|
107
|
+
stats_blue: str = '<:stats_blue:1529935766808035339>'
|
|
108
|
+
stats_green: str = '<:stats_green:1529935773082583081>'
|
|
109
|
+
stats_white: str = '<:stats_white:1529935765008552008>'
|
|
110
|
+
stop_button: str = '<:stop_button:1529935758721286277>'
|
|
111
|
+
terminal_blue: str = '<:terminal_blue:1529935761665953842>'
|
|
112
|
+
terminal_white: str = '<:terminal_white:1529935498548609094>'
|
|
113
|
+
toggle_off: str = '<:toggle_off:1529941551478865940>'
|
|
114
|
+
toggle_on: str = '<:toggle_on:1529941553999773928>'
|
|
115
|
+
trash: str = '<:trash:1529935502428602450>'
|
|
116
|
+
triangle_alert_red: str = '<:triangle_alert_red:1529935779290152980>'
|
|
117
|
+
triangle_alert_yellow: str = '<:triangle_alert_yellow:1529935783316685011>'
|
|
118
|
+
undo_blue: str = '<:undo_blue:1529935509584089270>'
|
|
119
|
+
undo_white: str = '<:undo_white:1529935788932993064>'
|
|
120
|
+
unplug: str = '<:unplug:1529935514252083451>'
|
|
121
|
+
up_green: str = '<:up_green:1529935605176336444>'
|
|
122
|
+
up_red: str = '<:up_red:1529935602399842314>'
|
|
123
|
+
user_check: str = '<:user_check:1529935797552414943>'
|
|
124
|
+
user_circle_red: str = '<:user_circle_red:1529935642342195392>'
|
|
125
|
+
user_join: str = '<:user_join:1529941555945930952>'
|
|
126
|
+
user_leave: str = '<:user_leave:1529941559405973675>'
|
|
127
|
+
user_minus: str = '<:user_minus:1529935801339744266>'
|
|
128
|
+
user_plus: str = '<:user_plus:1529935806826025060>'
|
|
129
|
+
user_x_blue: str = '<:user_x_blue:1529935522338701413>'
|
|
130
|
+
user_x_red: str = '<:user_x_red:1529935520065388717>'
|
|
131
|
+
userround1: str = '<:userround1:1529935791759823070>'
|
|
132
|
+
userround2: str = '<:userround2:1529935794645766164>'
|
|
133
|
+
users_blue: str = '<:users_blue:1529935809325830174>'
|
|
134
|
+
wave: str = '<:wave:1529935545415766016>'
|
|
135
|
+
wrench_red: str = '<:wrench_red:1529935819261870212>'
|
|
136
|
+
x_red: str = '<:x_red:1529935822344687787>'
|
|
137
|
+
zap_blue: str = '<:zap_blue:1529935832981569749>'
|
|
138
|
+
zap_green: str = '<:zap_green:1529935830020526273>'
|
|
139
|
+
zap_red: str = '<:zap_red:1529935525698601021>'
|
|
140
|
+
git_commit: str = '<:git_commit:1541978873162506371>'
|
|
141
|
+
git_draft: str = '<:git_draft:1541978870830600273>'
|
|
142
|
+
git_pull_request: str = '<:git_pull_request:1541978867592339506>'
|
|
143
|
+
git_review: str = '<:git_review:1541978866099163216>'
|
|
144
|
+
git_merge: str = '<:git_merge:1541978864698531910>'
|
|
145
|
+
git_rocket: str = '<:git_rocket:1541978853910646894>'
|
|
146
|
+
git_package: str = '<:git_package:1541978852794830969>'
|
|
147
|
+
|
|
148
|
+
git_merge_conflict: str = '<:git_merge_conflict:1541978863192776785>'
|
|
149
|
+
git_pull_request_closed: str = '<:git_pull_request_closed:1541978861842075658>'
|
|
150
|
+
git_pull_request_draft: str = '<:git_pull_request_draft:1541978860667805727>'
|
|
151
|
+
git_pull_request_create: str = '<:git_pull_request_create:1541978859602321509>'
|
|
152
|
+
git_branch: str = '<:git_branch:1541978858511663144>'
|
|
153
|
+
git_branch_plus: str = '<:git_branch_plus:1541978857354039316>'
|
|
154
|
+
git_branch_minus: str = '<:git_branch_minus:1541978856058265631>'
|
|
155
|
+
start_button: str = '<:start_button:1529935725749862504>'
|
|
156
|
+
|
|
157
|
+
git_graph: str = '<:git_graph:1541978854959226940>'
|
|
158
|
+
git_fork: str = '<:git_fork:1541978850458607686>'
|
|
159
|
+
git_compare: str = '<:git_compare:1541978848307191829>'
|
|
160
|
+
git_binary_blob: str = '<:git_binary_blob:1541978327211049040>'
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
import discord
|
|
4
|
+
|
|
5
|
+
from .events import build_event_embed
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class GitHubDispatcher:
|
|
12
|
+
def __init__(self, bot, database, owner_id):
|
|
13
|
+
self.bot = bot
|
|
14
|
+
self.database = database
|
|
15
|
+
self.owner_id = owner_id
|
|
16
|
+
|
|
17
|
+
async def handle(self, event, payload):
|
|
18
|
+
if event == 'ping':
|
|
19
|
+
logger.info(
|
|
20
|
+
'Received a GitHub ping for %s.',
|
|
21
|
+
(payload.get('repository') or {}).get('full_name', 'unknown repository'),
|
|
22
|
+
)
|
|
23
|
+
return 0
|
|
24
|
+
|
|
25
|
+
repo = payload.get('repository') or {}
|
|
26
|
+
owner = (repo.get('owner') or {}).get('login')
|
|
27
|
+
name = repo.get('name')
|
|
28
|
+
if not owner or not name:
|
|
29
|
+
logger.warning('Skipping GitHub event %s without repository details.', event)
|
|
30
|
+
return 0
|
|
31
|
+
|
|
32
|
+
category, embed = build_event_embed(event, payload, self.owner_id)
|
|
33
|
+
if category is None or embed is None:
|
|
34
|
+
return 0
|
|
35
|
+
|
|
36
|
+
links = await self.database.find_repo_links(owner, name)
|
|
37
|
+
delivered = 0
|
|
38
|
+
for link in links:
|
|
39
|
+
try:
|
|
40
|
+
if await self._send_to_link(link, category, embed, event, payload):
|
|
41
|
+
delivered += 1
|
|
42
|
+
except Exception:
|
|
43
|
+
logger.exception(
|
|
44
|
+
'Failed sending GitHub event %s for %s/%s to guild %s.',
|
|
45
|
+
event,
|
|
46
|
+
owner,
|
|
47
|
+
name,
|
|
48
|
+
link.guild_id,
|
|
49
|
+
)
|
|
50
|
+
return delivered
|
|
51
|
+
|
|
52
|
+
async def _send_to_link(self, link, category, embed, event, payload):
|
|
53
|
+
channel_id = await self.database.resolve_target_channel(link, category)
|
|
54
|
+
if channel_id is None:
|
|
55
|
+
return False
|
|
56
|
+
|
|
57
|
+
channel = self.bot.get_channel(channel_id)
|
|
58
|
+
if channel is None:
|
|
59
|
+
try:
|
|
60
|
+
channel = await self.bot.fetch_channel(channel_id)
|
|
61
|
+
except (discord.NotFound, discord.Forbidden):
|
|
62
|
+
logger.warning('Cannot access GitHub destination channel %s.', channel_id)
|
|
63
|
+
return False
|
|
64
|
+
|
|
65
|
+
if not isinstance(channel, discord.abc.Messageable):
|
|
66
|
+
return False
|
|
67
|
+
|
|
68
|
+
failed_workflow = (
|
|
69
|
+
event == 'workflow_run'
|
|
70
|
+
and (payload.get('workflow_run') or {}).get('conclusion') != 'success'
|
|
71
|
+
)
|
|
72
|
+
mentions = discord.AllowedMentions(
|
|
73
|
+
users=[discord.Object(id=self.owner_id)] if failed_workflow else False,
|
|
74
|
+
roles=False,
|
|
75
|
+
everyone=False,
|
|
76
|
+
replied_user=False,
|
|
77
|
+
)
|
|
78
|
+
try:
|
|
79
|
+
await channel.send(embed=embed, allowed_mentions=mentions)
|
|
80
|
+
return True
|
|
81
|
+
except (discord.Forbidden, discord.HTTPException):
|
|
82
|
+
logger.exception('Discord rejected GitHub event delivery to channel %s.', channel_id)
|
|
83
|
+
return False
|