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

Build Your Own GitHub Bot with Gidgethub

6b6e72d297aa0270654a0d4575f1287e?s=47 Mariatta
April 23, 2020
33

Build Your Own GitHub Bot with Gidgethub

An introduction about bots and automations on GitHub, and how to build one for yourself using gidgethub library.
Gidgethub version 4.0 provides support for GitHub actions, and version 4.1.0 provides support for GitHub Apps.
Check my detailed tutorial: https://github-app-tutorial.readthedocs.io/en/latest/
Presented as a lightning talk at PyLadies Seattle on April 2020

6b6e72d297aa0270654a0d4575f1287e?s=128

Mariatta

April 23, 2020
Tweet

Transcript

  1. PyLadies Seattle - April 2020 Build Your Own GitHub Bot

    With Gidgethub @mariatta
  2. What are GitHub bots? Applications that runs automations on GitHub

    using web hooks and APIs Build Your Own GitHub Bot with Gidgethub
 @mariatta PyLadies Seattle - April 2020

  3. GitHub Bots IRL PyLadies Seattle - April 2020
 Build Your

    Own GitHub Bot with Gidgethub
 @mariatta
  4. GitHub Bots IRL PyLadies Seattle - April 2020
 Build Your

    Own GitHub Bot with Gidgethub
 @mariatta
  5. How Does it Work? Pull request (opened, closed, labeled) Status

    (pending, success, failure) Pull request review (submitted, dismissed) … Events GitHub Webservice Docs: https://developer.github.com/webhooks PyLadies Seattle - April 2020
 Build Your Own GitHub Bot with Gidgethub
 @mariatta
  6. How Does it Work? Create a pull request Merge a

    pull request Comment on a pull request Apply labels to pull request … APIs Webservice GitHub Docs: https://developer.github.com/v3 PyLadies Seattle - April 2020
 Build Your Own GitHub Bot with Gidgethub
 @mariatta
  7. Gidgethub Async Python Library for working with GitHub APIs Works

    on Python 3.6+ Support for aiohttp, tornado, and httpx License: Apache 2.0 PyLadies Seattle - April 2020
 Build Your Own GitHub Bot with Gidgethub
 @mariatta
  8. Gidgethub for REST API calls import requests # construct the

    request headers request_headers = { "User-Agent": "cool-octocat-app", "Authorization": "token abcde", "Accept": "application/vnd.github.v3+json" } # make an API call url = "https://api.github.com/repos/mariatta/" "gidgethub/strange-relationship/issues" response = requests.get( url, headers=request_headers ) from gidgethub.aiohttp import GitHubAPI async with aiohttp.ClientSession() as session: gh = GitHubAPI( session, "cool-octocat-app", oauth_token="abcde" ) response = await gh.getitem( '/repos/mariatta/strange-relationship/ issues' ) With requests With gidgethub / aiohttp PyLadies Seattle - April 2020
 Build Your Own GitHub Bot with Gidgethub
 @mariatta
  9. Gidgethub for responding to webhooks @router.register("pull_request", action="opened") @router.register("pull_request", action="reopened") async

    def close_pr(event, gh, *args, **kwargs): await gh.patch( event.data[“pull_request"]["url"], data= {"state": "closed"} ) • Provides routings for handling web hook events • Verifies the webhook secret and payload delivery headers PyLadies Seattle - April 2020
 Build Your Own GitHub Bot with Gidgethub
 @mariatta
  10. Support for GitHub Apps (new in v 4.1.0) from gidgethub.apps

    import get_jwt private_key = """-----BEGIN RSA PRIVATE KEY----- zBgqFIin/uQEb0he006F9pNC6Kga0AMY5b0cCdZ4ge9qyFro2eVA ... -----END RSA PRIVATE KEY----- """ token = get_jwt(app_id=123, private_key=private_key) data = gh.getitem( "/app/installations", jwt=token, accept="application/vnd.github.machine-man-preview+json", ) PyLadies Seattle - April 2020
 Build Your Own GitHub Bot with Gidgethub
 @mariatta
  11. Support for GitHub Apps (new in v 4.1.0) from gidgethub.apps

    import get_installation_access_token private_key = """-----BEGIN RSA PRIVATE KEY----- zBgqFIin/uQEb0he006F9pNC6Kga0AMY5b0cCdZ4ge9qyFro2eVA ... -----END RSA PRIVATE KEY----- """ access_token_response = await get_installation_access_token( installation_id=123, app_id=456, private_key=private_key ) data = await gh.getitem("/rate_limit", oauth_token=access_token_response["token"]) PyLadies Seattle - April 2020
 Build Your Own GitHub Bot with Gidgethub
 @mariatta
  12. Support for GitHub Actions (new in v 4.0.0) gidgethub.actions.workspace() -

    Returns a pathlib.Path object representing the GitHub Workspace gidgethub.actions.event() - Returns the webhook event data gidgethub.actions.command() - Issue a workflow command PyLadies Seattle - April 2020
 Build Your Own GitHub Bot with Gidgethub
 @mariatta
  13. Build Your Own Bot https://github-app-tutorial.readthedocs.io/ https://gidgethub.readthedocs.io PyLadies Seattle - April

    2020
 Build Your Own GitHub Bot with Gidgethub
 @mariatta
  14. Thanks! @mariatta | mariatta@python.org PyLadies Seattle - April 2020
 Build

    Your Own GitHub Bot with Gidgethub
 @mariatta