Skip to content

从script传递参数,增加获取github event name #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions webhookit/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,23 @@ def post(self):
# webhook configs
config = WEBHOOKIT_CONFIGURE or {}

event_name = parser.get_event_name(self.request, data) or ''
repo_name = parser.get_repo_name(data) or ''
repo_branch = parser.get_repo_branch(data) or ''
webhook_key = '%s/%s' % (repo_name, repo_branch)
params = {
'event_name': event_name,
'repo_name': repo_name,
'repo_branch': repo_branch
}
# 需要出发操作的服务器 server 数组
servers = config.get(webhook_key, [])
if servers and len(servers) > 0:
# 存在 server,需要执行 shell 脚本
cnt = 0
for s in servers:
# 遍历执行
s['SCRIPT'] = s.get('SCRIPT').format(**params)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string.format(dict) 的输出是什么?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外这里可能有一个值引用修改的问题:

s['SCRIPT'] = s.get('SCRIPT').format(**params)

直接修改了原始配置中的 script 值,后面每次执行都会追加参数进去,这个你也可以再确认一下!

utils.log('Starting to execute %s' % s.get('SCRIPT', ''))
utils.do_webhook_shell(s, data)
webhook_cnt += 1
Expand Down
8 changes: 8 additions & 0 deletions webhookit/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
'''


# event name
def get_event_name(request, hook_data):
return request.headers.get('X-GitHub-Event') \
or request.get('X-Gitlab-Event') \
or hook_data.get('hook_name') \
or request.get('X-Gogs-Event')


# repo name
def get_repo_name(hook_data):
return hook_data.get('repository', {}).get('name', '') \
Expand Down