Skip to content
Permalink
Browse files

JENKINS-18885 and JENKINS-17855 fixing 404 URLs... round 2

  • Loading branch information...
kinow committed Jul 23, 2013
1 parent 7b23569 commit 21e7af07d5ae18327eb7a2626184970a6ff1d995
@@ -41,7 +41,7 @@
private static final long serialVersionUID = 520981690971849654L;
public static final String URL_NAME = "tapResults";
public static final String ICON_NAME = "/plugin/tap/icons/tap-24.png";
public static final String DISPLAY_NAME = "TAP";
public static final String DISPLAY_NAME = "TAP Extended Test Results";

private AbstractBuild<?, ?> build;

@@ -34,6 +34,7 @@
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;
@@ -68,7 +69,7 @@ public TapStreamResult(AbstractBuild<?, ?> owner, TapResult tapResult) {
* @see hudson.model.ModelObject#getDisplayName()
*/
public String getDisplayName() {
return "TAP Stream results";
return "TAP Stream Results";
}

/* (non-Javadoc)
@@ -177,18 +178,34 @@ public Object getDynamic(String name, StaplerRequest req, StaplerResponse rsp) {
* @return
*/
private TapTestResultResult getTapTestResultResult(String name) {
if (name != null && name.lastIndexOf("-") > 0) {
String fileName = name.substring(0, name.lastIndexOf("-"));
String testNumber = name.substring(name.lastIndexOf("-")+1);
for(TestSetMap tsm : tapResult.getTestSets()) {
if(tsm.getFileName().equals(fileName)) {
TestSet ts = tsm.getTestSet();
org.tap4j.model.TestResult desired = ts.getTestResult(Integer.parseInt(testNumber));
return new TapTestResultResult(owner, tsm, desired, this.tapResult.getTodoIsFailure());
}
if (name == null)
return null; // we don't allow null, nay!
if (name.lastIndexOf("-") <= 0)
return null; // ops, where's the - mate?

name = name.trim();

int rightIndex = name.length();
while (name.charAt(rightIndex-1) == '/') {
rightIndex -= 1;
}
int leftIndex = name.lastIndexOf('/') +1;

String testResultName = name.substring(leftIndex, rightIndex); // but we want the test result name (testSet1.tap)
if (testResultName.indexOf('-') <= 0) // plus the number (testSet1.tap-2)
return null;
String testNumber = testResultName.substring(testResultName.lastIndexOf('-')+1);
String fileName = name.substring(0, name.lastIndexOf('-'));

for(TestSetMap tsm : tapResult.getTestSets()) {
if(tsm.getFileName().equals(fileName)) {
TestSet ts = tsm.getTestSet();
org.tap4j.model.TestResult desired = ts.getTestResult(Integer.parseInt(testNumber));
return new TapTestResultResult(owner, tsm, desired, this.tapResult.getTodoIsFailure());
}
}
return null;

return null; // ops, something went wrong
}

}
@@ -23,16 +23,26 @@
*/
package org.tap4j.plugin.model;

import hudson.Functions;
import hudson.model.Item;
import hudson.model.AbstractBuild;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.tasks.test.TestObject;
import hudson.tasks.test.TestResult;

import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import jenkins.model.Jenkins;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
import org.tap4j.model.Comment;
import org.tap4j.model.Directive;
import org.tap4j.model.TestSet;
@@ -50,11 +60,13 @@
private static final String DURATION_KEY = "duration_ms";

private static final long serialVersionUID = -4499261655602135921L;
private static final Logger LOGGER = Logger.getLogger(TapTestResultResult.class.getName());

private final AbstractBuild<?, ?> owner;
private final org.tap4j.model.TestResult tapTestResult;
private final TestSetMap testSetMap;
private final Boolean todoIsFailure;

/**
* @deprecated
* @param owner
@@ -84,8 +96,7 @@ public TapTestResultResult(AbstractBuild<?, ?> owner,
* @see hudson.model.ModelObject#getDisplayName()
*/
public String getDisplayName() {
String name = tapTestResult.getDescription();
return name != null ? name : Integer.toString(tapTestResult.getTestNumber());
return getName();
}

/* (non-Javadoc)
@@ -130,8 +141,14 @@ public TestResult findCorrespondingResult(String id) {
*/
@Override
public String getName() {
String name = tapTestResult.getDescription();
return testSetMap.getFileName() + "-" + name != null ? name : Integer.toString(tapTestResult.getTestNumber());
StringBuilder buf = new StringBuilder();
buf.append(tapTestResult.getTestNumber());
String tapTestResultDescription = tapTestResult.getDescription();
if (StringUtils.isNotBlank(tapTestResultDescription)) {
buf.append(" - ");
buf.append(tapTestResultDescription);
}
return buf.toString();
}

public String getStatus() {
@@ -164,12 +181,88 @@ public String getFullName() {
return getName();
}

public String getRelativePathFrom(TestObject it) {
// if (it is one of my ancestors) {
// return a relative path from it
// } else {
// return a complete path starting with "/"
// }
if (it==this) {
return ".";
}

StringBuilder buf = new StringBuilder();
TestObject next = this;
TestObject cur = this;
// Walk up my ancesotors from leaf to root, looking for "it"
// and accumulating a relative url as I go
while (next!=null && it!=next) {
cur = next;
buf.insert(0,'/');
buf.insert(0,cur.getSafeName());
next = cur.getParent();
}
if (it==next) {
return buf.toString();
} else {
// Keep adding on to the string we've built so far

// Start with the test result action
AbstractTestResultAction action = getTestResultAction();
if (action==null) {
//LOGGER.warning("trying to get relative path, but we can't determine the action that owns this result.");
return ""; // this won't take us to the right place, but it also won't 404.
}
buf.insert(0,'/');
buf.insert(0,action.getUrlName());

// Now the build
AbstractBuild<?,?> myBuild = cur.getOwner();
if (myBuild ==null) {
//LOGGER.warning("trying to get relative path, but we can't determine the build that owns this result.");
return ""; // this won't take us to the right place, but it also won't 404.
}
//buf.insert(0,'/');
buf.insert(0,myBuild.getUrl());

// If we're inside a stapler request, just delegate to Hudson.Functions to get the relative path!
StaplerRequest req = Stapler.getCurrentRequest();
if (req!=null && myBuild instanceof Item) {
buf.insert(0, '/');
// Ugly but I don't see how else to convince the compiler that myBuild is an Item
Item myBuildAsItem = (Item) myBuild;
buf.insert(0, Functions.getRelativeLinkTo(myBuildAsItem));
} else {
// We're not in a stapler request. Okay, give up.
//LOGGER.info("trying to get relative path, but it is not my ancestor, and we're not in a stapler request. Trying absolute hudson url...");
String hudsonRootUrl = Jenkins.getInstance().getRootUrl();
if (hudsonRootUrl==null||hudsonRootUrl.length()==0) {
//LOGGER.warning("Can't find anything like a decent hudson url. Punting, returning empty string.");
return "";

}
//buf.insert(0, '/');
buf.insert(0, hudsonRootUrl);
}

//LOGGER.info("Here's our relative path: " + buf.toString());
return buf.toString();
}

}

/* (non-Javadoc)
* @see hudson.tasks.test.TestObject#getSafeName()
*/
@Override
public String getSafeName() {
return testSetMap.getFileName() + "-" + tapTestResult.getTestNumber();
String safeName = testSetMap.getFileName() + "-" + tapTestResult.getTestNumber();
try {
safeName = URLEncoder.encode(safeName, "UTF-8");
} catch (UnsupportedEncodingException uee) {
LOGGER.warning(uee.getMessage());
}
return safeName;
}

/* (non-Javadoc)

0 comments on commit 21e7af0

Please sign in to comment.
You can’t perform that action at this time.