Slide 1

Slide 1 text

Say It With Bots 1 Mariatta Wijaya BotSE 2022

Slide 2

Slide 2 text

Mariatta Wijaya @mariatta 2 Python Core Developer she/her Vancouver, Canada UTC - 7 Senior Developer Relations Engineer @ Google Cloud

Slide 3

Slide 3 text

Tutorial Goal 3 Implementing a GitHub Bot in Python by: - Using the Gidgethub Library - Reading the GitHub API documentation @mariatta Say It With Bots! https://github-app-tutorial.readthedocs.io/en/latest/ https://gidgethub.readthedocs.io/en/latest/

Slide 4

Slide 4 text

Gidgethub Library 4 An async library for working with GitHub APIs and Webhook events @mariatta Say It With Bots! Documentation: https://gidgethub.readthedocs.io/en/latest/ Source Code: https://github.com/brettcannon/gidgethub Requires Python 3.6+ Convenient utilities for: - GitHub API and GitHub App authentication - GitHub Webhook event validation - GitHub URL formatting, including enterprise GitHub URLs - GitHub webhook event handlers

Slide 5

Slide 5 text

GitHub Bot Example 5 @mariatta Say It With Bots!

Slide 6

Slide 6 text

How Does a GitHub Bot works? 6 Webhook Events GitHub API calls @mariatta Say It With Bots! 🤖

Slide 7

Slide 7 text

Webhook Events from GitHub 7 Webhook Events @mariatta Say It With Bots! 🤖 https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads HTTP POST requests from GitHub to your web service GitHub specific headers: X-GitHub Event, X-GitHub-Delivery, X-Hub- Signature, User-Agent Different Payload depending on the event Pull Request events Issue events Pull Request review events Etc

Slide 8

Slide 8 text

Calling GitHub APIs 8 @mariatta Say It With Bots! 🤖 https://docs.github.com/en/rest Calling GitHub API endpoints from your web service Authenticate to GitHub using OAuth token Various HTTP methods: GET, POST, PUT, etc GitHub API calls Opening an issue Closing an issue Etc Commenting on an issue/ PR

Slide 9

Slide 9 text

Calling GitHub APIs 9 @mariatta Say It With Bots! https://github-app-tutorial.readthedocs.io/en/latest/gh-api-cmd-line.html Pre-requisites (see above link for more details): - Install the libraries - Get a personal access token Using Gidgethub

Slide 10

Slide 10 text

Calling GitHub APIs 10 @mariatta Say It With Bots! https://docs.github.com/en/rest/issues/issues#create-an-issue Creating an Issue POST /repos/{owner}/{repo}/issues Body Parameters title string or integer Required The title of the issue body string The contents of the issue response = await gh.post( '/repos/{owner}/{repo}/issues ', data={ 'title': 'Found a bug!', 'body': 'Use more emoji!', } ) https://github-app-tutorial.readthedocs.io/en/latest/gh-api-cmd-line.html

Slide 11

Slide 11 text

Calling GitHub APIs 11 @mariatta Say It With Bots! https://docs.github.com/en/rest/issues/comments#create-an-issue-comment Create an issue comment POST /repos/{owner}/{repo}/issues/{issue_number}/comments Body Parameters body string Required The contents of the issue response = await gh.post( '/repos/{owner}/{repo}/issues/{issue_number}/comments', data={ 'body': 'Thank you for reporting this issue', } ) https://github-app-tutorial.readthedocs.io/en/latest/gh-api-cmd-line.html

Slide 12

Slide 12 text

GitHub Webhook Events 12 @mariatta Say It With Bots! https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads Webhook payload object common properties action string Most web hook payloads contain an action property that contains the specific activity that triggered the event. sender object The user that triggered the event. repository object The repository where the event occurred. installation object The GitHub App installation.

Slide 13

Slide 13 text

GitHub Webhook Events 13 @mariatta Say It With Bots! https://github-app-tutorial.readthedocs.io/en/latest/responding-to-webhook.html Activity related to an issue action string One of opened, edited, deleted, …. issue object The issue itself issues { "action": "opened", "issue": { "url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1", "repository_url": "https://api.github.com/repos/Codertocat/Hello-World", “html_url": "https://github.com/Codertocat/Hello-World/issues/1", "id": 444500041, "node_id": "MDU6SXNzdWU0NDQ1MDAwNDE=", "number": 1, "title": "Spelling error in the README file", ... } }

Slide 14

Slide 14 text

GitHub Webhook Events 14 @mariatta Say It With Bots! https://github-app-tutorial.readthedocs.io/en/latest/responding-to-webhook.html webhook event handler in gidgethub @router.register("issue", action="opened") async def issue_opened(event, gh, *args, **kwargs): ... response = await gh.post( f"{issue_url}/comments", data={"body": msg}, oauth_token=installation_access_token["token"], )

Slide 15

Slide 15 text

GitHub App 15 @mariatta Say It With Bots! https://docs.github.com/en/developers/apps/building-github-apps/creating-a-github-app Create an “app” on GitHub - Specify your webhook URL - Subscribe to the webhook events - Generate private key for authentication

Slide 16

Slide 16 text

GitHub App 16 @mariatta Say It With Bots! Working examples: https://github.com/Mariatta/github_app_boilerplate More thorough tutorial: https://github-app-tutorial.readthedocs.io/en/latest/ (takes about 1 - 2 hours)

Slide 17

Slide 17 text

THANK YOU 17 @mariatta Say It With Bots! BotSE 2022