Skip to content

Commit

Permalink
[JENKINS-74011] Extract JavaScript block in `PageStatePreloadDecorato…
Browse files Browse the repository at this point in the history
…r/header.jelly`
  • Loading branch information
basil committed Nov 19, 2024
1 parent be4d0c9 commit 455d26c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class FavoriteListStatePreloaderTest extends PipelineBaseTest {

Expand Down Expand Up @@ -66,15 +63,13 @@ public void simpleTest() throws Exception {
.asString();

Document doc = Jsoup.parse(response.getBody());
String script = doc.select("head script").toString();
Optional<String> preloadLine = Arrays.stream(script.split("\n")).filter(s -> s.trim().startsWith("setState('favoritesList'")).findFirst();
if (!preloadLine.isPresent()) fail("preloadLine missing in page");
String script = doc.select("head script#blueocean-page-state-preload-decorator-data").html().toString();
JSONObject json = JSONObject.fromObject(script);

Pattern pa = Pattern.compile("^\\s*setState\\('favoritesList',\\s*(.*)\\);$");
final Matcher matcher = pa.matcher(preloadLine.get());
if (!matcher.find()) fail("invalid preload line: " + preloadLine.get());
Assert.assertTrue(json.containsKey("favoritesList"));
JSONArray favoritesList = json.getJSONArray("favoritesList");

final String data = matcher.group(1);
final String data = favoritesList.toString();
Assert.assertTrue("master branch not include or not primary", data.contains("{\"name\":\"p-master/master\",\"primary\":true}"));
Assert.assertTrue("feature2 branch not include or primary", data.contains("{\"name\":\"p-master/feature2\",\"primary\":false}"));
Assert.assertTrue("feature4 branch not include or primary", data.contains("{\"name\":\"p-master/feature4\",\"primary\":false}"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.SAXException;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
Expand All @@ -55,10 +56,11 @@ public void test() throws IOException, ExecutionException, InterruptedException,
BlueOceanUrlMapper mapper = BlueOceanUrlMapper.all().get(0);
String projectBlueUrl = j.jenkins.getRootUrl() + mapper.getUrl(freestyleProject);
Document doc = Jsoup.connect(projectBlueUrl + "/activity/").get();
String script = doc.select("head script").toString();
String script = doc.select("head script#blueocean-page-state-preload-decorator-data").html().toString();
JSONObject json = JSONObject.fromObject(script);

Assert.assertTrue(script.contains(String.format("setState('prefetchdata.%s',", PipelineStatePreloader.class.getSimpleName())));
Assert.assertTrue(script.contains(String.format("setState('prefetchdata.%s',", PipelineActivityStatePreloader.class.getSimpleName())));
Assert.assertTrue(json.containsKey(String.format("prefetchdata.%s", PipelineStatePreloader.class.getSimpleName())));
Assert.assertTrue(json.containsKey(String.format("prefetchdata.%s", PipelineActivityStatePreloader.class.getSimpleName())));
Assert.assertTrue(script.contains("\"restUrl\":\"/blue/rest/organizations/jenkins/pipelines/freestyle/runs/?start=0&limit=26\""));
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,13 @@
<?jelly escape-by-default='false'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler">
<script>//&lt;![CDATA[
// construct the state object parent path inside window.$blueocean.
var stateRoot = window.$blueocean = (window.$blueocean || {});
(function () {
function setState(statePropertyPath, state) {
var pathTokens = statePropertyPath.split('.');
var contextObj = stateRoot;

// Basically an Array shift
function nextToken() {
var nextToken = pathTokens[0];
pathTokens = pathTokens.slice(1);
return nextToken;
}

var pathToken = nextToken();

// Construct up to, but not including, the last point in the graph.
while (pathTokens.length !== 0) {
if (!contextObj[pathToken]) {
contextObj[pathToken] = {};
}
contextObj = contextObj[pathToken];
pathToken = nextToken();
}
// And set the state on the last object on the graph.
contextObj[pathToken] = state;
}

<j:forEach var="preloader" items="${it.pageStatePreloaders}">
// State Preloader: ${preloader.class.name}
<j:set var="stateJson" value="${preloader.stateJson}"/>
<j:if test="${stateJson != null}">
setState('${preloader.statePropertyPath}', ${stateJson});
</j:if>
</j:forEach>
})();
//]]&gt;
<script id="blueocean-page-state-preload-decorator-data" type="application/json">
{<j:forEach var="preloader" items="${it.pageStatePreloaders}" varStatus="st">
<!-- State Preloader: ${preloader.class.name} -->
<j:set var="stateJson" value="${preloader.stateJson}"/>
<j:if test="${stateJson != null}">
"${preloader.statePropertyPath}": ${stateJson}<j:if test="${!st.last}">,</j:if>
</j:if>
</j:forEach>}
</script>
<st:adjunct includes="io.jenkins.blueocean.PageStatePreloadDecorator.preloader"/>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// construct the state object parent path inside window.$blueocean.
var stateRoot = window.$blueocean = (window.$blueocean || {});
(function () {
function setState(statePropertyPath, state) {
var pathTokens = statePropertyPath.split('.');
var contextObj = stateRoot;

// Basically an Array shift
function nextToken() {
var nextToken = pathTokens[0];
pathTokens = pathTokens.slice(1);
return nextToken;
}

var pathToken = nextToken();

// Construct up to, but not including, the last point in the graph.
while (pathTokens.length !== 0) {
if (!contextObj[pathToken]) {
contextObj[pathToken] = {};
}
contextObj = contextObj[pathToken];
pathToken = nextToken();
}
// And set the state on the last object on the graph.
contextObj[pathToken] = state;
}
const data = JSON.parse(document.getElementById('blueocean-page-state-preload-decorator-data').textContent);
for (const [key, value] of Object.entries(data)) {
setState(key, value);
}
})();

0 comments on commit 455d26c

Please sign in to comment.