I stopped due to the fact (and built in automation has the same) - The event "page updated" or "page status changed" fires after the page is persisted with the content status.
So - When my automation - or Scriptrunner listener - sets the content status back from "Verified" to "In Progress" - the will be an extra Page version (14) shown as "Verified" in the History, but it has actually changed from the actually verified (13):
For this to be acceptable, we needed to change the Content Status due save, before creating a new version with "Verified"
This was a test for using Content Status in Cloud:
Listener
A listener that:
- Maintain latest approved version in a Page property
- If a "Verified" version is Edited, move the Page to "In Progres"
Code
import groovy.json.JsonSlurper
Boolean propertyExists = true
Integer propertyVersionInt = 0
String pageId = page.id
String lastVerifiedVersion = 0
//Get Page State
def result1 = Unirest.get("https://site.atlassian.net/wiki/rest/api/content/" + pageId + "/state")
.header("Accept", "application/json")
.asJson();
def json1 = new JsonSlurper().parseText(result1.getBody().toString())
def contentStatus = json1.contentState.name
//Get Page Version:
def result2 = Unirest.get("https://site.atlassian.net/wiki/rest/api/content/" + pageId + "/version")
.header("Accept", "application/json")
.asJson();
def json2 = new JsonSlurper().parseText(result2.getBody().toString())
def pageVersion = json2.results.number[0]
//Does property and Key exist:
def result3 = Unirest.get("https://site.atlassian.net/wiki/rest/api/content/" + pageId + "/property/lastVerififedVersion")
.header("Accept", "application/json")
.asJson();
if (result3.getStatus().toString() == "404")
{
propertyExists = false
}
else
{
def json3 = new JsonSlurper().parseText(result3.getBody().toString())
lastVerifiedVersion = json3.value.verified
def propertyVersion = json3.version.number
propertyVersionInt = propertyVersion.toInteger() + 1
}
if (contentStatus == "Verified")
{
logger.info("Page Version: " + pageVersion)
logger.info("Last Verified Version: "+ lastVerifiedVersion)
if (pageVersion.toInteger() > lastVerifiedVersion.toInteger()-1)
{
logger.info ("Page updated by a User in Verified Mode - Bump back to In progress")
//Get space key
def result5 = Unirest.get("https://site.atlassian.net/wiki/rest/api/content/" + pageId)
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.asJson();
def json5 = new JsonSlurper().parseText(result5.getBody().toString())
def spaceKey = json5.space.key
//Get Space content status
def result6 = Unirest.get("https://site.atlassian.net/wiki/rest/api/space/" + spaceKey + "/state/settings")
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.asJson();
def json6 = new JsonSlurper().parseText(result6.getBody().toString())
def inProgressId = json6.spaceContentStates.find{it.name == "In progress"}.id
def inProgressColor= json6.spaceContentStates.find{it.name == "In progress"}.color
def inProgressName ="In progress"
// Set Content Status to "In progress"
logger.info("Set Content Status to: In progress")
def result7 = Unirest.put("https://site.atlassian.net/wiki/rest/api/content/" + pageId + "/state?status=current")
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.body("{\"id\":\"" + inProgressId.toString() + "\",\"color\":\"" + inProgressColor + "\",\"name\":\"" + inProgressName + "\"}")
.asJson();
}
else
{
if (!propertyExists)
{
//Create property
logger.info ("Create property: lastVerififedVersion")
def result4 = Unirest.post("https://site.atlassian.net/wiki/rest/api/content/" + pageId + "/property/lastVerififedVersion")
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.body("{\"key\":\"lastVerififedVersion\",\"value\":{\"verified\":\"" + pageVersion + "\"}}")
.asJson();
}
else
{
//Update property
logger.info ("Update property: lastVerififedVersion - new version: " + pageVersion)
def result4 = Unirest.put("https://site.atlassian.net/wiki/rest/api/content/" +pageId + "/property/lastVerififedVersion")
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.body("{\"key\":\"lastVerififedVersion\",\"value\":{\"verified\":\"" + pageVersion + "\"},\"version\":{\"number\":" + propertyVersionInt + "}}")
.asJson();
}
}
}
Macro
That shows if You are on the newest Verified version or not:
Code
import groovy.json.JsonSlurper
//Get Page:
def result1 = Unirest.get("https://site.atlassian.net/wiki/rest/api/content/" + parameters.pageId + "/version")
.header("Accept", "application/json")
.asJson();
def json1 = new JsonSlurper().parseText(result1.getBody().toString())
logger.info ("Result: " + result1.getBody().toString())
def currentVersion = json1.results.number[0]
//Get Last verified version:
def result2 = Unirest.get("https://site.atlassian.net/wiki/rest/api/content/" + parameters.pageId + "/property/lastVerififedVersion")
.header("Accept", "application/json")
.asJson();
def json2 = new JsonSlurper().parseText(result2.getBody().toString())
def lastVerifiedVersion = json2.value.verified
logger.info ("Current: " + currentVersion)
logger.info ("Latest: " + lastVerifiedVersion)
if (currentVersion.toInteger() == lastVerifiedVersion.toInteger())
{
return "<ac:structured-macro ac:name=\"tip\" ac:schema-version=\"1\" ac:macro-id=\"3733808d-4209-48db-9a27-2086bf26ab7f\"><ac:rich-text-body><p>Your are on the latest verified version: " + lastVerifiedVersion + "</p></ac:rich-text-body></ac:structured-macro>"
}
else
{
return "<ac:structured-macro ac:name=\"warning\" ac:schema-version=\"1\" ac:macro-id=\"3733808d-4209-48db-9a27-2086bf26ab7f\"><ac:rich-text-body><p>Your are not on the latest verified version - Latest is: <a href='https://site.atlassian.net/wiki/pages/viewpage.action?pageId=" + parameters.pageId + "&pageVersion=" + lastVerifiedVersion + "'>" + lastVerifiedVersion + "</a></p></ac:rich-text-body></ac:structured-macro>"
}

