|
5 | 5 | import hudson.model.AbstractDescribableImpl;
|
6 | 6 | import hudson.model.Descriptor;
|
7 | 7 | import io.jenkins.plugins.enums.NoticeOccasionEnum;
|
| 8 | +import java.lang.reflect.InvocationTargetException; |
8 | 9 | import java.util.Arrays;
|
9 | 10 | import java.util.HashSet;
|
10 | 11 | import java.util.Set;
|
11 | 12 | import java.util.stream.Collectors;
|
12 | 13 | import lombok.Getter;
|
13 | 14 | import lombok.Setter;
|
14 | 15 | import lombok.ToString;
|
| 16 | +import lombok.extern.slf4j.Slf4j; |
| 17 | +import org.apache.commons.beanutils.BeanUtils; |
15 | 18 | import org.apache.commons.lang.StringUtils;
|
16 | 19 | import org.kohsuke.stapler.DataBoundConstructor;
|
17 | 20 |
|
|
21 | 24 | @Getter
|
22 | 25 | @Setter
|
23 | 26 | @ToString
|
| 27 | +@Slf4j |
24 | 28 | public class DingTalkNotifierConfig extends AbstractDescribableImpl<DingTalkNotifierConfig> {
|
25 | 29 |
|
26 |
| - private boolean checked; |
27 |
| - |
28 |
| - private String robotId; |
29 |
| - |
30 |
| - private String robotName; |
31 |
| - |
32 |
| - private boolean atAll; |
33 |
| - |
34 |
| - private String atMobile; |
35 |
| - |
36 |
| - private String content; |
37 |
| - |
38 |
| - private Set<String> noticeOccasions; |
39 |
| - |
40 |
| - private static Set<String> getDefaultNoticeOccasions() { |
41 |
| - return DingTalkGlobalConfig.getInstance().getNoticeOccasions(); |
42 |
| - } |
43 |
| - |
44 |
| - public Set<String> getNoticeOccasions() { |
45 |
| - return noticeOccasions == null ? getDefaultNoticeOccasions() : noticeOccasions; |
46 |
| - } |
47 |
| - |
48 |
| - public Set<String> resolveAtMobiles(EnvVars envVars) { |
49 |
| - if (StringUtils.isEmpty(atMobile)) { |
50 |
| - return new HashSet<>(16); |
51 |
| - } |
52 |
| - String realMobile = envVars.expand(atMobile); |
53 |
| - return Arrays.stream( |
54 |
| - StringUtils.split( |
55 |
| - // 支持多行,一行支持多个手机号 |
56 |
| - realMobile.replace("\n", ","), |
57 |
| - "," |
58 |
| - ) |
59 |
| - ) |
60 |
| - .collect(Collectors.toSet()); |
61 |
| - } |
62 |
| - |
63 |
| - public String getContent() { |
64 |
| - return content == null ? "" : content; |
65 |
| - } |
66 |
| - |
67 |
| - @DataBoundConstructor |
68 |
| - public DingTalkNotifierConfig( |
69 |
| - boolean checked, |
70 |
| - String robotId, |
71 |
| - String robotName, |
72 |
| - boolean atAll, |
73 |
| - String atMobile, |
74 |
| - String content, |
75 |
| - Set<String> noticeOccasions) { |
76 |
| - this.checked = checked; |
77 |
| - this.robotId = robotId; |
78 |
| - this.robotName = robotName; |
79 |
| - this.atAll = atAll; |
80 |
| - this.atMobile = atMobile; |
81 |
| - this.content = content; |
82 |
| - this.noticeOccasions = noticeOccasions; |
83 |
| - } |
84 |
| - |
85 |
| - public DingTalkNotifierConfig(DingTalkRobotConfig robotConfig) { |
86 |
| - this( |
87 |
| - false, |
88 |
| - robotConfig.getId(), |
89 |
| - robotConfig.getName(), |
90 |
| - false, |
91 |
| - null, |
92 |
| - null, |
93 |
| - getDefaultNoticeOccasions()); |
94 |
| - } |
95 |
| - |
96 |
| - public void copy(DingTalkNotifierConfig notifierConfig) { |
97 |
| - this.setChecked(notifierConfig.isChecked()); |
98 |
| - this.setAtAll(notifierConfig.isAtAll()); |
99 |
| - this.setAtMobile(notifierConfig.getAtMobile()); |
100 |
| - this.setContent(notifierConfig.getContent()); |
101 |
| - this.setNoticeOccasions(notifierConfig.getNoticeOccasions()); |
102 |
| - } |
103 |
| - |
104 |
| - @Extension |
105 |
| - public static class DingTalkNotifierConfigDescriptor extends Descriptor<DingTalkNotifierConfig> { |
106 |
| - |
107 |
| - /** |
108 |
| - * 通知时机列表 |
109 |
| - * |
110 |
| - * @return 通知时机 |
111 |
| - */ |
112 |
| - public NoticeOccasionEnum[] getNoticeOccasionTypes() { |
113 |
| - return NoticeOccasionEnum.values(); |
114 |
| - } |
115 |
| - } |
| 30 | + private boolean disabled; |
| 31 | + private boolean checked; |
| 32 | + |
| 33 | + private String robotId; |
| 34 | + |
| 35 | + private String robotName; |
| 36 | + |
| 37 | + private boolean atAll; |
| 38 | + |
| 39 | + private String atMobile; |
| 40 | + |
| 41 | + private String content; |
| 42 | + |
| 43 | + private Set<String> noticeOccasions; |
| 44 | + |
| 45 | + private static Set<String> getDefaultNoticeOccasions() { |
| 46 | + return DingTalkGlobalConfig.getInstance().getNoticeOccasions(); |
| 47 | + } |
| 48 | + |
| 49 | + public Set<String> getNoticeOccasions() { |
| 50 | + return noticeOccasions == null ? getDefaultNoticeOccasions() : noticeOccasions; |
| 51 | + } |
| 52 | + |
| 53 | + public Set<String> resolveAtMobiles(EnvVars envVars) { |
| 54 | + if (StringUtils.isEmpty(atMobile)) { |
| 55 | + return new HashSet<>(16); |
| 56 | + } |
| 57 | + String realMobile = envVars.expand(atMobile); |
| 58 | + return Arrays.stream( |
| 59 | + StringUtils.split( |
| 60 | + // 支持多行,一行支持多个手机号 |
| 61 | + realMobile.replace("\n", ","), |
| 62 | + "," |
| 63 | + ) |
| 64 | + ) |
| 65 | + .collect(Collectors.toSet()); |
| 66 | + } |
| 67 | + |
| 68 | + public String getContent() { |
| 69 | + return content == null ? "" : content; |
| 70 | + } |
| 71 | + |
| 72 | + @DataBoundConstructor |
| 73 | + public DingTalkNotifierConfig( |
| 74 | + boolean disabled, |
| 75 | + boolean checked, |
| 76 | + String robotId, |
| 77 | + String robotName, |
| 78 | + boolean atAll, |
| 79 | + String atMobile, |
| 80 | + String content, |
| 81 | + Set<String> noticeOccasions) { |
| 82 | + this.disabled = disabled; |
| 83 | + this.checked = checked; |
| 84 | + this.robotId = robotId; |
| 85 | + this.robotName = robotName; |
| 86 | + this.atAll = atAll; |
| 87 | + this.atMobile = atMobile; |
| 88 | + this.content = content; |
| 89 | + this.noticeOccasions = noticeOccasions; |
| 90 | + } |
| 91 | + |
| 92 | + public DingTalkNotifierConfig(DingTalkRobotConfig robotConfig) { |
| 93 | + this( |
| 94 | + false, |
| 95 | + false, |
| 96 | + robotConfig.getId(), |
| 97 | + robotConfig.getName(), |
| 98 | + false, |
| 99 | + null, |
| 100 | + null, |
| 101 | + getDefaultNoticeOccasions() |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + public void copy(DingTalkNotifierConfig notifierConfig) { |
| 106 | + try { |
| 107 | + BeanUtils.copyProperties(this, notifierConfig); |
| 108 | + } catch (IllegalAccessException | InvocationTargetException e) { |
| 109 | + log.error("读取机器人配置失败", e); |
| 110 | + throw new RuntimeException(e); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Extension |
| 115 | + public static class DingTalkNotifierConfigDescriptor extends Descriptor<DingTalkNotifierConfig> { |
| 116 | + |
| 117 | + /** |
| 118 | + * 通知时机列表 |
| 119 | + * |
| 120 | + * @return 通知时机 |
| 121 | + */ |
| 122 | + public NoticeOccasionEnum[] getNoticeOccasionTypes() { |
| 123 | + return NoticeOccasionEnum.values(); |
| 124 | + } |
| 125 | + } |
116 | 126 | }
|
0 commit comments