Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Automating GitHub Workflow with Bots

Mariatta
PRO
February 28, 2018

Automating GitHub Workflow with Bots

GitHub provides a great platform for collaborating and maintaining open source projects.
Take it to the next level by creating custom GitHub bots.
By delegating some of the chores to the bots, project maintainers get to spend more time developing and collaborating with other contributors.
Learn how the Core Python team automated their workflow by building GitHub bots to help maintain one of the most popular open source projects, CPython.

Presented at Richmond Code and Coffee Pop-Up.

Mariatta
PRO

February 28, 2018
Tweet

More Decks by Mariatta

Other Decks in Programming

Transcript

  1. Automating GitHub Workflow with Bots
    @mariatta
    Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta

    View Slide

  2. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Hi! !
    @mariatta
    Platform Engineer
    Python Core Developer

    View Slide

  3. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Python
    github.com/python/cpython
    5000+ pull requests
    500+ contributors, 30 core devs

    View Slide

  4. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Boring chores

    View Slide

  5. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    For every pull request …
    CLA signed?
    Issue number included?
    News entry?
    Tests passed?
    Ready for review?
    Backport needed?

    View Slide

  6. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    For every pull request …
    CLA signed?
    Issue number included?
    News entry?
    Tests passed?
    Ready for review?
    Backport needed?

    View Slide

  7. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Meet CPython’s GitHub Bots
    https://github.com/the-knights-who-say-ni
    https://github.com/python/the-knights-who-say-ni
    the-knights-who-say-ni
    https://github.com/bedevere-bot
    https://github.com/python/bedevere
    bedevere
    https://github.com/miss-islington
    https://github.com/python/miss-islington
    miss-islington

    View Slide

  8. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    the-knights-who-say-ni

    View Slide

  9. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    bedevere

    View Slide

  10. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    bedevere

    View Slide

  11. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    bedevere

    View Slide

  12. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    bedevere

    View Slide

  13. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    bedevere

    View Slide

  14. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    miss-islington ⛏

    View Slide

  15. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    miss-islington ⛏

    View Slide

  16. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    miss-islington ⛏

    View Slide

  17. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Cherry-picking
    $ git fetch upstream
    $ git checkout -b backport-3384d38d-3.7 upstream/3.7
    $ git cherry-pick -x 3384d38d51a2c3450e742175db5d6d638fa5d2eb
    $ git push origin backport-3384d38d-3.7
    $ git checkout master
    $ git branch -D backport-3384d38d-3.7

    View Slide

  18. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    miss-islington ⛏

    View Slide

  19. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    miss-islington ⛏

    View Slide

  20. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Building the GitHub Bot

    View Slide

  21. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    GitHub Webhooks
    https://developer.github.com/webhooks/

    Events
    pull_request ("opened", "closed", "labeled")
    status ("pending", "success", "failure")
    pull_request_review ("submitted", "dismissed")
    GitHub
    Webservice

    View Slide

  22. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    GitHub REST API v3
    https://developer.github.com/v3/
    REST API
    Create a pull request
    Merge a pull request
    Apply label to pull request
    GitHub

    Webservice
    Comment on a pull request

    View Slide

  23. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    The webservice
    Python 3.6+

    pip install aiohttp
    http://aiohttp.readthedocs.io/en/stable/
    https://github.com/aio-libs/aiohttp
    aiohttp - Asyncio HTTP Server / Client
    by Andrew Svetlov

    View Slide

  24. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    The webservice
    Python 3.6+

    pip install gidgethub
    https://gidgethub.readthedocs.io/en/stable/
    https://github.com/brettcannon/gidgethub
    gidgethub - Async library for calling GitHub’s API
    by Brett Cannon

    View Slide

  25. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Receive the webhook

    @router.register("pull_request_review", action="submitted")
    async def pr_reviewed(event, gh, *args, **kwargs):
    reviewer = event.data["review"]["user"]["login"]
    approved = event.data["review"]["state"] == "approved"
    if approved and await util.is_core_dev(gh, reviewer):
    ...
    # merge the PR, leave a comment to say thanks
    Source: https://github.com/python/miss-islington/blob/master/miss_islington/status_change.py

    View Slide

  26. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Receive the webhook

    @router.register("pull_request_review", action="submitted")
    async def pr_reviewed(event, gh, *args, **kwargs):
    reviewer = event.data["review"]["user"]["login"]
    approved = event.data["review"]["state"] == "approved"
    if approved and await util.is_core_dev(gh, reviewer):
    ...
    # merge the PR, leave a comment to say thanks
    Source: https://github.com/python/miss-islington/blob/master/miss_islington/status_change.py

    View Slide

  27. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Receive the webhook

    @router.register("pull_request_review", action="submitted")
    async def pr_reviewed(event, gh, *args, **kwargs):
    reviewer = event.data["review"]["user"]["login"]
    approved = event.data["review"]["state"] == "approved"
    if approved and await util.is_core_dev(gh, reviewer):
    ...
    # merge the PR, leave a comment to say thanks
    Source: https://github.com/python/miss-islington/blob/master/miss_islington/status_change.py

    View Slide

  28. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Receive the webhook

    @router.register("pull_request_review", action="submitted")
    async def pr_reviewed(event, gh, *args, **kwargs):
    reviewer = event.data["review"]["user"]["login"]
    approved = event.data["review"]["state"] == "approved"
    if approved and await util.is_core_dev(gh, reviewer):
    ...
    # merge the PR, leave a comment to say thanks
    Source: https://github.com/python/miss-islington/blob/master/miss_islington/status_change.py

    View Slide

  29. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Receive the webhook

    @router.register("pull_request_review", action="submitted")
    async def pr_reviewed(event, gh, *args, **kwargs):
    reviewer = event.data["review"]["user"]["login"]
    approved = event.data["review"]["state"] == "approved"
    if approved and await util.is_core_dev(gh, reviewer):
    ...
    # merge the PR, leave a comment to say thanks
    Source: https://github.com/python/miss-islington/blob/master/miss_islington/status_change.py

    View Slide

  30. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Receive the webhook

    @router.register("pull_request_review", action="submitted")
    async def pr_reviewed(event, gh, *args, **kwargs):
    reviewer = event.data["review"]["user"]["login"]
    approved = event.data["review"]["state"] == "approved"
    if approved and await util.is_core_dev(gh, reviewer):
    ...
    # merge the PR, leave a comment to say thanks
    Source: https://github.com/python/miss-islington/blob/master/miss_islington/status_change.py

    View Slide

  31. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Receive the webhook

    @router.register("pull_request_review", action="submitted")
    async def pr_reviewed(event, gh, *args, **kwargs):
    reviewer = event.data["review"]["user"]["login"]
    approved = event.data["review"]["state"] == "approved"
    if approved and await util.is_core_dev(gh, reviewer):
    ...
    # merge the PR, leave a comment to say thanks
    Source: https://github.com/python/miss-islington/blob/master/miss_islington/status_change.py

    View Slide

  32. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Say thanks?

    Source: https://developer.github.com/v3/issues/comments/#create-a-comment

    View Slide

  33. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Say thanks

    pr_number = 5590
    data = {"body": "Thanks, @Mariatta!"}
    await gh.post(f"/repos/python/cpython/issues/{pr_number}/comments",
    data=data)
    Source: https://github.com/python/miss-islington/blob/master/miss_islington/status_change.py

    View Slide

  34. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Say thanks

    pr_number = 5590
    data = {"body": "Thanks, @Mariatta!"}
    await gh.post(f"/repos/python/cpython/issues/{pr_number}/comments",
    data=data)
    Source: https://github.com/python/miss-islington/blob/master/miss_islington/status_change.py

    View Slide

  35. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Merge the Pull Request?

    Source: https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button

    View Slide

  36. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Merge the Pull Request

    pr_number = 5590
    await gh.put(f"/repos/python/cpython/pulls/{pr_number}/merge",
    data={"commit_title": "Fix typo in f-strings docs",
    "commit_message": "Shorten the comment to: 'using integer
    format specifier'"),
    "sha": sha,
    "merge_method": "squash"
    }
    )
    Source: https://github.com/python/miss-islington/blob/master/miss_islington/status_change.py

    View Slide

  37. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Merge the Pull Request

    pr_number = 5590
    await gh.put(f"/repos/python/cpython/pulls/{pr_number}/merge",
    data={"commit_title": "Fix typo in f-strings docs",
    "commit_message": "Shorten the comment to: 'using integer
    format specifier'"),
    "sha": sha,
    "merge_method": "squash"
    }
    )
    Source: https://github.com/python/miss-islington/blob/master/miss_islington/status_change.py

    View Slide

  38. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Build your own GitHub Bot

    View Slide

  39. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Open Source
    https://github.com/the-knights-who-say-ni
    https://github.com/python/the-knights-who-say-ni
    the-knights-who-say-ni
    https://github.com/bedevere-bot
    https://github.com/python/bedevere
    bedevere
    https://github.com/miss-islington
    https://github.com/python/miss-islington
    miss-islington

    View Slide

  40. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    Dependencies
    http://aiohttp.readthedocs.io/en/stable/
    https://github.com/aio-libs/aiohttp
    aiohttp - Asyncio HTTP Server / Client
    https://gidgethub.readthedocs.io/en/stable/
    https://github.com/brettcannon/gidgethub
    gidgethub - Async library for calling GitHub’s API
    by Andrew Svetlov
    by Brett Cannon

    View Slide

  41. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    GitHub API docs
    https://developer.github.com/webhooks/
    https://developer.github.com/v3/

    View Slide

  42. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with Bots
 @mariatta
    @mariatta
    Thanks!
    [email protected]

    mariatta.ca

    View Slide