Skip to content

The Unified and Autonomous Development Platform

feat: Service accounts not recognized as committers (OD-2723)

Robin Shen637cdd93169b
diff --git a/server-core/src/main/java/io/onedev/server/model/EmailAddress.java b/server-core/src/main/java/io/onedev/server/model/EmailAddress.java
index d9dfedd67f..b163676e6d 100644
--- a/server-core/src/main/java/io/onedev/server/model/EmailAddress.java
+++ b/server-core/src/main/java/io/onedev/server/model/EmailAddress.java
@@ -1,17 +1,25 @@
package io.onedev.server.model;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.Index;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.Table;
+import javax.validation.constraints.Email;
+import javax.validation.constraints.NotEmpty;
+
+import org.hibernate.annotations.Cache;
+import org.hibernate.annotations.CacheConcurrencyStrategy;
+
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
+
+import io.onedev.server.annotation.Editable;
import io.onedev.server.rest.annotation.Immutable;
import io.onedev.server.util.CryptoUtils;
import io.onedev.server.util.facade.EmailAddressFacade;
-import io.onedev.server.annotation.Editable;
-import org.hibernate.annotations.Cache;
-import org.hibernate.annotations.CacheConcurrencyStrategy;
-
-import javax.persistence.*;
-import javax.validation.constraints.Email;
-import javax.validation.constraints.NotEmpty;
@Editable
@Entity
@@ -107,5 +115,5 @@ public class EmailAddress extends AbstractEntity {
return new EmailAddressFacade(getId(), getOwner().getId(), getValue(),
isPrimary(), isGit(), isOpen(), getVerificationCode());
}
-
+
}
diff --git a/server-core/src/main/java/io/onedev/server/model/User.java b/server-core/src/main/java/io/onedev/server/model/User.java
index 9d6ef2ba44..bdda3cd4a6 100644
--- a/server-core/src/main/java/io/onedev/server/model/User.java
+++ b/server-core/src/main/java/io/onedev/server/model/User.java
@@ -3,7 +3,6 @@ package io.onedev.server.model;
import static io.onedev.server.model.User.PROP_FULL_NAME;
import static io.onedev.server.model.User.PROP_NAME;
import static io.onedev.server.model.User.Type.AI;
-import static io.onedev.server.model.User.Type.SERVICE;
import static io.onedev.server.security.SecurityUtils.asPrincipals;
import static io.onedev.server.security.SecurityUtils.asUserPrincipal;
import static io.onedev.server.security.SecurityUtils.isAdministrator;
@@ -54,10 +53,10 @@ import io.onedev.server.model.support.NamedProjectQuery;
import io.onedev.server.model.support.QueryPersonalization;
import io.onedev.server.model.support.TwoFactorAuthentication;
import io.onedev.server.model.support.build.NamedBuildQuery;
-import io.onedev.server.model.support.workspace.NamedWorkspaceQuery;
import io.onedev.server.model.support.issue.NamedIssueQuery;
import io.onedev.server.model.support.pack.NamedPackQuery;
import io.onedev.server.model.support.pullrequest.NamedPullRequestQuery;
+import io.onedev.server.model.support.workspace.NamedWorkspaceQuery;
import io.onedev.server.security.SecurityUtils;
import io.onedev.server.service.EmailAddressService;
import io.onedev.server.service.SettingService;
@@ -82,11 +81,15 @@ public class User extends AbstractEntity implements AuthenticationInfo {
public static final Long ROOT_ID = 1L;
+ private static final String SERVICE_ACCOUNT_EMAIL_PREFIX = "sc-";
+
+ private static final String AI_ACCOUNT_EMAIL_PREFIX = "ai-";
+
+ private static final String SERVICE_OR_AI_ACCOUNT_EMAIL_SUFFIX = "@onedev";
+
public static final String SYSTEM_NAME = "OneDev";
public static final String SYSTEM_EMAIL_ADDRESS = "system@onedev";
-
- public static final String AI_EMAIL_ADDRESS = "ai@onedev";
public static final String UNKNOWN_NAME = "unknown";
@@ -769,11 +772,11 @@ public class User extends AbstractEntity implements AuthenticationInfo {
}
public PersonIdent asPerson() {
- if (getType() == SERVICE) {
- throw new ExplicitException("Service account does not have git identity");
- } else if (getType() == AI) {
- return new PersonIdent(getName(), User.AI_EMAIL_ADDRESS);
- } else if (isUnknown()) {
+ var serviceOrAiAccountEmailAddress = getServiceOrAiAccountEmailAddress();
+ if (serviceOrAiAccountEmailAddress != null)
+ return new PersonIdent(getName(), serviceOrAiAccountEmailAddress.getValue());
+
+ if (isUnknown()) {
throw new ExplicitException("Unknown user does not have git identity");
} else if (isSystem()) {
return new PersonIdent(User.SYSTEM_NAME, User.SYSTEM_EMAIL_ADDRESS);
@@ -1253,7 +1256,11 @@ public class User extends AbstractEntity implements AuthenticationInfo {
}
@Nullable
- public EmailAddress getPrimaryEmailAddress() {
+ public EmailAddress getPrimaryEmailAddress() {
+ var serviceOrAiAccountEmailAddress = getServiceOrAiAccountEmailAddress();
+ if (serviceOrAiAccountEmailAddress != null)
+ return serviceOrAiAccountEmailAddress;
+
if (primaryEmailAddress == null)
primaryEmailAddress = Optional.ofNullable(getEmailAddressService().findPrimary(this));
return primaryEmailAddress.orElse(null);
@@ -1261,6 +1268,10 @@ public class User extends AbstractEntity implements AuthenticationInfo {
@Nullable
public EmailAddress getGitEmailAddress() {
+ var serviceOrAiAccountEmailAddress = getServiceOrAiAccountEmailAddress();
+ if (serviceOrAiAccountEmailAddress != null)
+ return serviceOrAiAccountEmailAddress;
+
if (gitEmailAddress == null)
gitEmailAddress = Optional.ofNullable(getEmailAddressService().findGit(this));
return gitEmailAddress.orElse(null);
@@ -1268,11 +1279,32 @@ public class User extends AbstractEntity implements AuthenticationInfo {
@Nullable
public EmailAddress getPublicEmailAddress() {
+ var serviceOrAiAccountEmailAddress = getServiceOrAiAccountEmailAddress();
+ if (serviceOrAiAccountEmailAddress != null)
+ return serviceOrAiAccountEmailAddress;
+
if (publicEmailAddress == null)
publicEmailAddress = Optional.ofNullable(getEmailAddressService().findPublic(this));
return publicEmailAddress.orElse(null);
}
+ @Nullable
+ public EmailAddress getServiceOrAiAccountEmailAddress() {
+ if (getType() == Type.SERVICE || getType() == Type.AI) {
+ var emailAddress = new EmailAddress();
+ var emailPrefix = getType() == Type.SERVICE ? SERVICE_ACCOUNT_EMAIL_PREFIX : AI_ACCOUNT_EMAIL_PREFIX;
+ emailAddress.setValue(emailPrefix + getId() + SERVICE_OR_AI_ACCOUNT_EMAIL_SUFFIX);
+ emailAddress.setOwner(this);
+ emailAddress.setPrimary(true);
+ emailAddress.setGit(true);
+ emailAddress.setOpen(true);
+ emailAddress.setVerificationCode(null);
+ return emailAddress;
+ } else {
+ return null;
+ }
+ }
+
public void addEmailAddress(EmailAddress emailAddress) {
emailAddress.setOwner(this);
emailAddress.setPrimary(getEmailAddresses().stream().noneMatch(it->it.isPrimary()));
@@ -1316,6 +1348,21 @@ public class User extends AbstractEntity implements AuthenticationInfo {
return entitledAis;
}
+ @Nullable
+ public static Long getServiceOrAiAccountId(String value) {
+ if (value.endsWith(SERVICE_OR_AI_ACCOUNT_EMAIL_SUFFIX)) {
+ value = value.substring(0, value.length() - SERVICE_OR_AI_ACCOUNT_EMAIL_SUFFIX.length());
+ if (value.startsWith(SERVICE_ACCOUNT_EMAIL_PREFIX)) {
+ var serviceAccountId = value.substring(SERVICE_ACCOUNT_EMAIL_PREFIX.length());
+ return Long.parseLong(serviceAccountId);
+ } else if (value.startsWith(AI_ACCOUNT_EMAIL_PREFIX)) {
+ var aiAccountId = value.substring(AI_ACCOUNT_EMAIL_PREFIX.length());
+ return Long.parseLong(aiAccountId);
+ }
+ }
+ return null;
+ }
+
public static String encodeWorkspaceDataKey(String dataKey) {
try {
return URLEncoder.encode(dataKey, StandardCharsets.UTF_8.name());
diff --git a/server-core/src/main/java/io/onedev/server/service/impl/DefaultEmailAddressService.java b/server-core/src/main/java/io/onedev/server/service/impl/DefaultEmailAddressService.java
index 647ce2e33b..f2d23b7043 100644
--- a/server-core/src/main/java/io/onedev/server/service/impl/DefaultEmailAddressService.java
+++ b/server-core/src/main/java/io/onedev/server/service/impl/DefaultEmailAddressService.java
@@ -13,6 +13,7 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.hazelcast.core.HazelcastInstance;
+import io.onedev.commons.utils.StringUtils;
import io.onedev.server.cluster.ClusterService;
import io.onedev.server.event.Listen;
import io.onedev.server.event.entity.EntityPersisted;
@@ -28,6 +29,7 @@ import io.onedev.server.persistence.annotation.Sessional;
import io.onedev.server.persistence.annotation.Transactional;
import io.onedev.server.service.EmailAddressService;
import io.onedev.server.service.SettingService;
+import io.onedev.server.service.UserService;
import io.onedev.server.util.facade.EmailAddressCache;
import io.onedev.server.util.facade.EmailAddressFacade;
@@ -48,6 +50,9 @@ public class DefaultEmailAddressService extends BaseEntityService<EmailAddress>
@Inject
private ClusterService clusterService;
+
+ @Inject
+ private UserService userService;
private volatile EmailAddressCache cache;
@@ -67,10 +72,15 @@ public class DefaultEmailAddressService extends BaseEntityService<EmailAddress>
@Override
public EmailAddress findByValue(String value) {
EmailAddressFacade facade = cache.findByValue(value);
- if (facade != null)
- return load(facade.getId());
- else
+ if (facade != null) {
+ if (facade.getId() != null) {
+ return load(facade.getId());
+ } else {
+ return userService.load(facade.getOwnerId()).getServiceOrAiAccountEmailAddress();
+ }
+ } else {
return null;
+ }
}
@Sessional
@@ -82,9 +92,8 @@ public class DefaultEmailAddressService extends BaseEntityService<EmailAddress>
@Sessional
@Override
public EmailAddress findByPersonIdent(PersonIdent personIdent) {
- EmailAddressFacade facade = cache.findByPersonIdent(personIdent);
- if (facade != null)
- return load(facade.getId());
+ if (StringUtils.isNotBlank(personIdent.getEmailAddress()))
+ return findByValue(personIdent.getEmailAddress());
else
return null;
}
diff --git a/server-core/src/main/java/io/onedev/server/util/facade/EmailAddressCache.java b/server-core/src/main/java/io/onedev/server/util/facade/EmailAddressCache.java
index 30b01e4522..aeb624b3a7 100644
--- a/server-core/src/main/java/io/onedev/server/util/facade/EmailAddressCache.java
+++ b/server-core/src/main/java/io/onedev/server/util/facade/EmailAddressCache.java
@@ -5,9 +5,6 @@ import java.util.Map;
import org.jspecify.annotations.Nullable;
-import org.apache.commons.lang3.StringUtils;
-import org.eclipse.jgit.lib.PersonIdent;
-
import io.onedev.server.model.User;
import io.onedev.server.util.MapProxy;
@@ -27,20 +24,16 @@ public class EmailAddressCache extends MapProxy<Long, EmailAddressFacade> {
@Nullable
public EmailAddressFacade findByValue(String value) {
value = value.toLowerCase();
+ var serviceOrAiAccountId = User.getServiceOrAiAccountId(value);
+ if (serviceOrAiAccountId != null)
+ return new EmailAddressFacade(null, serviceOrAiAccountId, value, true, true, true, null);
+
for (EmailAddressFacade facade: values()) {
if (facade.getValue().equals(value))
return facade;
}
return null;
}
-
- @Nullable
- public EmailAddressFacade findByPersonIdent(PersonIdent personIdent) {
- if (StringUtils.isNotBlank(personIdent.getEmailAddress()))
- return findByValue(personIdent.getEmailAddress());
- else
- return null;
- }
@Nullable
public EmailAddressFacade findPrimary(Long userId) {