tools: Allow specifying more options when capturing screenshot.

The name and directory into which the screenshot should be captured can be
specified now, apart from specifying the fixture file to use.
This commit is contained in:
Puneeth Chaganti
2020-04-26 12:05:53 +05:30
committed by Tim Abbott
parent 4e5c30e7fd
commit 88c297dffe
3 changed files with 44 additions and 18 deletions

View File

@@ -35,7 +35,8 @@ from zerver.lib.actions import (
do_create_user, notify_created_bot, bulk_add_subscriptions, do_change_avatar_fields)
from zerver.lib.streams import create_stream_if_needed
from zerver.lib.upload import upload_avatar_image
from zerver.lib.integrations import WebhookIntegration, INTEGRATIONS, split_fixture_path
from zerver.lib.integrations import (
WebhookIntegration, INTEGRATIONS, split_fixture_path, ScreenshotConfig, get_fixture_and_image_paths)
from zerver.lib.webhooks.common import get_fixture_http_headers
from zerver.lib.storage import static_path
@@ -85,12 +86,6 @@ def get_requests_headers(integration_name: str, fixture_name: str) -> Dict[str,
return {fix_name(k): v for k, v in headers.items()}
def webhook_json_fixture(path: str) -> str:
path = os.path.abspath(path)
if not (os.path.exists(path) and path.endswith('.json') and 'webhooks' in path):
raise argparse.ArgumentTypeError('Not a valid webhook JSON fixture')
return path
def custom_headers(headers_json: str) -> Dict[str, str]:
if not headers_json:
return {}
@@ -140,19 +135,25 @@ def capture_last_message_screenshot(bot: UserProfile, image_path: str) -> None:
screenshot_script = os.path.join(TOOLS_DIR, 'message-screenshot.js')
subprocess.check_call(['node', screenshot_script, message_id, image_path])
def generate_screenshot_from_config(integration_name: str, screenshot_config: ScreenshotConfig) -> None:
integration = get_integration(integration_name)
fixture_path, image_path = get_fixture_and_image_paths(integration, screenshot_config)
bot = create_integration_bot(integration)
create_integration_stream(integration, bot)
message_sent = send_bot_payload_message(bot, integration, fixture_path, options.custom_headers)
if message_sent:
capture_last_message_screenshot(bot, image_path)
parser = argparse.ArgumentParser()
parser.add_argument('fixture', type=webhook_json_fixture, help='Path to the fixture to use')
parser.add_argument('integration', type=str, help='Name of the integration')
parser.add_argument('fixture', type=str, help='Name of the fixture file to use')
parser.add_argument('--image-name', type=str, default='001.png', help='Name for the screenshot image')
parser.add_argument('--image-dir', type=str, help='Directory name where to save the screenshot image')
parser.add_argument('-H', '--custom-headers',
type=custom_headers,
help='Any additional headers to be sent with the request.')
options = parser.parse_args()
options = parser.parse_args()
prepare_puppeteer_run()
integration_name, fixture_name = split_fixture_path(options.fixture)
integration = get_integration(integration_name)
bot = create_integration_bot(integration)
create_integration_stream(integration, bot)
message_sent = send_bot_payload_message(bot, integration, options.fixture, options.custom_headers)
if message_sent:
image_path = static_path('images/integrations/{name}/001.png'.format(name=integration.name))
capture_last_message_screenshot(bot, image_path)
screenshot_config = ScreenshotConfig(options.fixture, options.image_name, options.image_dir)
generate_screenshot_from_config(options.integration, screenshot_config)

View File

@@ -197,6 +197,22 @@ def split_fixture_path(path: str) -> Tuple[str, str]:
integration_name = os.path.split(os.path.dirname(path))[-1]
return integration_name, fixture_name
# FIXME: Change to namedtuple if we drop Python3.6: No default values support on namedtuples (or dataclass)
class ScreenshotConfig:
def __init__(self, fixture_name: str, image_name: str='001.png', image_dir: Optional[str]=None):
self.fixture_name = fixture_name
self.image_name = image_name
self.image_dir = image_dir
def get_fixture_and_image_paths(integration: WebhookIntegration,
screenshot_config: ScreenshotConfig) -> Tuple[str, str]:
fixture_dir = os.path.join('zerver', 'webhooks', integration.name, 'fixtures')
fixture_path = os.path.join(fixture_dir, screenshot_config.fixture_name)
image_dir = screenshot_config.image_dir or integration.name
image_name = screenshot_config.image_name
image_path = os.path.join('static/images/integrations', image_dir, image_name)
return fixture_path, image_path
class HubotIntegration(Integration):
GIT_URL_TEMPLATE = "https://github.com/hubot-scripts/hubot-{}"

View File

@@ -1,5 +1,6 @@
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.integrations import split_fixture_path
from zerver.lib.integrations import (
split_fixture_path, get_fixture_and_image_paths, INTEGRATIONS, ScreenshotConfig, WebhookIntegration)
class IntegrationsTestCase(ZulipTestCase):
@@ -8,3 +9,11 @@ class IntegrationsTestCase(ZulipTestCase):
integration_name, fixture_name = split_fixture_path(path)
self.assertEqual(integration_name, 'semaphore')
self.assertEqual(fixture_name, 'push')
def test_get_fixture_and_image_paths(self) -> None:
integration = INTEGRATIONS['airbrake']
assert isinstance(integration, WebhookIntegration)
screenshot_config = ScreenshotConfig('error_message.json', '002.png', 'ci')
fixture_path, image_path = get_fixture_and_image_paths(integration, screenshot_config)
self.assertEqual(fixture_path, 'zerver/webhooks/airbrake/fixtures/error_message.json')
self.assertEqual(image_path, 'static/images/integrations/ci/002.png')