Let gitLab events directly initiate reminders in the enterprise wechat, replacing the traditional email reminders to achieve a better message delivery rate. If your division still does not have similar notification mechanism at present, might as well try, perhaps because this and get boss appreciation, begin happy and sweet life.

Let’s cut the crap and get right to the project.

Project address: Github

Technology stack: typescript + Express

Process:

The original purpose of this project was to establish a Merge Requrest plus Code Review message notification mechanism. The whole Merge Request mechanism is as follows:

  1. The user will initiate a Merge Request, and the description in the Merge Request will be assigned to the corresponding user. The @user will perform a Code Review for your Code as a Code Reviewer, and the @user will be notified in the wechat group of the enterprise. Remind me to conduct Code Review. The notification message is the content of the title item in MR.

  2. If the Code Reviewer believes there is no Code quality problem after Code Review, reply 1 or done in the comment of the Merge Request. If all Code Reviewer reviews have been completed, the Code quality Review will be considered to have passed.

  3. When Code Review is approved, a Merge Request reminder Merge notification is issued, and the notary is the author of the repository and the user specified by assignee when issuing the Merge Request

  4. Merge Request After the Merge Request is complete, a Merge Request notification is sent and the initiator is notified.

Bots were later enhanced to include scheduled tasks and pipeline event notifications.

This mechanism has achieved good results in the department’s projects.

Webhooks currently support the following GitLab events:

gitlab webhook

Configuration gitlab webhook

You need the maintainer permission of the project to set Webhooks

Interface path URL: /gitlab_hook, that is, the project deployment address plus this path, is gitlab Webhook URL.

Secret Token: gitlab-webhook, which is required when adding webhooks to projects to enhance interface security. The default value is gitlab-webhook and can be changed in SRC/appconfig. ts

Trigger: indicates the event notification you want to receive

merge_request

Provides support for merge_request events in GitLab Webhook, currently supports the following types of reminders:

  • When a Merge Request is newly opened, the repository author and the specified user of assignee are reminded

    When the Code Reivew person is not specified in description when initiating MR, the notification is as follows

    When a Code Review person is specified, the notification is as follows

  • Merge Request alerts when updates occur (except for Title and Label updates)

  • The repository author and specified users of Assignee are notified of the Merge Request when it is merged

note

Merge Request provides the event notification function when comments occur in Merge Request. Currently, the following types of notifications are supported

  • When a comment occurs in the Merge Request

  • When code is commented on in the Merge Request

    When a comment 1 or done is made in the Merge Request and the reviewer is a Code reviewer, the Code Review is considered to have passed

Timing task

A scheduled task was established to query all projects for pending Merge Requests every non-weekend day, and a backlog notification was sent to the corresponding project robot

gitlab api

api url

Gitlab REST API is used in this project. Please determine the API address according to the Gitlab service you actually use. Path: SRC/appconfig.ts

access_token

Gitlab API is used in this project. Access to gitlab API requires access_token, which is configured in SRC/Appconfig. ts. Please replace your token here.

Enterprise wechat robot

The above gitLab Webhook reminder function relies on the enterprise wechat robot, which is responsible for sending messages to the enterprise wechat group. Each robot has a unique key, so please keep the key value of the group robot well to prevent leakage

Official Configuration Description

Configuration of robot key

In the SRC /config/projectKeys file in the code, a mapping relationship between swarm robot key and gitlab project information is maintained, which is modified by referring to the following configuration

module.exports = {
  // projectName
  "project name": {
    id: "<project id>".// gitlab project id
    key: "<your robot key>" // Enterprise wechat robot key
  }
Copy the code

How to send messages to @enterprise wechat users

When sending messages through the interface, you cannot directly use the @ user name to send messages to the corresponding user. Therefore, you must use the user ID to send messages to the @ user. User ID is different according to the setting rules of each company. You need to find the administrator who manages the enterprise wechat background in the company to ask for the user ID setting rules.

In the code, the user related to the GitLab event will be queried to get its Chinese name, and then a transformation will be performed according to the user ID rules to get the real user ID, so that the @ user can be correct.

The related code is in the usernameToUserid method of SRC /utils/index.ts. Please modify it according to different ID rules

About code reviews and MR reviews

Both of these two comment events are Note events in GitLab. By distinguishing the attributes in the event, we can determine whether the comments are code comments or MR events themselves

This is MR Comment

This is code review

The reason why I choose to use MR comments as the mark of the completion of Code Review is that GITlab version 12 is used in my project. Originally, GITlab’s Approval was used as the Code approval rule of Merge Request. However, it will not be supported until 13.2, so it has to be abandoned. Since reviews are used to judge whether Code Review is approved or not, a behavioral agreement needs to be reached between teams.

How to package and deploy

The first deployment

  1. Prepare a server with node environment
  2. Used in this projectyarn buildnpm run buildThe command is packaged into the out folder.
  3. Copy all files in the out folder to the directory specified by the server, then switch to the directory and useyarnnpm iInstall dependencies
  4. Run commands in the directorypm2 start pm2.jsonThe default listening port is 16080, which can be changed in pm2.json.

Version update

  1. Go to the server where the service is deployed and switch to the directory where the project resides
  2. Copy the packaged code in and overwrite the old code
  3. Execute the commandpm2 restart webhook-server. (Webhook-server is the name of the pM2 startup project, which can be changed in pm2. Json)

Effect of the test

You can perform a Test on the Web UI. Click Test to select an event and an event notification will be sent.

Click the Edit button to view the history of WebHook notifications, the content of messages sent, and the content of WebHook responses