diff --git a/inspirehep/modules/workflows/tasks/submission.py b/inspirehep/modules/workflows/tasks/submission.py index 1eeab9468b..c5e2cfbcfe 100644 --- a/inspirehep/modules/workflows/tasks/submission.py +++ b/inspirehep/modules/workflows/tasks/submission.py @@ -88,7 +88,7 @@ def submit_snow_ticket(obj, queue, template, context, caller, recid, ticket_id_k ticket_payload["recid"] = str(recid) ticket_payload['subject'] = context.get('subject', 'No subject') response = requests.post( - "{inspirehep_url}/tickets/create-with-template".format( + "{inspirehep_url}/tickets/create".format( inspirehep_url=current_app.config["INSPIREHEP_URL"] ), headers=_get_headers_for_hep_root_table_request(), @@ -155,40 +155,35 @@ def _create_ticket(obj, eng): backoff.expo, requests.exceptions.RequestException, base=4, max_tries=5 ) def reply_snow_ticket(obj, ticket_id, context_factory, user, template=None): - if not template: - reply_message = obj.extra_data.get("reason", "") - response = requests.post( - "{inspirehep_url}/tickets/reply".format( - inspirehep_url=current_app.config["INSPIREHEP_URL"] - ), - headers=_get_headers_for_hep_root_table_request(), - data=json.dumps( - { - "ticket_id": str(ticket_id), - "reply_message": reply_message, - "user_email": user.email - } - ), - ) - response.raise_for_status() - else: + if template: template_context = context_factory(user, obj) template = _get_ticket_template_name(template) - response = requests.post( - "{inspirehep_url}/tickets/reply-with-template".format( - inspirehep_url=current_app.config["INSPIREHEP_URL"] - ), - headers=_get_headers_for_hep_root_table_request(), - data=json.dumps( - { - "ticket_id": str(ticket_id), - "template": template, - "template_context": template_context, - "user_email": user.email - } - ), + data = json.dumps( + { + "ticket_id": str(ticket_id), + "template": template, + "template_context": template_context, + "user_email": user.email + } ) - response.raise_for_status() + else: + reply_message = obj.extra_data.get("reason", "") + data = json.dumps( + { + "ticket_id": str(ticket_id), + "reply_message": reply_message, + "user_email": user.email + } + ) + + response = requests.post( + "{inspirehep_url}/tickets/reply".format( + inspirehep_url=current_app.config["INSPIREHEP_URL"] + ), + headers=_get_headers_for_hep_root_table_request(), + data=data + ) + response.raise_for_status() def reply_ticket(template=None, context_factory=None, keep_new=False): @@ -222,13 +217,6 @@ def _reply_ticket(obj, eng): obj.log.error("No ticket ID found!") return {} - user = User.query.get(obj.id_user) - if not user: - obj.log.error( - "No user found for object %s, skipping ticket creation", obj.id - ) - return {} - if template: context = {} if context_factory: @@ -250,18 +238,34 @@ def _reply_ticket(obj, eng): @backoff.on_exception(backoff.expo, rt.ConnectionError, base=4, max_tries=5) -def close_snow_ticket(ticket_id): +def close_snow_ticket(obj, ticket_id, context_factory, template=None): + if template: + user = User.query.get(obj.id_user) + if not user: + obj.log.error( + "No user found for object %s, skipping ticket creation", obj.id + ) + return {} + + template_context = context_factory(user, obj) + template = _get_ticket_template_name(template) + data = json.dumps({"ticket_id": str(ticket_id), + "template": template, + "template_context": template_context}) + else: + data = json.dumps({"ticket_id": str(ticket_id)}) + response = requests.post( "{inspirehep_url}/tickets/resolve".format( inspirehep_url=current_app.config["INSPIREHEP_URL"] ), headers=_get_headers_for_hep_root_table_request(), - data=json.dumps({"ticket_id": str(ticket_id)}), + data=data ) response.raise_for_status() -def close_ticket(ticket_id_key="ticket_id"): +def close_ticket(ticket_id_key="ticket_id", template=None, context_factory=None): """Close the ticket associated with this record found in given key.""" @with_debug_logging @@ -272,7 +276,7 @@ def _close_ticket(obj, eng): obj.log.error("No ticket ID found!") return {} if current_app.config["FEATURE_FLAG_ENABLE_SNOW"]: - close_snow_ticket(ticket_id) + close_snow_ticket(obj, ticket_id, context_factory, template) else: if not rt_instance: obj.log.error("No RT instance available. Skipping!") diff --git a/inspirehep/modules/workflows/workflows/article.py b/inspirehep/modules/workflows/workflows/article.py index 2ce16780d7..4bea8a9eb3 100644 --- a/inspirehep/modules/workflows/workflows/article.py +++ b/inspirehep/modules/workflows/workflows/article.py @@ -211,11 +211,12 @@ ] -NOTIFY_NOT_ACCEPTED = [ +NOTIFY_AND_CLOSE_NOT_ACCEPTED = [ IF( is_submission, - do_not_repeat('reply_ticket_submission_not_accepted')( - reply_ticket( + do_not_repeat('close_ticket_submission_not_accepted')( + close_ticket( + ticket_id_key="ticket_id", context_factory=reply_ticket_context ), ), @@ -223,11 +224,12 @@ ] -NOTIFY_ALREADY_EXISTING = [ +NOTIFY_AND_CLOSE_ALREADY_EXISTING = [ reject_record('Article was already found on INSPIRE'), mark('approved', False), - do_not_repeat('reply_ticket_user_submission_already_in_inspire')( - reply_ticket( + do_not_repeat('close_ticket_user_submission_already_in_inspire')( + close_ticket( + ticket_id_key="ticket_id", template=( "literaturesuggest/tickets/" "user_rejected_exists.html" @@ -235,19 +237,17 @@ context_factory=reply_ticket_context ), ), - do_not_repeat('close_ticket_user_submission_already_in_inspire')( - close_ticket(ticket_id_key="ticket_id") - ), save_workflow, stop_processing, ] -NOTIFY_ACCEPTED = [ +NOTIFY_AND_CLOSE_ACCEPTED = [ IF( is_submission, - do_not_repeat('reply_ticket_user_submission_accepted')( - reply_ticket( + do_not_repeat('close_ticket_user_submission_accepted')( + close_ticket( + ticket_id_key="ticket_id", template='literaturesuggest/tickets/user_accepted.html', context_factory=reply_ticket_context, ), @@ -352,7 +352,7 @@ is_submission, IF( is_marked('is-update'), - NOTIFY_ALREADY_EXISTING + NOTIFY_AND_CLOSE_ALREADY_EXISTING ) ) ] @@ -626,18 +626,12 @@ class Article(object): STORE_RECORD + SEND_TO_LEGACY + STORE_ROOT + - NOTIFY_ACCEPTED + + NOTIFY_AND_CLOSE_ACCEPTED + NOTIFY_CURATOR_IF_NEEDED + CREATE_CORE_SELECTION_WF ), - NOTIFY_NOT_ACCEPTED, + NOTIFY_AND_CLOSE_NOT_ACCEPTED, ), - IF( - is_submission, - do_not_repeat('close_ticket_user_submission')( - close_ticket(ticket_id_key="ticket_id") - ), - ) ] + FIND_NEXT_AND_RUN_IF_NECESSARY ) diff --git a/inspirehep/modules/workflows/workflows/author.py b/inspirehep/modules/workflows/workflows/author.py index a3f018319c..dec7a3679d 100644 --- a/inspirehep/modules/workflows/workflows/author.py +++ b/inspirehep/modules/workflows/workflows/author.py @@ -57,15 +57,13 @@ ] -NOTIFY_ACCEPTED = [ - do_not_repeat('reply_ticket_author_submission_accepted')( - reply_ticket( +NOTIFY_AND_CLOSE_ACCEPTED = [ + do_not_repeat('close_ticket_author_submission_accepted')( + close_ticket( + ticket_id_key="ticket_id", template="authors/tickets/user_accepted_author.html", context_factory=reply_ticket_context) ), - do_not_repeat('close_ticket_author_submission_accepted')( - close_ticket(ticket_id_key="ticket_id") - ), ] @@ -144,7 +142,7 @@ class Author(object): ( [store_record] + SEND_TO_LEGACY + - NOTIFY_ACCEPTED + + NOTIFY_AND_CLOSE_ACCEPTED + CLOSE_TICKET_IF_NEEDED ), NOTIFY_NOT_ACCEPTED diff --git a/tests/integration/workflows/test_workflow_tasks_submission.py b/tests/integration/workflows/test_workflow_tasks_submission.py index 59153325e6..aeb1127ddf 100644 --- a/tests/integration/workflows/test_workflow_tasks_submission.py +++ b/tests/integration/workflows/test_workflow_tasks_submission.py @@ -46,7 +46,7 @@ def test_reply_ticket_calls_tickets_reply_when_template_is_set(workflow_app): with requests_mock.Mocker() as request_mocker: request_mocker.register_uri( "POST", - "http://web:8000/tickets/reply-with-template", + "http://web:8000/tickets/reply", json={"message": "Ticket was updated with the reply"}, headers=_get_headers_for_hep_root_table_request(), status_code=200, @@ -103,7 +103,7 @@ def test_create_ticket_calls_tickets_create_with_template(workflow_app): with requests_mock.Mocker() as request_mocker: request_mocker.register_uri( "POST", - "http://web:8000/tickets/create-with-template", + "http://web:8000/tickets/create", json={"ticket_id": "123", "ticket_url": "http//test.com/1"}, headers=_get_headers_for_hep_root_table_request(), status_code=200,