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

Say It With Bots!

Say It With Bots!

In this workshop, you will learn how to build a bot on GitHub using Python and the gidgethub library. By automating your workflow using GitHub bots, you can improve your productivity and focus on the important tasks. You will learn about GitHub APIs, GitHub Apps, and authentication throughout this tutorial.

Presented for BotSE 2022

http://botse.org/

Mariatta

May 09, 2022
Tweet

More Decks by Mariatta

Other Decks in Programming

Transcript

  1. Mariatta Wijaya @mariatta 2 Python Core Developer she/her Vancouver, Canada

    UTC - 7 Senior Developer Relations Engineer @ Google Cloud
  2. 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/
  3. 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
  4. How Does a GitHub Bot works? 6 Webhook Events GitHub

    API calls @mariatta Say It With Bots! 🤖
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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.
  11. 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", ... } }
  12. 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"], )
  13. 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
  14. 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)