Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,23 @@ public void run() {
*/
public void unregisterFor(GitHubRepositoryName name, List<GitHubRepositoryName> aliveRepos) {
try {
GHRepository repo = checkNotNull(
from(name.resolve(allowedToManageHooks())).firstMatch(withAdminAccess()).orNull(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so null is a valid case here, but checkNotNull is throwing NPE. Just remove checkNotNull and return from method without anything.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optionally for debug purposes print into

   GHRepository repo = from(name.resolve(allowedToManageHooks())).firstMatch(withAdminAccess()).orNull();
   if (isNull(repo)) {
       LOGGER.debug("Skipped removing GitHub webhook for {} because not configured to Manage Hooks or "
                            + "there are no credentials with admin access to manage hooks", name);
       return;
   }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I got this comment properly, or my implementation change for that matter :)

I believe the original code had null as valid case of the not-finding-a-match via orNull(), and handling that via checkNotNull().

My intention was to pick out the same match-or-not logic into the repo variable, which same way can end up null, but do the same checkNotNull handling after checking if we are at all configured to manage the hooks.

IIRC resolving of the repo with each and any call to register/unregister routines was also dictated by selftests somehow. My first shot was to check enablement of webhook management, log and return if it is not enabled - and then the tests failed until I changed the call chain to what is here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So registration of error should happen only for available allowedToManage repos but without admin access

/* This is overcomplicated to satisfy unit tests that expect certain codepaths to be called */
Iterable<GHRepository> manageableIterable = name.resolve(allowedToManageHooks());
GHRepository repo = from(manageableIterable).firstMatch(withAdminAccess()).orNull();

if (!(manageableIterable.iterator().hasNext())) {
if (repo == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repo will be always null if there is no repos in iterator

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, makes sense :)

LOGGER.debug("Skipped removing GitHub webhook for {} because not configured to Manage Hooks, "
+ "also there are no credentials with admin access to manage such hooks", name);
} else {
LOGGER.debug("Skipped removing GitHub webhook for {} because not configured to Manage Hooks", name);
}
GitHubHookRegisterProblemMonitor.get().registerProblem(name, new Exception(
"Skipped removing GitHub webhook because not configured to Manage Hooks"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not a problem if it configured not to manage, so message is not need here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. But without this, the selftests fail in many places because they expect a problem to be registered. And I am not aware enough about the intricacies of the plugin to change this too (neither how to do so OTOH, nor if eventually doing so would break/neuter some other tests).

return;
}

repo = checkNotNull(repo,
"There are no credentials with admin access to manage hooks on %s", name
);

Expand Down Expand Up @@ -178,8 +193,25 @@ protected Function<GitHubRepositoryName, GHHook> createHookSubscribedTo(final Li
@Override
protected GHHook applyNullSafe(@Nonnull GitHubRepositoryName name) {
try {
GHRepository repo = checkNotNull(
from(name.resolve(allowedToManageHooks())).firstMatch(withAdminAccess()).orNull(),
/* This is overcomplicated to satisfy unit tests that expect certain codepaths to be called */
Iterable<GHRepository> manageableIterable = name.resolve(allowedToManageHooks());
GHRepository repo = from(manageableIterable).firstMatch(withAdminAccess()).orNull();

if (!(manageableIterable.iterator().hasNext())) {
if (repo == null) {
LOGGER.debug("Skipped adding GitHub webhook for {} because not configured to Manage "
+ "Hooks, also there are no credentials with admin access to manage such hooks",
name);
} else {
LOGGER.debug("Skipped adding GitHub webhook for {} because not configured to Manage Hooks",
name);
}
GitHubHookRegisterProblemMonitor.get().registerProblem(name, new Exception(
"Skipped adding GitHub webhook because not configured to Manage Hooks"));
return null;
}

repo = checkNotNull(repo,
"There are no credentials with admin access to manage hooks on %s", name
);

Expand Down