forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-integration-docs-screenshot
executable file
·316 lines (264 loc) · 11.1 KB
/
generate-integration-docs-screenshot
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python3
"""Create or update a webhook integration screenshot using a test fixture."""
import argparse
import base64
import os
import subprocess
import sys
from typing import Any, Dict, Optional, Tuple
from urllib.parse import parse_qsl, urlencode
TOOLS_DIR = os.path.abspath(os.path.dirname(__file__))
ROOT_DIR = os.path.dirname(TOOLS_DIR)
sys.path.insert(0, ROOT_DIR)
# check for the venv
from tools.lib import sanity_check
sanity_check.check_venv(__file__)
from scripts.lib.setup_path import setup_path
setup_path()
os.environ["DJANGO_SETTINGS_MODULE"] = "zproject.settings"
import django
django.setup()
import orjson
import requests
import zulip
from scripts.lib.zulip_tools import BOLDRED, ENDC
from tools.lib.test_script import prepare_puppeteer_run
from zerver.actions.create_user import do_create_user, notify_created_bot
from zerver.actions.streams import bulk_add_subscriptions
from zerver.actions.user_settings import do_change_avatar_fields
from zerver.lib.integrations import (
DOC_SCREENSHOT_CONFIG,
INTEGRATIONS,
BaseScreenshotConfig,
Integration,
ScreenshotConfig,
WebhookIntegration,
get_fixture_and_image_paths,
split_fixture_path,
)
from zerver.lib.storage import static_path
from zerver.lib.streams import create_stream_if_needed
from zerver.lib.upload import upload_avatar_image
from zerver.lib.webhooks.common import get_fixture_http_headers
from zerver.models import Message, UserProfile, get_realm, get_user_by_delivery_email
def create_integration_bot(integration: Integration, bot_name: Optional[str] = None) -> UserProfile:
realm = get_realm("zulip")
owner = get_user_by_delivery_email("[email protected]", realm)
bot_email = f"{integration.name}[email protected]"
if bot_name is None:
bot_name = f"{integration.name.capitalize()} Bot"
try:
bot = UserProfile.objects.get(email=bot_email)
except UserProfile.DoesNotExist:
bot = do_create_user(
email=bot_email,
password="123",
realm=owner.realm,
full_name=bot_name,
bot_type=UserProfile.INCOMING_WEBHOOK_BOT,
bot_owner=owner,
acting_user=owner,
)
notify_created_bot(bot)
bot_avatar_path = integration.get_bot_avatar_path()
if bot_avatar_path is not None:
bot_avatar_path = static_path(bot_avatar_path)
if os.path.isfile(bot_avatar_path):
with open(bot_avatar_path, "rb") as f:
upload_avatar_image(f, owner, bot)
do_change_avatar_fields(bot, UserProfile.AVATAR_FROM_USER, acting_user=owner)
return bot
def create_integration_stream(integration: Integration, bot: UserProfile) -> None:
assert isinstance(bot.bot_owner, UserProfile)
realm = bot.bot_owner.realm
stream, created = create_stream_if_needed(realm, integration.stream_name)
bulk_add_subscriptions(realm, [stream], [bot, bot.bot_owner], acting_user=bot)
def get_fixture_info(fixture_path: str) -> Tuple[Any, bool, str]:
json_fixture = fixture_path.endswith(".json")
_, fixture_name = split_fixture_path(fixture_path)
if fixture_name:
if json_fixture:
with open(fixture_path, "rb") as fb:
data = orjson.loads(fb.read())
else:
with open(fixture_path) as f:
data = f.read().strip()
else:
data = ""
return data, json_fixture, fixture_name
def get_integration(integration_name: str) -> Integration:
integration = INTEGRATIONS[integration_name]
return integration
def get_requests_headers(integration_name: str, fixture_name: str) -> Dict[str, Any]:
headers = get_fixture_http_headers(integration_name, fixture_name)
def fix_name(header: str) -> str:
header = header if not header.startswith("HTTP_") else header[len("HTTP_") :]
return header.replace("_", "-")
return {fix_name(k): v for k, v in headers.items()}
def custom_headers(headers_json: str) -> Dict[str, str]:
if not headers_json:
return {}
try:
return orjson.loads(headers_json)
except orjson.JSONDecodeError as ve:
raise argparse.ArgumentTypeError(
"Encountered an error while attempting to parse custom headers: {}\n"
"Note: all strings must be enclosed within \"\" instead of ''".format(ve)
)
def send_bot_mock_message(
bot: UserProfile, integration: Integration, fixture_path: str, config: BaseScreenshotConfig
) -> None:
# Delete all messages, so new message is the only one it's message group
Message.objects.filter(sender=bot).delete()
data, _, _ = get_fixture_info(fixture_path)
assert bot.bot_owner is not None
url = f"{bot.bot_owner.realm.uri}"
client = zulip.Client(email=bot.email, api_key=bot.api_key, site=url)
try:
request = {
"type": "stream",
"to": integration.stream_name,
"topic": data["subject"],
"content": data["body"],
}
client.send_message(request)
except KeyError:
print(
"{} contains invalid configuration. "
'Fields "subject" and "body" are required for non-webhook integrations.'.format(
fixture_path
)
)
sys.exit(1)
def send_bot_payload_message(
bot: UserProfile, integration: WebhookIntegration, fixture_path: str, config: ScreenshotConfig
) -> bool:
# Delete all messages, so new message is the only one it's message group
Message.objects.filter(sender=bot).delete()
data, json_fixture, fixture_name = get_fixture_info(fixture_path)
headers = get_requests_headers(integration.name, fixture_name)
headers.update(config.custom_headers)
if config.use_basic_auth:
credentials = base64.b64encode(f"{bot.email}:{bot.api_key}".encode()).decode()
auth = f"basic {credentials}"
headers.update(dict(Authorization=auth))
assert isinstance(bot.bot_owner, UserProfile)
stream = integration.stream_name or "devel"
url = f"{bot.bot_owner.realm.uri}/{integration.url}"
params = {"api_key": bot.api_key, "stream": stream}
params.update(config.extra_params)
extra_args = {}
if not json_fixture and data:
# We overwrite any params in fixture with our params. stream name, for
# example, may be defined in the fixture.
assert isinstance(data, str)
parsed_params = dict(parse_qsl(data))
parsed_params.update(params)
params = parsed_params
elif config.payload_as_query_param:
params[config.payload_param_name] = orjson.dumps(data).decode()
else:
extra_args = {"json": data}
url = f"{url}?{urlencode(params)}"
try:
response = requests.post(url=url, headers=headers, **extra_args)
except requests.exceptions.ConnectionError:
print(
"This tool needs the local dev server to be running. "
"Please start it using tools/run-dev.py before running this tool."
)
sys.exit(1)
if response.status_code != 200:
print(response.json())
print("Failed to trigger webhook")
return False
else:
print(f"Triggered {integration.name} webhook")
return True
def capture_last_message_screenshot(bot: UserProfile, image_path: str) -> None:
message = Message.objects.filter(sender=bot).last()
realm = get_realm("zulip")
if message is None:
print(f"No message found for {bot.full_name}")
return
message_id = str(message.id)
screenshot_script = os.path.join(TOOLS_DIR, "message-screenshot.js")
subprocess.check_call(["node", screenshot_script, message_id, image_path, realm.uri])
def generate_screenshot_from_config(
integration_name: str, screenshot_config: BaseScreenshotConfig
) -> None:
integration = get_integration(integration_name)
fixture_path, image_path = get_fixture_and_image_paths(integration, screenshot_config)
bot = create_integration_bot(integration, screenshot_config.bot_name)
create_integration_stream(integration, bot)
if isinstance(integration, WebhookIntegration):
assert isinstance(
screenshot_config, ScreenshotConfig
), "Webhook integrations require ScreenshotConfig"
message_sent = send_bot_payload_message(bot, integration, fixture_path, screenshot_config)
else:
send_bot_mock_message(bot, integration, fixture_path, screenshot_config)
message_sent = True
if message_sent:
capture_last_message_screenshot(bot, image_path)
print(f"Screenshot captured to: {BOLDRED}{image_path}{ENDC}")
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--all", action="store_true")
group.add_argument("--integration", help="Name of the integration")
parser.add_argument("--fixture", help="Name of the fixture file to use")
parser.add_argument("--image-name", help="Name for the screenshot image")
parser.add_argument("--image-dir", help="Directory name where to save the screenshot image")
parser.add_argument("--bot-name", help="Name to use for the bot")
parser.add_argument(
"-A", "--use-basic-auth", action="store_true", help="Add basic auth headers to the request"
)
parser.add_argument(
"-Q",
"--payload-as-query-param",
action="store_true",
help="Send payload as query param instead of body",
)
parser.add_argument("-P", "--payload-param-name", help="Param name to use for the payload")
parser.add_argument(
"-H",
"--custom-headers",
type=custom_headers,
help="Any additional headers to be sent with the request.",
)
options = parser.parse_args()
prepare_puppeteer_run()
if options.all:
for key, value in vars(options).items():
if key != "all" and value:
print("Generating screenshots for all integrations. Ignoring all command-line options")
for integration_name, screenshot_configs in DOC_SCREENSHOT_CONFIG.items():
for screenshot_config in screenshot_configs:
generate_screenshot_from_config(integration_name, screenshot_config)
elif options.fixture:
config = dict(
fixture_name=options.fixture,
use_basic_auth=options.use_basic_auth,
custom_headers=options.custom_headers,
payload_as_query_param=options.payload_as_query_param,
)
if options.image_name:
config["image_name"] = options.image_name
if options.image_dir:
config["image_dir"] = options.image_dir
if options.bot_name:
config["bot_name"] = options.bot_name
if options.payload_param_name:
config["payload_param_name"] = options.payload_param_name
screenshot_config = ScreenshotConfig(**config)
generate_screenshot_from_config(options.integration, screenshot_config)
elif options.integration in DOC_SCREENSHOT_CONFIG:
configs = DOC_SCREENSHOT_CONFIG[options.integration]
for screenshot_config in configs:
generate_screenshot_from_config(options.integration, screenshot_config)
else:
parser.error(
"Could not find configuration for integration. "
"You can specify a fixture file to use, using the --fixture flag. "
"Or add a configuration to zerver.lib.integrations.DOC_SCREENSHOT_CONFIG",
)