Merge pull request #25 from tituspijean/enh-webhook-pr

Handle github-actions PRs to trigger CI
This commit is contained in:
Alexandre Aubin 2021-09-22 15:32:30 +02:00 committed by GitHub
commit 31421c52e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

26
run.py
View file

@ -1021,8 +1021,8 @@ async def github(request):
# We expect issue comments (issue = also PR in github stuff...)
# - *New* comments
# - On issue/PRs which are still open
if hook_type != "issue_comment" \
or hook_infos["action"] != "created" \
if hook_type == "issue_comment":
if hook_infos["action"] != "created" \
or hook_infos["issue"]["state"] != "open" \
or "pull_request" not in hook_infos["issue"]:
# Nothing to do but success anyway (204 = No content)
@ -1048,9 +1048,25 @@ async def github(request):
if not await is_user_in_organization(hook_infos["comment"]["user"]["login"]):
# Unauthorized
abort(403, "Unauthorized")
# Fetch the PR infos (yeah they ain't in the initial infos we get @_@)
pr_infos_url = hook_infos["issue"]["pull_request"]["url"]
elif hook_type == "pull_request":
if hook_infos["action"] != "opened":
# Nothing to do but success anyway (204 = No content)
abort(204, "Nothing to do")
# We only accept PRs that are created by github-action bot
if hook_infos["pull_request"]["user"]["login"] != "github-actions[bot]" \
or not hook_infos["pull_request"]["head"]["ref"].startswith("ci-auto-update-"):
# Unauthorized
abort(204, "Nothing to do")
# Fetch the PR infos (yeah they ain't in the initial infos we get @_@)
pr_infos_url = hook_infos["pull_request"]["url"]
else:
# Nothing to do but success anyway (204 = No content)
abort(204, "Nothing to do")
async with aiohttp.ClientSession() as session:
async with session.get(pr_infos_url) as resp:
pr_infos = await resp.json()
@ -1075,8 +1091,10 @@ async def github(request):
# Answer with comment with link+badge for the job
async def comment(body):
if hook_type == "issue_comment":
comments_url = hook_infos["issue"]["comments_url"]
else:
comments_url = hook_infos["pull_request"]["comments_url"]
token = open("./github_bot_token").read().strip()
async with aiohttp.ClientSession(headers={"Authorization": f"token {token}"}) as session: