Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ticket opening from topology #401

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions requirements-rootless.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ urllib3==1.23
Werkzeug>=0.14.1,<0.15
WTForms>=2.2.1,<2.3
xmltodict>=0.11.0,<0.12
bleach
Markdown
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ xmltodict>=0.11.0,<0.12
mod-wsgi>=4.6.4,<4.7
# note: python-ldap requires the openldap header files (redhat package openldap-devel, ubuntu package libldap2-dev)
python-ldap>=3.1.0,<3.2.0
bleach
Markdown
67 changes: 67 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import sys
import traceback
import urllib.parse
import json
import requests
import bleach
import markdown

from webapp import default_config
from webapp.common import to_xml_bytes, Filters
Expand Down Expand Up @@ -108,6 +112,69 @@ def contacts():
app.log_exception(sys.exc_info())
return Response("Error getting users", status=503) # well, it's better than crashing

@app.route('/ticket')
def ticket():
return _fix_unicode(render_template('ticket.html.j2'))

@app.route('/submitTicket', methods=['POST'])
def submitTicket():
information = request.form.to_dict(flat=False)
# First, check that the request passed captcha
if 'g-recaptcha-response' not in information:
return "Hello Bot"
captcha_url = "https://www.google.com/recaptcha/api/siteverify"
captcha_data = {
'secret': app.config['GOOGLE_CAPTCHA_KEY'],
'response': information['g-recaptcha-response'][0]
}
captcha_response = requests.post(captcha_url, data = captcha_data)
json_response = captcha_response.json()
if json_response['success'] is False:
return "Failed to request Captcha information"

if json_response['score'] < 0.5:
return "Your captcha score was too low, not submitting ticket"

cc_emails = set()
for facility in global_data.get_topology().resources_by_facility.values():
for resource in facility:
if resource.name in information['resource-list']:
resource_tree = resource.get_tree(authorized = True)
contact_list = resource_tree['ContactLists']['ContactList']
for contact in contact_list:
if contact["ContactType"] == "Administrative Contact":
for inner_contact in contact['Contacts']['Contact']:
cc_emails.add(inner_contact['Email'])

# Use Bleach (from mozilla) to sanitize the html input
cleaned_desc = bleach.clean(information['ticket-message'][0])
# Then convert mardown to HTML
html_desc = markdown.markdown(cleaned_desc)

ticket = {
'subject': information['subject'][0],
'description': html_desc,
'email': information['requester'][0],
'priority': 1,
'status': 2,
'cc_emails': list(cc_emails),
'type': 'Other'
}
headers = { 'Content-Type' : 'application/json' }

api_key = app.config['FRESHDESK_API_KEY']
url_request = requests.post("https://opensciencegrid.freshdesk.com/api/v2/tickets",
data = json.dumps(ticket),
auth = (api_key, 'x'),
headers = headers)

if url_request.status_code in [ 200, 201 ]:
json_return = url_request.json()
return_str = "Created Ticket {}".format(json_return['id'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should tell users that they'll receive an email shortly. Would it be possible to have this show up as a message on the form page? The page with the return_str is a little bare.

return return_str
else:
message = url_request.json()['message']
return "Failed to submit ticket with error: {}".format(message)

@app.route('/miscproject/xml')
def miscproject_xml():
Expand Down
14 changes: 3 additions & 11 deletions src/templates/base.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,20 @@
href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha256-eSi1q2PG6J7g7ib17yAaWMcrr5GrtohYChqibrV7PBE="
crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"
matyasselmeci marked this conversation as resolved.
Show resolved Hide resolved
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
{% block morehead %}{% endblock %}
</head>
<body>
{% if want_js -%}
<div class="nojs"><!-- there's some JS below to remove this if jquery successfully loads -->
<div class="alert alert-danger" role="alert">Please enable JavaScript and allow our JavaScript libraries to load.</div>
</div>
{%- endif %}
<h1 class="text-center">{{ self.title() }}{% if config.INSTANCE_NAME %} ({{ config.INSTANCE_NAME }}){% endif %}</h1>
{% block content %}{% endblock %}
{% if want_js -%}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"
integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"
integrity="sha256-VsEqElsCHSGmnmHXGQzvoWjWwoznFSZc6hs7ARLRacQ="
crossorigin="anonymous"></script>
<script>$(".nojs").remove();</script>
{%- endif %}

<!-- Freshdesk Support Widget -->
<script type="text/javascript" src="https://s3.amazonaws.com/assets.freshdesk.com/widget/freshwidget.js"></script>
Expand Down
1 change: 1 addition & 0 deletions src/templates/homepage.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ OSG Topology Interface
<h2>Human-readable pages</h2>
<ul>
<li><a href="generate_downtime">Generate a downtime entry for a resource</a></li>
<li><a href="ticket">Generate a ticket for a resource</a></li>
<li><a href="contacts">List of OSG contacts</a></li>
<li><a href="map/iframe">Map of OSG sites</a></li>
</ul>
Expand Down
153 changes: 153 additions & 0 deletions src/templates/ticket.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{% set want_js = true %}
{% extends "base.html.j2" %}
{% block title -%}
OSG Ticket
{%- endblock %}
morehead

{% block morehead %}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.bootstrap3.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/js/standalone/selectize.min.js" integrity="sha256-+C0A5Ilqmu4QcSPxrlGpaZxJ04VjsRjKu+G82kl5UJk=" crossorigin="anonymous"></script>
<script src="https://www.google.com/recaptcha/api.js?render=6LdJl44UAAAAAGaTKjXWjhPJ5MvyXCAYGxd2m4TS"></script>

<style>
.selectize-control .option .name {
display: block;
}
.selectize-control .option .facility {
font-size: 12px;
display: block;
color: #a0a0a0;
}
.selectize-control .item a {
color: #006ef5;
}
.selectize-control .item.active a {
color: #303030;
}
.selectize-control .optgroup-header {
display: block;
font-size: 1.2em;
color: #303030;
}
</style>


<script>

$(function(){

grecaptcha.ready(function() {
grecaptcha.execute('6LdJl44UAAAAAGaTKjXWjhPJ5MvyXCAYGxd2m4TS', {action: 'ticket'}).then(function(token) {
$("#g-recaptcha-response").attr("value", token);
});
});

var $select = $('#resource-list').selectize({
maxItems: null,
valueField: 'name',
labelField: 'name',
searchField: ['name', 'facility'],
options: [],
create: false,
optgroupField: 'facility',
optgroupLabelField: 'facility',
optgroupValueField: 'facility',
render: {
option: function(data, escape) {
return '<div class="option">' +
'<span class="name">' + escape(data.name) + '</span>' +
'<span class="facility">' + escape(data.type) + '</span>' +
'</div>';
},
item: function(data, escape) {
return '<div class="item">' + escape(data.name) + '</a></div>';
},
optgroup_header: function(data, escape) {
return '<div class="optgroup-header">' + escape(data.facility) + '</div>';
}
}});

var control = $select[0].selectize;
// Everything goes here
// Parse the XML
function parseResources(data) {
// The returned data should be XML
$(data).find("ResourceGroup").each(function(index, rg) {
var facility = $(rg).find("Facility").find("Name").first();
console.log(facility[0].innerHTML);
var facility_name = facility[0].innerHTML;
control.addOptionGroup(facility_name, {
facility: facility_name
})
var resource_list = $(rg).find("Resource");
for (var i = 0; i < resource_list.length; i++) {
var resource_name = $(resource_list[i]).find("Name")[0].innerHTML;
var resource_id = $(resource_list[i]).find("ID")[0].innerHTML;
var services = $(resource_list[i]).find("Services")[0];
var service = $(services).find("Service")[0]
var type = $(service).find("Name")[0].innerHTML;
//console.log(resource_name)
control.addOption({
id: resource_id,
name: resource_name,
facility: facility_name,
type: type
});
}
console.log(rg);
});

}

// Load the resource
function loadResources() {
$.get("/rgsummary/xml", parseResources);
}

loadResources();

});


</script>



{% endblock %}

{% block content %}
<div class="container">
<p class="lead">
Use this form to open tickets regarding issues on <a href="https://opensciencegrid.org/">Open Science Grid</a> Resources. After submitting this form you will receive an email confirmation of the ticket creation.
</p>
<hr />
<form action="/submitTicket" method="POST">
<div class="form-group">
<label for="resource-list">Resource(s)</label>
<select name="resource-list" id="resource-list" multiple placeholder="Pick OSG Resource(s)..."></select>
</div>
<div class="form-group">
<label for="requester">Requester Email</label>
<input type="email" name="requester" class="form-control" id="requester" required placeholder="Requester Email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$"></input>
</div>
<div class="form-group">
<label for="Subject">Summary</label>
<input type="text" name="subject" class="form-control" id="subject" required></input>
</div>
<div class="form-group">
<label for="ticket-message">
Description of the issue
</label>
<textarea name="ticket-message" id="ticket-message" class="form-control" rows="10" cols="50" required></textarea>
<small id="ticket-help" class="form-text text-muted">Supports Markdown <a href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet">syntax</a></small>
</div>
<input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response" class="g-recaptcha"></input>
<button type="submit" class="btn btn-primary">Submit</button>
</form>



</div>
{% endblock %}