The Unified and Autonomous Development Platform
Add more mail checking log
Robin Shen6af3866cbef8
diff --git a/server-core/src/main/java/io/onedev/server/mail/DefaultMailManager.java b/server-core/src/main/java/io/onedev/server/mail/DefaultMailManager.javaindex 7c82ebbe9b..72db572e1e 100644--- a/server-core/src/main/java/io/onedev/server/mail/DefaultMailManager.java+++ b/server-core/src/main/java/io/onedev/server/mail/DefaultMailManager.java@@ -372,187 +372,200 @@ public class DefaultMailManager implements MailManager {@SuppressWarnings("unchecked")@Transactionalprotected void onMessage(MailSendSetting sendSetting, MailCheckSetting checkSetting, Message message) throws MessagingException, IOException {- String[] toHeader = message.getHeader("To");- String[] fromHeader = message.getHeader("From");- String[] ccHeader = message.getHeader("Cc");- if (toHeader != null && toHeader.length != 0) {- if (fromHeader == null || fromHeader.length == 0)- throw new ExplicitException("Invalid email message: no from address found");-- InternetAddress from = InternetAddress.parse(fromHeader[0], true)[0];+ String[] toHeader = message.getHeader("To");+ String[] fromHeader = message.getHeader("From");+ String[] ccHeader = message.getHeader("Cc");+ if (toHeader != null && toHeader.length != 0) {+ if (fromHeader == null || fromHeader.length == 0)+ throw new ExplicitException("Invalid email message: no from address found");++ InternetAddress from = InternetAddress.parse(fromHeader[0], true)[0];- EmailAddress fromAddressEntity = emailAddressManager.findByValue(from.getAddress());- if (fromAddressEntity != null && !fromAddressEntity.isVerified()) {- logger.error("Another account uses email address '{}' but not verified", from.getAddress());- } else {- User user = fromAddressEntity != null?fromAddressEntity.getOwner():null;- SenderAuthorization authorization = null;- String designatedProject = null;- ServiceDeskSetting serviceDeskSetting = settingManager.getServiceDeskSetting();- if (serviceDeskSetting != null) {- authorization = serviceDeskSetting.getSenderAuthorization(from.getAddress());- designatedProject = serviceDeskSetting.getDesignatedProject(from.getAddress());- }- ParsedEmailAddress parsedSystemAddress = ParsedEmailAddress.parse(checkSetting.getCheckAddress());-- Collection<Issue> issues = new ArrayList<>();- Collection<PullRequest> pullRequests = new ArrayList<>();- Collection<InternetAddress> involved = new ArrayList<>();+ EmailAddress fromAddressEntity = emailAddressManager.findByValue(from.getAddress());+ if (fromAddressEntity != null && !fromAddressEntity.isVerified()) {+ logger.error("Another account uses email address '{}' but not verified", from.getAddress());+ } else {+ User user = fromAddressEntity != null?fromAddressEntity.getOwner():null;+ SenderAuthorization authorization = null;+ String designatedProject = null;+ ServiceDeskSetting serviceDeskSetting = settingManager.getServiceDeskSetting();+ if (serviceDeskSetting != null) {+ authorization = serviceDeskSetting.getSenderAuthorization(from.getAddress());+ designatedProject = serviceDeskSetting.getDesignatedProject(from.getAddress());+ }+ ParsedEmailAddress parsedSystemAddress = ParsedEmailAddress.parse(checkSetting.getCheckAddress());++ Collection<Issue> issues = new ArrayList<>();+ Collection<PullRequest> pullRequests = new ArrayList<>();+ Collection<InternetAddress> involved = new ArrayList<>();- List<InternetAddress> receivers = new ArrayList<>();- receivers.addAll(Arrays.asList(InternetAddress.parse(toHeader[0], true)));-- if (ccHeader != null && ccHeader.length != 0)- receivers.addAll(Arrays.asList(InternetAddress.parse(ccHeader[0], true)));-- List<String> receiverEmailAddresses =- receivers.stream().map(it->it.getAddress()).collect(Collectors.toList());+ List<InternetAddress> receivers = new ArrayList<>();+ receivers.addAll(Arrays.asList(InternetAddress.parse(toHeader[0], true)));++ if (ccHeader != null && ccHeader.length != 0)+ receivers.addAll(Arrays.asList(InternetAddress.parse(ccHeader[0], true)));++ List<String> receiverEmailAddresses =+ receivers.stream().map(it->it.getAddress()).collect(Collectors.toList());++ for (InternetAddress receiver: receivers) {+ logger.trace("Processing on behalf of receiver '" + receiver.getAddress() + "'");- for (InternetAddress receiver: receivers) {- ParsedEmailAddress parsedReceiverAddress = ParsedEmailAddress.parse(receiver.getAddress());- if (parsedReceiverAddress.toString().equals(parsedSystemAddress.toString())) {- if (serviceDeskSetting != null) {- if (designatedProject == null)- throw new ExplicitException("No project designated for sender: " + from.getAddress());- Project project = projectManager.findByPath(designatedProject);- if (project == null) {- String errorMessage = String.format(- "Sender project does not exist (sender: %s, project: %s)",- from.getAddress(), designatedProject);- throw new ExplicitException(errorMessage);- }- checkPermission(from, project, new AccessProject(), user, authorization);- issues.add(openIssue(message, project, from, user, authorization));- } else {- throw new ExplicitException("Unable to create issue from email as service desk is not enabled");+ ParsedEmailAddress parsedReceiverAddress = ParsedEmailAddress.parse(receiver.getAddress());+ if (parsedReceiverAddress.toString().equals(parsedSystemAddress.toString())) {+ logger.trace("Message is targeting system address");+ if (serviceDeskSetting != null) {+ if (designatedProject == null)+ throw new ExplicitException("No project designated for sender: " + from.getAddress());+ Project project = projectManager.findByPath(designatedProject);+ if (project == null) {+ String errorMessage = String.format(+ "Sender project does not exist (sender: %s, project: %s)",+ from.getAddress(), designatedProject);+ throw new ExplicitException(errorMessage);}- } else if (parsedReceiverAddress.getDomain().equals(parsedSystemAddress.getDomain())- && parsedReceiverAddress.getName().startsWith(parsedSystemAddress.getName() + "+")) {- String subAddress = parsedReceiverAddress.getName().substring(parsedSystemAddress.getName().length()+1);- if (subAddress.equals(MailManager.TEST_SUB_ADDRESS)) {- continue;- } else if (subAddress.contains("~")) {- Long entityId;- try {- entityId = Long.parseLong(StringUtils.substringAfter(subAddress, "~"));- } catch (NumberFormatException e) {- throw new ExplicitException("Invalid id specified in receipient address: " + parsedReceiverAddress);- }- if (subAddress.contains("issue")) {- Issue issue = issueManager.get(entityId);- if (issue == null)- throw new ExplicitException("Non-existent issue specified in receipient address: " + parsedReceiverAddress);- if (subAddress.contains("unsubscribe")) {- if (user != null) {- IssueWatch watch = issueWatchManager.find(issue, user);- if (watch != null) {- watch.setWatching(false);- issueWatchManager.save(watch);- String subject = "Unsubscribed successfully from issue " + issue.getFQN();- String body = "You will no longer receive notifications of issue " + issue.getFQN() + " unless mentioned. "- + "However if you subscribed to certain issue queries, you may still get notifications of newly "- + "created issues matching those queries. In this case, you will need to login to your account "- + "and unsubscribe those queries.";- sendMailAsync(Lists.newArrayList(from.getAddress()), Lists.newArrayList(), Lists.newArrayList(),- subject, body, body, null, getMessageId(message));- }- }- } else {- checkPermission(from, issue.getProject(), new AccessProject(), user, authorization);- addComment(sendSetting, issue, message, from, receiverEmailAddresses, user, authorization);- issues.add(issue);- }- } else if (subAddress.contains("pullrequest")) {- PullRequest pullRequest = pullRequestManager.get(entityId);- if (pullRequest == null)- throw new ExplicitException("Non-existent pull request specified in receipient address: " + parsedReceiverAddress);- if (subAddress.contains("unsubscribe")) {- if (user != null) {- PullRequestWatch watch = pullRequestWatchManager.find(pullRequest, user);- if (watch != null) {- watch.setWatching(false);- pullRequestWatchManager.save(watch);- String subject = "Unsubscribed successfully from pull request " + pullRequest.getFQN();- String body = "You will no longer receive notifications of pull request " + pullRequest.getFQN()- + " unless mentioned. However if you subscribed to certain pull request queries, you may still "- + "get notifications of newly submitted pull request matching those queries. In this case, you "- + "will need to login to your account and unsubscribe those queries.";- sendMailAsync(Lists.newArrayList(from.getAddress()), Lists.newArrayList(), Lists.newArrayList(),- subject, body, body, null, getMessageId(message));- }+ checkPermission(from, project, new AccessProject(), user, authorization);+ issues.add(openIssue(message, project, from, user, authorization));+ } else {+ throw new ExplicitException("Unable to create issue from email as service desk is not enabled");+ }+ } else if (parsedReceiverAddress.getDomain().equals(parsedSystemAddress.getDomain())+ && parsedReceiverAddress.getName().startsWith(parsedSystemAddress.getName() + "+")) {+ String subAddress = parsedReceiverAddress.getName().substring(parsedSystemAddress.getName().length()+1);+ if (subAddress.equals(MailManager.TEST_SUB_ADDRESS)) {+ continue;+ } else if (subAddress.contains("~")) {+ logger.trace("Message is targeting a sub address");++ Long entityId;+ try {+ entityId = Long.parseLong(StringUtils.substringAfter(subAddress, "~"));+ } catch (NumberFormatException e) {+ throw new ExplicitException("Invalid id specified in receipient address: " + parsedReceiverAddress);+ }+ if (subAddress.contains("issue")) {+ logger.trace("Message is an issue reply");++ Issue issue = issueManager.get(entityId);+ if (issue == null)+ throw new ExplicitException("Non-existent issue specified in receipient address: " + parsedReceiverAddress);+ if (subAddress.contains("unsubscribe")) {+ if (user != null) {+ IssueWatch watch = issueWatchManager.find(issue, user);+ if (watch != null) {+ watch.setWatching(false);+ issueWatchManager.save(watch);+ String subject = "Unsubscribed successfully from issue " + issue.getFQN();+ String body = "You will no longer receive notifications of issue " + issue.getFQN() + " unless mentioned. "+ + "However if you subscribed to certain issue queries, you may still get notifications of newly "+ + "created issues matching those queries. In this case, you will need to login to your account "+ + "and unsubscribe those queries.";+ sendMailAsync(Lists.newArrayList(from.getAddress()), Lists.newArrayList(), Lists.newArrayList(),+ subject, body, body, null, getMessageId(message));}- } else {- checkPermission(from, pullRequest.getTargetProject(), new ReadCode(), user, authorization);- addComment(sendSetting, pullRequest, message, from, receiverEmailAddresses, user, authorization);- pullRequests.add(pullRequest);}} else {- throw new ExplicitException("Invalid receipient address: " + parsedReceiverAddress);+ checkPermission(from, issue.getProject(), new AccessProject(), user, authorization);+ addComment(sendSetting, issue, message, from, receiverEmailAddresses, user, authorization);+ issues.add(issue);}- } else {- Project project = projectManager.findByServiceDeskName(subAddress);- if (project == null)- project = projectManager.findByPath(subAddress);+ } else if (subAddress.contains("pullrequest")) {+ logger.trace("Message is a pull request reply");- if (project == null)- throw new ExplicitException("Non-existent project specified in receipient address: " + parsedReceiverAddress);- if (serviceDeskSetting != null) {- checkPermission(from, project, new AccessProject(), user, authorization);- logger.debug("Creating issue via email (project: {})...", project.getPath());- issues.add(openIssue(message, project, from, user, authorization));+ PullRequest pullRequest = pullRequestManager.get(entityId);+ if (pullRequest == null)+ throw new ExplicitException("Non-existent pull request specified in receipient address: " + parsedReceiverAddress);+ if (subAddress.contains("unsubscribe")) {+ if (user != null) {+ PullRequestWatch watch = pullRequestWatchManager.find(pullRequest, user);+ if (watch != null) {+ watch.setWatching(false);+ pullRequestWatchManager.save(watch);+ String subject = "Unsubscribed successfully from pull request " + pullRequest.getFQN();+ String body = "You will no longer receive notifications of pull request " + pullRequest.getFQN()+ + " unless mentioned. However if you subscribed to certain pull request queries, you may still "+ + "get notifications of newly submitted pull request matching those queries. In this case, you "+ + "will need to login to your account and unsubscribe those queries.";+ sendMailAsync(Lists.newArrayList(from.getAddress()), Lists.newArrayList(), Lists.newArrayList(),+ subject, body, body, null, getMessageId(message));+ }+ }} else {- throw new ExplicitException("Unable to create issue from email as service desk is not enabled");+ checkPermission(from, pullRequest.getTargetProject(), new ReadCode(), user, authorization);+ addComment(sendSetting, pullRequest, message, from, receiverEmailAddresses, user, authorization);+ pullRequests.add(pullRequest);}+ } else {+ throw new ExplicitException("Invalid receipient address: " + parsedReceiverAddress);}} else {- involved.add(receiver);- }- }-- for (Issue issue: issues) {- for (InternetAddress each: involved) {- EmailAddress emailAddressEntity = emailAddressManager.findByValue(each.getAddress());- if (emailAddressEntity != null && !emailAddressEntity.isVerified()) {- logger.error("Another account uses email address '{}' but not verified", each.getAddress());+ logger.trace("Message is targeting service desk '" + subAddress + "'...");++ Project project = projectManager.findByServiceDeskName(subAddress);+ if (project == null)+ project = projectManager.findByPath(subAddress);++ if (project == null)+ throw new ExplicitException("Non-existent project specified in receipient address: " + parsedReceiverAddress);+ if (serviceDeskSetting != null) {+ checkPermission(from, project, new AccessProject(), user, authorization);+ logger.debug("Creating issue via email (project: {})...", project.getPath());+ issues.add(openIssue(message, project, from, user, authorization));} else {- if (serviceDeskSetting != null)- authorization = serviceDeskSetting.getSenderAuthorization(each.getAddress());- user = emailAddressEntity != null?emailAddressEntity.getOwner():null;- try {- checkPermission(each, issue.getProject(), new AccessProject(), user, authorization);- if (user == null)- user = createUser(each, issue.getProject(), authorization.getAuthorizedRole());- issueWatchManager.watch(issue, user, true);- if (issue.isConfidential())- issueAuthorizationManager.authorize(issue, user);- } catch (UnauthorizedException e) {- logger.error("Error adding receipient to watch list", e);- }+ throw new ExplicitException("Unable to create issue from email as service desk is not enabled");}}+ } else {+ involved.add(receiver);}- for (PullRequest pullRequest: pullRequests) {- for (InternetAddress each: involved) {- EmailAddress emailAddressEntity = emailAddressManager.findByValue(each.getAddress());- if (emailAddressEntity != null && !emailAddressEntity.isVerified()) {- logger.error("Another account uses email address '{}' but not verified", each.getAddress());- } else {- user = emailAddressEntity != null?emailAddressEntity.getOwner():null;- if (serviceDeskSetting != null)- authorization = serviceDeskSetting.getSenderAuthorization(each.getAddress());- try {- checkPermission(each, pullRequest.getProject(), new ReadCode(), user, authorization);- if (user == null)- user = createUser(each, pullRequest.getProject(), authorization.getAuthorizedRole());- pullRequestWatchManager.watch(pullRequest, user, true);- } catch (UnauthorizedException e) {- logger.error("Error adding receipient to watch list", e);- }- }+ }++ for (Issue issue: issues) {+ for (InternetAddress each: involved) {+ EmailAddress emailAddressEntity = emailAddressManager.findByValue(each.getAddress());+ if (emailAddressEntity != null && !emailAddressEntity.isVerified()) {+ logger.error("Another account uses email address '{}' but not verified", each.getAddress());+ } else {+ if (serviceDeskSetting != null)+ authorization = serviceDeskSetting.getSenderAuthorization(each.getAddress());+ user = emailAddressEntity != null?emailAddressEntity.getOwner():null;+ try {+ checkPermission(each, issue.getProject(), new AccessProject(), user, authorization);+ if (user == null)+ user = createUser(each, issue.getProject(), authorization.getAuthorizedRole());+ issueWatchManager.watch(issue, user, true);+ if (issue.isConfidential())+ issueAuthorizationManager.authorize(issue, user);+ } catch (UnauthorizedException e) {+ logger.error("Error adding receipient to watch list", e);+ }}}}+ for (PullRequest pullRequest: pullRequests) {+ for (InternetAddress each: involved) {+ EmailAddress emailAddressEntity = emailAddressManager.findByValue(each.getAddress());+ if (emailAddressEntity != null && !emailAddressEntity.isVerified()) {+ logger.error("Another account uses email address '{}' but not verified", each.getAddress());+ } else {+ user = emailAddressEntity != null?emailAddressEntity.getOwner():null;+ if (serviceDeskSetting != null)+ authorization = serviceDeskSetting.getSenderAuthorization(each.getAddress());+ try {+ checkPermission(each, pullRequest.getProject(), new ReadCode(), user, authorization);+ if (user == null)+ user = createUser(each, pullRequest.getProject(), authorization.getAuthorizedRole());+ pullRequestWatchManager.watch(pullRequest, user, true);+ } catch (UnauthorizedException e) {+ logger.error("Error adding receipient to watch list", e);+ }+ }+ }+ }}+ } else {+ logger.trace("Ignore message as 'To' header is not available");+ }}private void removeNodesAfter(Node node) {@@ -632,6 +645,7 @@ public class DefaultMailManager implements MailManager {comment.setIssue(issue);if (user == null)user = createUser(author, issue.getProject(), authorization.getAuthorizedRole());+ logger.trace("Creating issue comment on behalf of user '" + user.getName() + "'");comment.setUser(user);String content = stripQuotation(sendSetting, readText(issue.getProject(), issue.getUUID(), message));if (content != null) {@@ -647,6 +661,7 @@ public class DefaultMailManager implements MailManager {comment.setRequest(pullRequest);if (user == null)user = createUser(author, pullRequest.getProject(), authorization.getAuthorizedRole());+ logger.trace("Creating pull request comment on behalf of user '" + user.getName() + "'");comment.setUser(user);String content = stripQuotation(sendSetting, readText(pullRequest.getProject(), pullRequest.getUUID(), message));if (content != null) {diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/Office365Setting.java b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/Office365Setting.javaindex fe99e835eb..e2bc160a07 100644--- a/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/Office365Setting.java+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/mailsetting/Office365Setting.java@@ -25,10 +25,10 @@ public class Office365Setting extends MailSetting {private static final long serialVersionUID = 1L;- private String tenantId;-private String clientId;+ private String tenantId;+private String clientSecret;private String userPrincipalName;@@ -39,16 +39,6 @@ public class Office365Setting extends MailSetting {private InboxPollSetting inboxPollSetting;- @Editable(order=50, name="Directory (tenant) ID", description="Specify ID of the Azure AD this OneDev instance is registered in")- @NotEmpty- public String getTenantId() {- return tenantId;- }-- public void setTenantId(String tenantId) {- this.tenantId = tenantId;- }-@Editable(order=100, name="Application (client) ID", description="Specify application (tenant) ID of this OneDev instance registered in Azure AD")@NotEmptypublic String getClientId() {@@ -59,6 +49,16 @@ public class Office365Setting extends MailSetting {this.clientId = clientId;}+ @Editable(order=150, name="Directory (tenant) ID", description="Specify ID of the Azure AD this OneDev instance is registered in")+ @NotEmpty+ public String getTenantId() {+ return tenantId;+ }++ public void setTenantId(String tenantId) {+ this.tenantId = tenantId;+ }+@Editable(order=200, description="Client secret of this OneDev instance registered in Azure AD")@Password@NotEmpty