Prototyping a plugin using User macros.
As a newbie in js I'm struggling with understanding the escaping of URL:s.
From the following post I get that the Ajax call expects "No quotes": https://community.atlassian.com/t5/Answers-Developer-Questions/AJAX-call-to-external-API-in-user-macro/qaq-p/494128
AJS.$.ajax({
url: URL_WITH_NO_QUOTES,
type: "GET",
contentType: "application/json",
success: function(msg){
alert(msg);
}
});
When experimenting with a call with a JQL with example below:
It works as long as I have an JQL that does nott contain any space or quotes.
For generic JQL:s I've attempted using "encodeURIComponent" or "encodeURI" - the REST call fails in the Ajax call, but works fine in a browser. I don't understand why?
Also tried with hardcoded "paths" with %20, %22 and so on, they alos work OK in broowser or command line, but fails in the USer Macro.
From: https://community.atlassian.com/t5/Jira-questions/How-to-perform-calculations-in-confluence-using-Jira-issues/qaq-p/1162542
jQuery.ajax({
type: "GET",
url: "<Confluence URL>/plugins/servlet/applinks/proxy?appId=<UUID>&path=<JIRA URL>/rest/api/latest/search/jql=<YOUR_JQL>",
dataType: "json",
async: false,
success: function( data ) {
//process data and render it in html tags
}
});
Another example attempting to get the Sum Story points printed from a JQL.
The path looks OK in console in browser, but fails in Ajax call! And Ok in browser
<script>
theurl = "/rest/scriptrunner-jira/latest/jqlfunctions/aggregateResult?jql=" + "$paramJql" + " AND issueFunction in aggregateExpression(\"Totalpoints\", \"storyPoints.sum()\")"
// theurl = "/rest/scriptrunner-jira/latest/jqlfunctions/aggregateResult?jql=" + "$paramJql" + " AND issueFunction in aggregateExpression(\"Totalpoints\", \"storyPoints.sum()\")"
// theurl = '/rest/scriptrunner-jira/latest/jqlfunctions/aggregateResult?jql=' + encodeURIComponent("$paramJql" + ' AND issueFunction in aggregateExpression("Totalpoints", "storyPoints.sum()")')
//encodeURIComponent
//encodeURI
console.log( "theurl: " + theurl);
AJS.toInit(function(){
jQuery.ajax({
type: "GET",
url: "https://confluence.assaabloy.net/plugins/servlet/applinks/proxy?appId=<UUID>&path=" + theurl,
dataType: "json",
async: false}).done(function (response) {
var txt3 = document.createElement("p"); // Create with DOM
var divContainer = document.getElementById("showJira");
one = response.Totalpoints;
txt3.innerHTML = txt3.innerHTML + "Total StoryPoints: " + one;
divContainer.innerHTML = "";
console.log( "Total StoryPoints: " + one);
console.log( "response: " + response);
divContainer.appendChild(txt3);
divContainer.innerHTML = "";
divContainer.appendChild(txt3);
});
});
</script>
The Ajax call returns with null.