Forráskód Böngészése

更新通知接收配置接口

reghao 2 éve
szülő
commit
e556d9b36e

+ 49 - 6
manager/src/main/java/cn/reghao/devops/manager/notification/controller/NotifyReceiverController.java

@@ -1,9 +1,13 @@
 package cn.reghao.devops.manager.notification.controller;
 
+import cn.reghao.devops.manager.notification.db.repository.EmailAccountRepository;
 import cn.reghao.devops.manager.notification.db.repository.NotifyReceiverRepository;
+import cn.reghao.devops.manager.notification.model.NotifyType;
+import cn.reghao.devops.manager.notification.model.po.EmailAccount;
 import cn.reghao.devops.manager.notification.model.po.NotifyReceiver;
 import cn.reghao.devops.manager.util.db.PageSort;
 import cn.reghao.jutil.jdk.result.WebResult;
+import cn.reghao.jutil.jdk.string.StringRegexp;
 import io.swagger.annotations.Api;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageRequest;
@@ -13,6 +17,8 @@ import org.springframework.ui.Model;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.List;
+
 /**
  * @author reghao
  * @date 2024-01-26 20:32:24
@@ -22,9 +28,12 @@ import org.springframework.web.bind.annotation.*;
 @RequestMapping("/sys/notify/receiver")
 public class NotifyReceiverController {
     private final NotifyReceiverRepository notifyReceiverRepository;
+    private final EmailAccountRepository emailAccountRepository;
 
-    public NotifyReceiverController(NotifyReceiverRepository notifyReceiverRepository) {
+    public NotifyReceiverController(NotifyReceiverRepository notifyReceiverRepository,
+                                    EmailAccountRepository emailAccountRepository) {
         this.notifyReceiverRepository = notifyReceiverRepository;
+        this.emailAccountRepository = emailAccountRepository;
     }
 
     @GetMapping("")
@@ -41,6 +50,33 @@ public class NotifyReceiverController {
         return "/sys/notify/receiveradd";
     }
 
+    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE)
+    @ResponseBody
+    public String saveReceiver(@Validated NotifyReceiver notifyReceiver) {
+        String type = notifyReceiver.getType();
+        NotifyReceiver notifyReceiver1 = notifyReceiverRepository.findByType(type);
+        if (notifyReceiver1 != null) {
+            String errMsg = String.format("NotifyType %s already exist", type);
+            return WebResult.failWithMsg(errMsg);
+        }
+
+        if (type.equals(NotifyType.email.name())) {
+            List<EmailAccount> list = emailAccountRepository.findAll();
+            if (list.isEmpty()) {
+                return WebResult.failWithMsg("没有可以发送通知的邮箱, 请先配置");
+            }
+
+            String email = notifyReceiver.getUrl();
+            boolean matched = StringRegexp.matchEmail(email);
+            if (!matched) {
+                return WebResult.failWithMsg("邮箱地址格式不正确, 请检查");
+            }
+        }
+
+        notifyReceiverRepository.save(notifyReceiver);
+        return WebResult.success();
+    }
+
     @GetMapping("/edit/{name}")
     public String receiverEdit(@PathVariable("name") String name, Model model) throws Exception {
         NotifyReceiver notifyReceiver = notifyReceiverRepository.findByName(name);
@@ -48,17 +84,24 @@ public class NotifyReceiverController {
         return "/sys/notify/receiveredit";
     }
 
-    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE)
+    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
     @ResponseBody
-    public String saveReceiver(@Validated NotifyReceiver notifyReceiver) {
-        String type = notifyReceiver.getType();
-        NotifyReceiver notifyReceiver1 = notifyReceiverRepository.findByType(type);
+    public String editReceiver(@Validated NotifyReceiver notifyReceiver) {
+        String name = notifyReceiver.getName();
+        NotifyReceiver notifyReceiver1 = notifyReceiverRepository.findByName(name);
+        if (notifyReceiver1 == null) {
+            String errMsg = String.format("NotifyReceiver %s not exist", name);
+            return WebResult.failWithMsg(errMsg);
+        }
+
+        notifyReceiver1.setUrl(notifyReceiver.getUrl());
+        notifyReceiverRepository.save(notifyReceiver1);
         return WebResult.success();
     }
 
     @DeleteMapping(value = "/delete/{name}", produces = MediaType.APPLICATION_JSON_VALUE)
     @ResponseBody
-    public String deleteReceiver(@PathVariable("name") String name) throws Exception {
+    public String deleteReceiver(@PathVariable("name") String name) {
         NotifyReceiver notifyReceiver = notifyReceiverRepository.findByName(name);
         if (notifyReceiver != null) {
             notifyReceiverRepository.delete(notifyReceiver);

+ 5 - 1
manager/src/main/java/cn/reghao/devops/manager/notification/model/NotifyType.java

@@ -5,5 +5,9 @@ package cn.reghao.devops.manager.notification.model;
  * @date 2024-01-26 21:52:32
  */
 public enum NotifyType {
-    webhook, email
+    webhook, email;
+
+    public String getName() {
+        return this.name();
+    }
 }

+ 1 - 1
manager/src/main/resources/templates/sys/notify/receiveredit.html

@@ -5,7 +5,7 @@
 
 <body>
 <div class="layui-form timo-compile">
-    <form th:action="@{/sys/notify/receiver/save}">
+    <form th:action="@{/sys/notify/receiver/edit}">
         <div class="layui-form-item">
             <label class="layui-form-label required">通知类型</label>
             <div class="layui-input-inline">