Skip to content

Commit

Permalink
Merge PR jenkinsci#79 Move property settings at each jobs
Browse files Browse the repository at this point in the history
to post-build actions
  • Loading branch information
samrocketman committed May 31, 2015
2 parents 6105eab + 3695c7d commit d48854f
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 348 deletions.
59 changes: 16 additions & 43 deletions src/main/java/jenkins/plugins/slack/ActiveNotifier.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package jenkins.plugins.slack;


import hudson.EnvVars;
import hudson.Util;
import hudson.EnvVars;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Cause;
import hudson.model.CauseAction;
import hudson.model.Hudson;
import hudson.model.Result;
import hudson.model.Run;
import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.AffectedFile;
Expand Down Expand Up @@ -45,32 +42,15 @@ public ActiveNotifier(SlackNotifier notifier, BuildListener listener) {
}

private SlackService getSlack(AbstractBuild r) {
AbstractProject<?, ?> project = r.getProject();
String projectRoom = Util.fixEmpty(project.getProperty(SlackNotifier.SlackJobProperty.class).getRoom());
String teamDomain = Util.fixEmpty(project.getProperty(SlackNotifier.SlackJobProperty.class).getTeamDomain());
String token = Util.fixEmpty(project.getProperty(SlackNotifier.SlackJobProperty.class).getToken());

EnvVars env = null;
try {
env = r.getEnvironment(listener);
} catch (Exception e) {
listener.getLogger().println("Error retrieving environment vars: " + e.getMessage());
env = new EnvVars();
}
teamDomain = env.expand(teamDomain);
token = env.expand(token);
projectRoom = env.expand(projectRoom);

return notifier.newSlackService(teamDomain, token, projectRoom);
return notifier.newSlackService(r, listener);
}

public void deleted(AbstractBuild r) {
}

public void started(AbstractBuild build) {

AbstractProject<?, ?> project = build.getProject();
SlackNotifier.SlackJobProperty jobProperty = project.getProperty(SlackNotifier.SlackJobProperty.class);

CauseAction causeAction = build.getAction(CauseAction.class);

Expand All @@ -87,7 +67,7 @@ public void started(AbstractBuild build) {
if (changes != null) {
notifyStart(build, changes);
} else {
notifyStart(build, getBuildStatusMessage(build, false, jobProperty.includeCustomMessage()));
notifyStart(build, getBuildStatusMessage(build, false, notifier.includeCustomMessage()));
}
}

Expand All @@ -106,31 +86,25 @@ public void finalized(AbstractBuild r) {

public void completed(AbstractBuild r) {
AbstractProject<?, ?> project = r.getProject();
SlackNotifier.SlackJobProperty jobProperty = project.getProperty(SlackNotifier.SlackJobProperty.class);
if (jobProperty == null) {
logger.warning("Project " + project.getName() + " has no Slack configuration.");
return;
}
Result result = r.getResult();
AbstractBuild<?, ?> previousBuild = project.getLastBuild();
do {
previousBuild = previousBuild.getPreviousCompletedBuild();
} while (previousBuild != null && previousBuild.getResult() == Result.ABORTED);
Result previousResult = (previousBuild != null) ? previousBuild.getResult() : Result.SUCCESS;
if ((result == Result.ABORTED && jobProperty.getNotifyAborted())
if ((result == Result.ABORTED && notifier.getNotifyAborted())
|| (result == Result.FAILURE
&& (previousResult != Result.FAILURE || jobProperty.getNotifyRepeatedFailure())
&& jobProperty.getNotifyFailure())
|| (result == Result.NOT_BUILT && jobProperty.getNotifyNotBuilt())
&& (previousResult != Result.FAILURE || notifier.getNotifyRepeatedFailure())
&& notifier.getNotifyFailure())
|| (result == Result.NOT_BUILT && notifier.getNotifyNotBuilt())
|| (result == Result.SUCCESS
&& (previousResult == Result.FAILURE || previousResult == Result.UNSTABLE)
&& jobProperty.getNotifyBackToNormal())
|| (result == Result.SUCCESS && jobProperty.getNotifySuccess())
|| (result == Result.UNSTABLE && jobProperty.getNotifyUnstable())) {
getSlack(r).publish(getBuildStatusMessage(r, jobProperty.includeTestSummary(),
jobProperty.includeCustomMessage()),
getBuildColor(r));
if (jobProperty.getShowCommitList()) {
&& notifier.getNotifyBackToNormal())
|| (result == Result.SUCCESS && notifier.getNotifySuccess())
|| (result == Result.UNSTABLE && notifier.getNotifyUnstable())) {
getSlack(r).publish(getBuildStatusMessage(r, notifier.includeTestSummary(),
notifier.includeCustomMessage()), getBuildColor(r));
if (notifier.getShowCommitList()) {
getSlack(r).publish(getCommitList(r), getBuildColor(r));
}
}
Expand Down Expand Up @@ -323,8 +297,7 @@ public MessageBuilder appendTestSummary() {

public MessageBuilder appendCustomMessage() {
AbstractProject<?, ?> project = build.getProject();
String customMessage = Util.fixEmpty(project.getProperty(SlackNotifier.SlackJobProperty.class)
.getCustomMessage());
String customMessage = notifier.getCustomMessage();
EnvVars envVars = new EnvVars();
try {
envVars = build.getEnvironment(new LogTaskListener(logger, INFO));
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/jenkins/plugins/slack/SlackListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.TaskListener;
import hudson.model.BuildListener;
import hudson.model.listeners.RunListener;
import hudson.tasks.Publisher;

Expand Down Expand Up @@ -51,7 +51,6 @@ FineGrainedNotifier getNotifier(AbstractProject project, TaskListener listener)
Map<Descriptor<Publisher>, Publisher> map = project.getPublishersList().toMap();
for (Publisher publisher : map.values()) {
if (publisher instanceof SlackNotifier) {
((SlackNotifier)publisher).update();
return new ActiveNotifier((SlackNotifier) publisher, (BuildListener)listener);
}
}
Expand Down
Loading

0 comments on commit d48854f

Please sign in to comment.