Skip to content

Instantly share code, notes, and snippets.

@jechlin-adaptavist
Last active February 19, 2024 17:04
Show Gist options
  • Save jechlin-adaptavist/cfdc4ba39ef73f3cff988b8c24abd3f5 to your computer and use it in GitHub Desktop.
Save jechlin-adaptavist/cfdc4ba39ef73f3cff988b8c24abd3f5 to your computer and use it in GitHub Desktop.
import java.util.stream.StreamSupport
def apiToken = API_TOKEN // Create a "Script Variable" for your api token with the name API_TOKEN. Create one at https://id.atlassian.com/manage-profile/security/api-tokens
def aqlQuery = 'objectType = Host' // AQL query - use the same query for the dropdown
def projectKey = 'JRA' // Project that we store the map of asset IDs and names on - any will do but use the one associated with this business process
def workspaceId = 'eaf39d78-2ea5-4a88-ad4f-35feb122f0c7' // Workspace ID - for assets - get it from https://<your-site>.atlassian.net/rest/servicedeskapi/assets/workspace
def search = { ->
def iterator = new Iterator<List<Map>>() {
private boolean hasMoreResults = true
private int offset = 0
@Override
boolean hasNext() {
hasMoreResults
}
@Override
List<Map> next() {
def results = post("https://api.atlassian.com/jsm/assets/workspace/$workspaceId/v1/object/aql?startAt=$offset")
.header("Content-Type", "application/json")
.header("Authorization", "Basic " + Base64.getEncoder().encodeToString(apiToken.bytes))
.body([
qlQuery: aqlQuery,
])
.asObject(Map)
.body
def assets = results.values as List
offset += assets.size()
hasMoreResults = results.total as Integer > offset
assets
}
}
StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false)
.flatMap(List::stream)
.iterator() as Iterator<Map>
}
def status = put("/rest/api/3/project/$projectKey/properties/KVP_List")
.header("Content-Type", "application/json")
.body(search().collectEntries { [it.id, it.name] } as Map)
.asString()
.status
assert status >= 200 && status < 300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment