Skip to content

Commit

Permalink
[JENKINS-106] Fixes for settings migration to 2.0
Browse files Browse the repository at this point in the history
- Unit tests for configuration migration

- Fix for premature exit of migration because a job without the old
  settings is encountered

- Fix for jobs that have Slack job properties because they were edited
  after the was plugin installed, but do not send notifications

- Fix to remove the Slack job properties from the configuration, even if
  there is no notifier
  • Loading branch information
OwensCode committed Dec 31, 2015
1 parent af8e4eb commit a18c6f5
Show file tree
Hide file tree
Showing 22 changed files with 646 additions and 22 deletions.
60 changes: 38 additions & 22 deletions src/main/java/jenkins/plugins/slack/SlackNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -474,31 +474,47 @@ public void onLoaded() {

final SlackJobProperty slackJobProperty = p.getProperty(SlackJobProperty.class);

if(slackJobProperty == null) {
logger.info("Configuration is already uptodate, exiting migration");
return;
if (slackJobProperty == null) {
logger.info(String
.format("Configuration is already up to date for \"%s\", skipping migration",
p.getName()));
continue;
}

SlackNotifier slackNotifier = p.getPublishersList().get(SlackNotifier.class);

//map settings
slackNotifier.teamDomain = slackJobProperty.getTeamDomain();
slackNotifier.authToken = slackJobProperty.getToken();
slackNotifier.room = slackJobProperty.getRoom();
slackNotifier.startNotification = slackJobProperty.getStartNotification();

slackNotifier.notifyAborted = slackJobProperty.getNotifyAborted();
slackNotifier.notifyFailure = slackJobProperty.getNotifyFailure();
slackNotifier.notifyNotBuilt = slackJobProperty.getNotifyNotBuilt();
slackNotifier.notifySuccess = slackJobProperty.getNotifySuccess();
slackNotifier.notifyUnstable = slackJobProperty.getNotifyUnstable();
slackNotifier.notifyBackToNormal = slackJobProperty.getNotifyBackToNormal();
slackNotifier.notifyRepeatedFailure = slackJobProperty.getNotifyRepeatedFailure();

slackNotifier.includeTestSummary = slackJobProperty.includeTestSummary();
slackNotifier.commitInfoChoice = slackJobProperty.getShowCommitList() ? CommitInfoChoice.AUTHORS_AND_TITLES : CommitInfoChoice.NONE;
slackNotifier.includeCustomMessage = slackJobProperty.includeCustomMessage();
slackNotifier.customMessage = slackJobProperty.getCustomMessage();

if (slackNotifier == null) {
logger.info(String
.format("Configuration does not have a notifier for \"%s\", not migrating settings",
p.getName()));
} else {

//map settings
if (StringUtils.isBlank(slackNotifier.teamDomain)) {
slackNotifier.teamDomain = slackJobProperty.getTeamDomain();
}
if (StringUtils.isBlank(slackNotifier.authToken)) {
slackNotifier.authToken = slackJobProperty.getToken();
}
if (StringUtils.isBlank(slackNotifier.room)) {
slackNotifier.room = slackJobProperty.getRoom();
}

slackNotifier.startNotification = slackJobProperty.getStartNotification();

slackNotifier.notifyAborted = slackJobProperty.getNotifyAborted();
slackNotifier.notifyFailure = slackJobProperty.getNotifyFailure();
slackNotifier.notifyNotBuilt = slackJobProperty.getNotifyNotBuilt();
slackNotifier.notifySuccess = slackJobProperty.getNotifySuccess();
slackNotifier.notifyUnstable = slackJobProperty.getNotifyUnstable();
slackNotifier.notifyBackToNormal = slackJobProperty.getNotifyBackToNormal();
slackNotifier.notifyRepeatedFailure = slackJobProperty.getNotifyRepeatedFailure();

slackNotifier.includeTestSummary = slackJobProperty.includeTestSummary();
slackNotifier.commitInfoChoice = slackJobProperty.getShowCommitList() ? CommitInfoChoice.AUTHORS_AND_TITLES : CommitInfoChoice.NONE;
slackNotifier.includeCustomMessage = slackJobProperty.includeCustomMessage();
slackNotifier.customMessage = slackJobProperty.getCustomMessage();
}

try {
//property section is not used anymore - remove
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package jenkins.plugins.slack.config;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import hudson.matrix.MatrixProject;
import hudson.model.FreeStyleProject;
import jenkins.model.Jenkins;
import jenkins.plugins.slack.CommitInfoChoice;
import jenkins.plugins.slack.SlackNotifier;
import jenkins.plugins.slack.SlackNotifier.SlackJobProperty;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;

@SuppressWarnings("deprecation")
public class BackwordsCompatible_1_8_SlackNotifierTest {

@Rule
public JenkinsRule j = new JenkinsRule();

public Jenkins jenkins;

@Before
public void setup() {
jenkins = j.getInstance();
}

@Test
@LocalData
public void testBasicMigration() {
FreeStyleProject project = (FreeStyleProject) jenkins.getItem("Test_Slack_Plugin");
SlackNotifier notifier = project.getPublishersList().get(SlackNotifier.class);

assertEquals("jenkins-slack-plugin", notifier.getTeamDomain());
assertEquals("auth-token-for-test", notifier.getAuthToken());
assertEquals("http://localhost:8080/", notifier.getBuildServerUrl());
assertEquals("#slack-plugin-testing", notifier.getRoom());

assertFalse(notifier.getStartNotification());
assertTrue(notifier.getNotifySuccess());
assertFalse(notifier.getNotifyAborted());
assertFalse(notifier.getNotifyNotBuilt());
assertFalse(notifier.getNotifyUnstable());
assertTrue(notifier.getNotifyFailure());
assertFalse(notifier.getNotifyBackToNormal());
assertFalse(notifier.getNotifyRepeatedFailure());
assertFalse(notifier.includeTestSummary());
assertEquals(CommitInfoChoice.NONE, notifier.getCommitInfoChoice());
assertFalse(notifier.includeCustomMessage());
assertEquals("", notifier.getCustomMessage());

assertNull(project.getProperty(SlackJobProperty.class));
}

@Test
@LocalData
public void testGlobalSettingsOverriden() {
FreeStyleProject project = (FreeStyleProject) jenkins.getItem("Test_Slack_Plugin");
SlackNotifier notifier = project.getPublishersList().get(SlackNotifier.class);

assertEquals("jenkins-slack-plugin", notifier.getTeamDomain());
assertEquals("auth-token-for-test", notifier.getAuthToken());
assertEquals("http://localhost:8080/", notifier.getBuildServerUrl());
assertEquals("#slack-plugin-testing", notifier.getRoom());

assertFalse(notifier.getStartNotification());
assertTrue(notifier.getNotifySuccess());
assertFalse(notifier.getNotifyAborted());
assertFalse(notifier.getNotifyNotBuilt());
assertFalse(notifier.getNotifyUnstable());
assertTrue(notifier.getNotifyFailure());
assertFalse(notifier.getNotifyBackToNormal());
assertFalse(notifier.getNotifyRepeatedFailure());
assertFalse(notifier.includeTestSummary());
assertEquals(CommitInfoChoice.NONE, notifier.getCommitInfoChoice());
assertFalse(notifier.includeCustomMessage());
assertEquals("", notifier.getCustomMessage());

assertNull(project.getProperty(SlackJobProperty.class));
}

@Test
@LocalData
public void testGlobalSettingsNotOverridden() throws IOException {
FreeStyleProject project = (FreeStyleProject) jenkins.getItem("Test_Slack_Plugin");
SlackNotifier notifier = project.getPublishersList().get(SlackNotifier.class);

assertEquals("", notifier.getTeamDomain());
assertEquals("", notifier.getAuthToken());
assertEquals(j.getURL().toString(), notifier.getBuildServerUrl());
assertEquals("", notifier.getRoom());

assertFalse(notifier.getStartNotification());
assertTrue(notifier.getNotifySuccess());
assertFalse(notifier.getNotifyAborted());
assertFalse(notifier.getNotifyNotBuilt());
assertFalse(notifier.getNotifyUnstable());
assertTrue(notifier.getNotifyFailure());
assertFalse(notifier.getNotifyBackToNormal());
assertFalse(notifier.getNotifyRepeatedFailure());
assertFalse(notifier.includeTestSummary());
assertEquals(CommitInfoChoice.NONE, notifier.getCommitInfoChoice());
assertFalse(notifier.includeCustomMessage());
assertEquals("", notifier.getCustomMessage());

assertNull(project.getProperty(SlackJobProperty.class));
}

@Test
@LocalData
public void testMigrationFromNoPlugin() {
FreeStyleProject project1 = (FreeStyleProject) jenkins.getItem("Test_01");
assertNull(project1.getPublishersList().get(SlackNotifier.class));
assertNull(project1.getProperty(SlackJobProperty.class));

FreeStyleProject project2 = (FreeStyleProject) jenkins.getItem("Test_02");
assertNull(project2.getPublishersList().get(SlackNotifier.class));
assertNull(project2.getProperty(SlackJobProperty.class));

MatrixProject project3 = (MatrixProject) jenkins.getItem("Test_03");
assertNull(project3.getPublishersList().get(SlackNotifier.class));
assertNull(project3.getProperty(SlackJobProperty.class));
}

@Test
@LocalData
public void testMigrationOfSomeJobs() throws IOException {
// Project without Slack notifications
FreeStyleProject project1 = (FreeStyleProject) jenkins.getItem("Test_01");
assertNull(project1.getPublishersList().get(SlackNotifier.class));
assertNull(project1.getProperty(SlackJobProperty.class));

// Another project without Slack notifications
MatrixProject project3 = (MatrixProject) jenkins.getItem("Test_03");
assertNull(project3.getPublishersList().get(SlackNotifier.class));
assertNull(project3.getProperty(SlackJobProperty.class));

// Project with Slack notifications
FreeStyleProject project2 = (FreeStyleProject) jenkins.getItem("Test_02");
SlackNotifier notifier = project2.getPublishersList().get(SlackNotifier.class);
assertNotNull(notifier);

assertEquals("", notifier.getTeamDomain());
assertEquals("", notifier.getAuthToken());
assertEquals(j.getURL().toString(), notifier.getBuildServerUrl());
assertEquals("", notifier.getRoom());

assertTrue(notifier.getStartNotification());
assertTrue(notifier.getNotifySuccess());
assertTrue(notifier.getNotifyAborted());
assertTrue(notifier.getNotifyNotBuilt());
assertTrue(notifier.getNotifyUnstable());
assertTrue(notifier.getNotifyFailure());
assertTrue(notifier.getNotifyBackToNormal());
assertTrue(notifier.getNotifyRepeatedFailure());
assertTrue(notifier.includeTestSummary());
assertEquals(CommitInfoChoice.AUTHORS_AND_TITLES, notifier.getCommitInfoChoice());
assertTrue(notifier.includeCustomMessage());
assertEquals("Custom message for 1.8 plugin.", notifier.getCustomMessage());

assertNull(project2.getProperty(SlackJobProperty.class));
}

@Test
@LocalData
public void testMigrationWithNoNotifier() {
FreeStyleProject project = (FreeStyleProject) jenkins.getItem("Test_01");
assertNull(project.getPublishersList().get(SlackNotifier.class));
assertNull(project.getProperty(SlackJobProperty.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version='1.0' encoding='UTF-8'?>
<hudson>
</hudson>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description></description>
<keepDependencies>false</keepDependencies>
<properties>
<jenkins.plugins.slack.SlackNotifier_-SlackJobProperty plugin="slack@1.8.1-SNAPSHOT">
<teamDomain></teamDomain>
<token></token>
<room></room>
<startNotification>false</startNotification>
<notifySuccess>true</notifySuccess>
<notifyAborted>false</notifyAborted>
<notifyNotBuilt>false</notifyNotBuilt>
<notifyUnstable>false</notifyUnstable>
<notifyFailure>true</notifyFailure>
<notifyBackToNormal>false</notifyBackToNormal>
<notifyRepeatedFailure>false</notifyRepeatedFailure>
<includeTestSummary>false</includeTestSummary>
<showCommitList>false</showCommitList>
<includeCustomMessage>false</includeCustomMessage>
<customMessage></customMessage>
</jenkins.plugins.slack.SlackNotifier_-SlackJobProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>echo &quot;Hello World&quot;</command>
</hudson.tasks.Shell>
</builders>
<publishers>
<jenkins.plugins.slack.SlackNotifier plugin="slack@1.8.1-SNAPSHOT">
<teamDomain>jenkins-slack-plugin</teamDomain>
<authToken>auth-token-for-test</authToken>
<buildServerUrl>http://localhost:8080/</buildServerUrl>
<room>#slack-plugin-testing</room>
</jenkins.plugins.slack.SlackNotifier>
</publishers>
<buildWrappers/>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version='1.0' encoding='UTF-8'?>
<hudson>
</hudson>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version='1.0' encoding='UTF-8'?>
<jenkins.plugins.slack.SlackNotifier_-DescriptorImpl plugin="slack@1.8.1-SNAPSHOT">
<teamDomain>jenkins-slack-plugin-global</teamDomain>
<token>auth-token-for-test-global</token>
<room>#slack-plugin-testing-global</room>
<buildServerUrl>http://localhost:8080/global/</buildServerUrl>
</jenkins.plugins.slack.SlackNotifier_-DescriptorImpl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description></description>
<keepDependencies>false</keepDependencies>
<properties>
<jenkins.plugins.slack.SlackNotifier_-SlackJobProperty plugin="slack@1.8.1-SNAPSHOT">
<teamDomain></teamDomain>
<token></token>
<room></room>
<startNotification>false</startNotification>
<notifySuccess>true</notifySuccess>
<notifyAborted>false</notifyAborted>
<notifyNotBuilt>false</notifyNotBuilt>
<notifyUnstable>false</notifyUnstable>
<notifyFailure>true</notifyFailure>
<notifyBackToNormal>false</notifyBackToNormal>
<notifyRepeatedFailure>false</notifyRepeatedFailure>
<includeTestSummary>false</includeTestSummary>
<showCommitList>false</showCommitList>
<includeCustomMessage>false</includeCustomMessage>
<customMessage></customMessage>
</jenkins.plugins.slack.SlackNotifier_-SlackJobProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>echo &quot;Hello World&quot;</command>
</hudson.tasks.Shell>
</builders>
<publishers>
<jenkins.plugins.slack.SlackNotifier plugin="slack@1.8.1-SNAPSHOT">
<teamDomain></teamDomain>
<authToken></authToken>
<buildServerUrl></buildServerUrl>
<room></room>
</jenkins.plugins.slack.SlackNotifier>
</publishers>
<buildWrappers/>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version='1.0' encoding='UTF-8'?>
<hudson>
</hudson>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version='1.0' encoding='UTF-8'?>
<jenkins.plugins.slack.SlackNotifier_-DescriptorImpl plugin="slack@1.8.1-SNAPSHOT">
<teamDomain>jenkins-slack-plugin-global</teamDomain>
<token>auth-token-for-test-global</token>
<room>#slack-plugin-testing-global</room>
<buildServerUrl>http://localhost:8080/global/</buildServerUrl>
</jenkins.plugins.slack.SlackNotifier_-DescriptorImpl>
Loading

0 comments on commit a18c6f5

Please sign in to comment.