{"action":"created","comment":{"url":"https://api.github.com/repos/inspirehep/inspire-next/pulls/comments/121645944","pull_request_review_id":43683291,"id":121645944,"diff_hunk":"@@ -24,26 +24,222 @@\n \n from __future__ import absolute_import, division, print_function\n \n-from flask import current_app\n-from rt import Rt, ConnectionError\n+from os.path import join\n+\n+from flask import render_template\n+\n+from rt import ALL_QUEUES, ConnectionError\n+\n+from .proxies import rt_instance\n \n \n def get_instance():\n-    \"\"\"Make a RT instance and return it.\"\"\"\n-    url = current_app.config.get(\"CFG_BIBCATALOG_SYSTEM_RT_URL\", \"\")\n-    login = current_app.config.get(\"CFG_BIBCATALOG_SYSTEM_RT_DEFAULT_USER\", \"\")\n-    password = current_app.config.get(\"CFG_BIBCATALOG_SYSTEM_RT_DEFAULT_PWD\", \"\")\n-\n-    if url:\n-        tracker = Rt(\n-            url=url,\n-            default_login=login,\n-            default_password=password,\n-        )\n-        tracker.login()\n-        return tracker\n+    return rt_instance\n \n \n def retry_if_connection_problems(exception):\n     \"\"\"Return True if exception is an ConnectionError in Rt.\"\"\"\n     return isinstance(exception, ConnectionError)\n+\n+\n+def create_ticket(queue, requestors, subject, body, recid=None, **kwargs):\n+    \"\"\"Creates new RT ticket and returns new ticket id.\n+\n+    :param queue: queue name for the ticket\n+    :type queue: string\n+\n+    :param requestors: username to set to requestors field of the ticket\n+    :type requestors: string\n+\n+    :param body: message body of the ticket\n+    :type body: string\n+\n+    :param recid: record id to be set custom RecordID field\n+    :type recid: integer\n+\n+    :param kwargs: Other arguements possible to set:\n+\n+                   Cc, AdminCc, Owner, Status,Priority, InitialPriority,\n+                   FinalPriority, TimeEstimated, Starts, Due,\n+                   ... (according to RT fields)\n+\n+                   Custom fields CF.{<CustomFieldName>} could be set\n+                   with keywords CF_CustomFieldName.\n+    :returns: ID of the new ticket or ``-1``, if it fails\n+    :rtype: integer\n+    \"\"\"\n+    body = _strip_lines(body)\n+\n+    payload = dict(\n+        Queue=queue,\n+        Subject=subject,\n+        Text=body,\n+        **kwargs\n+    )\n+\n+    if recid:\n+        payload[\"CF_RecordID\"] = str(recid)\n+\n+    # Check if requests is set and also ignore admin due to RT mail loop\n+    if requestors and \"admin@inspirehep.net\" not in requestors:\n+        payload[\"requestors\"] = requestors\n+\n+    return rt_instance.create_ticket(**payload)\n+\n+\n+def create_ticket_with_template(queue,\n+                                requestors,\n+                                template_path,\n+                                template_context,\n+                                recid=None,\n+                                **kwargs):\n+    \"\"\"Creates new RT ticket with a body that is rendered template\n+\n+    :param queue: queue name for the ticket\n+    :type queue: string\n+\n+    :param requestors: username to set to requestors field of the ticket\n+    :type requestors: string\n+\n+    :param template_path: path to the template for the ticket body\n+\n+    :param template_context: context object to be used to render template\n+\n+    :param recid: record id to be set custom RecordID field\n+    :type recid: integer\n+\n+    :param kwargs: Other arguements possible to set:\n+\n+                Cc, AdminCc, Owner, Status,Priority, InitialPriority,\n+                FinalPriority, TimeEstimated, Starts, Due,\n+                ... (according to RT fields)\n+\n+                Custom fields CF.{<CustomFieldName>} could be set\n+                with keywords CF_CustomFieldName.\n+    :returns: ID of the new ticket or ``-1``, if it fails\n+    :rtype: integer\n+    \"\"\"\n+    body = render_template(template_path, **template_context).strip()\n+    subject = template_context.get(\"subject\")\n+    # FIXME: raise exception if subject is None ???\n+    return create_ticket(queue, requestors, subject, body, recid, **kwargs)\n+\n+\n+def resolve_ticket(ticket_id):\n+    \"\"\"Resolves the given ticket\n+\n+    :type ticket_id: integer\n+    \"\"\"\n+    try:\n+        rt_instance.edit_ticket(\n+            ticket_id=ticket_id,\n+            Status=\"resolved\",\n+        )\n+    except IndexError:\n+        # Raise exception only ticket isn't already resolved\n+        ticket = rt_instance.get_ticket(ticket_id)\n+        if ticket[\"Status\"] != \"resolved\":\n+            raise\n+\n+\n+def get_queues():\n+    \"\"\"Returns list of all queues as {id, name} dict\n+\n+    :rtype: dict - with ``name (string)``, ``id (integer)`` properties\n+    \"\"\"\n+    return _get_all_of(\"queue\")\n+\n+\n+def get_users():\n+    \"\"\"Returns list of all users as {id, name} dict\n+\n+    :rtype: dict - with ``name (string)``, ``id (integer)`` properties\n+    \"\"\"\n+    return _get_all_of(\"user\")\n+\n+\n+def _get_all_of(query_type):\n+    \"\"\"Utility function to share the code for performing custom search queries\n+     and parsing the result\n+\n+    :param query_type: the type of quer, either ``'queue'`` or ``'user'``\n+\n+    :rtype: dict - with ``name (string)``, ``id (integer)`` properties\n+    \"\"\"\n+    query_suffix = query_type + \"?query=\"\n+    url = join(rt_instance.url, \"search\", query_suffix)\n+    response = rt_instance.session.get(url)\n+    raw_result = response.content.decode(response.encoding.lower())\n+    # parse raw result\n+    lines = raw_result.split(\"\\n\")\n+    # remove status and empty lines\n+    del lines[:2]\n+    del lines[-3:]\n+    # create dict for each result item\n+    return map(_query_result_item_id_name_mapper, lines)\n+\n+\n+def _query_result_item_id_name_mapper(raw_item):\n+    \"\"\"Mapper function\n+\n+    Takes a string like ``'17: CoolUser'`` and\n+    returns ``{'id': '17', 'name': 'CoolUser'}``\n+    \"\"\"\n+    parts = raw_item.split(\": \")\n+    return dict(id=parts[0], name=parts[1])\n+\n+\n+def _strip_lines(multiline_string):","path":"inspirehep/utils/tickets.py","position":184,"original_position":184,"commit_id":"32740c2914f63a5a2a6bee25d5520700d5b32b4e","original_commit_id":"32740c2914f63a5a2a6bee25d5520700d5b32b4e","user":{"login":"jmartinm","id":580727,"avatar_url":"https://avatars1.githubusercontent.com/u/580727?v=3","gravatar_id":"","url":"https://api.github.com/users/jmartinm","html_url":"https://github.com/jmartinm","followers_url":"https://api.github.com/users/jmartinm/followers","following_url":"https://api.github.com/users/jmartinm/following{/other_user}","gists_url":"https://api.github.com/users/jmartinm/gists{/gist_id}","starred_url":"https://api.github.com/users/jmartinm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmartinm/subscriptions","organizations_url":"https://api.github.com/users/jmartinm/orgs","repos_url":"https://api.github.com/users/jmartinm/repos","events_url":"https://api.github.com/users/jmartinm/events{/privacy}","received_events_url":"https://api.github.com/users/jmartinm/received_events","type":"User","site_admin":false},"body":"A test for this would be useful","created_at":"2017-06-13T11:03:47Z","updated_at":"2017-06-13T11:06:10Z","html_url":"https://github.com/inspirehep/inspire-next/pull/2430#discussion_r121645944","pull_request_url":"https://api.github.com/repos/inspirehep/inspire-next/pulls/2430","_links":{"self":{"href":"https://api.github.com/repos/inspirehep/inspire-next/pulls/comments/121645944"},"html":{"href":"https://github.com/inspirehep/inspire-next/pull/2430#discussion_r121645944"},"pull_request":{"href":"https://api.github.com/repos/inspirehep/inspire-next/pulls/2430"}}},"pull_request":{"url":"https://api.github.com/repos/inspirehep/inspire-next/pulls/2430","id":125313740,"html_url":"https://github.com/inspirehep/inspire-next/pull/2430","diff_url":"https://github.com/inspirehep/inspire-next/pull/2430.diff","patch_url":"https://github.com/inspirehep/inspire-next/pull/2430.patch","issue_url":"https://api.github.com/repos/inspirehep/inspire-next/issues/2430","number":2430,"state":"open","locked":false,"title":"[WIP] rt tickets utility module","user":{"login":"harunurhan","id":4868667,"avatar_url":"https://avatars2.githubusercontent.com/u/4868667?v=3","gravatar_id":"","url":"https://api.github.com/users/harunurhan","html_url":"https://github.com/harunurhan","followers_url":"https://api.github.com/users/harunurhan/followers","following_url":"https://api.github.com/users/harunurhan/following{/other_user}","gists_url":"https://api.github.com/users/harunurhan/gists{/gist_id}","starred_url":"https://api.github.com/users/harunurhan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/harunurhan/subscriptions","organizations_url":"https://api.github.com/users/harunurhan/orgs","repos_url":"https://api.github.com/users/harunurhan/repos","events_url":"https://api.github.com/users/harunurhan/events{/privacy}","received_events_url":"https://api.github.com/users/harunurhan/received_events","type":"User","site_admin":false},"body":"* Implements Rt module (fixes #2418)","created_at":"2017-06-13T09:10:00Z","updated_at":"2017-06-13T11:06:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c4b40532180f5d850fe9c7aa1d29f7c3e3c53548","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/inspirehep/inspire-next/pulls/2430/commits","review_comments_url":"https://api.github.com/repos/inspirehep/inspire-next/pulls/2430/comments","review_comment_url":"https://api.github.com/repos/inspirehep/inspire-next/pulls/comments{/number}","comments_url":"https://api.github.com/repos/inspirehep/inspire-next/issues/2430/comments","statuses_url":"https://api.github.com/repos/inspirehep/inspire-next/statuses/32740c2914f63a5a2a6bee25d5520700d5b32b4e","head":{"label":"harunurhan:fix-2418","ref":"fix-2418","sha":"32740c2914f63a5a2a6bee25d5520700d5b32b4e","user":{"login":"harunurhan","id":4868667,"avatar_url":"https://avatars2.githubusercontent.com/u/4868667?v=3","gravatar_id":"","url":"https://api.github.com/users/harunurhan","html_url":"https://github.com/harunurhan","followers_url":"https://api.github.com/users/harunurhan/followers","following_url":"https://api.github.com/users/harunurhan/following{/other_user}","gists_url":"https://api.github.com/users/harunurhan/gists{/gist_id}","starred_url":"https://api.github.com/users/harunurhan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/harunurhan/subscriptions","organizations_url":"https://api.github.com/users/harunurhan/orgs","repos_url":"https://api.github.com/users/harunurhan/repos","events_url":"https://api.github.com/users/harunurhan/events{/privacy}","received_events_url":"https://api.github.com/users/harunurhan/received_events","type":"User","site_admin":false},"repo":{"id":60278643,"name":"inspire-next","full_name":"harunurhan/inspire-next","owner":{"login":"harunurhan","id":4868667,"avatar_url":"https://avatars2.githubusercontent.com/u/4868667?v=3","gravatar_id":"","url":"https://api.github.com/users/harunurhan","html_url":"https://github.com/harunurhan","followers_url":"https://api.github.com/users/harunurhan/followers","following_url":"https://api.github.com/users/harunurhan/following{/other_user}","gists_url":"https://api.github.com/users/harunurhan/gists{/gist_id}","starred_url":"https://api.github.com/users/harunurhan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/harunurhan/subscriptions","organizations_url":"https://api.github.com/users/harunurhan/orgs","repos_url":"https://api.github.com/users/harunurhan/repos","events_url":"https://api.github.com/users/harunurhan/events{/privacy}","received_events_url":"https://api.github.com/users/harunurhan/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/harunurhan/inspire-next","description":"INSPIRE overlay repository to be used with https://github.com/inspirehep/invenio@labs branch","fork":true,"url":"https://api.github.com/repos/harunurhan/inspire-next","forks_url":"https://api.github.com/repos/harunurhan/inspire-next/forks","keys_url":"https://api.github.com/repos/harunurhan/inspire-next/keys{/key_id}","collaborators_url":"https://api.github.com/repos/harunurhan/inspire-next/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/harunurhan/inspire-next/teams","hooks_url":"https://api.github.com/repos/harunurhan/inspire-next/hooks","issue_events_url":"https://api.github.com/repos/harunurhan/inspire-next/issues/events{/number}","events_url":"https://api.github.com/repos/harunurhan/inspire-next/events","assignees_url":"https://api.github.com/repos/harunurhan/inspire-next/assignees{/user}","branches_url":"https://api.github.com/repos/harunurhan/inspire-next/branches{/branch}","tags_url":"https://api.github.com/repos/harunurhan/inspire-next/tags","blobs_url":"https://api.github.com/repos/harunurhan/inspire-next/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/harunurhan/inspire-next/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/harunurhan/inspire-next/git/refs{/sha}","trees_url":"https://api.github.com/repos/harunurhan/inspire-next/git/trees{/sha}","statuses_url":"https://api.github.com/repos/harunurhan/inspire-next/statuses/{sha}","languages_url":"https://api.github.com/repos/harunurhan/inspire-next/languages","stargazers_url":"https://api.github.com/repos/harunurhan/inspire-next/stargazers","contributors_url":"https://api.github.com/repos/harunurhan/inspire-next/contributors","subscribers_url":"https://api.github.com/repos/harunurhan/inspire-next/subscribers","subscription_url":"https://api.github.com/repos/harunurhan/inspire-next/subscription","commits_url":"https://api.github.com/repos/harunurhan/inspire-next/commits{/sha}","git_commits_url":"https://api.github.com/repos/harunurhan/inspire-next/git/commits{/sha}","comments_url":"https://api.github.com/repos/harunurhan/inspire-next/comments{/number}","issue_comment_url":"https://api.github.com/repos/harunurhan/inspire-next/issues/comments{/number}","contents_url":"https://api.github.com/repos/harunurhan/inspire-next/contents/{+path}","compare_url":"https://api.github.com/repos/harunurhan/inspire-next/compare/{base}...{head}","merges_url":"https://api.github.com/repos/harunurhan/inspire-next/merges","archive_url":"https://api.github.com/repos/harunurhan/inspire-next/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/harunurhan/inspire-next/downloads","issues_url":"https://api.github.com/repos/harunurhan/inspire-next/issues{/number}","pulls_url":"https://api.github.com/repos/harunurhan/inspire-next/pulls{/number}","milestones_url":"https://api.github.com/repos/harunurhan/inspire-next/milestones{/number}","notifications_url":"https://api.github.com/repos/harunurhan/inspire-next/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/harunurhan/inspire-next/labels{/name}","releases_url":"https://api.github.com/repos/harunurhan/inspire-next/releases{/id}","deployments_url":"https://api.github.com/repos/harunurhan/inspire-next/deployments","created_at":"2016-06-02T16:11:15Z","updated_at":"2016-06-02T16:11:18Z","pushed_at":"2017-06-13T09:08:25Z","git_url":"git://github.com/harunurhan/inspire-next.git","ssh_url":"git@github.com:harunurhan/inspire-next.git","clone_url":"https://github.com/harunurhan/inspire-next.git","svn_url":"https://github.com/harunurhan/inspire-next","homepage":"https://labs.inspirehep.net","size":64222,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"inspirehep:master","ref":"master","sha":"ff7abdb96b019078cf196a04f5a72a985f271944","user":{"login":"inspirehep","id":356664,"avatar_url":"https://avatars0.githubusercontent.com/u/356664?v=3","gravatar_id":"","url":"https://api.github.com/users/inspirehep","html_url":"https://github.com/inspirehep","followers_url":"https://api.github.com/users/inspirehep/followers","following_url":"https://api.github.com/users/inspirehep/following{/other_user}","gists_url":"https://api.github.com/users/inspirehep/gists{/gist_id}","starred_url":"https://api.github.com/users/inspirehep/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/inspirehep/subscriptions","organizations_url":"https://api.github.com/users/inspirehep/orgs","repos_url":"https://api.github.com/users/inspirehep/repos","events_url":"https://api.github.com/users/inspirehep/events{/privacy}","received_events_url":"https://api.github.com/users/inspirehep/received_events","type":"Organization","site_admin":false},"repo":{"id":15221515,"name":"inspire-next","full_name":"inspirehep/inspire-next","owner":{"login":"inspirehep","id":356664,"avatar_url":"https://avatars0.githubusercontent.com/u/356664?v=3","gravatar_id":"","url":"https://api.github.com/users/inspirehep","html_url":"https://github.com/inspirehep","followers_url":"https://api.github.com/users/inspirehep/followers","following_url":"https://api.github.com/users/inspirehep/following{/other_user}","gists_url":"https://api.github.com/users/inspirehep/gists{/gist_id}","starred_url":"https://api.github.com/users/inspirehep/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/inspirehep/subscriptions","organizations_url":"https://api.github.com/users/inspirehep/orgs","repos_url":"https://api.github.com/users/inspirehep/repos","events_url":"https://api.github.com/users/inspirehep/events{/privacy}","received_events_url":"https://api.github.com/users/inspirehep/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/inspirehep/inspire-next","description":"The next version of INSPIRE","fork":false,"url":"https://api.github.com/repos/inspirehep/inspire-next","forks_url":"https://api.github.com/repos/inspirehep/inspire-next/forks","keys_url":"https://api.github.com/repos/inspirehep/inspire-next/keys{/key_id}","collaborators_url":"https://api.github.com/repos/inspirehep/inspire-next/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/inspirehep/inspire-next/teams","hooks_url":"https://api.github.com/repos/inspirehep/inspire-next/hooks","issue_events_url":"https://api.github.com/repos/inspirehep/inspire-next/issues/events{/number}","events_url":"https://api.github.com/repos/inspirehep/inspire-next/events","assignees_url":"https://api.github.com/repos/inspirehep/inspire-next/assignees{/user}","branches_url":"https://api.github.com/repos/inspirehep/inspire-next/branches{/branch}","tags_url":"https://api.github.com/repos/inspirehep/inspire-next/tags","blobs_url":"https://api.github.com/repos/inspirehep/inspire-next/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/inspirehep/inspire-next/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/inspirehep/inspire-next/git/refs{/sha}","trees_url":"https://api.github.com/repos/inspirehep/inspire-next/git/trees{/sha}","statuses_url":"https://api.github.com/repos/inspirehep/inspire-next/statuses/{sha}","languages_url":"https://api.github.com/repos/inspirehep/inspire-next/languages","stargazers_url":"https://api.github.com/repos/inspirehep/inspire-next/stargazers","contributors_url":"https://api.github.com/repos/inspirehep/inspire-next/contributors","subscribers_url":"https://api.github.com/repos/inspirehep/inspire-next/subscribers","subscription_url":"https://api.github.com/repos/inspirehep/inspire-next/subscription","commits_url":"https://api.github.com/repos/inspirehep/inspire-next/commits{/sha}","git_commits_url":"https://api.github.com/repos/inspirehep/inspire-next/git/commits{/sha}","comments_url":"https://api.github.com/repos/inspirehep/inspire-next/comments{/number}","issue_comment_url":"https://api.github.com/repos/inspirehep/inspire-next/issues/comments{/number}","contents_url":"https://api.github.com/repos/inspirehep/inspire-next/contents/{+path}","compare_url":"https://api.github.com/repos/inspirehep/inspire-next/compare/{base}...{head}","merges_url":"https://api.github.com/repos/inspirehep/inspire-next/merges","archive_url":"https://api.github.com/repos/inspirehep/inspire-next/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/inspirehep/inspire-next/downloads","issues_url":"https://api.github.com/repos/inspirehep/inspire-next/issues{/number}","pulls_url":"https://api.github.com/repos/inspirehep/inspire-next/pulls{/number}","milestones_url":"https://api.github.com/repos/inspirehep/inspire-next/milestones{/number}","notifications_url":"https://api.github.com/repos/inspirehep/inspire-next/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/inspirehep/inspire-next/labels{/name}","releases_url":"https://api.github.com/repos/inspirehep/inspire-next/releases{/id}","deployments_url":"https://api.github.com/repos/inspirehep/inspire-next/deployments","created_at":"2013-12-16T09:11:58Z","updated_at":"2017-06-08T18:54:32Z","pushed_at":"2017-06-13T10:39:27Z","git_url":"git://github.com/inspirehep/inspire-next.git","ssh_url":"git@github.com:inspirehep/inspire-next.git","clone_url":"https://github.com/inspirehep/inspire-next.git","svn_url":"https://github.com/inspirehep/inspire-next","homepage":"https://labs.inspirehep.net","size":64297,"stargazers_count":27,"watchers_count":27,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":40,"mirror_url":null,"open_issues_count":277,"forks":40,"open_issues":277,"watchers":27,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/inspirehep/inspire-next/pulls/2430"},"html":{"href":"https://github.com/inspirehep/inspire-next/pull/2430"},"issue":{"href":"https://api.github.com/repos/inspirehep/inspire-next/issues/2430"},"comments":{"href":"https://api.github.com/repos/inspirehep/inspire-next/issues/2430/comments"},"review_comments":{"href":"https://api.github.com/repos/inspirehep/inspire-next/pulls/2430/comments"},"review_comment":{"href":"https://api.github.com/repos/inspirehep/inspire-next/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/inspirehep/inspire-next/pulls/2430/commits"},"statuses":{"href":"https://api.github.com/repos/inspirehep/inspire-next/statuses/32740c2914f63a5a2a6bee25d5520700d5b32b4e"}}}}
{"actor":{"display_login":"jmartinm"}}