Skip to content

Commit

Permalink
Revert pipeline changes for the UpstreamStatusListener.
Browse files Browse the repository at this point in the history
Fixes #506, but means we won't be able to use that functionality in pipelines until there
is a deeper fix.
  • Loading branch information
Ben Patterson committed Apr 14, 2017
1 parent d802a6e commit febd4bb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.jenkinsci.plugins.ghprb.upstream;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.*;
import hudson.tasks.BuildWrapper;

import jenkins.tasks.SimpleBuildWrapper;
import org.jenkinsci.plugins.ghprb.extensions.comments.GhprbBuildResultMessage;
import org.kohsuke.github.GHCommitState;
import org.kohsuke.stapler.DataBoundConstructor;
Expand All @@ -23,27 +20,28 @@
* @author Kevin Suwala
*/

public class GhprbUpstreamStatus extends SimpleBuildWrapper {
public class GhprbUpstreamStatus extends BuildWrapper {
private final Boolean showMatrixStatus;
private final String commitStatusContext;
private final String triggeredStatus;
private final String startedStatus;
private final String statusUrl;
private final Boolean addTestResults;
private final List<GhprbBuildResultMessage> completedStatus;


// sets the context and message as env vars so that they are available in the Listener class
@Override
public void setUp(Context context, Run<?, ?> run, FilePath filePath, Launcher launcher, TaskListener taskListener, EnvVars envVars) throws IOException, InterruptedException {
envVars.put("ghprbShowMatrixStatus",Boolean.toString(getShowMatrixStatus()));
envVars.put("ghprbUpstreamStatus", "true");
envVars.put("ghprbCommitStatusContext", getCommitStatusContext());
envVars.put("ghprbTriggeredStatus", getTriggeredStatus());
envVars.put("ghprbStartedStatus", getStartedStatus());
envVars.put("ghprbStatusUrl", getStatusUrl());
envVars.put("ghprbAddTestResults", Boolean.toString(getAddTestResults()));

public void makeBuildVariables(@SuppressWarnings("rawtypes") AbstractBuild build, Map<String,String> variables){
variables.put("ghprbShowMatrixStatus",Boolean.toString(getShowMatrixStatus()));
variables.put("ghprbUpstreamStatus", "true");
variables.put("ghprbCommitStatusContext", getCommitStatusContext());
variables.put("ghprbTriggeredStatus", getTriggeredStatus());
variables.put("ghprbStartedStatus", getStartedStatus());
variables.put("ghprbStatusUrl", getStatusUrl());
variables.put("ghprbAddTestResults", Boolean.toString(getAddTestResults()));
Map<GHCommitState, StringBuilder> statusMessages = new HashMap<GHCommitState, StringBuilder>(5);

for (GhprbBuildResultMessage message : getCompletedStatus()) {
GHCommitState state = message.getResult();
StringBuilder sb;
Expand All @@ -56,13 +54,26 @@ public void setUp(Context context, Run<?, ?> run, FilePath filePath, Launcher la
}
sb.append(message.getMessage());
}

for (Entry<GHCommitState, StringBuilder> next : statusMessages.entrySet()) {
String key = String.format("ghprb%sMessage", next.getKey().name());
envVars.put(key, next.getValue().toString());
variables.put(key, next.getValue().toString());
}
}

@Override
@SuppressWarnings("unchecked")
public BuildWrapper.Environment setUp(@SuppressWarnings("rawtypes") AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
makeBuildVariables(build, build.getBuildVariables());
return new Environment(){};
}

@Override
@SuppressWarnings("unchecked")
public void preCheckout(@SuppressWarnings("rawtypes") AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
makeBuildVariables(build, build.getBuildVariables());
}

@DataBoundConstructor
public GhprbUpstreamStatus(
Boolean showMatrixStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import hudson.Extension;
import hudson.Launcher;
import hudson.model.*;
import hudson.model.BuildListener;
import hudson.model.Environment;
import hudson.model.TaskListener;
import hudson.model.AbstractBuild;
import hudson.model.listeners.RunListener;

import org.jenkinsci.plugins.ghprb.Ghprb;
Expand All @@ -15,7 +18,6 @@
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -29,13 +31,13 @@
*/

@Extension
public class GhprbUpstreamStatusListener extends RunListener<Run<?, ?>> {
public class GhprbUpstreamStatusListener extends RunListener<AbstractBuild<?, ?>> {
private static final Logger logger = Logger.getLogger(GhprbUpstreamStatusListener.class.getName());

private GHRepository repo;

// Gets all the custom env vars needed to send information to GitHub
private Map<String, String> returnEnvironmentVars(Run<?, ?> build, TaskListener listener){
private Map<String, String> returnEnvironmentVars(AbstractBuild<?, ?> build, TaskListener listener){
Map<String, String> envVars = Ghprb.getEnvVars(build, listener);

if (!envVars.containsKey("ghprbUpstreamStatus")) {
Expand All @@ -46,7 +48,7 @@ private Map<String, String> returnEnvironmentVars(Run<?, ?> build, TaskListener
.getGitHubAuth(envVars.get("ghprbCredentialsId"));

try {
GitHub gh = auth.getConnection(build.getParent());
GitHub gh = auth.getConnection(build.getProject());
repo = gh.getRepository(envVars.get("ghprbGhRepository"));
return envVars;
} catch (Exception e) {
Expand Down Expand Up @@ -76,8 +78,25 @@ private GhprbSimpleStatus returnGhprbSimpleStatus(Map<String, String> envVars) {
);
}

// Sets the status as pending when the job starts and then calls the createCommitStatus method to send it to GitHub
@Override
public void onStarted(Run<?, ?> build, TaskListener listener) {
public Environment setUpEnvironment(@SuppressWarnings("rawtypes") AbstractBuild build, Launcher launcher, BuildListener listener) {
Map<String, String> envVars = returnEnvironmentVars(build, listener);
if (envVars != null) {
logger.log(Level.FINE, "Job: " + build.getFullDisplayName() + " Attempting to send GitHub commit status");

try {
returnGhprbSimpleStatus(envVars).onEnvironmentSetup(build, listener, repo);
} catch (GhprbCommitStatusException e) {
e.printStackTrace();
}
}

return new Environment(){};
}

@Override
public void onStarted(AbstractBuild<?, ?> build, TaskListener listener) {
Map<String, String> envVars = returnEnvironmentVars(build, listener);
if (envVars == null) {
return;
Expand All @@ -92,7 +111,7 @@ public void onStarted(Run<?, ?> build, TaskListener listener) {

// Sets the status to the build result when the job is done, and then calls the createCommitStatus method to send it to GitHub
@Override
public void onCompleted(Run<?, ?> build, TaskListener listener) {
public void onCompleted(AbstractBuild<?, ?> build, TaskListener listener) {
Map<String, String> envVars = returnEnvironmentVars(build, listener);
if (envVars == null) {
return;
Expand Down

0 comments on commit febd4bb

Please sign in to comment.