From f52c2a58275ae79375267c3a7839c192d9905da8 Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Mon, 7 Aug 2017 20:39:41 +0200 Subject: [PATCH] Avoiding NPE when user not authenticated #75 --- CHANGELOG.md | 6 +++++ .../bjurr/sbcc/SbccRepositoryMergeCheck.java | 22 ++++++++++--------- .../java/se/bjurr/sbcc/UserValidator.java | 7 ++++-- .../se/bjurr/sbcc/settings/SbccSettings.java | 4 ++-- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31b1d7c..c304da0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ Changelog of Simple Bitbucket Commit Checker. +## Unreleased +### No issue + doc + + [975020fd5483f13](https://github.com/tomasbjerre/simple-bitbucket-commit-checker/commit/975020fd5483f13) Tomas Bjerre *2017-08-03 17:27:46* + ## 3.7 ### No issue move CSS and JS into /static so they get loaded automatically when the form is displayed diff --git a/src/main/java/se/bjurr/sbcc/SbccRepositoryMergeCheck.java b/src/main/java/se/bjurr/sbcc/SbccRepositoryMergeCheck.java index 0283124..358e157 100644 --- a/src/main/java/se/bjurr/sbcc/SbccRepositoryMergeCheck.java +++ b/src/main/java/se/bjurr/sbcc/SbccRepositoryMergeCheck.java @@ -3,6 +3,7 @@ import static com.atlassian.bitbucket.hook.repository.RepositoryHookResult.accepted; import static com.atlassian.bitbucket.repository.RefChangeType.ADD; import static java.util.logging.Level.SEVERE; +import static se.bjurr.sbcc.settings.SbccSettings.sscSettings; import java.util.ArrayList; import java.util.List; @@ -10,7 +11,6 @@ import java.util.logging.Logger; import se.bjurr.sbcc.commits.ChangeSetsService; -import se.bjurr.sbcc.settings.SbccSettings; import se.bjurr.sbcc.settings.ValidationException; import com.atlassian.applinks.api.ApplicationLinkService; @@ -55,27 +55,29 @@ public SbccRepositoryMergeCheck( @Override public RepositoryHookResult preUpdate( PreRepositoryHookContext context, PullRequestMergeHookRequest request) { - PullRequest pullRequest = request.getPullRequest(); - ScmHookDetails scmHookDetails = request.getScmHookDetails().orElse(null); - Repository repositoryWithCommits = request.getFromRef().getRepository(); - Repository repositoryWithSettings = request.getToRef().getRepository(); - Optional settings = repositoryHook.findSettings(repositoryWithSettings); + final PullRequest pullRequest = request.getPullRequest(); + final ScmHookDetails scmHookDetails = request.getScmHookDetails().orElse(null); + final Repository repositoryWithCommits = request.getFromRef().getRepository(); + final Repository repositoryWithSettings = request.getToRef().getRepository(); + final Optional settings = repositoryHook.findSettings(repositoryWithSettings); if (!settings.isPresent()) { return accepted(); } try { - boolean shouldCheckPr = SbccSettings.sscSettings(settings.get()).shouldCheckPullRequests(); + final boolean shouldCheckPr = + sscSettings(settings.get()) // + .shouldCheckPullRequests(); if (!shouldCheckPr) { return accepted(); } - } catch (ValidationException e) { + } catch (final ValidationException e) { logger.log(SEVERE, "Could not read settings", e); return accepted(); } - List refChanges = new ArrayList<>(); - RefChange refChange = + final List refChanges = new ArrayList<>(); + final RefChange refChange = new RefChange() { @Override public RefChangeType getType() { diff --git a/src/main/java/se/bjurr/sbcc/UserValidator.java b/src/main/java/se/bjurr/sbcc/UserValidator.java index 5edf14c..1b92677 100644 --- a/src/main/java/se/bjurr/sbcc/UserValidator.java +++ b/src/main/java/se/bjurr/sbcc/UserValidator.java @@ -22,10 +22,13 @@ public boolean shouldIgnoreChecksForUser() { private boolean shouldIgnoreByUserNamePattern() { return settings.getIgnoreUsersPattern().isPresent() - && matches(settings.getIgnoreUsersPattern().get(), currentUser.getName()); + && (currentUser == null + || matches(settings.getIgnoreUsersPattern().get(), currentUser.getName())); } private boolean shouldIgnoreServiceUser() { - return settings.allowServiceUsers() && currentUser.getType().equals(SERVICE); + return settings.allowServiceUsers() + && currentUser != null + && currentUser.getType().equals(SERVICE); } } diff --git a/src/main/java/se/bjurr/sbcc/settings/SbccSettings.java b/src/main/java/se/bjurr/sbcc/settings/SbccSettings.java index 456ade0..6c073a9 100644 --- a/src/main/java/se/bjurr/sbcc/settings/SbccSettings.java +++ b/src/main/java/se/bjurr/sbcc/settings/SbccSettings.java @@ -163,7 +163,7 @@ public static SbccSettings sscSettings(Settings settings) throws ValidationExcep if (!isNullOrEmpty(settings.getString(SETTING_SIZE))) { sbccSettings.withCheckCommitSize(parseInt(settings.getString(SETTING_SIZE))); } - } catch (Exception e) { + } catch (final Exception e) { throw new ValidationException(SETTING_SIZE, "Not an integer!"); } for (int g = 0; g < 1000; g++) { @@ -228,7 +228,7 @@ private SbccSettings withRequireMatchingCommitterNameSlug( } private void withIgnoreUsersPattern(String string) { - this.ignoreUsersPattern = emptyToNull(string); + this.ignoreUsersPattern = emptyToNull(nullToEmpty(string).trim()); } private SbccSettings withShouldCheckPullRequestsMessage(String string) {