Skip to content

Commit c7a7df3

Browse files
author
zhifu.xin
committed
webhook终版提交
1 parent f68a278 commit c7a7df3

File tree

5 files changed

+383
-314
lines changed

5 files changed

+383
-314
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<module>springboot-erupt</module>
3131
<module>springboot-magic-api</module>
3232
<module>springboot-test</module>
33-
<module>springboot-webhook-wechat</module>
33+
<!-- <module>springboot-webhook-wechat</module>-->
3434
</modules>
3535

3636
<properties>

springboot-webhook-wechat/pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55
<parent>
6-
<groupId>com.notebook</groupId>
7-
<artifactId>Springboot-Notebook</artifactId>
8-
<version>0.0.1-SNAPSHOT</version>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.3.3.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
910
</parent>
1011
<artifactId>springboot-webhook-wechat</artifactId>
1112
<version>0.0.1-SNAPSHOT</version>

springboot-webhook-wechat/src/main/java/com/xiaofu/webhook/WebhookApplication.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
import org.springframework.context.annotation.ComponentScan;
77

88
@ComponentScan("com.xiaofu.webhook")
9-
@ForestScan(basePackages = {"com.xiaofu.douyin.client"})
109
@SpringBootApplication
1110
public class WebhookApplication {
1211

1312
public static void main(String[] args) {
1413
SpringApplication.run(WebhookApplication.class, args);
1514
}
1615

17-
1816
}

springboot-webhook-wechat/src/main/java/com/xiaofu/webhook/controller/WebhookController.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.ArrayList;
1717
import java.util.Arrays;
1818
import java.util.Date;
19+
import java.util.List;
1920
import java.util.logging.SimpleFormatter;
2021

2122
@Slf4j
@@ -34,34 +35,52 @@ public class WebhookController {
3435
*/
3536
@PostMapping("/webhook")
3637
public String webhookGithub(@RequestBody GithubWebhookPullVo webhook) {
37-
3838
log.info("webhook 入参接收 weChatWebhook {}", JSON.toJSONString(webhook));
3939
// 仓库名
4040
String name = webhook.getRepository().getName();
41-
42-
// 拥有者
43-
String owner = webhook.getRepository().getOwner().getLogin();
44-
GithubUser ownerUser = JSON.parseObject(HttpUtil.sendGet(GITHUB_API + owner), GithubUser.class);
45-
46-
// 提交者
47-
String sender = webhook.getSender().getLogin();
48-
GithubUser senderUser = JSON.parseObject(HttpUtil.sendGet(GITHUB_API + sender), GithubUser.class);
49-
50-
StringBuffer sb = new StringBuffer();
5141
SimpleDateFormat simpleFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
5242
String now = simpleFormatter.format(new Date());
53-
sb.append("提交者:[" + senderUser.getName() + "],");
54-
sb.append("于:" + now + ",");
55-
sb.append("向作者:[" + ownerUser.getName() + "]的,远程仓库" + name + "提交代码");
43+
String content = "";
44+
if (webhook.getCommits().size() > 0) {
45+
GithubWebhookPullVo.CommitsDTO commitsDTO = webhook.getCommits().get(0);
46+
content += "提交者:[ " + commitsDTO.getCommitter().getName() + " ] \r\n" +
47+
"时间:[ " + now + " ]\n" +
48+
"向" + "远程仓库 [ " + name + " ]推送代码 \n" +
49+
"提交详情: \n";
5650

51+
List<String> addeds = commitsDTO.getAdded();
52+
if (addeds.size() > 0) {
53+
content += "添加文件: \n[\n";
54+
for (int i = 0; i < addeds.size(); i++) {
55+
content += (i + 1) + "、" + addeds.get(i) + "\n";
56+
}
57+
content += "] , \n";
58+
}
59+
List<String> modifieds = commitsDTO.getModified();
60+
if (modifieds.size() > 0) {
61+
content += "修改文件: \n[\n";
62+
for (int i = 0; i < modifieds.size(); i++) {
63+
content += (i + 1) + "、" + modifieds.get(i) + "\n";
64+
}
65+
content += "] , \n";
66+
}
67+
List<String> removeds = commitsDTO.getRemoved();
68+
if (removeds.size() > 0) {
69+
content += "删除文件: \n[\n";
70+
for (int i = 0; i < removeds.size(); i++) {
71+
content += (i + 1) + "、" + removeds.get(i) + "\n";
72+
}
73+
content += "]";
74+
}
75+
}
76+
log.info(content);
5777
WeChatWebhook weChatWebhook = new WeChatWebhook();
5878
weChatWebhook.setMsgtype("text");
5979
WeChatWebhook.TextDTO textDTO = new WeChatWebhook.TextDTO();
60-
textDTO.setContent(sb.toString());
80+
textDTO.setContent(content);
6181
textDTO.setMentionedList(Arrays.asList("@all"));
6282
textDTO.setMentionedMobileList(Arrays.asList("@all"));
6383
weChatWebhook.setText(textDTO);
64-
6584
/**
6685
* 组装参数后向企业微信发送webhook请求
6786
*/

0 commit comments

Comments
 (0)