From 38aa44bbe812c6520bd814184d45fd92fb8988dd Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Sun, 9 Apr 2023 11:06:06 -0500 Subject: [PATCH] Get SpaceDock co-authors from POST form lists --- netkan/netkan/spacedock_adder.py | 10 +--------- netkan/netkan/webhooks/spacedock_add.py | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/netkan/netkan/spacedock_adder.py b/netkan/netkan/spacedock_adder.py index 04d7154..a5fab62 100644 --- a/netkan/netkan/spacedock_adder.py +++ b/netkan/netkan/spacedock_adder.py @@ -84,19 +84,11 @@ def try_add(self) -> bool: @staticmethod def _pr_body(info: Dict[str, Any]) -> str: try: - shared_authors = info.get('shared_authors', []) - # It's supposed to be a list of dicts, SpaceDock has a bug right now where it's a string - bad_author = (not isinstance(shared_authors, list) - or any(not isinstance(auth, dict) for auth in shared_authors)) - if bad_author: - logging.error('shared_authors should be list of dicts, is: %s', shared_authors) return SpaceDockAdder.PR_BODY_TEMPLATE.safe_substitute(defaultdict( lambda: '', {**info, 'all_authors_md': ', '.join(SpaceDockAdder.USER_TEMPLATE.safe_substitute(defaultdict(lambda: '', a)) - for a in ([info] - if bad_author else - [info, *info.get('shared_authors', [])]))})) + for a in info.get('all_authors', []))})) except Exception as exc: # Log the input on failure logging.error('Failed to generate pull request body from %s', info) diff --git a/netkan/netkan/webhooks/spacedock_add.py b/netkan/netkan/webhooks/spacedock_add.py index eb70e38..6727e95 100644 --- a/netkan/netkan/webhooks/spacedock_add.py +++ b/netkan/netkan/webhooks/spacedock_add.py @@ -1,7 +1,8 @@ from hashlib import md5 import json -from typing import Tuple, Dict, Any, TYPE_CHECKING +from typing import Tuple, TYPE_CHECKING from flask import Blueprint, current_app, request +from werkzeug.datastructures import ImmutableMultiDict from ..common import sqs_batch_entries from .config import current_config @@ -43,8 +44,21 @@ def add_hook(game_id: str) -> Tuple[str, int]: return '', 204 -def batch_message(raw: Dict[str, Any], game_id: str) -> SendMessageBatchRequestEntryTypeDef: - body = json.dumps(raw) +def batch_message(raw: 'ImmutableMultiDict[str, str]', game_id: str) -> SendMessageBatchRequestEntryTypeDef: + body = json.dumps({**raw, + # Turn the separate user property lists into a list of user dicts so JSON can encode it + # (the original properties will only have the first user) + 'all_authors': [{'username': user_tuple[0], + 'user_github': user_tuple[1], + 'user_forum_id': user_tuple[2], + 'user_forum_username': user_tuple[3], + 'email': user_tuple[4]} + for user_tuple + in zip(raw.getlist('username'), + raw.getlist('user_github'), + raw.getlist('user_forum_id'), + raw.getlist('user_forum_username'), + raw.getlist('email'))]}) return { 'Id': '1', 'MessageBody': body,