Skip to content

Instantly share code, notes, and snippets.

@rlsit
Last active June 2, 2019 19:25
Show Gist options
  • Save rlsit/15f9d1e99274cfdbd2b400ea6284965b to your computer and use it in GitHub Desktop.
Save rlsit/15f9d1e99274cfdbd2b400ea6284965b to your computer and use it in GitHub Desktop.
our own crutchy solution for https://jira.atlassian.com/browse/JSDSERVER-170 linking a additional confluence space into jira issues
//
// a hackish way to link a additional confluence space into jira issues
// if you like to use this, pls search for all TODO tags and adjust at least these to your needs
//
// requires: js-combinatorics
//
// I publish this code snippet in the hope someone might find it useful and with the express note:
// * no warranty
// * use at your own risk
// * ugly code combined with cerebral diarrhea: you might risk to vomit
//
// TODO: the confluence space key to search in
var cspacekey = "CONFSPACEKEY"
// TODO: define the Confluence APPlikation link ID
// find this via /rest/applinks/1.0/applicationlink
var cappid = "00000000-6666-0000-1111-000000000000"
// TODO: define list of keywords that should be ignored
var IGNOREWORDS = [
'computer', 'test', 'problem'
]
// TODO: define list of keywords that should be used as negative search args
var NEGATIVESEARCHWORDS = [ "XXX", "TODO" ]
// +++++++++++++++++++ //
// REST ajax functions //
// +++++++++++++++++++ //
function isAssignableUser() {
ret = false;
AJS.$.ajax({
url: contextPath+"/rest/api/2/mypermissions",
type: 'get',
dataType: 'json',
jsonp: false,
async: false,
success: function(data) {
ret = data.permissions.ASSIGNABLE_USER.havePermission
},
error: function(o, err, exx) {
console.error(err+":"+exx)
}
});
return ret;
}
function getUser() {
AJS.$.ajax({
url: contextPath+"/rest/auth/1/session",
type: 'get',
dataType: 'json',
jsonp: false,
async: false,
success: function(data) {
ret = data.name
},
error: function(o, err, exx) {
console.error(err+":"+exx)
}
});
return ret;
}
// +++++++++++++++++++++++ //
// jira 2 confluence link //
// +++++++++++++++++++++++ //
var cqlSearchAndDisplayResults = []
function cqlSearchAndDisplay(cql, maxResults) {
var maxResults = maxResults || 10
var ret = 0
var restURL = AJS.params.baseURL+"/rest/confluenceIssueLink/1/confluence/search?query="+encodeURIComponent(cql)+"&appId="+cappid+"&maxResults="+maxResults+"&spaceKey="+cspacekey
AJS.$("#JIRA2CONFLUENCEcont").append("<!-- // "+restURL+" // -->")
var singleLineH = 20
AJS.$.ajax({
url: restURL,
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
results = data.values
for (var r=0; r<results.length; r++) {
res = results[r]
if ( cqlSearchAndDisplayResults.indexOf(res.url) >= 0) {
// avoid duplicates
continue
}
cqlSearchAndDisplayResults.push(res.url)
AJS.$("#JIRA2CONFLUENCEcont").append("<div id='JIRA2CONFLUENCEcont"+r+"' style='margin:2px;height:"+singleLineH+"px;overflow:hidden;'>"
+"<a href='"+res.url+"' target='_blank'>"+res.title+"</a> <span style='font-size: small;color:grey'>"
+res.excerpt+"</span></div>\n")
AJS.$("#JIRA2CONFLUENCEcont"+r).click(function() {
var currentH = AJS.$(this).height();
console.log("currentH:"+currentH)
if (currentH == singleLineH) {
AJS.$(this).css('height', 'auto');
var fullHeight = AJS.$(this).height();
AJS.$(this).height(currentH);
AJS.$(this).animate({height: fullHeight}, 500);
} else {
AJS.$(this).animate({height: singleLineH}, 500);
}
})
}
ret = results.length
},
error: function(o, err, exx) {
AJS.$("#JIRA2CONFLUENCEcont").append('<div style="color:red"><b>'+err+'</b><br />'+exx+'</div>')
}
})
return ret
}
var assignableUser = isAssignableUser()
var jiraUser = getUser()
function Jira2ConfDo() {
if ( AJS.$("#JIRA2CONFLUENCEcont").length > 0 ) {
// avoid duplicated calls
return true
}
// TODO: define condition to match the jira project key where to display the additional list of links
if ( WRM._unparsedData["projectKey"]="\"JIRAPROJECTKEY\"" && assignableUser && AJS.$("#summary-val").text()) {
console.log("jira2confluence start ...")
// keep this lowercase
AJS.$('<div id="JIRA2CONFLUENCE" class="module toggle-wrap expanded">'
+'<div class="mod-header"><h2 class="toggle-title">Confluence Links</h2></div>'
+'<div class="mod-content" id="JIRA2CONFLUENCEcont"></div></div>').insertBefore( AJS.$('#knowledgebase-issueview-panel') );
console.log("jira2confluence JIRA2CONFLUENCE created ...")
summarywords = AJS.$("#summary-val").text().split(/[- \/,.]/)
var sumwordarr = []
for (var i=0; i < summarywords.length; i++) {
// strip non word chars
sumword = summarywords[i].replace(/\W+/g, '').toLowerCase();
// ignore numbers only
if ( /^\d+$/.test(sumword) ) {
continue
}
if (sumword.length < 3 || AJS.$.inArray(sumword, IGNOREWORDS) > -1) {
continue
}
//console.log("jira2confluence sumword: "+sumword)
sumwordarr.push(sumword)
}
// add issuetype, labels and components
var components = AJS.$('#components-field').text().split(/[ ,]+/)
for (var i=0; i < components.length; i++) {
kw = components[i].trim().toLowerCase()
if (kw.length == 0) continue
if (kw == 'none') continue
if ( AJS.$.inArray(kw, sumwordarr) == -1) {
sumwordarr.push(kw)
}
}
var labels = AJS.$("#wrap-labels").text().trim().split(/[ ,:]+/)
labels.splice(0,1)
labels.splice(-1,1)
for (var i=0; i < labels.length; i++) {
kw = labels[i].trim().toLowerCase()
if (kw.length == 0) continue
if (kw == 'none') continue
if ( AJS.$.inArray(kw, sumwordarr) == -1) {
sumwordarr.push(kw)
}
}
var issuetype = AJS.$("#type-val").text().trim().toLowerCase()
var ignoreIssueTypesAsKeyword = [ "incident", "change", "problem" ]
if (ignoreIssueTypesAsKeyword.indexOf(issuetype) >= 0) {
sumwordarr.push(issuetype)
}
var cqlresults = 0
if (sumwordarr.length > 1) {
// match min. 2 summary words
// js-combinatorics loaded via Announcement Banner: version 0.5.4
sumwordarrcomb = Combinatorics.combination(sumwordarr, 2)
var cql = ""
while(cmb = sumwordarrcomb.next()) {
if ( cql.length > 0 ) {
cql += " OR "
}
cql += "("+cmb.join(" ")+")"
}
if (cql.length > 0) {
// ignore NEGATIVESEARCHWORDS
cql = "("+cql+") -"+NEGATIVESEARCHWORDS.join(" -")
cqlresults = cqlSearchAndDisplay(cql)
}
}
if ( cqlresults <= 3) {
// if the search matching 2 words did not find sufficient results
// fall back to simple search
cql = "("+sumwordarr.join(" OR ")+")-"+NEGATIVESEARCHWORDS.join("-")
if (cqlSearchAndDisplay(cql, 10-cqlresults) == 0) {
AJS.$("#JIRA2CONFLUENCEcont").append('<b>no results</b>')
console.log("jira2confluence : no results")
}
}
console.log("jira2confluence done")
}
}
// hackish way to fix the late load in servicedesk view
function Jira2ConfLoadTry() {
if (!($("#knowledgebase-issueview-panel").size() && AJS.params.baseURL)) {
window.requestAnimationFrame(Jira2ConfLoadTry);
} else {
Jira2ConfDo()
}
}
Jira2ConfLoadTry()
// vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2 smartindent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment