This case is included in github.com/chengxy-nds/Springboot-...
Hello, I am Xiaofu~
Recent tasks include code submission monitoring with webhook, which means that when someone submits code to a remote warehouse, a message will be sent to the enterprise WeChat group, such as @XXX submitting XXXX code to XXX project at XXX time.
As for what to do with such a tool, there is no way to crush people at the top level. In fact, I refuse it in my heart and it always feels as strange as being monitored. Is it hard not to find a Bug that I usually secretly pick up code and silently fix?
webhook
webhook is also known as hook. If you are unfamiliar with hook, let's change the concept. Callback URLs should have been heard of. For example, three-party platforms like WeChat Payment support configuring callback URLs to notify payment status.
When an event is triggered, such as "push code to a remote repository" or "raise an issue", the source site can issue an HTTP request to the URL of the webhook configuration.
The following is a workflow for this tool. A developer submitting code to a GitHub project triggers a pull event for GitHub, followed by a POST request to a tripartite URL configured in the GitHub webhook, which can be platforms such as Pins, Flybooks, and Enterprise WeChat.
Here we use GitHub +Enterprise WeChat to implement code submission monitoring and automatically push messages to the Enterprise Wechat group.
Configure GitHub webhook
Start with Settings for the GitHub project to do the basic configuration of the webhook.
There are four main configurations:
Payload URL callback service address;
Content type callback request header, recommended JSON format;
For security verification purposes, Secret adds the following two attributes to the request header to distinguish the source of the request and avoid malicious access to exposed requests.
X-Hub-Signature: sha1=2478e400758f6114aa18abc4380ef8fad0b16fb9 X-Hub-Signature-256: sha256=68bde5bee18bc36fd95c9b71b4a89f238cb01ab3bf92fd67de3a1de12b4f5c72
Finally, we choose which events trigger webhook callbacks, push events (code push events), everything (all events), and certain events.
We can view webhook callback records in Recent Deliveries, complete request and parameter data, and redelivery to simulate sending requests.
Configure Enterprise WeChat
Enterprise WeChat is actually easier to configure. First, we create a group with the option to add a robot on the right-click of the group. The webhook address will be generated after successful addition. We just send POST requests to this address and we get push messages in the group.
Message content supports four message types: text, markdown, image, and news. It also supports @group members within a group, which are illustrated in text format below.
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=145a516a-dd15-421f-97a3-ba3bf1479369' \ -H 'Content-Type: application/json' \ -d ' { "msgtype": "text", "text": { "content": "Hello, I'm a programmer" } }'
The direct request url found that the message was successfully pushed, indicating that the configuration is OK.
But here we find a problem. GitHub and Enterprise WeChat have a request to start and a request to accept a fixed data format. The data between the two interfaces is not compatible at all?
Request Forwarding
Since they are incompatible with each other, there is no way for them to do so. We can only make an adjustment in the middle by ourselves. Who can make both sides upset?
The logic of forwarding is also simple, simply accept the request data that GitHub calls back, modify the data format that is assembled into the requirements of Enterprise WeChat, and send it directly.
The data that GitHub pushes in, including warehouses, authors, submitters, submissions, and so on, is basically enough.
Code implementation is rough, let's see
@Slf4j @RestController public class WebhookController { private static String WECHAT_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=145a516a-dd15-421f-97a3-ba3bf1479369"; private static String GITHUB_API = "https://api.github.com/users/"; /** * @param webhook webhook * @author Something inside the programmer * @Description: github Callback * @date 2021/05/19 */ @PostMapping("/webhook") public String webhookGithub(@RequestBody GithubWebhookPullVo webhook) { log.info("webhook Access Receive weChatWebhook {}", JSON.toJSONString(webhook)); // Warehouse name String name = webhook.getRepository().getName(); SimpleDateFormat simpleFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String now = simpleFormatter.format(new Date()); String content = null; if (webhook.getCommits().size() > 0) { GithubWebhookPullVo.CommitsDTO commitsDTO = webhook.getCommits().get(0); content = "[" + commitsDTO.getCommitter().getName() + "]" + "On:" + now + "," + "To the author:[" + commitsDTO.getAuthor().getName() + "]Of, remote warehouse" + name + "Push Code" + "Details:"; List<String> addeds = commitsDTO.getAdded(); if (addeds.size() > 0) { content += "Add Files:"; for (int i = 0; i < addeds.size(); i++) { content = (i + 1) + content + addeds.get(i); } } List<String> modifieds = commitsDTO.getModified(); if (modifieds.size() > 0) { content += "Modify File:"; for (int i = 0; i < modifieds.size(); i++) { content = (i + 1) + content + modifieds.get(i); } } List<String> removeds = commitsDTO.getRemoved(); if (removeds.size() > 0) { content += "Delete Files:"; for (int i = 0; i < removeds.size(); i++) { content = (i + 1) + content + removeds.get(i); } } } log.info(content); WeChatWebhook weChatWebhook = new WeChatWebhook(); weChatWebhook.setMsgtype("text"); WeChatWebhook.TextDTO textDTO = new WeChatWebhook.TextDTO(); textDTO.setContent(content); textDTO.setMentionedList(Arrays.asList("@all")); textDTO.setMentionedMobileList(Arrays.asList("@all")); weChatWebhook.setText(textDTO); /** * Send a webhook request to Enterprise WeChat after assembling parameters */ log.info("Enterprise WeChat Send Parameters {}", JSON.toJSONString(weChatWebhook)); String post = HttpUtil.sendPostJsonBody(WECHAT_URL, JSON.toJSONString(weChatWebhook)); log.info("Enterprise WeChat Send Results post {}", post); return JSON.toJSONString(post); } }
Here's a reminder that some of the data that GitHub webhook calls back is not directly usable, and some scenarios call the GitHub API in exchange for some data.
Document address: https://docs.github.com/en/rest/reference
After the above configuration is completed, the forwarded code is deployed to the server, the whole link is tested to see the effect, and the POM is modified intentionally. XML file submission, found that after submitting the code, we successfully sent a message to Enterprise Wechat, which is consistent with our expected results.
Source address: github.com/chengxy-nds/Springboot-...
This project contains all the cases in my past articles, such as the source code of dithering watermarking tool, face recognition project, and various problem solving cases of redis, Seata, MQ and other middleware. Interested students can start, and the actual development will be used.
Hundreds of technical e-books have been sorted out, and students in need take them by themselves. The Technology Group is almost full, the students who think about it can add my friends and blow the technology with the big guys.