Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIX JENKINS-18048] Fix link of SCMTriggerCause from UpstreamCause
- UpstreamCause now tries to initialize upstream causes with the
  correct build in onLoad
- SCMTriggerCause stores build reference and uses its URL to build
  absolute link to changelog
  • Loading branch information
daniel-beck committed May 23, 2014
1 parent 016f304 commit e937b09
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
20 changes: 20 additions & 0 deletions core/src/main/java/hudson/model/Cause.java
Expand Up @@ -157,6 +157,26 @@ private UpstreamCause(String upstreamProject, int upstreamBuild, String upstream
this.upstreamCauses = upstreamCauses;
}

@Override
public void onLoad(@Nonnull AbstractBuild<?, ?> build) {
Item i = Jenkins.getInstance().getItemByFullName(this.upstreamProject);
if (i == null || !(i instanceof AbstractProject)) {
// cannot initialize upstream causes
return;
}

AbstractProject j = (AbstractProject)i;
AbstractBuild r = j.getBuildByNumber(this.getUpstreamBuild());
if (r == null) {
// build doesn't exist anymore
return;
}

for (Cause c : this.upstreamCauses) {
c.onLoad(r);
}
}

/**
* @since 1.515
*/
Expand Down
17 changes: 17 additions & 0 deletions core/src/main/java/hudson/triggers/SCMTrigger.java
Expand Up @@ -46,6 +46,8 @@
import hudson.util.SequentialExecutionQueue;
import org.apache.commons.io.FileUtils;
import org.apache.commons.jelly.XMLOutput;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.DataBoundConstructor;

Expand All @@ -72,6 +74,8 @@
import static java.util.logging.Level.*;
import jenkins.model.RunAction2;

import javax.annotation.Nonnull;

/**
* {@link Trigger} that checks for SCM updates periodically.
*
Expand Down Expand Up @@ -536,6 +540,8 @@ public static class SCMTriggerCause extends Cause {
*/
private String pollingLog;

private transient AbstractBuild<?, ?> build;

public SCMTriggerCause(File logFile) throws IOException {
// TODO: charset of this log file?
this(FileUtils.readFileToString(logFile));
Expand All @@ -553,8 +559,14 @@ public SCMTriggerCause() {
this("");
}

@Override
public void onLoad(@Nonnull AbstractBuild<?, ?> build) {
this.build = build;
}

@Override
public void onAddedTo(AbstractBuild build) {
this.build = build;
try {
BuildAction a = new BuildAction(build);
FileUtils.writeStringToFile(a.getPollingLogFile(),pollingLog);
Expand All @@ -570,6 +582,11 @@ public String getShortDescription() {
return Messages.SCMTrigger_SCMTriggerCause_ShortDescription();
}

@Restricted(DoNotUse.class)
public AbstractBuild<?, ?> getBuild() {
return this.build;
}

@Override
public boolean equals(Object o) {
return o instanceof SCMTriggerCause;
Expand Down
Expand Up @@ -23,5 +23,6 @@ THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<span><a href="pollingLog">${it.shortDescription}</a></span>
<!-- this link loses breadcrumb context, but need absolute URL for upstream cause support (JENKINS-18048) -->
<span><a href="${rootURL}/${it.build.url}pollingLog">${it.shortDescription}</a></span>
</j:jelly>

0 comments on commit e937b09

Please sign in to comment.