Skip to content

Commit

Permalink
JENKINS-18885 and JENKINS-17855 fixing 404 URLs... round 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kinow committed Jul 23, 2013
1 parent 7b23569 commit 21e7af0
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/tap4j/plugin/TapBuildAction.java
Expand Up @@ -41,7 +41,7 @@ public class TapBuildAction implements Action, Serializable, StaplerProxy {
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;

Expand Down
39 changes: 28 additions & 11 deletions src/main/java/org/tap4j/plugin/model/TapStreamResult.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

}
105 changes: 99 additions & 6 deletions src/main/java/org/tap4j/plugin/model/TapTestResultResult.java
Expand Up @@ -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;
Expand All @@ -50,11 +60,13 @@ public class TapTestResultResult extends TestResult {
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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 21e7af0

Please sign in to comment.