Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License

Copyright (c) 2009-2014 Jenkins contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
<name>GitHub plugin</name>
<url>http://wiki.jenkins-ci.org/display/JENKINS/Github+Plugin</url>

<licenses>
<license>
<name>Apache 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<developers>
<developer>
<id>kohsuke</id>
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/com/cloudbees/jenkins/GitHubCommitNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import hudson.plugins.git.Revision;
import hudson.util.ListBoxModel;
import javax.annotation.Nonnull;
import org.jenkinsci.plugins.github.util.BuildDataHelper;

/**
* Create commit status notifications on the commits based on the outcome of the build.
Expand Down Expand Up @@ -97,17 +98,8 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
return true;
}

private void updateCommitStatus(@Nonnull AbstractBuild<?, ?> build, @Nonnull BuildListener listener) throws InterruptedException, IOException {
BuildData buildData = build.getAction(BuildData.class);
if (buildData == null) {
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(Messages.GitHubCommitNotifier_NoLastRevisionError());
}

private void updateCommitStatus(@Nonnull AbstractBuild<?, ?> build, @Nonnull BuildListener listener) throws InterruptedException, IOException {
final ObjectId sha1 = BuildDataHelper.getCommitSHA1(build);
for (GitHubRepositoryName name : GitHubRepositoryNameContributor.parseAssociatedNames(build.getProject())) {
for (GHRepository repository : name.resolve()) {
GHCommitState state;
Expand All @@ -117,7 +109,10 @@ private void updateCommitStatus(@Nonnull AbstractBuild<?, ?> build, @Nonnull Bui
final String duration = Util.getTimeSpanString(System.currentTimeMillis() - build.getTimeInMillis());

Result result = build.getResult();
if (result.isBetterOrEqualTo(SUCCESS)) {
if (result == null) { // Build is ongoing
state = GHCommitState.PENDING;
msg = Messages.CommitNotifier_Pending(build.getDisplayName());
} else if (result.isBetterOrEqualTo(SUCCESS)) {
state = GHCommitState.SUCCESS;
msg = Messages.CommitNotifier_Success(build.getDisplayName(), duration);
} else if (result.isBetterOrEqualTo(UNSTABLE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,21 @@
import org.kohsuke.github.GHRepository;

import java.io.IOException;
import org.jenkinsci.plugins.github.util.BuildDataHelper;

@Extension
public class GitHubPendingCommitStatus extends Builder {
public class GitHubSetCommitStatusBuilder extends Builder {
@DataBoundConstructor
public GitHubPendingCommitStatus() {
public GitHubSetCommitStatusBuilder() {
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
BuildData buildData = build.getAction(BuildData.class);
String sha1 = ObjectId.toString(buildData.getLastBuiltRevision().getSha1());

public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
final ObjectId sha1 = BuildDataHelper.getCommitSHA1(build);
for (GitHubRepositoryName name : GitHubRepositoryNameContributor.parseAssociatedNames(build.getProject())) {
for (GHRepository repository : name.resolve()) {
listener.getLogger().println(Messages.GitHubCommitNotifier_SettingCommitStatus(repository.getUrl() + "/commit/" + sha1));
repository.createCommitStatus(sha1, GHCommitState.PENDING, build.getAbsoluteUrl(), Messages.CommitNotifier_Pending(build.getDisplayName()));
repository.createCommitStatus(ObjectId.toString(sha1), GHCommitState.PENDING, build.getAbsoluteUrl(), Messages.CommitNotifier_Pending(build.getDisplayName()));
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.jenkinsci.plugins.github.util;

import hudson.model.AbstractBuild;
import hudson.plugins.git.Revision;
import hudson.plugins.git.util.BuildData;
import java.io.IOException;
import javax.annotation.Nonnull;
import org.eclipse.jgit.lib.ObjectId;

/**
* Stores common methods for {@link BuildData} handling.
* @author Oleg Nenashev <o.v.nenashev@gmail.com>
* @since 1.10
*/
public class BuildDataHelper {

/**
* Gets SHA1 from the build.
* @param build
* @return SHA1 of the las
* @throws IOException Cannot get the info about commit ID
*/
public static @Nonnull ObjectId getCommitSHA1(@Nonnull AbstractBuild<?, ?> build) throws IOException {
BuildData buildData = build.getAction(BuildData.class);
if (buildData == null) {
throw new IOException(Messages.BuildDataHelper_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(Messages.BuildDataHelper_NoLastRevisionError());
}
return sha1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ CommitNotifier.Unstable=Build {0} found unstable in {1}
CommitNotifier.Failed=Build {0} failed in {1}
CommitNotifier.Pending=Build {0} in progress...
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BuildDataHelper.NoBuildDataError=Cannot retrieve Git metadata for the build
BuildDataHelper.NoLastRevisionError=Cannot determine sha1 of the commit. The status cannot be reported
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@
*/
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);
assertLogContains(org.jenkinsci.plugins.github.util.Messages.BuildDataHelper_NoBuildDataError(), b);
}

@Test
Expand All @@ -44,7 +40,7 @@ public void testNoBuildRevision() throws Exception, InterruptedException {
prj.getPublishersList().add(new GitHubCommitNotifier());
Build b = prj.scheduleBuild2(0).get();
assertBuildStatus(Result.FAILURE, b);
assertLogContains(Messages.GitHubCommitNotifier_NoLastRevisionError(), b);
assertLogContains(org.jenkinsci.plugins.github.util.Messages.BuildDataHelper_NoLastRevisionError(), b);
}

@Bug(25312)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.cloudbees.jenkins;

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

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

@Test
public void testNoBuildData() throws Exception, InterruptedException {
FreeStyleProject prj = createFreeStyleProject("23641_noBuildData");
prj.getBuildersList().add(new GitHubSetCommitStatusBuilder());
Build b = prj.scheduleBuild2(0).get();
assertBuildStatus(Result.FAILURE, b);
assertLogContains(org.jenkinsci.plugins.github.util.Messages.BuildDataHelper_NoBuildDataError(), b);
}

// TODO: test fails due to the fatal server communication attempt
@Test
@Ignore
public void testNoBuildRevision() throws Exception, InterruptedException {
FreeStyleProject prj = createFreeStyleProject();
prj.setScm(new GitSCM("http://non.existent.git.repo.nowhere/repo.git"));
prj.getBuildersList().add(new GitHubSetCommitStatusBuilder());
Build b = prj.scheduleBuild2(0).get();
assertBuildStatus(Result.FAILURE, b);
assertLogContains(org.jenkinsci.plugins.github.util.Messages.BuildDataHelper_NoLastRevisionError(), b);
}
}