You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IssueChangeNotification.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.issue.notification;
  21. import com.google.common.base.Strings;
  22. import java.io.Serializable;
  23. import java.util.Map;
  24. import javax.annotation.CheckForNull;
  25. import javax.annotation.Nullable;
  26. import org.sonar.api.notifications.Notification;
  27. import org.sonar.core.issue.DefaultIssue;
  28. import org.sonar.core.issue.FieldDiffs;
  29. import org.sonar.db.component.ComponentDto;
  30. import static org.sonar.server.issue.notification.AbstractNewIssuesEmailTemplate.FIELD_BRANCH;
  31. import static org.sonar.server.issue.notification.AbstractNewIssuesEmailTemplate.FIELD_PROJECT_KEY;
  32. import static org.sonar.server.issue.notification.AbstractNewIssuesEmailTemplate.FIELD_PROJECT_NAME;
  33. import static org.sonar.server.issue.notification.AbstractNewIssuesEmailTemplate.FIELD_PULL_REQUEST;
  34. public class IssueChangeNotification extends Notification {
  35. public static final String TYPE = "issue-changes";
  36. public IssueChangeNotification() {
  37. super(TYPE);
  38. }
  39. public IssueChangeNotification setIssue(DefaultIssue issue) {
  40. setFieldValue("key", issue.key());
  41. setFieldValue("assignee", issue.assignee());
  42. setFieldValue("message", issue.message());
  43. FieldDiffs currentChange = issue.currentChange();
  44. if (currentChange != null) {
  45. for (Map.Entry<String, FieldDiffs.Diff> entry : currentChange.diffs().entrySet()) {
  46. String type = entry.getKey();
  47. FieldDiffs.Diff diff = entry.getValue();
  48. setFieldValue("old." + type, neverEmptySerializableToString(diff.oldValue()));
  49. setFieldValue("new." + type, neverEmptySerializableToString(diff.newValue()));
  50. }
  51. }
  52. return this;
  53. }
  54. public IssueChangeNotification setProject(ComponentDto project) {
  55. return setProject(project.getKey(), project.name(), project.getBranch(), project.getPullRequest());
  56. }
  57. public IssueChangeNotification setProject(String projectKey, String projectName, @Nullable String branch, @Nullable String pullRequest) {
  58. setFieldValue(FIELD_PROJECT_NAME, projectName);
  59. setFieldValue(FIELD_PROJECT_KEY, projectKey);
  60. if (branch != null) {
  61. setFieldValue(FIELD_BRANCH, branch);
  62. }
  63. if (pullRequest != null) {
  64. setFieldValue(FIELD_PULL_REQUEST, pullRequest);
  65. }
  66. return this;
  67. }
  68. public IssueChangeNotification setComponent(ComponentDto component) {
  69. return setComponent(component.getKey(), component.longName());
  70. }
  71. public IssueChangeNotification setComponent(String componentKey, String componentName) {
  72. setFieldValue("componentName", componentName);
  73. setFieldValue("componentKey", componentKey);
  74. return this;
  75. }
  76. public IssueChangeNotification setChangeAuthorLogin(@Nullable String s) {
  77. if (s != null) {
  78. setFieldValue("changeAuthor", s);
  79. }
  80. return this;
  81. }
  82. public IssueChangeNotification setRuleName(@Nullable String s) {
  83. if (s != null) {
  84. setFieldValue("ruleName", s);
  85. }
  86. return this;
  87. }
  88. public IssueChangeNotification setComment(@Nullable String s) {
  89. if (s != null) {
  90. setFieldValue("comment", s);
  91. }
  92. return this;
  93. }
  94. @CheckForNull
  95. private static String neverEmptySerializableToString(@Nullable Serializable s) {
  96. return s != null ? Strings.emptyToNull(s.toString()) : null;
  97. }
  98. }