Skip to content

Commit

Permalink
feat(transformer): dingtalk_to_feishu support markdown type
Browse files Browse the repository at this point in the history
  • Loading branch information
elonzh committed Nov 2, 2020
1 parent 013ed0c commit 7eb0178
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
58 changes: 43 additions & 15 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
import os
import unittest

import requests

feishu_webhook = os.getenv("TRUMPET_FEISHU_WEBHOOK")
dingtalk_webhook = os.getenv("TRUMPET_DINGTALK_WEBHOOK")

# https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
# https://www.feishu.cn/hc/zh-cn/articles/360024984973-%E5%9C%A8%E7%BE%A4%E8%81%8A%E4%B8%AD%E4%BD%BF%E7%94%A8%E6%9C%BA%E5%99%A8%E4%BA%BA
resp = requests.post(
f"http://127.0.0.1:8080/transformers/dingtalk_to_feishu?trumpet_to={feishu_webhook}",
json={"msgtype": "text", "text": {"content": "快乐小神仙"}},
)
print(resp.status_code)
print(resp.headers)
print(resp.text)

resp = requests.post(
f"http://127.0.0.1:8080/transformers/feishu_to_dingtalk?trumpet_to={dingtalk_webhook}",
json={"msg_type": "text", "content": {"text": "快乐小神仙"}},
)
print(resp.status_code)
print(resp.headers)
print(resp.text)


class TestStringMethods(unittest.TestCase):
def test_dingtalk_to_feishu(self):
url = f"http://127.0.0.1:8080/transformers/dingtalk_to_feishu?trumpet_to={feishu_webhook}"
cases = [
{"msgtype": "text", "text": {"content": "快乐小神仙"}},
{
"markdown": {
"title": "哈哈哈 触发了 job test, 构建号:620",
"text": "###### 项目 [Unob](https://coding.net/p/unob)\n[哈哈哈](https://coding.net/u/ljaSkNTntD) 触发了 job \n> [test](https://coding.net/p/unob/ci/job/260491) 构建号:[620](https://coding.net/p/proj/ci/job/260491/build/620/pipeline)",
},
"msgtype": "markdown",
},
{
"markdown": {
"text": "###### 项目 [Unob](https://coding.net/p/unob)\n[哈哈哈](https://coding.net/u/ljaSkNTntD) 触发了 job \n> [test](https://coding.net/p/unob/ci/job/260491) 构建号:[620](https://coding.net/p/proj/ci/job/260491/build/620/pipeline)",
},
"msgtype": "markdown",
},
]
for case in cases:
resp = requests.post(url, json=case)
print(resp.status_code)
print(resp.headers)
print(resp.text)
assert resp.ok

def test_feishu_to_dingtalk(self):
url = f"http://127.0.0.1:8080/transformers/feishu_to_dingtalk?trumpet_to={dingtalk_webhook}"
cases = [{"msg_type": "text", "content": {"text": "快乐小神仙"}}]
for case in cases:
resp = requests.post(url, json=case)
print(resp.status_code)
print(resp.headers)
print(resp.text)
assert resp.ok


if __name__ == "__main__":
unittest.main()
6 changes: 6 additions & 0 deletions transformers/builtins/dingtalk_to_feishu.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def transform(raw):
body = {}
if msg_type == "text":
body = {"msg_type": "text", "content": {"text": origin_body["text"]["content"]}}
elif msg_type == "markdown":
title = origin_body["markdown"].get("title")
text = origin_body["markdown"].get("text", "")
if title:
text = title + "\n" + text
body = {"msg_type": "text", "content": {"text": text}}
return json.encode(body)
`
dingtalkToFeishu, err := transformers.NewTransformer(name, src)
Expand Down

0 comments on commit 7eb0178

Please sign in to comment.