app_webhook package

Submodules

app_webhook.admin module

app_webhook.apps module

class app_webhook.apps.AppWebhookConfig(app_name, app_module)[source]

Bases: django.apps.config.AppConfig

name = 'app_webhook'

app_webhook.hook module

定义hook的执行 WebHook: def _if_valid_source(self): 验证是否合法来源的webhook

可以post一个自定义的key,{“sec_code”:”my_password”},然后验证这个key是否符合 github的webhook按照github的签名方式来计算:header里有一个’X-Hub-Signature’, 然后对payload用预先在github上预留的code进行签名,二者相符则签名通过

自定义的webhook,就直接在post发送的json里加一个{“sec_code”:”…”}来验证

def set_fields(self): 设定需要写入的字段,根据webhook的来源不同,这里写入的数据由自已定义并处理

class app_webhook.hook.GithubHook(request, sec_code=None)[source]

Bases: app_webhook.hook.WebHook

set_fields()[source]
class app_webhook.hook.GithubHookFeiProject(request, sec_code=None)[source]

Bases: app_webhook.hook.WebHook

set_fields()[source]
class app_webhook.hook.HhxxGitHook(request, sec_code=None)[source]

Bases: app_webhook.hook.WebHook

用于接受本地git server 在post-receive发过来的请求

:curl -H “Content-Type:application/json” -X POST -d ‘{“sec_code”:””}’ <http://site/…>

post-receive hook script:
用git log分别取出当前的head和上一次的head,再取出当前的commt message,转成json发出去
#!/bin/sh
# for post commit
# exec git update-server-info

AFTER=$(git log –pretty=%h |head -1)
BEFORE=$(git log –pretty=%h |head -2|tail -1)
COMMIT_MESSAGE=$(git log –pretty=%s |head -1)
tmp=’{“sec_code”:”xxxxxx”,”after”:’”$AFTER”’,”before”:’”$BEFORE”’,”commit_message”:’”$COMMIT_MESSAGE”’}’
echo $tmp | curl -i -H “Content-Type:application/json” -X POST -d @- webhook.hhxx.me/webhook/hhxx/
set_fields()[source]
class app_webhook.hook.WebHook(request, sec_code=None)[source]

Bases: abc.ABC

接受一个请求,然后将必须的几个属性赋值 log = WebHook() <…验证是否允许的来源…> self.verified = True log.set_log_fields() 从self.data_dict里取值 log.from_site = … 无法set_log_fields的项,手动赋值一遍 log.shell_script = ‘<执行的命令>’ log.save_log() :这样就执行脚本并且保存日志 必须的属性包括:

from_site: 需要知道这个请求来自于何处 shell_script: 需要知道收到请求后去执行哪个脚本 verified: 需要先赋值为True才会保存;验证需要自行处理,因为每个webhook的验证方式不一样

可以考虑post过来的json包括一个{“sec”: sec_code},然后每次验证它

Parameters

{[type]} -- [description] (object) –

Returns

[type] – [description]

convert_data_to_dict()[source]

处理get或post请求,获取有用的数据 TODO: 要考虑各种异常

gen_webhooklog()[source]

获取日志各字段的值

save_log()[source]
abstract set_fields()[source]
exception app_webhook.hook.WebhookSourceNotValid[source]

Bases: Exception

app_webhook.models module

GithubLog: 记录github的webhook # 都是github webhook里的数据 ref = before = after =

# “repository”:{“full_name”: …} repo_name =

# “repository”:{“html_url”: …} html_url =

# “repository”:{“hooks_url”: …} hooks_url =

class app_webhook.models.WebhookLog(id, from_site, request_url, date, time, ref, before, after, repo_name, html_url, hooks_url, commit_message, extra_info)[source]

Bases: django.db.models.base.Model

exception DoesNotExist

Bases: django.core.exceptions.ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: django.core.exceptions.MultipleObjectsReturned

after

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

before

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

commit_message

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

date

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

extra_info

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

from_site

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

get_next_by_date(*, field=<django.db.models.fields.DateField: date>, is_next=True, **kwargs)
get_previous_by_date(*, field=<django.db.models.fields.DateField: date>, is_next=False, **kwargs)
hooks_url

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

html_url

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

objects = <django.db.models.manager.Manager object>
ref

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

repo_name

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

request_url

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

time

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

app_webhook.shell_cmd module

执行shell cmd

class app_webhook.shell_cmd.Cmds(cmds)[source]

Bases: object

run()[source]

app_webhook.tests module

app_webhook.urls module

app_webhook.views module

app_webhook.views.alive(request)[source]

app_webhook.views_github module

app_webhook.views_github.github_hook(request)[source]
app_webhook.views_github.list_githublog(request, count=5)[source]

app_webhook.views_github_fei module

For github fei repo A Django demo

app_webhook.views_github_fei.fei_project(request)[source]

app_webhook.views_hhxx module

接受从hhxx.me发出的webhook

app_webhook.views_hhxx.hhxx_hook(request)[source]

Module contents

一个用于接受webhook的app |hook.py 定义了每个不同的webhook |views_xxx.py处理实际的请求,例:

:views_github.py仅处理来自github的webhook :views_hhxx.py仅处理来自hhxx.com的webhook