Skip to content

Commit

Permalink
feat: freestyle 中 at 功能支持环境变量
Browse files Browse the repository at this point in the history
close #190
  • Loading branch information
liuweiGL committed Jan 14, 2023
1 parent 346fcee commit 16c0863
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## 开发服务

`IDEA` 右侧 `maven` 控制面板中添加 `hpi:run` 到启动配置:
![启动配置](./docs../assets/contribuitingConfig.png)
![启动配置](./docs/assets/contribuiting-config.png)

## 开发约定

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/io/jenkins/plugins/DingTalkNotifierConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.jenkins.plugins;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
Expand Down Expand Up @@ -44,11 +45,18 @@ public Set<String> getNoticeOccasions() {
return noticeOccasions == null ? getDefaultNoticeOccasions() : noticeOccasions;
}

public Set<String> getAtMobiles() {
public Set<String> resolveAtMobiles(EnvVars envVars) {
if (StringUtils.isEmpty(atMobile)) {
return new HashSet<>(16);
}
return Arrays.stream(StringUtils.split(atMobile.replace("\n", ","), ","))
String realMobile = envVars.expand(atMobile);
return Arrays.stream(
StringUtils.split(
// 支持多行,一行支持多个手机号
realMobile.replace("\n", ","),
","
)
)
.collect(Collectors.toSet());
}

Expand Down Expand Up @@ -95,6 +103,7 @@ public void copy(DingTalkNotifierConfig notifierConfig) {

@Extension
public static class DingTalkNotifierConfigDescriptor extends Descriptor<DingTalkNotifierConfig> {

/**
* 通知时机列表
*
Expand Down
62 changes: 38 additions & 24 deletions src/main/java/io/jenkins/plugins/DingTalkRunListener.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
package io.jenkins.plugins;

import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Cause;
import hudson.model.Cause.RemoteCause;
import hudson.model.Cause.UpstreamCause;
import hudson.model.Cause.UserIdCause;
import hudson.model.Job;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.User;
import hudson.model.Cause.*;
import hudson.model.listeners.RunListener;
import io.jenkins.plugins.enums.BuildStatusEnum;
import io.jenkins.plugins.enums.MsgTypeEnum;
Expand All @@ -31,8 +22,17 @@
import io.jenkins.plugins.service.impl.DingTalkServiceImpl;
import io.jenkins.plugins.tools.Logger;
import io.jenkins.plugins.tools.Utils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import jenkins.model.Jenkins;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;

/**
* 所有项目触发
Expand All @@ -58,7 +58,12 @@ public void onStarted(Run<?, ?> run, TaskListener listener) {
public void onCompleted(Run<?, ?> run, @NonNull TaskListener listener) {
Result result = run.getResult();
NoticeOccasionEnum noticeOccasion = getNoticeOccasion(result);
this.send(run, listener, noticeOccasion);
try {
this.send(run, listener, noticeOccasion);
} catch (Exception e) {
e.printStackTrace();
log(listener, "发送消息时报错: %s", e);
}
}

private NoticeOccasionEnum getNoticeOccasion(Result result) {
Expand Down Expand Up @@ -104,7 +109,7 @@ private void log(TaskListener listener, String formatMsg, Object... args) {
boolean verbose = globalConfig.isVerbose();
if (verbose) {
// Logger.line(listener, LineType.START);
Logger.debug(listener, "钉钉插件" + formatMsg, args);
Logger.debug(listener, "[钉钉插件]" + formatMsg, args);
// Logger.line(listener, LineType.END);
}
}
Expand All @@ -129,7 +134,8 @@ private Map<String, String> getUser(Run<?, ?> run, TaskListener listener) {
}
if (executorName == null) {
log(listener, "未获取到构建人信息,将尝试从构建信息中模糊匹配。");
executorName = run.getCauses().stream().map(Cause::getShortDescription)
executorName = run.getCauses()
.stream().map(Cause::getShortDescription)
.collect(Collectors.joining());
}
} else {
Expand Down Expand Up @@ -179,10 +185,13 @@ private void send(Run<?, ?> run, TaskListener listener, NoticeOccasionEnum notic
return;
}

// 环境变量
EnvVars envVars = getEnvVars(run, listener);

// 执行人信息
Map<String, String> user = getUser(run, listener);
String executorName = user.get("name");
String executorMobile = user.get("mobile");
String executorName = envVars.get("EXECUTOR_NAME", user.get("name"));
String executorMobile = envVars.get("EXECUTOR_MOBILE", user.get("mobile"));

// 项目信息
String projectName = job.getFullDisplayName();
Expand All @@ -197,9 +206,6 @@ private void send(Run<?, ?> run, TaskListener listener, NoticeOccasionEnum notic
List<String> result = new ArrayList<>();
List<DingTalkNotifierConfig> notifierConfigs = property.getCheckedNotifierConfigs();

// 环境变量
EnvVars envVars = getEnvVars(run, listener);

for (DingTalkNotifierConfig item : notifierConfigs) {
boolean skipped = skip(listener, noticeOccasion, item);

Expand All @@ -210,16 +216,22 @@ private void send(Run<?, ?> run, TaskListener listener, NoticeOccasionEnum notic
String robotId = item.getRobotId();
String content = item.getContent();
boolean atAll = item.isAtAll();
Set<String> atMobiles = item.getAtMobiles();
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).replaceAll("\\\\n", "\n"))
.jobUrl(jobUrl)
.statusType(statusType)
.duration(duration)
.executorName(executorName)
.executorMobile(executorMobile)
.content(
envVars.expand(content).replaceAll("\\\\n", "\n")
)
.build()
.toMarkdown();

Expand All @@ -232,7 +244,9 @@ private void send(Run<?, ?> run, TaskListener listener, NoticeOccasionEnum notic
.title(
String.format("%s %s", projectName, statusLabel)
)
.text(text).btns(btns).build();
.text(text)
.btns(btns)
.build();

log(listener, "当前机器人信息,%s", Utils.toJson(item));
log(listener, "发送的消息详情,%s", Utils.toJson(message));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/jenkins/plugins/model/MessageModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public At getAt() {
at.setAtMobiles(
atMobiles.stream()
.map(String::trim)
.filter(item -> !StringUtils.isEmpty(item))
.filter(StringUtils::isNotEmpty)
.collect(Collectors.toList()));
}
at.setIsAtAll(atAll);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
atAll是否 @ 全部。
atAll: 是否 @ 全部。
<br>
输入框钉钉里面配置的手机号码,用于 @ 对应的人多个值使用逗号或者换行分割。
输入框: 钉钉里面配置的手机号码,用于 @ 对应的人多个值使用逗号或者换行分割,支持环境变量
Original file line number Diff line number Diff line change
@@ -1 +1 @@
自定义的消息内容,支持环境变量,需要使用 markdown 格式。
自定义的消息内容,需要使用 markdown 格式,支持环境变量

0 comments on commit 16c0863

Please sign in to comment.