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

Automating GitHub Workflow with Bots

Mariatta
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

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
  2. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with

    Bots
 @mariatta Hi! ! @mariatta Platform Engineer Python Core Developer
  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
  4. 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?
  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?
  6. 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
  7. 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
  8. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with

    Bots
 @mariatta Building the GitHub Bot
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with

    Bots
 @mariatta Say thanks? Source: https://developer.github.com/v3/issues/comments/#create-a-comment
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with

    Bots
 @mariatta Build your own GitHub Bot
  27. 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
  28. 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
  29. Code & Coffee Pop-Up Richmond 
 Automating GitHub Workflow with

    Bots
 @mariatta GitHub API docs https://developer.github.com/webhooks/ https://developer.github.com/v3/