Slide 1

Slide 1 text

PyLadies Seattle - April 2020 Build Your Own GitHub Bot With Gidgethub @mariatta

Slide 2

Slide 2 text

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


Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Thanks! @mariatta | [email protected] PyLadies Seattle - April 2020
 Build Your Own GitHub Bot with Gidgethub
 @mariatta