├── .gitignore ├── LICENSE ├── README.md ├── setup.py └── src └── sentry_wechat ├── __init__.py ├── forms.py └── plugin.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 安生 4 | Copyright (c) 2020 Whzcorcd 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sentry-wechat 2 | 3 | Sentry 企业微信通知插件 4 | 5 | > *项目修改自 [anshengme/sentry-dingding](https://github.com/anshengme/sentry-dingding),感谢原作者 [anshengme](https://github.com/anshengme)* 6 | 7 | ## Install 8 | 9 | ### docker 10 | 11 | 拉取官方镜像,修改其中的 `requirements.txt`,加入单独一行 `sentry-wechat~=0.03` 即可 12 | 13 | ### python 14 | 15 | pip 安装依赖包即可 16 | 17 | ```bash 18 | $ pip install sentry-wechat 19 | ``` 20 | 21 | ## Usage 22 | 23 | 在项目的所有集成页面找到 `Wechat Work` 插件启用,并设置 `key` 24 | 25 | ## License 26 | 27 | MIT 28 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | ############################################# 5 | # File Name: setup.py 6 | # Author: whzcorcd 7 | # Mail: whzcorcd@gmail.com 8 | # Created Time: 2020-06-08 9 | ############################################# 10 | 11 | from setuptools import setup, find_packages 12 | 13 | with open("README.md", "r") as fh: 14 | long_description = fh.read() 15 | 16 | setup( 17 | name="sentry-wechat", 18 | version='0.0.3', 19 | author='whzcorcd', 20 | author_email='whzcorcd@gmail.com', 21 | url='https://github.com/corcd/sentry-wechat', 22 | description='A sentry extension which share information to Wechat Work', 23 | long_description=long_description, 24 | long_description_content_type="text/markdown", 25 | license='MIT', 26 | keywords='sentry wechat', 27 | include_package_data=True, 28 | zip_safe=False, 29 | package_dir={'': 'src'}, 30 | packages=find_packages('src'), 31 | install_requires=[ 32 | 'sentry>=9.0.0', 33 | 'requests', 34 | ], 35 | entry_points={ 36 | 'sentry.plugins': [ 37 | 'sentry_wechat = sentry_wechat.plugin:WechatPlugin' 38 | ] 39 | }, 40 | classifiers=[ 41 | 'Programming Language :: Python :: 2.7', 42 | "License :: OSI Approved :: MIT License", 43 | ] 44 | ) 45 | -------------------------------------------------------------------------------- /src/sentry_wechat/__init__.py: -------------------------------------------------------------------------------- 1 | ''' 2 | @Author: Whzcorcd 3 | @Date: 2020-06-08 09:15:30 4 | @LastEditors: Wzhcorcd 5 | @LastEditTime: 2020-06-08 14:16:27 6 | @Description: file content 7 | ''' 8 | # coding: utf-8 9 | 10 | VERSION = "0.0.3" 11 | -------------------------------------------------------------------------------- /src/sentry_wechat/forms.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | from django import forms 4 | 5 | 6 | class WechatOptions(forms.Form): 7 | key = forms.CharField( 8 | max_length=255, 9 | help_text='Wechat Wrok robot webhook key' 10 | ) 11 | -------------------------------------------------------------------------------- /src/sentry_wechat/plugin.py: -------------------------------------------------------------------------------- 1 | ''' 2 | @Author: Whzcorcd 3 | @Date: 2020-06-08 09:15:49 4 | @LastEditors: Wzhcorcd 5 | @LastEditTime: 2020-06-08 14:15:18 6 | @Description: file content 7 | ''' 8 | # coding: utf-8 9 | 10 | import json 11 | 12 | import requests 13 | from sentry.plugins.bases.notify import NotificationPlugin 14 | 15 | import sentry_wechat 16 | from .forms import WechatOptions 17 | 18 | Wechat_API = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={key}" 19 | 20 | 21 | class WechatPlugin(NotificationPlugin): 22 | """ 23 | Sentry extension to Share information to Wechat Work. 24 | """ 25 | author = 'whzcorcd' 26 | author_url = 'https://github.com/corcd/sentry-wechat' 27 | version = sentry_wechat.VERSION 28 | description = 'Share information to Wechat Work.' 29 | resource_links = [ 30 | ('Source', 'https://github.com/corcd/sentry-wechat'), 31 | ('Bug Tracker', 'https://github.com/corcd/sentry-wechat/issues'), 32 | ('README', 'https://github.com/corcd/sentry-wechat/blob/master/README.md'), 33 | ] 34 | 35 | slug = 'Wechat Wrok' 36 | title = 'Wechat Wrok' 37 | conf_key = slug 38 | conf_title = title 39 | project_conf_form = WechatOptions 40 | 41 | def is_configured(self, project): 42 | """ 43 | Check if plugin is configured. 44 | """ 45 | return bool(self.get_option('key', project)) 46 | 47 | def notify_users(self, group, event, *args, **kwargs): 48 | self.post_process(group, event, *args, **kwargs) 49 | 50 | def post_process(self, group, event, *args, **kwargs): 51 | """ 52 | Process error. 53 | """ 54 | if not self.is_configured(group.project): 55 | return 56 | 57 | if group.is_ignored(): 58 | return 59 | 60 | key = self.get_option('key', group.project) 61 | send_url = Wechat_API.format(key=key) 62 | title = u"New alert from {}".format(event.project.slug) 63 | 64 | data = { 65 | "msgtype": "markdown", 66 | "markdown": { 67 | "content": u"#### {title} \n > {message} [href]({url})".format( 68 | title=title, 69 | message=event.title or event.message, 70 | url=u"{}events/{}/".format( 71 | group.get_absolute_url(), event.event_id), 72 | ) 73 | } 74 | } 75 | requests.post( 76 | url=send_url, 77 | headers={"Content-Type": "application/json"}, 78 | data=json.dumps(data).encode("utf-8") 79 | ) 80 | --------------------------------------------------------------------------------