Due to the inconsistent message formats between GitLab event notifications and DingTalk, a direct push is not feasible. This project serves as an intermediary service for message translation, which enables the push of GitLab events to DingTalk.
- Create a new robot and obtain
access_token
. Refer to this document - Insert this link into GitLab's
webhook
:http://${yourhost}:6688/webhook?access_token=${access_token}
Note: This project supports both single robot and multiple robots mode. The default robot can be set by the environment variable ACCESS_TOKEN
. For multiple robots, you only need to add the access_token
parameter to the link for distinction. The links are as follows:
http://${yourhost}:6688/webhook # default robot
http://${yourhost}:6688/webhook?access_token=${access_token1} # robot with access_token1
http://${yourhost}:6688/webhook?access_token=${access_token2} # robot with access_token2
If the ACCESS_TOKEN
environment variable does not exist and no parameter is passed, an error will be reported.
npm install -g pm2
git clone git@github.com:wyyxdgm/gitlab-dingtalk.git
cd gitlab-dingtalk && npm install
ACCESS_TOKEN=${access_token} && \
TEMPLATE=default && PORT=6688 && \
npm start
Replace the access_token
in the following code with the robot's access_token
.
docker run -e ACCESS_TOKEN=${access_token} -e TEMPLATE=default -e PORT=6688 -p 6688:6688 -d wyyxdgm/gitlab-dingtalk
Create docker-compose.yml as follows:Replace ${access_token}
with the robot's access_token
.
version: "3"
services:
app:
image: "wyyxdgm/gitlab-dingtalk"
restart: always
container_name: gitlab-dingtalk
ports:
- "6688:6688"
environment:
- ACCESS_TOKEN=${access_token}
- PORT=6688
- TEMPLATE=default
command: ["npm", "start"]
Execute
# Start up
docker compose -f docker-compose.yml up -d
# Shut down
docker compose -f docker-compose.yml down
The default template file is at src/templates/default/index.js
, you can directly modify it according to the Template Standard. The code is as follows:
/**
* text type
* text String Required Text content
* isAtAll Optional Whether copying everyone or not
*/
function text(text = "", isAtAll = false) {
return {
msgtype: "text",
text: {
content: text,
},
at: {
isAtAll: isAtAll,
},
};
}
/**
* link type
* text String Required Text content
* title String Required Message title
* picUrl String Required Display picture
* messageUrl String Required URL to be redirected after clicking the message
*/
function link(linkObject) {
return {
msgtype: "link",
link: linkObject,
};
}
module.exports = {
_: (_) => text(`gitlab event[${_.object_kind}]`), // default
push: (_) =>
link({
text: _.commits.map((c) => c.message).join("\n"),
title: `gitlab event[${_.object_kind}] by ${_.user_name}`,
picUrl: `${_.user_avatar}`,
messageUrl: `${_.repository.homepage}/commit/${_.checkout_sha}`,
}), // Push event
tag: () =>
link({
text: `tag:${_.ref}`,
title: `gitlab event[${_.object_kind}] by ${_.user_name}`,
picUrl: `${_.user_avatar}`,
messageUrl: `${_.repository.homepage}/tags/${_.ref.replace("refs/tags/", "")}`,
}), // Tag event
issues: () => null, // Issues event
coc: () => null, // Applying default, comment on commit event
comr: () => null, // Applying default, comment on MR event
coi: () => null, // Applying default, comment on issue event
cocs: () => null, // Applying default, comment on code snippet event
};
-
Choose a local directory as the template directory, for example
./default
-
Copy a version of src/templates/default/index.js to
./default/index.js
. -
Refer to src/templates/README.md to adjust the content of
./default/index.js
-
Start it up using the following method:
- Docker
Replace ./default
with the path of the local template folder and ${access_token}
, then execute
docker run --privileged -e ACCESS_TOKEN=${access_token} \
-e TEMPLATE=default -e PORT=6688 -p 6688:6688 \
-v ./default:/opt/app/src/templates/default \
-d wyyxdgm/gitlab-dingtalk
- Docker Compose
Create a new file docker-compose.yml
, the content is as follows:
version: "3"
services:
app:
image: "wyyxdgm/gitlab-dingtalk"
restart: always
container_name: gitlab-dingtalk
ports:
- "6688:6688"
environment:
- ACCESS_TOKEN=${access_token}
- PORT=6688
- TEMPLATE=default
volumes:
- ./default:/opt/app/src/templates/default
privileged: true
command: ["npm", "start"]
Replace ./default
with your local template folder path and ${access_token}
, then execute.
# Start up
docker compose -f docker-compose.yml up -d
Create a new templates
folder, for storing multiple template folders as follows:
./templates
├── default
│ └── index.js
├── template1
│ └── index.js
└── template2
└── index.js
- Docker
Replace ${template_name}
with the name of the local template subfolder, and ${access_token}
, then execute.
docker run --privileged -e ACCESS_TOKEN=${access_token} \
-e TEMPLATE=${template_name} -e PORT=6688 -p 6688:6688 \
-v ./tempaltes:/opt/app/src/templates \
-d wyyxdgm/gitlab-dingtalk
- Docker Compose
The content of docker-compose.yml
is as follows:
version: "3"
services:
app:
image: "wyyxdgm/gitlab-dingtalk"
restart: always
container_name: gitlab-dingtalk
ports:
- "6688:6688"
environment:
- ACCESS_TOKEN=${access_token}
- PORT=6688
- TEMPLATE=${template_name}
volumes:
- ./tempaltes:/opt/app/src/templates
privileged: true
command: ["npm", "start"]
Replace ./templates
with your local templates folder path, replace the template name ${template_name}
, and ${access_token}
, then execute.
# Start up
docker compose -f docker-compose.yml up -d
Refer to local gitlab ${host}/help/web_hooks/web_hooks
, For more detail events data, please refer toEVENT.md
- Push events
- Tag events
- Issues events
- Comment on commit
- Comment on merge request
- Comment on issue
- Comment on code snippet
- Merge request events
- header:
X-Gitlab-Event: Push Hook
- default template: Link
- header:
X-Gitlab-Event: Tag Push Hook
- default template: Link
- header:
X-Gitlab-Event: Issue Hook
- default template: Text
- header:
X-Gitlab-Event: Note Hook
- default template: Text
- header:
X-Gitlab-Event: Note Hook
- default template: Text
- header:
X-Gitlab-Event: Note Hook
- default template: Text
- header:
X-Gitlab-Event: Merge Request Hook
- default template: Text
- header:
X-Gitlab-Event: Merge Request Hook
- default template: Link