自动编辑存储库 Webhook 的创建。
代码示例:
import requests
repo_url = "https://api.github.com/repos/username/repo-name/hooks"
payload = {
"name": "web",
"active": True,
"config": {
"url": "https://example.com/webhook",
"content_type": "json"
}
}
headers = {
"Authorization": "token "
}
# 发送 POST 请求创建 Webhook
response = requests.post(repo_url, json=payload, headers=headers)
# 如果响应状态码为 201 表示创建成功,则自动编辑 Webhook
if response.status_code == 201:
webhook_id = response.json()["id"]
edit_url = f"{repo_url}/{webhook_id}"
edit_payload = {
"config": {
"url": "https://new-example.com/webhook",
"content_type": "json"
}
}
response = requests.patch(edit_url, json=edit_payload, headers=headers)
if response.status_code == 200:
print("Webhook created and edited successfully.")
else:
print(f"An error occurred while editing the webhook: {response.text}")
else:
print(f"An error occurred while creating the webhook: {response.text}")
from github import Github
access_token = ""
g = Github(access_token)
repo = g.get_repo("username/repo-name")
hook_config = {
"url": "https://example.com/webhook",
"content_type": "json"
}
hook = repo.create_hook(name="web", config=hook_config, events=["push"], active=True)
# 获取 Webhook ID 并自动编辑
if hook.id:
hook.edit(config={
"url": "https://new-example.com/webhook",
"content_type": "json"
})
print("Webhook created and edited successfully.")
else:
print("An error occurred while creating the webhook.")