Skip to content

Commit

Permalink
Merge pull request #4333 from PascalEgn/refactor_snow_ticket_logic
Browse files Browse the repository at this point in the history
snow: adjust workflows to new snow endpoints
  • Loading branch information
drjova authored Jan 22, 2024
2 parents 9c6db67 + 5bd46d8 commit 2660d43
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 73 deletions.
90 changes: 47 additions & 43 deletions inspirehep/modules/workflows/tasks/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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!")
Expand Down
36 changes: 15 additions & 21 deletions inspirehep/modules/workflows/workflows/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,43 +211,43 @@
]


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
),
),
)
]


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"
),
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,
),
Expand Down Expand Up @@ -352,7 +352,7 @@
is_submission,
IF(
is_marked('is-update'),
NOTIFY_ALREADY_EXISTING
NOTIFY_AND_CLOSE_ALREADY_EXISTING
)
)
]
Expand Down Expand Up @@ -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
)
12 changes: 5 additions & 7 deletions inspirehep/modules/workflows/workflows/author.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
),
]


Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/workflows/test_workflow_tasks_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2660d43

Please sign in to comment.