Skip to content
Permalink
Browse files

Fix JENKINS-39662: NullPointerException for multi-config projects #close

  • Loading branch information...
Daniel15 committed Nov 12, 2016
1 parent 25ebe03 commit b473330e7b889a4ca784b132e3b561a91ff75bb4
@@ -19,6 +19,7 @@
import hudson.tasks.Publisher;
import jenkins.tasks.SimpleBuildStep;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.githubissues.exceptions.GitHubRepositoryException;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHRepository;
import org.kohsuke.stapler.DataBoundConstructor;
@@ -50,13 +51,35 @@ public BuildStepMonitor getRequiredMonitorService() {

/**
* Gets the GitHub repository for the specified job.
* @param job The job
* @return The GitHub repository
*
* @param run The run
* @return The GitHub repository, or null if there's no GitHub repository for this run
* @throws GitHubRepositoryException when the GitHub repository can not be loaded
*/
public GHRepository getRepoForJob(Job<?, ?> job) {
GithubProjectProperty foo = job.getProperty(GithubProjectProperty.class);
GitHubRepositoryName repoName = GitHubRepositoryName.create(foo.getProjectUrlStr());
return repoName == null ? null : repoName.resolveOne();
private GHRepository getRepoForRun(Run<?, ?> run) throws GitHubRepositoryException {
Job<?, ?> rootJob = run.getParent();
if (run instanceof AbstractBuild<?, ?>) {
// If the run is an AbstractBuild, it could be a build that contains a root build (for example, a
// multi-build project). In that case, we need to get the root project as that's where the GitHub settings
// are configured.
rootJob = ((AbstractBuild) run).getRootBuild().getProject();
}
GithubProjectProperty gitHubProperty = rootJob.getProperty(GithubProjectProperty.class);
if (gitHubProperty == null) {
throw new GitHubRepositoryException("GitHub property not configured");
}
GitHubRepositoryName repoName = GitHubRepositoryName.create(gitHubProperty.getProjectUrlStr());
if (repoName == null) {
throw new GitHubRepositoryException("GitHub project not configured");
}
GHRepository repo = repoName.resolveOne();
if (repo == null) {
throw new GitHubRepositoryException(
"Could not connect to GitHub repository. Please double-check that you have correctly configured a " +
"GitHub API key."
);
}
return repo;
}

@Override
@@ -87,9 +110,11 @@ public void perform(
}

// If we got here, we need to grab the repo to create an issue (or close an existing issue)
GHRepository repo = getRepoForJob(run.getParent());
if (repo == null) {
logger.println("WARNING: No GitHub config available for this job, GitHub Issue Notifier will not run!");
GHRepository repo;
try {
repo = getRepoForRun(run);
} catch (GitHubRepositoryException ex) {
logger.println("WARNING: No GitHub config available for this job, GitHub Issue Notifier will not run! Error: " + ex.getMessage());
return;
}

@@ -0,0 +1,20 @@
/**
* Copyright (c) 2016-present, Daniel Lo Nigro (Daniel15)
* All rights reserved.
*
* This source code is licensed under the MIT-style license found in the
* LICENSE file in the root directory of this source tree.
*/
package org.jenkinsci.plugins.githubissues.exceptions;

/**
* Exception thrown when we fail to load the GitHub repository details for a project.
*/
public class GitHubRepositoryException extends Exception {
public GitHubRepositoryException(String message) {
super(message);
}
public GitHubRepositoryException(String message, Throwable throwable) {
super(message, throwable);
}
}

0 comments on commit b473330

Please sign in to comment.
You can’t perform that action at this time.