Trying to create Script CQL Function to find related content from the labels on the current page.

Nathan Britton October 2, 2017

I am using ScriptRunner for Confluence and am attempting to expand the "Search Page By Label" Custom CQL Function example to be able to pull all the labels from the current page (where the CQL function is called) and return the related pages.

My ultimate goal is to not have to replicate adding labels on a page twice; once on the page itself and then once again in a related content by label macro. I would lie to simply add the CQL Search macro to my custom page template and allow the CQL function to provide the related content results when the page is loaded.

I have attempted to find a solution through both Atlassian and Adaptavist sites but have had no luck thus far. Also, to be completely honest, I am not familiar with groovy.

I have the following working if I pass in a single label from the CQL Search macro:

import com.atlassian.confluence.labels.Label
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.Space
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator

def spaceManager = ComponentLocator.getComponent(SpaceManager)
def pageManager = ComponentLocator.getComponent(PageManager)
String label = params[0]
String spaceKey = "OCCIKB"
def space = spaceManager.getSpace(spaceKey)
def ids = []

for (Page page : pageManager.getPages(space, true)) {
for (Label pageLabel : page.getLabels()) {
if (pageLabel.getName().equalsIgnoreCase(label)) {
ids << String.valueOf(page.getId())
}
}
}
return ids

What I would like to do is change the script to pull the list of labels from the current page and iterate over the list of labels and return the page ids of the pages that contain matching labels.

I have modified the above script to this:

import com.atlassian.confluence.labels.Label
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.Space
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator

def spaceManager = ComponentLocator.getComponent(SpaceManager)
def pageManager = ComponentLocator.getComponent(PageManager)
String spaceKey = "OCCIKB"
def space = spaceManager.getSpace(spaceKey)
def ids = []

for (Page page : pageManager.getPages(space, true)) {
for (Label myPageLabels : Page.getLabels()) {
String myPageLabel = myPageLabels
for (Label pageLabel : page.getLabels()) {
if (pageLabel.getName().equalsIgnoreCase(myPageLabel)) {
ids << String.valueOf(page.getId())
}
}
}
}
return ids

but am getting the following error returned:

Error rendering macro 'cql-search' : No signature of method: static com.atlassian.confluence.pages.Page.getLabels() is applicable for argument types: () values: [] Possible solutions: getLabels(), getLabelUtil(), getLabellings(), getParent(), getTeamLabels(com.atlassian.user.User), getClass()

I would appreciate any assistance in helping me understand how I can pull the labels attached to the current page.

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events