1
1
package com .ctrip .framework .apollo .portal .components .emailbuilder ;
2
2
3
3
4
+ import com .google .common .collect .Lists ;
5
+
4
6
import com .ctrip .framework .apollo .common .constants .ReleaseOperation ;
5
7
import com .ctrip .framework .apollo .common .dto .ReleaseDTO ;
6
8
import com .ctrip .framework .apollo .common .entity .AppNamespace ;
9
+ import com .ctrip .framework .apollo .common .utils .BeanUtils ;
7
10
import com .ctrip .framework .apollo .core .enums .ConfigFileFormat ;
8
11
import com .ctrip .framework .apollo .core .enums .Env ;
9
12
import com .ctrip .framework .apollo .portal .constant .RoleType ;
16
19
import com .ctrip .framework .apollo .portal .service .ReleaseService ;
17
20
import com .ctrip .framework .apollo .portal .service .RolePermissionService ;
18
21
import com .ctrip .framework .apollo .portal .service .ServerConfigService ;
22
+ import com .ctrip .framework .apollo .portal .spi .UserService ;
19
23
import com .ctrip .framework .apollo .portal .util .RoleUtils ;
20
24
21
25
import org .apache .commons .lang .time .FastDateFormat ;
22
26
import org .springframework .beans .factory .annotation .Autowired ;
23
27
import org .springframework .beans .factory .annotation .Value ;
28
+ import org .springframework .util .CollectionUtils ;
24
29
25
30
import java .util .ArrayList ;
31
+ import java .util .Collections ;
32
+ import java .util .HashSet ;
26
33
import java .util .List ;
27
34
import java .util .Set ;
35
+ import java .util .regex .Matcher ;
28
36
29
37
import javax .annotation .PostConstruct ;
30
38
@@ -62,7 +70,6 @@ public abstract class ConfigPublishEmailBuilder {
62
70
private FastDateFormat dateFormat = FastDateFormat .getInstance ("yyyy-MM-dd hh:mm:ss" );
63
71
64
72
65
- private String emailAddressSuffix ;
66
73
private String emailSender ;
67
74
68
75
@ Autowired
@@ -73,10 +80,11 @@ public abstract class ConfigPublishEmailBuilder {
73
80
private ReleaseService releaseService ;
74
81
@ Autowired
75
82
private AppNamespaceService appNamespaceService ;
83
+ @ Autowired
84
+ private UserService userService ;
76
85
77
86
@ PostConstruct
78
87
public void init () {
79
- emailAddressSuffix = serverConfigService .getValue ("email.address.suffix" );
80
88
emailSender = serverConfigService .getValue ("email.sender" );
81
89
}
82
90
@@ -103,15 +111,31 @@ private List<String> recipients(String appId, String namespaceName) {
103
111
Set <UserInfo > releaseRoleUsers =
104
112
rolePermissionService
105
113
.queryUsersWithRole (RoleUtils .buildNamespaceRoleName (appId , namespaceName , RoleType .RELEASE_NAMESPACE ));
114
+ Set <UserInfo > owners = rolePermissionService .queryUsersWithRole (RoleUtils .buildAppMasterRoleName (appId ));
106
115
107
- List <String > recipients = new ArrayList <>(modifyRoleUsers .size () + releaseRoleUsers .size ());
116
+ Set <String > userIds = new HashSet <>(modifyRoleUsers .size () + releaseRoleUsers . size () + owners .size ());
108
117
109
118
for (UserInfo userInfo : modifyRoleUsers ) {
110
- recipients .add (userInfo .getUserId () + emailAddressSuffix );
119
+ userIds .add (userInfo .getUserId ());
111
120
}
112
121
113
122
for (UserInfo userInfo : releaseRoleUsers ) {
114
- recipients .add (userInfo .getUserId () + emailAddressSuffix );
123
+ userIds .add (userInfo .getUserId ());
124
+ }
125
+
126
+ for (UserInfo userInfo : owners ) {
127
+ userIds .add (userInfo .getUserId ());
128
+ }
129
+
130
+ List <UserInfo > userInfos = userService .findByUserIds (Lists .newArrayList (userIds ));
131
+
132
+ if (CollectionUtils .isEmpty (userInfos )){
133
+ return Collections .emptyList ();
134
+ }
135
+
136
+ List <String > recipients = new ArrayList <>(userInfos .size ());
137
+ for (UserInfo userInfo : userInfos ){
138
+ recipients .add (userInfo .getEmail ());
115
139
}
116
140
117
141
return recipients ;
@@ -124,15 +148,15 @@ protected String renderEmailCommonContent(String template, Env env, ReleaseHisto
124
148
}
125
149
126
150
private String renderReleaseBasicInfo (String template , Env env , ReleaseHistoryBO releaseHistory ) {
127
- String renderResult = template .replaceAll (EMAIL_CONTENT_FIELD_APPID , releaseHistory .getAppId ());
128
- renderResult = renderResult .replaceAll (EMAIL_CONTENT_FIELD_ENV , env .toString ());
129
- renderResult = renderResult .replaceAll (EMAIL_CONTENT_FIELD_CLUSTER , releaseHistory .getClusterName ());
130
- renderResult = renderResult .replaceAll (EMAIL_CONTENT_FIELD_NAMESPACE , releaseHistory .getNamespaceName ());
131
- renderResult = renderResult .replaceAll (EMAIL_CONTENT_FIELD_OPERATOR , releaseHistory .getOperator ());
132
- renderResult = renderResult .replaceAll (EMAIL_CONTENT_FIELD_RELEASE_TITLE , releaseHistory .getReleaseTitle ());
151
+ String renderResult = template .replaceAll (EMAIL_CONTENT_FIELD_APPID , Matcher . quoteReplacement ( releaseHistory .getAppId () ));
152
+ renderResult = renderResult .replaceAll (EMAIL_CONTENT_FIELD_ENV , Matcher . quoteReplacement ( env .toString () ));
153
+ renderResult = renderResult .replaceAll (EMAIL_CONTENT_FIELD_CLUSTER , Matcher . quoteReplacement ( releaseHistory .getClusterName () ));
154
+ renderResult = renderResult .replaceAll (EMAIL_CONTENT_FIELD_NAMESPACE , Matcher . quoteReplacement ( releaseHistory .getNamespaceName () ));
155
+ renderResult = renderResult .replaceAll (EMAIL_CONTENT_FIELD_OPERATOR , Matcher . quoteReplacement ( releaseHistory .getOperator () ));
156
+ renderResult = renderResult .replaceAll (EMAIL_CONTENT_FIELD_RELEASE_TITLE , Matcher . quoteReplacement ( releaseHistory .getReleaseTitle () ));
133
157
renderResult =
134
158
renderResult .replaceAll (EMAIL_CONTENT_FIELD_RELEASE_ID , String .valueOf (releaseHistory .getReleaseId ()));
135
- renderResult = renderResult .replaceAll (EMAIL_CONTENT_FIELD_RELEASE_COMMENT , releaseHistory .getReleaseComment ());
159
+ renderResult = renderResult .replaceAll (EMAIL_CONTENT_FIELD_RELEASE_COMMENT , Matcher . quoteReplacement ( releaseHistory .getReleaseComment () ));
136
160
renderResult = renderResult .replaceAll (EMAIL_CONTENT_FIELD_APOLLO_SERVER_ADDRESS , getApolloPortalAddress ());
137
161
return renderResult
138
162
.replaceAll (EMAIL_CONTENT_FIELD_RELEASE_TIME , dateFormat .format (releaseHistory .getReleaseTime ()));
@@ -150,7 +174,8 @@ private String renderDiffContent(String template, Env env, ReleaseHistoryBO rele
150
174
//don't show diff content if namespace's format is file
151
175
if (appNamespace == null ||
152
176
!appNamespace .getFormat ().equals (ConfigFileFormat .Properties .getValue ())) {
153
- return template ;
177
+
178
+ return template .replaceAll (EMAIL_CONTENT_FIELD_DIFF , "请点击链接到Apollo上查看" );
154
179
}
155
180
156
181
ReleaseCompareResult result = getReleaseCompareResult (env , releaseHistory );
@@ -177,7 +202,8 @@ private String renderDiffContent(String template, Env env, ReleaseHistoryBO rele
177
202
changesHtmlBuilder .append ("</tr>" );
178
203
}
179
204
180
- String renderResult = template .replaceAll (EMAIL_CONTENT_FIELD_DIFF , changesHtmlBuilder .toString ());
205
+ String diffContent = Matcher .quoteReplacement (changesHtmlBuilder .toString ());
206
+ String renderResult = template .replaceAll (EMAIL_CONTENT_FIELD_DIFF , diffContent );
181
207
return renderResult .replaceAll (EMAIL_CONTENT_DIFF_HAS_CONTENT_SWITCH , "" );
182
208
}
183
209
@@ -214,5 +240,4 @@ private String cutOffString(String source) {
214
240
return source ;
215
241
}
216
242
217
-
218
243
}
0 commit comments