Skip to content

Commit

Permalink
Allow Commit Notifications to be used as Workflow Build Steps.
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurschreiber committed Dec 28, 2015
1 parent 5e14cca commit 731ffae
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
26 changes: 14 additions & 12 deletions src/main/java/com/cloudbees/jenkins/GitHubCommitNotifier.java
@@ -1,17 +1,20 @@
package com.cloudbees.jenkins;

import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Notifier;
import hudson.tasks.Publisher;
import hudson.util.ListBoxModel;
import jenkins.tasks.SimpleBuildStep;

import org.eclipse.jgit.lib.ObjectId;
import org.jenkinsci.plugins.github.common.ExpandableMessage;
import org.jenkinsci.plugins.github.util.BuildDataHelper;
Expand Down Expand Up @@ -41,7 +44,7 @@
*
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
public class GitHubCommitNotifier extends Notifier {
public class GitHubCommitNotifier extends Notifier implements SimpleBuildStep {
private static final ExpandableMessage DEFAULT_MESSAGE = new ExpandableMessage("");

private ExpandableMessage statusMessage = DEFAULT_MESSAGE;
Expand Down Expand Up @@ -100,12 +103,12 @@ public BuildStepMonitor getRequiredMonitorService() {
}

@Override
public boolean perform(AbstractBuild<?, ?> build,
public void perform(Run<?, ?> build,
FilePath ws,
Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
TaskListener listener) throws InterruptedException, IOException {
try {
updateCommitStatus(build, listener);
return true;
} catch (IOException error) {
final Result buildResult = getEffectiveResultOnFailure();
if (buildResult.equals(FAILURE)) {
Expand All @@ -118,19 +121,18 @@ public boolean perform(AbstractBuild<?, ?> build,
build.setResult(buildResult);
}
}
return true;
}

private void updateCommitStatus(@Nonnull AbstractBuild<?, ?> build,
@Nonnull BuildListener listener) throws InterruptedException, IOException {
private void updateCommitStatus(@Nonnull Run<?, ?> build,
@Nonnull TaskListener listener) throws InterruptedException, IOException {
final String sha1 = ObjectId.toString(BuildDataHelper.getCommitSHA1(build));

StatusResult status = statusFrom(build);
String message = defaultIfEmpty(firstNonNull(statusMessage, DEFAULT_MESSAGE)
.expandAll(build, listener), status.getMsg());
String contextName = displayNameFor(build.getProject());
String contextName = displayNameFor(build.getParent());

for (GitHubRepositoryName name : GitHubRepositoryNameContributor.parseAssociatedNames(build.getProject())) {
for (GitHubRepositoryName name : GitHubRepositoryNameContributor.parseAssociatedNames(build.getParent())) {
for (GHRepository repository : name.resolve()) {

listener.getLogger().println(
Expand All @@ -146,7 +148,7 @@ private void updateCommitStatus(@Nonnull AbstractBuild<?, ?> build,
}
}

private static StatusResult statusFrom(@Nonnull AbstractBuild<?, ?> build) {
private static StatusResult statusFrom(@Nonnull Run<?, ?> build) {
Result result = build.getResult();

// We do not use `build.getDurationString()` because it appends 'and counting' (build is still running)
Expand Down
@@ -1,12 +1,15 @@
package com.cloudbees.jenkins;

import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import jenkins.tasks.SimpleBuildStep;

import org.eclipse.jgit.lib.ObjectId;
import org.jenkinsci.plugins.github.common.ExpandableMessage;
import org.jenkinsci.plugins.github.util.BuildDataHelper;
Expand All @@ -23,7 +26,7 @@
import static org.apache.commons.lang3.StringUtils.defaultIfEmpty;

@Extension
public class GitHubSetCommitStatusBuilder extends Builder {
public class GitHubSetCommitStatusBuilder extends Builder implements SimpleBuildStep {
private static final ExpandableMessage DEFAULT_MESSAGE = new ExpandableMessage("");

private ExpandableMessage statusMessage = DEFAULT_MESSAGE;
Expand All @@ -48,17 +51,18 @@ public void setStatusMessage(ExpandableMessage statusMessage) {
}

@Override
public boolean perform(AbstractBuild<?, ?> build,
Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
public void perform(Run<?, ?> build,
FilePath workspace,
Launcher launcher,
TaskListener listener) throws InterruptedException, IOException {
final String sha1 = ObjectId.toString(BuildDataHelper.getCommitSHA1(build));
String message = defaultIfEmpty(
firstNonNull(statusMessage, DEFAULT_MESSAGE).expandAll(build, listener),
Messages.CommitNotifier_Pending(build.getDisplayName())
);
String contextName = displayNameFor(build.getProject());
String contextName = displayNameFor(build.getParent());

for (GitHubRepositoryName name : GitHubRepositoryNameContributor.parseAssociatedNames(build.getProject())) {
for (GitHubRepositoryName name : GitHubRepositoryNameContributor.parseAssociatedNames(build.getParent())) {
for (GHRepository repository : name.resolve()) {
listener.getLogger().println(
GitHubCommitNotifier_SettingCommitStatus(repository.getHtmlUrl() + "/commit/" + sha1)
Expand All @@ -70,7 +74,6 @@ public boolean perform(AbstractBuild<?, ?> build,
contextName);
}
}
return true;
}

@Extension
Expand Down
@@ -1,6 +1,6 @@
package org.jenkinsci.plugins.github.util;

import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.plugins.git.Revision;
import hudson.plugins.git.util.BuildData;
import org.eclipse.jgit.lib.ObjectId;
Expand All @@ -27,7 +27,7 @@ private BuildDataHelper() {
* @throws IOException Cannot get the info about commit ID
*/
@Nonnull
public static ObjectId getCommitSHA1(@Nonnull AbstractBuild<?, ?> build) throws IOException {
public static ObjectId getCommitSHA1(@Nonnull Run<?, ?> build) throws IOException {
BuildData buildData = build.getAction(BuildData.class);
if (buildData == null) {
throw new IOException(Messages.BuildDataHelper_NoBuildDataError());
Expand Down

0 comments on commit 731ffae

Please sign in to comment.