-
-
Notifications
You must be signed in to change notification settings - Fork 64
Implement a simple github webook server to build the doc. #101
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
"""Github hook server. | ||
|
||
This is a simple HTTP server handling Github Webhooks requests to | ||
build the doc when needed. | ||
|
||
It needs a GH_SECRET environment variable to be able to receive hooks | ||
on `/hook/github`. | ||
|
||
Its logging can be configured by giving a yaml file path to the | ||
`--logging-config` argument. | ||
|
||
By default the loglevel is `DEBUG` on `stderr`, the default config can | ||
be found in the code so one can bootstrap a different config from it. | ||
""" | ||
|
||
from pathlib import Path | ||
import argparse | ||
import asyncio | ||
import logging.config | ||
import os | ||
|
||
from aiohttp import web | ||
from gidgethub import sansio | ||
import yaml | ||
|
||
from build_docs import VERSIONS | ||
|
||
|
||
__version__ = "0.0.1" | ||
|
||
DEFAULT_LOGGING_CONFIG = """ | ||
--- | ||
|
||
version: 1 | ||
disable_existing_loggers: false | ||
formatters: | ||
normal: | ||
format: '%(asctime)s - %(levelname)s - %(message)s' | ||
handlers: | ||
stderr: | ||
class: logging.StreamHandler | ||
stream: ext://sys.stderr | ||
level: DEBUG | ||
formatter: normal | ||
loggers: | ||
build_docs_server: | ||
level: DEBUG | ||
handlers: [stderr] | ||
aiohttp.access: | ||
level: DEBUG | ||
handlers: [stderr] | ||
aiohttp.client: | ||
level: DEBUG | ||
handlers: [stderr] | ||
aiohttp.internal: | ||
level: DEBUG | ||
handlers: [stderr] | ||
aiohttp.server: | ||
level: DEBUG | ||
handlers: [stderr] | ||
aiohttp.web: | ||
level: DEBUG | ||
handlers: [stderr] | ||
aiohttp.websocket: | ||
level: DEBUG | ||
handlers: [stderr] | ||
""" | ||
|
||
logger = logging.getLogger("build_docs_server") | ||
|
||
|
||
async def version(request): | ||
return web.json_response( | ||
{ | ||
"name": "docs.python.org Github handler", | ||
"version": __version__, | ||
"source": "https://github.com/python/docsbuild-scripts", | ||
} | ||
) | ||
|
||
|
||
async def child_waiter(app): | ||
while True: | ||
try: | ||
status = os.waitid(os.P_ALL, 0, os.WNOHANG | os.WEXITED) | ||
logger.debug("Child completed with status %s", str(status)) | ||
except ChildProcessError: | ||
await asyncio.sleep(600) | ||
|
||
|
||
async def start_child_waiter(app): | ||
app["child_waiter"] = asyncio.ensure_future(child_waiter(app)) | ||
|
||
|
||
async def stop_child_waiter(app): | ||
app["child_waiter"].cancel() | ||
|
||
|
||
async def hook(request): | ||
body = await request.read() | ||
event = sansio.Event.from_http( | ||
request.headers, body, secret=os.environ.get("GH_SECRET") | ||
) | ||
if event.event != "push": | ||
logger.debug("Received a %s event, nothing to do.", event.event) | ||
return web.Response() | ||
touched_files = ( | ||
set(event.data["head_commit"]["added"]) | ||
| set(event.data["head_commit"]["modified"]) | ||
| set(event.data["head_commit"]["removed"]) | ||
) | ||
if not any("Doc" in touched_file for touched_file in touched_files): | ||
logger.debug("No documentation file modified, ignoring.") | ||
return web.Response() # Nothing to do | ||
branch = event.data["ref"].split("/")[-1] | ||
known_branches = {version.branch for version in VERSION} | ||
if branch not in known_branches: | ||
logger.warning("Ignoring a change in branch %s (unknown branch)", branch) | ||
return web.Response() # Nothing to do | ||
logger.debug("Forking a build for branch %s", branch) | ||
pid = os.fork() | ||
if pid == 0: | ||
os.execl( | ||
"/usr/bin/env", | ||
"/usr/bin/env", | ||
"python", | ||
"build_docs.py", | ||
"--branch", | ||
branch, | ||
) | ||
else: | ||
return web.Response() | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
parser.add_argument("--path", help="Unix socket to listen for connections.") | ||
parser.add_argument("--port", help="Local port to listen for connections.") | ||
parser.add_argument( | ||
"--logging-config", | ||
help="yml file containing a Python logging dictconfig, see README.md", | ||
) | ||
return parser.parse_args() | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
logging.config.dictConfig( | ||
yaml.load( | ||
Path(args.logging_config).read_text() | ||
if args.logging_config | ||
else DEFAULT_LOGGING_CONFIG, | ||
Loader=yaml.SafeLoader, | ||
) | ||
) | ||
app = web.Application() | ||
app.on_startup.append(start_child_waiter) | ||
app.on_cleanup.append(stop_child_waiter) | ||
app.add_routes( | ||
[ | ||
web.get("/", version), | ||
web.post("/hooks/github", hook), | ||
] | ||
) | ||
web.run_app(app, path=args.path, port=args.port) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
sentry-sdk | ||
aiohttp | ||
gidgethub | ||
jinja2 | ||
pyyaml | ||
sentry-sdk | ||
zc.lockfile |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.