Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-33306] Stack trace printed when repo:status privilege missing
  • Loading branch information
recena authored and recena committed Apr 7, 2016
1 parent 3425908 commit 25cab34
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 58 deletions.
Expand Up @@ -92,29 +92,38 @@ private static void createBuildCommitStatus(Run<?,?> build, TaskListener listene
} catch (IllegalStateException ise) {
url = "http://unconfigured-jenkins-location/" + build.getUrl();
}
Result result = build.getResult();
String revisionToNotify = resolveHeadCommit(repo, revision);
if (Result.SUCCESS.equals(result)) {
createCommitStatus(repo, revisionToNotify, GHCommitState.SUCCESS, url, Messages.GitHubBuildStatusNotification_CoomitStatus_Good());
} else if (Result.UNSTABLE.equals(result)) {
createCommitStatus(repo, revisionToNotify, GHCommitState.FAILURE, url, Messages.GitHubBuildStatusNotification_CommitStatus_Unstable());
} else if (Result.FAILURE.equals(result)) {
createCommitStatus(repo, revisionToNotify, GHCommitState./* TODO or ERROR? */FAILURE, url, Messages.GitHubBuildStatusNotification_CommitStatus_Failure());
} else if (result != null) { // ABORTED etc.
createCommitStatus(repo, revisionToNotify, GHCommitState.ERROR, url, Messages.GitHubBuildStatusNotification_CommitStatus_Other());
} else {
createCommitStatus(repo, revisionToNotify, GHCommitState.PENDING, url, Messages.GitHubBuildStatusNotification_CommitStatus_Pending());
}
if (result != null) {
listener.getLogger().format("%n" + Messages.GitHubBuildStatusNotification_CommitStatusSet() + "%n%n");
boolean ignoreError = false;
try {
Result result = build.getResult();
String revisionToNotify = resolveHeadCommit(repo, revision);
if (Result.SUCCESS.equals(result)) {
createCommitStatus(repo, revisionToNotify, GHCommitState.SUCCESS, url, Messages.GitHubBuildStatusNotification_CoomitStatus_Good());
} else if (Result.UNSTABLE.equals(result)) {
createCommitStatus(repo, revisionToNotify, GHCommitState.FAILURE, url, Messages.GitHubBuildStatusNotification_CommitStatus_Unstable());
} else if (Result.FAILURE.equals(result)) {
createCommitStatus(repo, revisionToNotify, GHCommitState.FAILURE, url, Messages.GitHubBuildStatusNotification_CommitStatus_Failure());
} else if (result != null) { // ABORTED etc.
createCommitStatus(repo, revisionToNotify, GHCommitState.ERROR, url, Messages.GitHubBuildStatusNotification_CommitStatus_Other());
} else {
ignoreError = true;
createCommitStatus(repo, revisionToNotify, GHCommitState.PENDING, url, Messages.GitHubBuildStatusNotification_CommitStatus_Pending());
}
if (result != null) {
listener.getLogger().format("%n" + Messages.GitHubBuildStatusNotification_CommitStatusSet() + "%n%n");
}
} catch (FileNotFoundException fnfe) {
if (!ignoreError) {
listener.getLogger().format("%nCould not update commit status, please check if your scan " +
"credentials belong to a member of the organization or a collaborator of the " +
"repository and repo:status scope is selected%n%n");
LOGGER.log(Level.FINE, null, fnfe);
}
}
}
}
} catch (FileNotFoundException fnfe) {
listener.getLogger().format("%nCould not update commit status, please check if your scan credentials belong to a member of the organization or a collaborator of the repository%n%n");
} catch (IOException ioe) {
listener.getLogger().format("%nCould not update commit status. Message: %s%n%n", ioe.getMessage());
LOGGER.log(Level.WARNING, "Could not update commit status", ioe);
LOGGER.log(Level.FINE, "Could not update commit status", ioe);
}

}
Expand Down Expand Up @@ -167,7 +176,7 @@ private static GitHubSCMSource getSCMSource(final SCMSourceOwner scmSourceOwner)
@Extension public static class PRJobScheduledListener extends QueueListener {

/**
* Manages the Github Commit Pending Status.
* Manages the GitHub Commit Pending Status.
*/
@Override public void onEnterWaiting(Queue.WaitingItem wi) {
if (wi.task instanceof Job) {
Expand All @@ -191,8 +200,12 @@ private static GitHubSCMSource getSCMSource(final SCMSourceOwner scmSourceOwner)
// In fact the submitter might push another commit before this build even starts.
createCommitStatus(repo, pr.getHead().getSha(), GHCommitState.PENDING, url, "This pull request is scheduled to be built");
}
} catch (FileNotFoundException fnfe) {
LOGGER.log(Level.WARNING, "Could not update commit status to PENDING. Valid scan credentials? Valid scopes?");
LOGGER.log(Level.FINE, null, fnfe);
} catch (IOException ioe) {
LOGGER.log(Level.WARNING, "Could not update commit status", ioe);
LOGGER.log(Level.WARNING, "Could not update commit status to PENDING. Message: " + ioe.getMessage());
LOGGER.log(Level.FINE, null, ioe);
}
}
}
Expand All @@ -202,6 +215,7 @@ private static GitHubSCMSource getSCMSource(final SCMSourceOwner scmSourceOwner)

/**
* With this listener one notifies to GitHub when the SCM checkout process has started.
* Possible option: GHCommitState.PENDING
*/
@Extension public static class JobCheckOutListener extends SCMListener {

Expand All @@ -213,10 +227,10 @@ private static GitHubSCMSource getSCMSource(final SCMSourceOwner scmSourceOwner)

/**
* With this listener one notifies to GitHub the build result.
* Possible options: GHCommitState.SUCCESS, GHCommitState.ERROR or GHCommitState.FAILURE
*/
@Extension public static class JobCompletedListener extends RunListener<Run<?,?>> {

@SuppressWarnings("deprecation") // Run.getAbsoluteUrl appropriate here
@Override public void onCompleted(Run<?, ?> build, TaskListener listener) {
createBuildCommitStatus(build, listener);
}
Expand Down
Expand Up @@ -275,46 +275,46 @@ private void doRetrieve(SCMHeadObserver observer, TaskListener listener, GHRepos
}
listener.getLogger().format("%n %d branches were processed%n", branches);

listener.getLogger().format("%n Getting remote pull requests...%n");
int pullrequests = 0;
for (GHPullRequest ghPullRequest : repo.getPullRequests(GHIssueState.OPEN)) {
PullRequestSCMHead head = new PullRequestSCMHead(ghPullRequest);
final String branchName = head.getName();
listener.getLogger().format("%n Checking pull request %s%n", HyperlinkNote.encodeTo(ghPullRequest.getHtmlUrl().toString(), "#" + branchName));
// FYI https://developer.github.com/v3/pulls/#response-1
Boolean mergeable = ghPullRequest.getMergeable();
if (!Boolean.TRUE.equals(mergeable)) {
listener.getLogger().format(" Not mergeable, skipping%n%n");
continue;
}
if (repo.getOwner().equals(ghPullRequest.getHead().getUser())) {
listener.getLogger().format(" Submitted from origin repository, skipping%n%n");
continue;
}
if (criteria != null) {
SCMSourceCriteria.Probe probe = getProbe(branchName, "pull request", "refs/pull/" + head.getNumber() + "/head", repo, listener);
if (criteria.isHead(probe, listener)) {
listener.getLogger().format(" Met criteria%n");
} else {
listener.getLogger().format(" Does not meet criteria%n");
continue;
}
}
String trustedBase = trustedReplacement(repo, ghPullRequest);
SCMRevision hash;
if (trustedBase == null) {
hash = new SCMRevisionImpl(head, ghPullRequest.getHead().getSha());
listener.getLogger().format("%n Getting remote pull requests...%n");
int pullrequests = 0;
for (GHPullRequest ghPullRequest : repo.getPullRequests(GHIssueState.OPEN)) {
PullRequestSCMHead head = new PullRequestSCMHead(ghPullRequest);
final String branchName = head.getName();
listener.getLogger().format("%n Checking pull request %s%n", HyperlinkNote.encodeTo(ghPullRequest.getHtmlUrl().toString(), "#" + branchName));
// FYI https://developer.github.com/v3/pulls/#response-1
Boolean mergeable = ghPullRequest.getMergeable();
if (!Boolean.TRUE.equals(mergeable)) {
listener.getLogger().format(" Not mergeable, skipping%n%n");
continue;
}
if (repo.getOwner().equals(ghPullRequest.getHead().getUser())) {
listener.getLogger().format(" Submitted from origin repository, skipping%n%n");
continue;
}
if (criteria != null) {
SCMSourceCriteria.Probe probe = getProbe(branchName, "pull request", "refs/pull/" + head.getNumber() + "/head", repo, listener);
if (criteria.isHead(probe, listener)) {
listener.getLogger().format(" Met criteria%n");
} else {
listener.getLogger().format(" (not from a trusted source)%n");
hash = new UntrustedPullRequestSCMRevision(head, ghPullRequest.getHead().getSha(), trustedBase);
}
observer.observe(head, hash);
if (!observer.isObserving()) {
return;
listener.getLogger().format(" Does not meet criteria%n");
continue;
}
pullrequests++;
}
listener.getLogger().format("%n %d pull requests were processed%n", pullrequests);
String trustedBase = trustedReplacement(repo, ghPullRequest);
SCMRevision hash;
if (trustedBase == null) {
hash = new SCMRevisionImpl(head, ghPullRequest.getHead().getSha());
} else {
listener.getLogger().format(" (not from a trusted source)%n");
hash = new UntrustedPullRequestSCMRevision(head, ghPullRequest.getHead().getSha(), trustedBase);
}
observer.observe(head, hash);
if (!observer.isObserving()) {
return;
}
pullrequests++;
}
listener.getLogger().format("%n %d pull requests were processed%n", pullrequests);

}

Expand Down

0 comments on commit 25cab34

Please sign in to comment.