Просмотр исходного кода

添加外部供外部调用的 webhook 接口

reghao 5 лет назад
Родитель
Сommit
487ce7c50c

+ 83 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/controller/WebhookController.java

@@ -0,0 +1,83 @@
+package cn.reghao.autodop.dmaster.app.controller;
+
+import cn.reghao.autodop.common.result.WebResult;
+import cn.reghao.autodop.dmaster.app.constant.AppEnv;
+import cn.reghao.autodop.dmaster.app.service.BuildService;
+import cn.reghao.autodop.dmaster.app.service.OssService;
+import cn.reghao.autodop.dmaster.app.service.build.BuildDispatcher;
+import cn.reghao.autodop.dmaster.app.vo.AppToBuild;
+import cn.reghao.autodop.dmaster.app.vo.PageList;
+import cn.reghao.autodop.dmaster.app.vo.log.BuildDeployLog;
+import cn.reghao.autodop.dmaster.app.vo.log.BuildLogVO;
+import cn.reghao.autodop.dmaster.app.vo.log.CommitLogVO;
+import cn.reghao.autodop.dmaster.app.vo.log.DeployLogVO;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+import org.tmatesoft.svn.core.SVNException;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * @author reghao
+ * @date 2019-08-30 18:49:15
+ */
+@Slf4j
+@Api(tags = "webhook")
+@RestController
+@RequestMapping("/api/hook")
+public class WebhookController {
+    private BuildDispatcher buildDispatcher;
+
+    public WebhookController(BuildDispatcher buildDispatcher) {
+        this.buildDispatcher = buildDispatcher;
+    }
+
+    @PostMapping("/pipeline/{appId}")
+    public String build1(@PathVariable("appId") String appId,
+                         @RequestHeader("X-Codeup-Event") String event,
+                         @RequestHeader("x-codeup-delivery") String deliveryId,
+                         @RequestBody String body) throws Exception {
+
+        if (event == null || deliveryId == null) {
+            return WebResult.fail("非 webhook 调用");
+        }
+
+        Set<String> apps = checkArgs(appId);
+        List<BuildDeployLog> buildDeployLogs;
+        if (apps.size() == 0) {
+            return WebResult.success("ok");
+        } else {
+            buildDeployLogs = buildDispatcher.dispatch(apps, true);
+            List<DeployLogVO> deployResultVOS = buildDeployLogs.stream()
+                    .filter(buildDeployLog -> buildDeployLog.getDeployLog() != null)
+                    .map(BuildDeployLog::deployLog)
+                    .collect(Collectors.toList());
+
+            return WebResult.success(deployResultVOS);
+        }
+    }
+
+    /**
+     * 检查参数
+     *
+     * @param
+     * @return
+     * @date 2019-12-02 下午2:36
+     */
+    private Set<String> checkArgs(String appId) {
+        String whiteSpace = "\\s+";
+        String[] array = appId.replaceAll(whiteSpace, "").split(",");
+        return new HashSet<>(Arrays.asList(array));
+    }
+}

+ 2 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/auth/jwt/JwtTokenFilter.java

@@ -27,8 +27,8 @@ public class JwtTokenFilter extends OncePerRequestFilter {
                                     FilterChain chain) throws ServletException, IOException {
         String uri = request.getRequestURI();
         String method = request.getMethod();
-        // TODO 不处理 OPTIONS 请求和非 /api 开头的请求
-        if ("OPTIONS".equals(method) || !uri.startsWith("/api")) {
+        // TODO 不处理 OPTIONS 请求、webhook 接口和非 /api 开头的请求
+        if ("OPTIONS".equals(method) || uri.startsWith("/api/hook") || !uri.startsWith("/api")) {
             chain.doFilter(request, response);
             return;
         }