Skip to content

Commit

Permalink
feat: FreeStyle 项目中允许完全自定义消息体
Browse files Browse the repository at this point in the history
close #204
  • Loading branch information
liuweiGL committed Mar 16, 2023
1 parent c708efc commit c66a0df
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 37 deletions.
8 changes: 8 additions & 0 deletions src/main/java/io/jenkins/plugins/DingTalkNotifierConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
@Slf4j
public class DingTalkNotifierConfig extends AbstractDescribableImpl<DingTalkNotifierConfig> {

private boolean raw;
private boolean disabled;
private boolean checked;

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

private String content;
private String message;

private Set<String> noticeOccasions;

Expand Down Expand Up @@ -71,33 +73,39 @@ public String getContent() {

@DataBoundConstructor
public DingTalkNotifierConfig(
boolean raw,
boolean disabled,
boolean checked,
String robotId,
String robotName,
boolean atAll,
String atMobile,
String content,
String message,
Set<String> noticeOccasions) {
this.raw = raw;
this.disabled = disabled;
this.checked = checked;
this.robotId = robotId;
this.robotName = robotName;
this.atAll = atAll;
this.atMobile = atMobile;
this.content = content;
this.message = message;
this.noticeOccasions = noticeOccasions;
}

public DingTalkNotifierConfig(DingTalkRobotConfig robotConfig) {
this(
false,
false,
false,
robotConfig.getId(),
robotConfig.getName(),
false,
null,
null,
null,
getDefaultNoticeOccasions()
);
}
Expand Down
71 changes: 43 additions & 28 deletions src/main/java/io/jenkins/plugins/DingTalkRunListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private BuildStatusEnum getBuildStatus(NoticeOccasionEnum noticeOccasion) {
case NOT_BUILT:
return BuildStatusEnum.NOT_BUILT;
default:
return null;
return BuildStatusEnum.UNKNOWN;
}
}

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

// 构建信息
BuildStatusEnum statusType = getBuildStatus(noticeOccasion);
String jobName = run.getDisplayName();
String jobUrl = rootPath + run.getUrl();
String duration = run.getDurationString();
BuildStatusEnum statusType = getBuildStatus(noticeOccasion);

// 设置环境变量
envVars.put("EXECUTOR_NAME", executorName == null ? "" : executorName);
envVars.put("EXECUTOR_MOBILE", executorMobile == null ? "" : executorMobile);
envVars.put("PROJECT_NAME", projectName);
envVars.put("PROJECT_URL", projectUrl);
envVars.put("JOB_NAME", jobName);
envVars.put("JOB_URL", jobUrl);
envVars.put("JOB_DURATION", duration);
envVars.put("JOB_STATUS", statusType.getLabel());

List<ButtonModel> btns = Utils.createDefaultBtns(jobUrl);
List<String> result = new ArrayList<>();
List<DingTalkNotifierConfig> notifierConfigs = property.getAvailableNotifierConfigs();
Expand All @@ -261,43 +272,47 @@ private void send(Run<?, ?> run, TaskListener listener, NoticeOccasionEnum notic

String robotId = item.getRobotId();
String content = item.getContent();
String message = item.getMessage();
boolean atAll = item.isAtAll();
Set<String> atMobiles = item.resolveAtMobiles(envVars);

if (StringUtils.isNotEmpty(executorMobile)) {
atMobiles.add(executorMobile);
}

String text = BuildJobModel.builder().projectName(projectName).projectUrl(projectUrl)
.jobName(jobName)
.jobUrl(jobUrl)
.statusType(statusType)
.duration(duration)
.executorName(executorName)
.executorMobile(executorMobile)
.content(
envVars.expand(content).replace("\\\\n", "\n")
)
.build()
.toMarkdown();

String statusLabel = statusType == null ? "unknown" : statusType.getLabel();

MessageModel message = MessageModel.builder()
.type(MsgTypeEnum.ACTION_CARD)
.atAll(atAll)
.atMobiles(atMobiles)
.title(
String.format("%s %s", projectName, statusLabel)
)
.text(text)
.btns(btns)
.build();
MessageModel msgModel =
item.isRaw() ? MessageModel.builder()
.type(MsgTypeEnum.MARKDOWN)
.text(
envVars.expand(message).replace("\\\\n", "\n")
).build()
: MessageModel.builder()
.type(MsgTypeEnum.ACTION_CARD)
.atAll(atAll)
.atMobiles(atMobiles)
.title(
String.format("%s %s", projectName, statusType.getLabel())
)
.text(
BuildJobModel.builder().projectName(projectName).projectUrl(projectUrl)
.jobName(jobName)
.jobUrl(jobUrl)
.statusType(statusType)
.duration(duration)
.executorName(executorName)
.executorMobile(executorMobile)
.content(
envVars.expand(content).replace("\\\\n", "\n")
)
.build()
.toMarkdown()
)
.btns(btns).build();

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

String msg = service.send(robotId, message);
String msg = service.send(robotId, msgModel);

if (msg != null) {
result.add(msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<st:adjunct includes="io.jenkins.plugins.jquery3"/>
<st:once>
<script type="text/javascript"
src="${rootURL}/plugin/dingding-notifications/scripts/notifierConfig.js"/>
</st:once>

<!-- 通知时机列表 -->
<j:set var="noticeOccasionTypes" value="${descriptor.getNoticeOccasionTypes()}"/>

<f:entry title="${instance.getRobotName()}">
<f:entry title="${instance.getRobotName()}" class="">
<f:advanced>
<f:entry title="停用" field="disabled">
<f:checkbox checked="${instance.getDisabled()}"/>
</f:entry>
<f:entry title="禁用内置消息" field="raw" class="notifier-config-raw">
<f:checkbox checked="${instance.getRaw()}"/>
</f:entry>
<f:entry title="通知时机" field="noticeOccasions">
<j:forEach var="noticeOccasionTypeItem"
items="${noticeOccasionTypes}">
Expand All @@ -27,13 +36,20 @@
</j:scope>
</j:forEach>
</f:entry>
<f:entry title="通知人" field="at">
<f:checkbox title="atAll" field="atAll"/>
<f:textarea field="atMobile"/>
</f:entry>
<f:entry field="content" title="自定义内容">
<f:textarea/>
</f:entry>
<div class="raw-content">
<f:entry field="message" title="自定义消息">
<f:textarea/>
</f:entry>
</div>
<div class="none-raw-content">
<f:entry title="通知人" field="at">
<f:checkbox title="atAll" field="atAll"/>
<f:textarea field="atMobile"/>
</f:entry>
<f:entry field="content" title="自定义内容">
<f:textarea/>
</f:entry>
</div>
</f:advanced>
</f:entry>
<f:invisibleEntry>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
自定义的消息内容,需要使用 markdown 格式,支持环境变量。
追加消息内容,需要使用 markdown 格式
<br>
<br>
支持的环境变量:
<ul>
<li><a href = 'https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables'>Jenkins
内置的环境变量</a></li>
<li>
<table>
<thead>
<th width='200px'>变量</th>
<th>描述</th>
</thead>
<tbody>
<tr>
<td>EXECUTOR_NAME</td>
<td>构建人姓名</td>
</tr>
<tr>
<td>EXECUTOR_MOBILE</td>
<td>构建人手机号</td>
</tr>
<tr>
<td>PROJECT_NAME</td>
<td>项目名称</td>
</tr>
<tr>
<td>PROJECT_URL</td>
<td>项目地址</td>
</tr>
<tr>
<td>JOB_NAME</td>
<td>任务名称</td>
</tr>
<tr>
<td>JOB_URL</td>
<td>任务地址</td>
</tr>
<tr>
<td>JOB_DURATION</td>
<td>任务持续时间</td>
</tr>
<tr>
<td>JOB_STATUS</td>
<td>任务状态</td>
</tr>
</tbody>
</table>
</li>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
自定义消息,支持 markdown 格式
<br>
<br>
支持的环境变量:
<ul>
<li><a href = 'https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables'>Jenkins
内置的环境变量</a></li>
<li>
<table>
<thead>
<th width='200px'>变量</th>
<th>描述</th>
</thead>
<tbody>
<tr>
<td>EXECUTOR_NAME</td>
<td>构建人姓名</td>
</tr>
<tr>
<td>EXECUTOR_MOBILE</td>
<td>构建人手机号</td>
</tr>
<tr>
<td>PROJECT_NAME</td>
<td>项目名称</td>
</tr>
<tr>
<td>PROJECT_URL</td>
<td>项目地址</td>
</tr>
<tr>
<td>JOB_NAME</td>
<td>任务名称</td>
</tr>
<tr>
<td>JOB_URL</td>
<td>任务地址</td>
</tr>
<tr>
<td>JOB_DURATION</td>
<td>任务持续时间</td>
</tr>
<tr>
<td>JOB_STATUS</td>
<td>任务状态</td>
</tr>
</tbody>
</table>
</li>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
禁用插件内部封装的消息仅发送下方的 <strong>自定义内容</strong>,相当于使用 markdown 完全自定义消息
15 changes: 15 additions & 0 deletions src/main/webapp/scripts/notifierConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(function($) {
$(function() {
$(document).on('change', '.notifier-config-raw input[name="_.raw"]',
function(event) {
if (event.target.checked) {
$('.raw-content').css('display', '')
$('.none-raw-content').css('display', 'none')
} else {
$('.raw-content').css('display', 'none')
$('.none-raw-content').css('display', '')
}
})
})

})(jQuery3 || jQuery)

0 comments on commit c66a0df

Please sign in to comment.