As promised from Get spaces by keys with the Confluence API article. below are the methods in a class that I created and examples of how to call them so that they're reusable.
I decided to create a separate article since the other one would have gotten huge and confusing.
You can call your package whatever you want. I have a folder in the ScriptRunner Script Editor called util where I put reusable methods.
I also left out the notes for each line of code in this article.
package util
import com.atlassian.confluence.api.model.content.Space
import com.atlassian.confluence.api.model.Expansion
import com.atlassian.confluence.api.service.content.SpaceService
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.atlassian.confluence.api.model.pagination.SimplePageRequest
import com.atlassian.confluence.api.model.pagination.PageResponse
import org.apache.log4j.Logger
import org.apache.log4j.Level
public class getSpace {
public static Space getSpaceByKey(String spaceKey) {
// find a single space by getting the space key
SpaceService spaceService = ScriptRunnerImpl.getPluginComponent(SpaceService)
try {
Space space = spaceService.find(new Expansion('name'))
.withKeys(spaceKey)
.fetch()
.get()
return space
} catch (NoSuchElementException nse) {
return null
}
}
public static List<Space> getSpacesByKeys(spaceKeys, int minResults, int maxResults) {
// find multiple spaces by passing a String array of space keys
SpaceService spaceService = ScriptRunnerImpl.getPluginComponent(SpaceService)
PageResponse<Space> spaceResponse = spaceService.find(new Expansion('name'))
.withKeys(spaceKeys)
.fetchMany(new SimplePageRequest(minResults, maxResults))
List<Space> spaces = spaceResponse.getResults()
return spaces
}
}
I included a Dynamic Form on this one to make it easy to lookup a spacekey, but really, you just need the line that starts with "Space" to get the space object.
import util.getSpace
import com.atlassian.confluence.api.model.content.Space
import com.onresolve.scriptrunner.parameters.annotation.*
@ShortTextInput(label = "Space Key", description = "Enter a single space key")
String spaceKey
Space space = getSpace.getSpaceByKey(spaceKey)
if (!space) {return "No spaces found."}
String result = "<p>${space.getKey()}: ${space.getName()}</p>"
spaceKeys must be a String Array or it won't work. Again, you really only need the line that starts with List<Space> (you don't have to specify the object type there), and you'll need to loop through the list to get the spaces. The rest is for the example.
import util.getSpace
import com.atlassian.confluence.api.model.content.Space
String result = ""
String [] spaceKeys = ['ABC','TEST']
List<Space> spaces = getSpace.getSpacesByKeys(spaceKeys,0,10)
if (!spaces) {return "No spaces found."}
spaces.each { space ->
result += "<p>${space.getKey()}: ${space.getName()}</p>"
}
return result
You'll still need the main class in full, but you can shrink the calling code down if you want.
import util.getSpace
def space = getSpace.getSpaceByKey(spaceKey)
import util.getSpace
def spaces = getSpace.getSpacesByKeys(['ABC','TEST'],0,10)
spaces.each { space ->
space.getName() do whatever...
}
There you go: a somewhat simple way of making what was in the first article reusable.
WW
2 comments