Skip to content

Commit c66a0df

Browse files
committed
feat: FreeStyle 项目中允许完全自定义消息体
close #204
1 parent c708efc commit c66a0df

File tree

7 files changed

+191
-37
lines changed

7 files changed

+191
-37
lines changed

src/main/java/io/jenkins/plugins/DingTalkNotifierConfig.java

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
@Slf4j
2828
public class DingTalkNotifierConfig extends AbstractDescribableImpl<DingTalkNotifierConfig> {
2929

30+
private boolean raw;
3031
private boolean disabled;
3132
private boolean checked;
3233

@@ -39,6 +40,7 @@ public class DingTalkNotifierConfig extends AbstractDescribableImpl<DingTalkNoti
3940
private String atMobile;
4041

4142
private String content;
43+
private String message;
4244

4345
private Set<String> noticeOccasions;
4446

@@ -71,33 +73,39 @@ public String getContent() {
7173

7274
@DataBoundConstructor
7375
public DingTalkNotifierConfig(
76+
boolean raw,
7477
boolean disabled,
7578
boolean checked,
7679
String robotId,
7780
String robotName,
7881
boolean atAll,
7982
String atMobile,
8083
String content,
84+
String message,
8185
Set<String> noticeOccasions) {
86+
this.raw = raw;
8287
this.disabled = disabled;
8388
this.checked = checked;
8489
this.robotId = robotId;
8590
this.robotName = robotName;
8691
this.atAll = atAll;
8792
this.atMobile = atMobile;
8893
this.content = content;
94+
this.message = message;
8995
this.noticeOccasions = noticeOccasions;
9096
}
9197

9298
public DingTalkNotifierConfig(DingTalkRobotConfig robotConfig) {
9399
this(
100+
false,
94101
false,
95102
false,
96103
robotConfig.getId(),
97104
robotConfig.getName(),
98105
false,
99106
null,
100107
null,
108+
null,
101109
getDefaultNoticeOccasions()
102110
);
103111
}

src/main/java/io/jenkins/plugins/DingTalkRunListener.java

+43-28
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private BuildStatusEnum getBuildStatus(NoticeOccasionEnum noticeOccasion) {
103103
case NOT_BUILT:
104104
return BuildStatusEnum.NOT_BUILT;
105105
default:
106-
return null;
106+
return BuildStatusEnum.UNKNOWN;
107107
}
108108
}
109109

@@ -244,10 +244,21 @@ private void send(Run<?, ?> run, TaskListener listener, NoticeOccasionEnum notic
244244
String projectUrl = job.getAbsoluteUrl();
245245

246246
// 构建信息
247-
BuildStatusEnum statusType = getBuildStatus(noticeOccasion);
248247
String jobName = run.getDisplayName();
249248
String jobUrl = rootPath + run.getUrl();
250249
String duration = run.getDurationString();
250+
BuildStatusEnum statusType = getBuildStatus(noticeOccasion);
251+
252+
// 设置环境变量
253+
envVars.put("EXECUTOR_NAME", executorName == null ? "" : executorName);
254+
envVars.put("EXECUTOR_MOBILE", executorMobile == null ? "" : executorMobile);
255+
envVars.put("PROJECT_NAME", projectName);
256+
envVars.put("PROJECT_URL", projectUrl);
257+
envVars.put("JOB_NAME", jobName);
258+
envVars.put("JOB_URL", jobUrl);
259+
envVars.put("JOB_DURATION", duration);
260+
envVars.put("JOB_STATUS", statusType.getLabel());
261+
251262
List<ButtonModel> btns = Utils.createDefaultBtns(jobUrl);
252263
List<String> result = new ArrayList<>();
253264
List<DingTalkNotifierConfig> notifierConfigs = property.getAvailableNotifierConfigs();
@@ -261,43 +272,47 @@ private void send(Run<?, ?> run, TaskListener listener, NoticeOccasionEnum notic
261272

262273
String robotId = item.getRobotId();
263274
String content = item.getContent();
275+
String message = item.getMessage();
264276
boolean atAll = item.isAtAll();
265277
Set<String> atMobiles = item.resolveAtMobiles(envVars);
266278

267279
if (StringUtils.isNotEmpty(executorMobile)) {
268280
atMobiles.add(executorMobile);
269281
}
270282

271-
String text = BuildJobModel.builder().projectName(projectName).projectUrl(projectUrl)
272-
.jobName(jobName)
273-
.jobUrl(jobUrl)
274-
.statusType(statusType)
275-
.duration(duration)
276-
.executorName(executorName)
277-
.executorMobile(executorMobile)
278-
.content(
279-
envVars.expand(content).replace("\\\\n", "\n")
280-
)
281-
.build()
282-
.toMarkdown();
283-
284-
String statusLabel = statusType == null ? "unknown" : statusType.getLabel();
285-
286-
MessageModel message = MessageModel.builder()
287-
.type(MsgTypeEnum.ACTION_CARD)
288-
.atAll(atAll)
289-
.atMobiles(atMobiles)
290-
.title(
291-
String.format("%s %s", projectName, statusLabel)
292-
)
293-
.text(text)
294-
.btns(btns)
295-
.build();
283+
MessageModel msgModel =
284+
item.isRaw() ? MessageModel.builder()
285+
.type(MsgTypeEnum.MARKDOWN)
286+
.text(
287+
envVars.expand(message).replace("\\\\n", "\n")
288+
).build()
289+
: MessageModel.builder()
290+
.type(MsgTypeEnum.ACTION_CARD)
291+
.atAll(atAll)
292+
.atMobiles(atMobiles)
293+
.title(
294+
String.format("%s %s", projectName, statusType.getLabel())
295+
)
296+
.text(
297+
BuildJobModel.builder().projectName(projectName).projectUrl(projectUrl)
298+
.jobName(jobName)
299+
.jobUrl(jobUrl)
300+
.statusType(statusType)
301+
.duration(duration)
302+
.executorName(executorName)
303+
.executorMobile(executorMobile)
304+
.content(
305+
envVars.expand(content).replace("\\\\n", "\n")
306+
)
307+
.build()
308+
.toMarkdown()
309+
)
310+
.btns(btns).build();
296311

297312
DingTalkUtils.log(listener, "当前机器人信息,%s", Utils.toJson(item));
298313
DingTalkUtils.log(listener, "发送的消息详情,%s", Utils.toJson(message));
299314

300-
String msg = service.send(robotId, message);
315+
String msg = service.send(robotId, msgModel);
301316

302317
if (msg != null) {
303318
result.add(msg);

src/main/resources/io/jenkins/plugins/DingTalkNotifierConfig/config.jelly

+24-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22

33
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
44
xmlns:t="/lib/hudson" xmlns:f="/lib/form">
5+
<st:adjunct includes="io.jenkins.plugins.jquery3"/>
6+
<st:once>
7+
<script type="text/javascript"
8+
src="${rootURL}/plugin/dingding-notifications/scripts/notifierConfig.js"/>
9+
</st:once>
10+
511
<!-- 通知时机列表 -->
612
<j:set var="noticeOccasionTypes" value="${descriptor.getNoticeOccasionTypes()}"/>
713

8-
<f:entry title="${instance.getRobotName()}">
14+
<f:entry title="${instance.getRobotName()}" class="">
915
<f:advanced>
1016
<f:entry title="停用" field="disabled">
1117
<f:checkbox checked="${instance.getDisabled()}"/>
1218
</f:entry>
19+
<f:entry title="禁用内置消息" field="raw" class="notifier-config-raw">
20+
<f:checkbox checked="${instance.getRaw()}"/>
21+
</f:entry>
1322
<f:entry title="通知时机" field="noticeOccasions">
1423
<j:forEach var="noticeOccasionTypeItem"
1524
items="${noticeOccasionTypes}">
@@ -27,13 +36,20 @@
2736
</j:scope>
2837
</j:forEach>
2938
</f:entry>
30-
<f:entry title="通知人" field="at">
31-
<f:checkbox title="atAll" field="atAll"/>
32-
<f:textarea field="atMobile"/>
33-
</f:entry>
34-
<f:entry field="content" title="自定义内容">
35-
<f:textarea/>
36-
</f:entry>
39+
<div class="raw-content">
40+
<f:entry field="message" title="自定义消息">
41+
<f:textarea/>
42+
</f:entry>
43+
</div>
44+
<div class="none-raw-content">
45+
<f:entry title="通知人" field="at">
46+
<f:checkbox title="atAll" field="atAll"/>
47+
<f:textarea field="atMobile"/>
48+
</f:entry>
49+
<f:entry field="content" title="自定义内容">
50+
<f:textarea/>
51+
</f:entry>
52+
</div>
3753
</f:advanced>
3854
</f:entry>
3955
<f:invisibleEntry>
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
自定义的消息内容,需要使用 markdown 格式,支持环境变量。
1+
追加消息内容,需要使用 markdown 格式
2+
<br>
3+
<br>
4+
支持的环境变量:
5+
<ul>
6+
<li><a href = 'https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables'>Jenkins
7+
内置的环境变量</a></li>
8+
<li>
9+
<table>
10+
<thead>
11+
<th width='200px'>变量</th>
12+
<th>描述</th>
13+
</thead>
14+
<tbody>
15+
<tr>
16+
<td>EXECUTOR_NAME</td>
17+
<td>构建人姓名</td>
18+
</tr>
19+
<tr>
20+
<td>EXECUTOR_MOBILE</td>
21+
<td>构建人手机号</td>
22+
</tr>
23+
<tr>
24+
<td>PROJECT_NAME</td>
25+
<td>项目名称</td>
26+
</tr>
27+
<tr>
28+
<td>PROJECT_URL</td>
29+
<td>项目地址</td>
30+
</tr>
31+
<tr>
32+
<td>JOB_NAME</td>
33+
<td>任务名称</td>
34+
</tr>
35+
<tr>
36+
<td>JOB_URL</td>
37+
<td>任务地址</td>
38+
</tr>
39+
<tr>
40+
<td>JOB_DURATION</td>
41+
<td>任务持续时间</td>
42+
</tr>
43+
<tr>
44+
<td>JOB_STATUS</td>
45+
<td>任务状态</td>
46+
</tr>
47+
</tbody>
48+
</table>
49+
</li>
50+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
自定义消息,支持 markdown 格式
2+
<br>
3+
<br>
4+
支持的环境变量:
5+
<ul>
6+
<li><a href = 'https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables'>Jenkins
7+
内置的环境变量</a></li>
8+
<li>
9+
<table>
10+
<thead>
11+
<th width='200px'>变量</th>
12+
<th>描述</th>
13+
</thead>
14+
<tbody>
15+
<tr>
16+
<td>EXECUTOR_NAME</td>
17+
<td>构建人姓名</td>
18+
</tr>
19+
<tr>
20+
<td>EXECUTOR_MOBILE</td>
21+
<td>构建人手机号</td>
22+
</tr>
23+
<tr>
24+
<td>PROJECT_NAME</td>
25+
<td>项目名称</td>
26+
</tr>
27+
<tr>
28+
<td>PROJECT_URL</td>
29+
<td>项目地址</td>
30+
</tr>
31+
<tr>
32+
<td>JOB_NAME</td>
33+
<td>任务名称</td>
34+
</tr>
35+
<tr>
36+
<td>JOB_URL</td>
37+
<td>任务地址</td>
38+
</tr>
39+
<tr>
40+
<td>JOB_DURATION</td>
41+
<td>任务持续时间</td>
42+
</tr>
43+
<tr>
44+
<td>JOB_STATUS</td>
45+
<td>任务状态</td>
46+
</tr>
47+
</tbody>
48+
</table>
49+
</li>
50+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
禁用插件内部封装的消息仅发送下方的 <strong>自定义内容</strong>,相当于使用 markdown 完全自定义消息
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(function($) {
2+
$(function() {
3+
$(document).on('change', '.notifier-config-raw input[name="_.raw"]',
4+
function(event) {
5+
if (event.target.checked) {
6+
$('.raw-content').css('display', '')
7+
$('.none-raw-content').css('display', 'none')
8+
} else {
9+
$('.raw-content').css('display', 'none')
10+
$('.none-raw-content').css('display', '')
11+
}
12+
})
13+
})
14+
15+
})(jQuery3 || jQuery)

0 commit comments

Comments
 (0)