2019-05-20 21:52:56 +02:00
|
|
|
// Main JavaScript file for the Integrations development panel at
|
|
|
|
// /devtools/integrations.
|
2019-04-14 15:28:19 +02:00
|
|
|
(function () {
|
|
|
|
|
2019-05-20 21:52:56 +02:00
|
|
|
// Data Segment: We lazy load the requested fixtures from the backend
|
|
|
|
// as and when required and then cache them here.
|
2019-04-14 15:28:19 +02:00
|
|
|
|
|
|
|
var loaded_fixtures = {};
|
|
|
|
var url_base = "/api/v1/external/";
|
|
|
|
|
2019-05-20 21:52:56 +02:00
|
|
|
// A map defining how to clear the various UI elements.
|
2019-04-14 15:28:19 +02:00
|
|
|
var clear_handlers = {
|
|
|
|
stream_name: "#stream_name",
|
|
|
|
topic_name: "#topic_name",
|
|
|
|
URL: "#URL",
|
2019-05-22 15:07:52 +02:00
|
|
|
results_notice: "#results_notice",
|
2019-04-14 15:28:19 +02:00
|
|
|
bot_name: function () { $('#bot_name').children()[0].selected = true; },
|
|
|
|
integration_name: function () { $('#integration_name').children()[0].selected = true; },
|
|
|
|
fixture_name: function () { $('#fixture_name').empty(); },
|
|
|
|
fixture_body: function () { $("#fixture_body")[0].value = ""; },
|
2019-05-21 05:38:58 +02:00
|
|
|
custom_http_headers: function () { $("#custom_http_headers")[0].value = "{}"; },
|
2019-05-18 20:59:37 +02:00
|
|
|
results: function () { $("#idp-results")[0].value = ""; },
|
2019-04-14 15:28:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function clear_elements(elements) {
|
2019-05-20 21:52:56 +02:00
|
|
|
// Supports strings (a selector to clear) or calling a function
|
|
|
|
// (for more complex logic).
|
2019-04-14 15:28:19 +02:00
|
|
|
elements.forEach(function (element_name) {
|
|
|
|
var handler = clear_handlers[element_name];
|
|
|
|
if (typeof handler === "string") {
|
|
|
|
var element_object = $(handler)[0];
|
|
|
|
element_object.value = "";
|
|
|
|
element_object.innerHTML = "";
|
|
|
|
} else {
|
|
|
|
handler();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-20 21:52:56 +02:00
|
|
|
// Success/failure colors used for displaying results to the user.
|
2019-05-22 15:07:52 +02:00
|
|
|
var results_notice_level_to_color_map = {
|
2019-04-14 15:28:19 +02:00
|
|
|
warning: "#be1931",
|
|
|
|
success: "#085d44",
|
|
|
|
};
|
|
|
|
|
2019-05-22 15:07:52 +02:00
|
|
|
function set_results_notice(msg, level) {
|
|
|
|
var results_notice_field = $("#results_notice")[0];
|
|
|
|
results_notice_field.innerHTML = msg;
|
|
|
|
results_notice_field.style.color = results_notice_level_to_color_map[level];
|
2019-04-14 15:28:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_api_key_from_selected_bot() {
|
|
|
|
return $("#bot_name").children("option:selected").val();
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_selected_integration_name() {
|
|
|
|
return $("#integration_name").children("option:selected").val();
|
|
|
|
}
|
|
|
|
|
2019-05-16 20:29:18 +02:00
|
|
|
function get_fixture_format(fixture_name) {
|
|
|
|
var pieces = fixture_name.split(".");
|
|
|
|
return pieces[pieces.length - 1];
|
|
|
|
}
|
|
|
|
|
2019-05-16 20:29:18 +02:00
|
|
|
function get_custom_http_headers() {
|
|
|
|
var custom_headers = $("#custom_http_headers").val();
|
|
|
|
if (custom_headers !== "") {
|
|
|
|
// JSON.parse("") would trigger an error, as empty strings do not qualify as JSON.
|
|
|
|
try {
|
|
|
|
// Let JavaScript validate the JSON for us.
|
|
|
|
custom_headers = JSON.stringify(JSON.parse(custom_headers));
|
|
|
|
} catch (err) {
|
2019-05-22 15:07:52 +02:00
|
|
|
set_results_notice("Custom HTTP headers are not in a valid JSON format.", "warning");
|
2019-05-16 20:29:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return custom_headers;
|
|
|
|
}
|
|
|
|
|
2019-05-18 20:59:37 +02:00
|
|
|
function set_results(response) {
|
2019-05-20 21:52:56 +02:00
|
|
|
/* The backend returns the JSON responses for each of the
|
|
|
|
send_message actions included in our request (which is just 1 for
|
|
|
|
send, but usually is several for send all). We display these
|
|
|
|
responses to the user in the "results" panel.
|
2019-05-18 20:59:37 +02:00
|
|
|
|
|
|
|
The following is a bit messy, but it's a devtool, so ultimately OK */
|
|
|
|
var responses = response.responses;
|
|
|
|
|
|
|
|
var data = "Results:\n\n";
|
|
|
|
responses.forEach(function (response) {
|
|
|
|
if (response.fixture_name !== undefined) {
|
|
|
|
data += "Fixture: " + response.fixture_name;
|
|
|
|
data += "\nStatus Code: " + response.status_code;
|
|
|
|
} else {
|
|
|
|
data += "Status Code: " + response.status_code;
|
|
|
|
}
|
|
|
|
data += "\nResponse: " + response.message + "\n\n";
|
|
|
|
});
|
|
|
|
$("#idp-results")[0].value = data;
|
|
|
|
}
|
|
|
|
|
2019-04-14 15:28:19 +02:00
|
|
|
function load_fixture_body(fixture_name) {
|
2019-05-20 21:52:56 +02:00
|
|
|
/* Given a fixture name, use the loaded_fixtures dictionary to set
|
|
|
|
* the fixture body field. */
|
2019-04-14 15:28:19 +02:00
|
|
|
var integration_name = get_selected_integration_name();
|
2019-06-07 20:06:06 +02:00
|
|
|
var fixture = loaded_fixtures[integration_name][fixture_name];
|
|
|
|
var fixture_body = fixture.body;
|
|
|
|
var headers = fixture.headers;
|
2019-04-14 15:28:19 +02:00
|
|
|
if (fixture_body === undefined) {
|
2019-05-22 15:07:52 +02:00
|
|
|
set_results_notice("Fixture does not have a body.", "warning");
|
2019-04-14 15:28:19 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-05-16 20:29:18 +02:00
|
|
|
if (get_fixture_format(fixture_name) === "json") {
|
2019-05-20 21:52:56 +02:00
|
|
|
// The 4 argument is pretty printer indentation.
|
|
|
|
fixture_body = JSON.stringify(fixture_body, null, 4);
|
2019-05-16 20:29:18 +02:00
|
|
|
}
|
2019-04-14 15:28:19 +02:00
|
|
|
$("#fixture_body")[0].value = fixture_body;
|
2019-06-07 20:06:06 +02:00
|
|
|
$("#custom_http_headers")[0].value = JSON.stringify(headers, null, 4);
|
2019-04-14 15:28:19 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function load_fixture_options(integration_name) {
|
2019-05-20 21:52:56 +02:00
|
|
|
/* Using the integration name and loaded_fixtures object to set
|
|
|
|
the fixture options for the fixture_names dropdown and also set
|
|
|
|
the fixture body to the first fixture by default. */
|
2019-04-14 15:28:19 +02:00
|
|
|
var fixtures_options_dropdown = $("#fixture_name")[0];
|
|
|
|
var fixtures_names = Object.keys(loaded_fixtures[integration_name]);
|
|
|
|
|
|
|
|
fixtures_names.forEach(function (fixture_name) {
|
|
|
|
var new_dropdown_option = document.createElement("option");
|
|
|
|
new_dropdown_option.value = fixture_name;
|
|
|
|
new_dropdown_option.innerHTML = fixture_name;
|
|
|
|
fixtures_options_dropdown.add(new_dropdown_option);
|
|
|
|
});
|
|
|
|
load_fixture_body(fixtures_names[0]);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_url() {
|
2019-05-20 21:52:56 +02:00
|
|
|
/* Construct the URL that the webhook should be targeting, using
|
|
|
|
the bot's API key and the integration name. The stream and topic
|
|
|
|
are both optional, and for the sake of completeness, it should be
|
|
|
|
noted that the topic is irrelavent without specifying the
|
|
|
|
stream. */
|
2019-04-14 15:28:19 +02:00
|
|
|
var url_field = $("#URL")[0];
|
|
|
|
|
|
|
|
var integration_name = get_selected_integration_name();
|
|
|
|
var api_key = get_api_key_from_selected_bot();
|
|
|
|
|
|
|
|
if (integration_name === "" || api_key === "") {
|
|
|
|
clear_elements(["URL"]);
|
|
|
|
} else {
|
|
|
|
var url = url_base + integration_name + "?api_key=" + api_key;
|
|
|
|
var stream_name = $("#stream_name").val();
|
|
|
|
if (stream_name !== "") {
|
|
|
|
url += "&stream=" + stream_name;
|
|
|
|
var topic_name = $("#topic_name").val();
|
|
|
|
if (topic_name !== "") {
|
|
|
|
url += "&topic=" + topic_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
url_field.value = url;
|
|
|
|
url_field.innerHTML = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// API Callers: These methods handle communicating with the Python backend API.
|
|
|
|
function handle_unsuccessful_response(response) {
|
|
|
|
try {
|
|
|
|
var status_code = response.statusCode().status;
|
|
|
|
response = JSON.parse(response.responseText);
|
2019-05-22 15:07:52 +02:00
|
|
|
set_results_notice("Result: " + "(" + status_code + ") " + response.msg, "warning");
|
2019-04-14 15:28:19 +02:00
|
|
|
} catch (err) {
|
2019-05-20 21:52:56 +02:00
|
|
|
// If the response is not a JSON response, then it is probably
|
|
|
|
// Django returning an HTML response containing a stack trace
|
|
|
|
// with useful debugging information regarding the backend
|
|
|
|
// code.
|
2019-04-14 15:28:19 +02:00
|
|
|
document.write(response.responseText);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_fixtures(integration_name) {
|
2019-05-20 21:52:56 +02:00
|
|
|
/* Request fixtures from the backend for any integrations that we
|
|
|
|
don't already have fixtures cached in loaded_fixtures). */
|
2019-04-14 15:28:19 +02:00
|
|
|
if (integration_name === "") {
|
2019-05-22 15:07:52 +02:00
|
|
|
clear_elements(["custom_http_headers", "fixture_body", "fixture_name", "URL", "results_notice"]);
|
2019-04-14 15:28:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loaded_fixtures[integration_name] !== undefined) {
|
|
|
|
load_fixture_options(integration_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-20 21:52:56 +02:00
|
|
|
// We don't have the fixtures for this integration; fetch them
|
|
|
|
// from the backend. Relative url pattern:
|
|
|
|
// /devtools/integrations/(?P<integration_name>.+)/fixtures
|
2019-04-14 15:28:19 +02:00
|
|
|
channel.get({
|
|
|
|
url: "/devtools/integrations/" + integration_name + "/fixtures",
|
2019-05-20 21:52:56 +02:00
|
|
|
// Since the user may add or modify fixtures as they edit.
|
|
|
|
idempotent: false,
|
2019-04-14 15:28:19 +02:00
|
|
|
success: function (response) {
|
|
|
|
loaded_fixtures[integration_name] = response.fixtures;
|
|
|
|
load_fixture_options(integration_name);
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
error: handle_unsuccessful_response,
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function send_webhook_fixture_message() {
|
2019-05-20 21:52:56 +02:00
|
|
|
/* Make sure that the user is sending valid JSON in the fixture
|
|
|
|
body and that the URL is not empty. Then simply send the fixture
|
|
|
|
body to the target URL. */
|
|
|
|
|
|
|
|
// Note: If the user just logged in to a different Zulip account
|
|
|
|
// using another tab while the integrations dev panel is open,
|
|
|
|
// then the csrf token that we have stored in the hidden input
|
|
|
|
// element would have been expired, leading to an error message
|
|
|
|
// when the user tries to send the fixture body.
|
2019-04-14 15:28:19 +02:00
|
|
|
var csrftoken = $("#csrftoken").val();
|
|
|
|
|
|
|
|
var url = $("#URL").val();
|
|
|
|
if (url === "") {
|
2019-05-22 15:07:52 +02:00
|
|
|
set_results_notice("URL can't be empty.", "warning");
|
2019-04-14 15:28:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var body = $("#fixture_body").val();
|
2019-05-16 20:29:18 +02:00
|
|
|
var fixture_name = $("#fixture_name").val();
|
|
|
|
var is_json = false;
|
|
|
|
if (fixture_name && get_fixture_format(fixture_name) === "json") {
|
|
|
|
try {
|
|
|
|
// Let JavaScript validate the JSON for us.
|
|
|
|
body = JSON.stringify(JSON.parse(body));
|
|
|
|
is_json = true;
|
|
|
|
} catch (err) {
|
2019-05-22 15:07:52 +02:00
|
|
|
set_results_notice("Invalid JSON in fixture body.", "warning");
|
2019-05-16 20:29:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-04-14 15:28:19 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 20:29:18 +02:00
|
|
|
var custom_headers = get_custom_http_headers();
|
2019-05-16 09:23:15 +02:00
|
|
|
|
2019-04-14 15:28:19 +02:00
|
|
|
channel.post({
|
|
|
|
url: "/devtools/integrations/check_send_webhook_fixture_message",
|
2019-05-16 20:29:18 +02:00
|
|
|
data: {url: url, body: body, custom_headers: custom_headers, is_json: is_json},
|
2019-04-14 15:28:19 +02:00
|
|
|
beforeSend: function (xhr) {xhr.setRequestHeader('X-CSRFToken', csrftoken);},
|
2019-05-18 20:59:37 +02:00
|
|
|
success: function (response) {
|
2019-05-20 21:52:56 +02:00
|
|
|
// If the previous fixture body was sent successfully,
|
|
|
|
// then we should change the success message up a bit to
|
|
|
|
// let the user easily know that this fixture body was
|
|
|
|
// also sent successfully.
|
2019-05-18 20:59:37 +02:00
|
|
|
set_results(response);
|
2019-05-22 15:07:52 +02:00
|
|
|
if ($("#results_notice")[0].innerHTML === "Success!") {
|
|
|
|
set_results_notice("Success!!!", "success");
|
2019-04-14 15:28:19 +02:00
|
|
|
} else {
|
2019-05-22 15:07:52 +02:00
|
|
|
set_results_notice("Success!", "success");
|
2019-04-14 15:28:19 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
error: handle_unsuccessful_response,
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-16 20:29:18 +02:00
|
|
|
function send_all_fixture_messages() {
|
|
|
|
/* Send all fixture messages for a given integration. */
|
|
|
|
var url = $("#URL").val();
|
|
|
|
var integration = get_selected_integration_name();
|
|
|
|
if (integration === "") {
|
2019-05-22 15:07:52 +02:00
|
|
|
set_results_notice("You have to select an integration first.");
|
2019-05-16 20:29:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var custom_headers = get_custom_http_headers();
|
|
|
|
|
|
|
|
var csrftoken = $("#csrftoken").val();
|
|
|
|
channel.post({
|
|
|
|
url: "/devtools/integrations/send_all_webhook_fixture_messages",
|
|
|
|
data: {url: url, custom_headers: custom_headers, integration_name: integration},
|
|
|
|
beforeSend: function (xhr) {xhr.setRequestHeader('X-CSRFToken', csrftoken);},
|
2019-05-18 20:59:37 +02:00
|
|
|
success: function (response) {set_results(response);},
|
2019-05-16 20:29:18 +02:00
|
|
|
error: handle_unsuccessful_response,
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-14 15:28:19 +02:00
|
|
|
// Initialization
|
|
|
|
$(function () {
|
|
|
|
clear_elements(["stream_name", "topic_name", "URL", "bot_name", "integration_name",
|
2019-05-22 15:07:52 +02:00
|
|
|
"fixture_name", "custom_http_headers", "fixture_body", "results_notice",
|
2019-05-18 20:59:37 +02:00
|
|
|
"results"]);
|
2019-04-14 15:28:19 +02:00
|
|
|
|
2019-05-16 15:40:53 +02:00
|
|
|
$("#stream_name")[0].value = "Denmark";
|
|
|
|
$("#topic_name")[0].value = "Integrations Testing";
|
|
|
|
|
2019-04-14 15:28:19 +02:00
|
|
|
var potential_default_bot = $("#bot_name")[0][1];
|
|
|
|
if (potential_default_bot !== undefined) {
|
|
|
|
potential_default_bot.selected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#integration_name').change(function () {
|
2019-05-22 15:07:52 +02:00
|
|
|
clear_elements(["custom_http_headers", "fixture_body", "fixture_name", "results_notice"]);
|
2019-04-14 15:28:19 +02:00
|
|
|
var integration_name = $(this).children("option:selected").val();
|
|
|
|
get_fixtures(integration_name);
|
|
|
|
update_url();
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#fixture_name').change(function () {
|
2019-05-22 15:07:52 +02:00
|
|
|
clear_elements(["fixture_body", "results_notice"]);
|
2019-04-14 15:28:19 +02:00
|
|
|
var fixture_name = $(this).children("option:selected").val();
|
|
|
|
load_fixture_body(fixture_name);
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#send_fixture_button').click(function () {
|
|
|
|
send_webhook_fixture_message();
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
|
2019-05-16 20:29:18 +02:00
|
|
|
$('#send_all_fixtures_button').click(function () {
|
2019-05-22 15:07:52 +02:00
|
|
|
clear_elements(["results_notice"]);
|
2019-05-16 20:29:18 +02:00
|
|
|
send_all_fixture_messages();
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
|
2019-04-14 15:28:19 +02:00
|
|
|
$("#bot_name").change(update_url);
|
|
|
|
|
|
|
|
$("#stream_name").change(update_url);
|
|
|
|
|
|
|
|
$("#topic_name").change(update_url);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}());
|