Skip to content

Commit

Permalink
[JENKINS-23641] - Direct tests for the issue
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
  • Loading branch information
oleg-nenashev committed Oct 13, 2014
1 parent c4347f8 commit 25c1e85
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/cloudbees/jenkins/GitHubCommitNotifier.java
Expand Up @@ -52,12 +52,12 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

BuildData buildData = build.getAction(BuildData.class);
if (buildData == null) {
throw new IOException("Cannot retrieve Git metadata for the build");
throw new IOException(Messages.GitHubCommitNotifier_NoBuildDataError());
}
final Revision lastBuildRevision = buildData.getLastBuiltRevision();
final ObjectId sha1 = lastBuildRevision != null ? lastBuildRevision.getSha1() : null;
if (sha1 == null) { // Nowhere to report => fail the build
throw new IOException("Cannot determine sha1 of the commit. The status cannot be reported");
throw new IOException(Messages.GitHubCommitNotifier_NoLastRevisionError());
}

for (GitHubRepositoryName name : GitHubRepositoryNameContributor.parseAssociatedNames(build.getProject())) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/com/cloudbees/jenkins/Messages.properties
Expand Up @@ -2,3 +2,5 @@ CommitNotifier.Success=Build {0} succeeded in {1}
CommitNotifier.Unstable=Build {0} found unstable in {1}
CommitNotifier.Failed=Build {0} failed in {1}
GitHubCommitNotifier.SettingCommitStatus=Setting commit status on GitHub for {0}
GitHubCommitNotifier.NoBuildDataError=Cannot retrieve Git metadata for the build
GitHubCommitNotifier.NoLastRevisionError=Cannot determine sha1 of the commit. The status cannot be reported
51 changes: 51 additions & 0 deletions src/test/java/com/cloudbees/jenkins/GitHubCommitNotifierTest.java
@@ -0,0 +1,51 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package com.cloudbees.jenkins;

import hudson.model.Build;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.plugins.git.GitSCM;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.JenkinsRule;

/**
* Tests for {@link GitHubCommitNotifier}.
* @author Oleg Nenashev <o.v.nenashev@gmail.com>
*/
public class GitHubCommitNotifierTest extends HudsonTestCase {

// @Rule
// public JenkinsRule r = new JenkinsRule();


@Test
@Bug(23641)
public void testNoBuildData() throws Exception, InterruptedException {
FreeStyleProject prj = createFreeStyleProject("23641_noBuildData");
prj.getPublishersList().add(new GitHubCommitNotifier());
Build b = prj.scheduleBuild2(0).get();
assertBuildStatus(Result.FAILURE, b);
assertLogContains(Messages.GitHubCommitNotifier_NoBuildDataError(), b);
}

@Test
@Bug(23641)
public void testNoBuildRevision() throws Exception, InterruptedException {
FreeStyleProject prj = createFreeStyleProject();
prj.setScm(new GitSCM("http://non.existent.git.repo.nowhere/repo.git"));
prj.getPublishersList().add(new GitHubCommitNotifier());
Build b = prj.scheduleBuild2(0).get();
assertBuildStatus(Result.FAILURE, b);
assertLogContains(Messages.GitHubCommitNotifier_NoLastRevisionError(), b);
}


}

0 comments on commit 25c1e85

Please sign in to comment.