Skip to content
This repository was archived by the owner on Feb 13, 2020. It is now read-only.

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
* Introduce delegate class
* Move pipeline class to separate package
  • Loading branch information
ikikko committed Dec 31, 2016
1 parent 8f9c96a commit 336e5fa
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.typetalk.api.Typetalk;
import org.jenkinsci.plugins.typetalk.support.Emoji;
import org.jenkinsci.plugins.typetalk.support.TypetalkMessage;
import org.jenkinsci.plugins.typetalk.delegate.BuildWrapperDelegate;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
Expand All @@ -37,46 +33,15 @@ public TypetalkBuildWrapper(String name, String topicNumber, boolean notifyStart

@Override
public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
if (notifyStart) {
listener.getLogger().println("Notifying build start to Typetalk...");

String message;
if (StringUtils.isBlank(notifyStartMessage)) {
TypetalkMessage typetalkMessage = new TypetalkMessage(Emoji.LOUDSPEAKER, "Build start");
message = typetalkMessage.buildMessageWithBuild(build);
} else{
message = build.getEnvironment(listener).expand(notifyStartMessage);
}
Long topicId = Long.valueOf(topicNumber);

Typetalk.createFromName(name).postMessage(topicId, message);
}
BuildWrapperDelegate delegate = new BuildWrapperDelegate(name, Long.valueOf(topicNumber), listener, build);
delegate.notifyStart(notifyStart, notifyStartMessage);

return new Environment() {
@Override
public boolean tearDown(AbstractBuild build, BuildListener listener) throws IOException, InterruptedException {
if (notifyEnd && isSuccessBuild(build)) {
listener.getLogger().println("Notifying build end to Typetalk...");

String message;
if (StringUtils.isBlank(notifyEndMessage)) {
TypetalkMessage typetalkMessage = new TypetalkMessage(Emoji.MEGA, "Build end");
message = typetalkMessage.buildMessageWithBuild(build);
} else {
message = build.getEnvironment(listener).expand(notifyEndMessage);
}
Long topicId = Long.valueOf(topicNumber);

Typetalk.createFromName(name).postMessage(topicId, message);
}

delegate.notifyEnd(notifyEnd, notifyEndMessage);
return true;
}

private boolean isSuccessBuild(AbstractBuild build) {
// When there is nothing failure (equals success), getResult hasn't been set yet.
return build.getResult() == null || build.getResult().equals(Result.SUCCESS);
}
};
}

Expand Down
19 changes: 2 additions & 17 deletions src/main/java/org/jenkinsci/plugins/typetalk/TypetalkNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import hudson.tasks.Publisher;
import hudson.util.Secret;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.typetalk.api.Typetalk;
import org.jenkinsci.plugins.typetalk.support.ResultSupport;
import org.jenkinsci.plugins.typetalk.support.TypetalkMessage;
import org.jenkinsci.plugins.typetalk.delegate.NotifyDelegate;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

Expand All @@ -40,20 +38,7 @@ public BuildStepMonitor getRequiredMonitorService() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {

ResultSupport resultSupport = new ResultSupport();
if (resultSupport.successFromPreviousBuild(build)) {
return true;
}

listener.getLogger().println("Notifying build result to Typetalk...");

TypetalkMessage typetalkMessage = resultSupport.convertBuildToMessage(build);
String message = typetalkMessage.buildMessageWithBuild(build);
Long topicId = Long.valueOf(topicNumber);

Typetalk.createFromName(name).postMessage(topicId, message);

new NotifyDelegate(name, Long.valueOf(topicNumber), listener, build).notifyResult();
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.jenkinsci.plugins.typetalk.delegate;

import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.typetalk.api.Typetalk;
import org.jenkinsci.plugins.typetalk.support.Emoji;
import org.jenkinsci.plugins.typetalk.support.TypetalkMessage;

import java.io.IOException;

public class BuildWrapperDelegate {

private final String name;

private final Long topicId;

private final TaskListener listener;

private final Run run;

public BuildWrapperDelegate(String name, Long topicId, TaskListener listener, Run run) {
this.name = name;
this.topicId = topicId;
this.listener = listener;
this.run = run;
}

public void notifyStart(boolean notifyStart, String notifyStartMessage) throws IOException, InterruptedException {
if (notifyStart) {
listener.getLogger().println("Notifying build start to Typetalk...");

String message;
if (StringUtils.isBlank(notifyStartMessage)) {
TypetalkMessage typetalkMessage = new TypetalkMessage(Emoji.LOUDSPEAKER, "Build start");
message = typetalkMessage.buildMessageWithBuild(run);
} else {
message = run.getEnvironment(listener).expand(notifyStartMessage);
}

Typetalk.createFromName(name).postMessage(topicId, message);
}
}

public void notifyEnd(boolean notifyEnd, String notifyEndMessage) throws IOException, InterruptedException {
if (notifyEnd && isSuccessBuild(run)) {
listener.getLogger().println("Notifying build end to Typetalk...");

String message;
if (StringUtils.isBlank(notifyEndMessage)) {
TypetalkMessage typetalkMessage = new TypetalkMessage(Emoji.MEGA, "Build end");
message = typetalkMessage.buildMessageWithBuild(run);
} else {
message = run.getEnvironment(listener).expand(notifyEndMessage);
}

Typetalk.createFromName(name).postMessage(topicId, message);
}
}

private boolean isSuccessBuild(Run run) {
// When there is nothing failure (equals success), getResult hasn't been set yet.
return run.getResult() == null || run.getResult().equals(Result.SUCCESS);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.jenkinsci.plugins.typetalk.delegate;

import hudson.model.Run;
import hudson.model.TaskListener;
import org.jenkinsci.plugins.typetalk.api.Typetalk;
import org.jenkinsci.plugins.typetalk.support.ResultSupport;
import org.jenkinsci.plugins.typetalk.support.TypetalkMessage;

import java.io.IOException;

public class NotifyDelegate {

private final String name;

private final Long topicId;

private final TaskListener listener;

private final Run run;

public NotifyDelegate(String name, Long topicId, TaskListener listener, Run run) {
this.name = name;
this.topicId = topicId;
this.listener = listener;
this.run = run;
}

public void notifyResult() throws IOException {
ResultSupport resultSupport = new ResultSupport();
if (resultSupport.successFromPreviousBuild(run)) {
return;
}

listener.getLogger().println("Notifying build result to Typetalk...");

TypetalkMessage typetalkMessage = resultSupport.convertBuildToMessage(run);
String message = typetalkMessage.buildMessageWithBuild(run);

Typetalk.createFromName(name).postMessage(topicId, message);
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package org.jenkinsci.plugins.typetalk;
package org.jenkinsci.plugins.typetalk.pipeline;


import hudson.Extension;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.typetalk.api.Typetalk;
import org.jenkinsci.plugins.typetalk.support.Emoji;
import org.jenkinsci.plugins.typetalk.support.TypetalkMessage;
import org.jenkinsci.plugins.typetalk.delegate.BuildWrapperDelegate;
import org.jenkinsci.plugins.workflow.steps.*;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
Expand All @@ -18,8 +14,6 @@

public class TypetalkBuildWrapperStep extends AbstractStepImpl {

// TODO move workflow package

private final @Nonnull String name;
private final @Nonnull Long topicId;
private boolean notifyStart;
Expand Down Expand Up @@ -118,24 +112,15 @@ public static class TypetalkBuildWrapperStepExecution extends AbstractStepExecut

@Override
public boolean start() throws Exception {
if (step.notifyStart) {
listener.getLogger().println("Notifying build start to Typetalk...");

String message;
if (StringUtils.isBlank(step.notifyStartMessage)) {
TypetalkMessage typetalkMessage = new TypetalkMessage(Emoji.LOUDSPEAKER, "Build start");
message = typetalkMessage.buildMessageWithBuild(run);
} else {
message = run.getEnvironment(listener).expand(step.notifyStartMessage);
}

Typetalk.createFromName(step.name).postMessage(step.topicId, message);
}
BuildWrapperDelegate delegate = new BuildWrapperDelegate(step.name, step.topicId, listener, run);

getContext().
newBodyInvoker().
withCallback(new TypetalkBuildWrapperStepCallback(step, listener, run)).
start();
delegate.notifyStart(step.notifyStart, step.notifyStartMessage);
getContext().newBodyInvoker().withCallback(new BodyExecutionCallback.TailCall() {
@Override
protected void finished(StepContext context) throws Exception {
delegate.notifyEnd(step.notifyEnd, step.notifyEndMessage);
}
}).start();

return false;
}
Expand All @@ -146,42 +131,4 @@ public void stop(@Nonnull Throwable throwable) throws Exception {
}
}

private static class TypetalkBuildWrapperStepCallback extends BodyExecutionCallback.TailCall {

private final TypetalkBuildWrapperStep step;

private final TaskListener listener;

private final Run run;

TypetalkBuildWrapperStepCallback(TypetalkBuildWrapperStep step, TaskListener listener, Run run) {
this.step = step;
this.listener = listener;
this.run = run;
}

@Override
protected void finished(StepContext context) throws Exception {
if (step.notifyEnd && isSuccessBuild(run)) {
listener.getLogger().println("Notifying build end to Typetalk...");

String message;
if (StringUtils.isBlank(step.notifyEndMessage)) {
TypetalkMessage typetalkMessage = new TypetalkMessage(Emoji.MEGA, "Build end");
message = typetalkMessage.buildMessageWithBuild(run);
} else {
message = run.getEnvironment(listener).expand(step.notifyEndMessage);
}

Typetalk.createFromName(step.name).postMessage(step.topicId, message);
}
}

private boolean isSuccessBuild(Run run) {
// When there is nothing failure (equals success), getResult hasn't been set yet.
return run.getResult() == null || run.getResult().equals(Result.SUCCESS);
}

}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package org.jenkinsci.plugins.typetalk;
package org.jenkinsci.plugins.typetalk.pipeline;

import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Run;
import hudson.model.TaskListener;
import org.jenkinsci.plugins.typetalk.api.Typetalk;
import org.jenkinsci.plugins.typetalk.support.ResultSupport;
import org.jenkinsci.plugins.typetalk.support.TypetalkMessage;
import org.jenkinsci.plugins.typetalk.delegate.NotifyDelegate;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution;
Expand Down Expand Up @@ -70,26 +65,12 @@ public static class TypetalkSendStepExecution extends AbstractSynchronousNonBloc
@StepContextParameter
transient Run run;

/**
* Almost same as {@link TypetalkNotifier#perform(AbstractBuild, Launcher, BuildListener)}
*/
@Override
protected Void run() throws Exception {
ResultSupport resultSupport = new ResultSupport();
if (resultSupport.successFromPreviousBuild(run)) {
return null;
}

listener.getLogger().println("Notifying build result to Typetalk...");

TypetalkMessage typetalkMessage = new ResultSupport().convertBuildToMessage(run);
String message = typetalkMessage.buildMessageWithBuild(run);

Typetalk.createFromName(step.name).postMessage(step.topicId, message);

new NotifyDelegate(step.name, step.topicId, listener, run).notifyResult();
return null;
}

}

}
}

0 comments on commit 336e5fa

Please sign in to comment.