Skip to content

Commit 2088896

Browse files
committed
fixed nullpointer for GogsBuildStatusNotification
1 parent 1170625 commit 2088896

File tree

1 file changed

+49
-47
lines changed

1 file changed

+49
-47
lines changed

src/main/java/com/cloudbees/jenkins/plugins/gogs/notification/GogsBuildStatusNotification.java

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import hudson.model.*;
1212
import hudson.model.listeners.RunListener;
1313
import hudson.model.listeners.SCMListener;
14-
import hudson.plugins.git.GitStatus;
1514
import hudson.scm.SCM;
1615
import hudson.scm.SCMRevisionState;
1716
import jenkins.plugins.git.AbstractGitSCMSource.SCMRevisionImpl;
@@ -62,54 +61,57 @@ private static GogsServerIssue createCommitStatus(@Nonnull GogsRepository repo,
6261
private static void createBuildCommitStatus(Run<?,?> build, TaskListener listener) {
6362
try {
6463
SCMSourceOwner scmSourceOwner = getSCMSourceOwner(build.getParent());
65-
GogsSCMSource source = getSCMSource(scmSourceOwner);
66-
int buildFailureLabelId = source.getBuildFailureLabelId();
67-
68-
GogsApi gogs = GogsApiConnector.connect(source.getGogsServerUrl(), source.getRepoOwner(), source.getRepository(), GogsApiConnector.lookupScanCredentials
69-
(scmSourceOwner, null, source.getCredentialsId()));
70-
71-
GogsRepository repo = gogs.getRepository();
72-
if (repo != null) {
73-
List<Cause> causes = build.getCauses();
74-
for(Cause cause : causes) {
75-
LOGGER.info(cause.getClass().getName() + " cause short desc: " + cause.getShortDescription());
76-
}
77-
78-
SCMRevisionAction action = build.getAction(SCMRevisionAction.class);
79-
if (action != null) {
80-
SCMRevision revision = action.getRevision();
81-
String url;
82-
try {
83-
url = build.getAbsoluteUrl();
84-
} catch (IllegalStateException ise) {
85-
url = "http://unconfigured-jenkins-location/" + build.getUrl();
64+
//no need to continue if there is no SCMSourceOwner
65+
if(scmSourceOwner != null) {
66+
GogsSCMSource source = getSCMSource(scmSourceOwner);
67+
int buildFailureLabelId = source.getBuildFailureLabelId();
68+
69+
GogsApi gogs = GogsApiConnector.connect(source.getGogsServerUrl(), source.getRepoOwner(), source.getRepository(), GogsApiConnector.lookupScanCredentials
70+
(scmSourceOwner, null, source.getCredentialsId()));
71+
72+
GogsRepository repo = gogs.getRepository();
73+
if (repo != null) {
74+
List<Cause> causes = build.getCauses();
75+
for (Cause cause : causes) {
76+
LOGGER.info(cause.getClass().getName() + " cause short desc: " + cause.getShortDescription());
8677
}
87-
boolean ignoreError = false;
88-
try {
89-
Result result = build.getResult();
90-
String revisionToNotify = resolveHeadCommit(revision);
91-
Job<?,?> job = build.getParent();
92-
GogsServerIssue issue = null;
93-
if (Result.UNSTABLE.equals(result)) {
94-
issue = createCommitStatus(repo, revisionToNotify, GogsCommitState.FAILURE, url, Messages.GogsBuildStatusNotification_CommitStatus_Unstable(), job, buildFailureLabelId);
95-
} else if (Result.FAILURE.equals(result)) {
96-
issue = createCommitStatus(repo, revisionToNotify, GogsCommitState.FAILURE, url, Messages.GogsBuildStatusNotification_CommitStatus_Failure(), job, buildFailureLabelId);
97-
} else if (!Result.SUCCESS.equals(result) && result != null) { // ABORTED etc.
98-
issue = createCommitStatus(repo, revisionToNotify, GogsCommitState.ERROR, url, Messages.GogsBuildStatusNotification_CommitStatus_Other(), job, buildFailureLabelId);
99-
}
100-
if(issue != null) {
101-
LOGGER.info("create issue with title: " + issue.getTitle());
102-
gogs.createIssue(issue);
103-
}
104-
if (result != null) {
105-
listener.getLogger().format("%n" + Messages.GogsBuildStatusNotification_CommitStatusSet() + "%n%n");
78+
79+
SCMRevisionAction action = build.getAction(SCMRevisionAction.class);
80+
if (action != null) {
81+
SCMRevision revision = action.getRevision();
82+
String url;
83+
try {
84+
url = build.getAbsoluteUrl();
85+
} catch (IllegalStateException ise) {
86+
url = "http://unconfigured-jenkins-location/" + build.getUrl();
10687
}
107-
} catch (FileNotFoundException fnfe) {
108-
if (!ignoreError) {
109-
listener.getLogger().format("%nCould not update commit status, please check if your scan " +
110-
"credentials belong to a member of the organization or a collaborator of the " +
111-
"repository and repo:status scope is selected%n%n");
112-
LOGGER.log(Level.FINE, null, fnfe);
88+
boolean ignoreError = false;
89+
try {
90+
Result result = build.getResult();
91+
String revisionToNotify = resolveHeadCommit(revision);
92+
Job<?, ?> job = build.getParent();
93+
GogsServerIssue issue = null;
94+
if (Result.UNSTABLE.equals(result)) {
95+
issue = createCommitStatus(repo, revisionToNotify, GogsCommitState.FAILURE, url, Messages.GogsBuildStatusNotification_CommitStatus_Unstable(), job, buildFailureLabelId);
96+
} else if (Result.FAILURE.equals(result)) {
97+
issue = createCommitStatus(repo, revisionToNotify, GogsCommitState.FAILURE, url, Messages.GogsBuildStatusNotification_CommitStatus_Failure(), job, buildFailureLabelId);
98+
} else if (!Result.SUCCESS.equals(result) && result != null) { // ABORTED etc.
99+
issue = createCommitStatus(repo, revisionToNotify, GogsCommitState.ERROR, url, Messages.GogsBuildStatusNotification_CommitStatus_Other(), job, buildFailureLabelId);
100+
}
101+
if (issue != null) {
102+
LOGGER.info("create issue with title: " + issue.getTitle());
103+
gogs.createIssue(issue);
104+
}
105+
if (result != null) {
106+
listener.getLogger().format("%n" + Messages.GogsBuildStatusNotification_CommitStatusSet() + "%n%n");
107+
}
108+
} catch (FileNotFoundException fnfe) {
109+
if (!ignoreError) {
110+
listener.getLogger().format("%nCould not update commit status, please check if your scan " +
111+
"credentials belong to a member of the organization or a collaborator of the " +
112+
"repository and repo:status scope is selected%n%n");
113+
LOGGER.log(Level.FINE, null, fnfe);
114+
}
113115
}
114116
}
115117
}

0 commit comments

Comments
 (0)