Do not fail if user is missing username attribute
The serviceuser plugin provides a commit validator that checking each
pushed commit, whether the committer is a serviceuser.
If the validation fails the push fails, even if the user is no
serviceuser.
Backtrace:
[SSH git-receive-pack xxx] WARN com.google.gerrit.server.git.
MultiProgressMonitor : MultiProgressMonitor worker did not call end()
before returning
[SSH git-receive-pack xxx] ERROR com.google.gerrit.server.git.receive.
AsyncReceiveCommits : error while processing push
java.util.concurrent.ExecutionException: java.util.NoSuchElementException:
No value present
Caused by: java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.get(Optional.java:148)
at com.googlesource.gerrit.plugins.serviceuser.GetServiceUser.
apply(GetServiceUser.java:61)
at com.googlesource.gerrit.plugins.serviceuser.ServiceUserResolver.
getAsServiceUser(ServiceUserResolver.java:91)
at com.googlesource.gerrit.plugins.serviceuser.ValidateServiceUserCommits.
onCommitReceived(ValidateServiceUserCommits.java:52)
Users without username attribute are valid in case of OAuth and other
providers until user once set them.
This change ignores users that are missing username attribute.
Change-Id: I505edc7689ab0e9bd8adee42508badf9bdcec867
Signed-off-by: Alon Bar-Lev <alon.barlev@gmail.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/GetServiceUser.java b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/GetServiceUser.java
index 784c6b0..f0b92dd 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/GetServiceUser.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/GetServiceUser.java
@@ -58,7 +58,7 @@
@Override
public Response<ServiceUserInfo> apply(ServiceUserResource rsrc)
throws IOException, RestApiException, PermissionBackendException {
- String username = rsrc.getUser().getUserName().get();
+ String username = rsrc.getUser().getUserName().orElseThrow(ResourceNotFoundException::new);
Config db = storageCache.get();
if (!db.getSubsections(USER).contains(username)) {
throw new ResourceNotFoundException(username);