Skip to content

Commit

Permalink
Replace AbstractProject and AbstractBuild with Job and Run (#107)
Browse files Browse the repository at this point in the history
This makes it possible to support pipelines. Pipelines don't use
AbstractProject and AbstractBuild. Instead, they use WorkflowJob and
WorkflowRun. The common ancestors are Job and Run.

In some cases, Job doesn't provide the needed API. However, both
AbstractProject and WorkflowJob implement ParameterizedJob interface,
which provides the required functionality.
  • Loading branch information
proski authored and jakub-bochenski committed Jul 5, 2019
1 parent d93b35a commit f04a51c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
import java.io.IOException;
Expand All @@ -16,57 +17,60 @@

/** Created by Nathan McCarthy */
@Extension
public class StashBuildListener extends RunListener<AbstractBuild<?, ?>> {
public class StashBuildListener extends RunListener<Run<?, ?>> {
private static final Logger logger =
Logger.getLogger(MethodHandles.lookup().lookupClass().getName());

@Override
public void onStarted(AbstractBuild<?, ?> abstractBuild, TaskListener listener) {
public void onStarted(Run<?, ?> run, TaskListener listener) {
logger.info("BuildListener onStarted called.");

StashCause cause = abstractBuild.getCause(StashCause.class);
StashCause cause = run.getCause(StashCause.class);
if (cause == null) {
return;
}

try {
abstractBuild.setDescription(cause.getShortDescription());
run.setDescription(cause.getShortDescription());
} catch (IOException e) {
logger.log(Level.SEVERE, "Can't update build description", e);
}
}

@Override
public void onCompleted(AbstractBuild<?, ?> abstractBuild, @Nonnull TaskListener listener) {
StashCause cause = abstractBuild.getCause(StashCause.class);
public void onCompleted(Run<?, ?> run, @Nonnull TaskListener listener) {
StashCause cause = run.getCause(StashCause.class);
if (cause == null) {
return;
}

StashBuildTrigger trigger =
ParameterizedJobMixIn.getTrigger(abstractBuild.getParent(), StashBuildTrigger.class);
ParameterizedJobMixIn.getTrigger(run.getParent(), StashBuildTrigger.class);
if (trigger == null) {
return;
}

StashRepository repository = trigger.getBuilder().getRepository();
Result result = abstractBuild.getResult();
Result result = run.getResult();
// Note: current code should no longer use "new JenkinsLocationConfiguration()"
// as only one instance per runtime is really supported by the current core.
JenkinsLocationConfiguration globalConfig = JenkinsLocationConfiguration.get();
String rootUrl = globalConfig == null ? null : globalConfig.getUrl();
String buildUrl = "";
if (rootUrl == null) {
buildUrl = " PLEASE SET JENKINS ROOT URL FROM GLOBAL CONFIGURATION " + abstractBuild.getUrl();
buildUrl = " PLEASE SET JENKINS ROOT URL FROM GLOBAL CONFIGURATION " + run.getUrl();
} else {
buildUrl = rootUrl + abstractBuild.getUrl();
buildUrl = rootUrl + run.getUrl();
}
repository.deletePullRequestComment(cause.getPullRequestId(), cause.getBuildStartCommentId());

String additionalComment = "";
StashPostBuildComment comments = null;

StashPostBuildComment comments =
abstractBuild.getProject().getPublishersList().get(StashPostBuildComment.class);
if (run instanceof AbstractBuild) {
AbstractBuild<?, ?> build = (AbstractBuild<?, ?>) run;
comments = build.getProject().getPublishersList().get(StashPostBuildComment.class);
}

if (comments != null) {
String buildComment =
Expand All @@ -77,28 +81,27 @@ public void onCompleted(AbstractBuild<?, ?> abstractBuild, @Nonnull TaskListener
if (buildComment != null && !buildComment.isEmpty()) {
String expandedComment;
try {
expandedComment =
Util.fixEmptyAndTrim(abstractBuild.getEnvironment(listener).expand(buildComment));
expandedComment = Util.fixEmptyAndTrim(run.getEnvironment(listener).expand(buildComment));
} catch (IOException | InterruptedException e) {
expandedComment = "Exception while expanding '" + buildComment + "': " + e;
}

additionalComment = "\n\n" + expandedComment;
}
}
String duration = abstractBuild.getDurationString();
String duration = run.getDurationString();
repository.postFinishedComment(
cause.getPullRequestId(),
cause.getSourceCommitHash(),
cause.getDestinationCommitHash(),
result,
buildUrl,
abstractBuild.getNumber(),
run.getNumber(),
additionalComment,
duration);

// Merge PR
if (trigger.getMergeOnSuccess() && abstractBuild.getResult() == Result.SUCCESS) {
if (trigger.getMergeOnSuccess() && run.getResult() == Result.SUCCESS) {
boolean mergeStat =
repository.mergePullRequest(cause.getPullRequestId(), cause.getPullRequestVersion());
if (mergeStat == true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.Queue;
import hudson.model.queue.Tasks;
import hudson.security.ACL;
Expand All @@ -21,6 +22,7 @@
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.ParameterizedJobMixIn.ParameterizedJob;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
Expand All @@ -29,7 +31,7 @@
import org.kohsuke.stapler.StaplerRequest;

/** Created by Nathan McCarthy */
public class StashBuildTrigger extends Trigger<AbstractProject<?, ?>> {
public class StashBuildTrigger extends Trigger<Job<?, ?>> {
private static final Logger logger =
Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
private final String projectPath;
Expand Down Expand Up @@ -120,11 +122,14 @@ public String getcredentialsId() {
}

private StandardUsernamePasswordCredentials getCredentials() {
// Cast is safe due to isApplicable() check
ParameterizedJob parameterizedJob = (ParameterizedJob) job;

return CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(
StandardUsernamePasswordCredentials.class,
this.job,
Tasks.getDefaultAuthenticationOf(this.job),
Tasks.getDefaultAuthenticationOf(parameterizedJob),
URIRequirementBuilder.fromUri(stashHost).build()),
CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialsId)));
}
Expand Down Expand Up @@ -178,11 +183,11 @@ public boolean isCancelOutdatedJobsEnabled() {
}

@Override
public void start(AbstractProject<?, ?> project, boolean newInstance) {
super.start(project, newInstance);
public void start(Job<?, ?> job, boolean newInstance) {
super.start(job, newInstance);
try {
Objects.requireNonNull(project, "project is null");
this.stashPullRequestsBuilder = new StashPullRequestsBuilder(project, this);
Objects.requireNonNull(job, "job is null");
this.stashPullRequestsBuilder = new StashPullRequestsBuilder(job, this);
} catch (NullPointerException e) {
logger.log(Level.SEVERE, "Can't start trigger", e);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static java.lang.String.format;

import hudson.model.AbstractProject;
import hudson.model.Job;
import java.lang.invoke.MethodHandles;
import java.util.Collection;
import java.util.logging.Logger;
Expand All @@ -13,15 +13,14 @@
public class StashPullRequestsBuilder {
private static final Logger logger =
Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
private AbstractProject<?, ?> project;
private Job<?, ?> job;
private StashBuildTrigger trigger;
private StashRepository repository;

public StashPullRequestsBuilder(
@Nonnull AbstractProject<?, ?> project, @Nonnull StashBuildTrigger trigger) {
this.project = project;
public StashPullRequestsBuilder(@Nonnull Job<?, ?> job, @Nonnull StashBuildTrigger trigger) {
this.job = job;
this.trigger = trigger;
this.repository = new StashRepository(project, trigger);
this.repository = new StashRepository(job, trigger);
}

public void run() {
Expand All @@ -31,10 +30,6 @@ public void run() {
this.repository.addFutureBuildTasks(targetPullRequests);
}

public AbstractProject<?, ?> getProject() {
return this.project;
}

public StashBuildTrigger getTrigger() {
return this.trigger;
}
Expand Down

0 comments on commit f04a51c

Please sign in to comment.