{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "from ghapi.core import *\n", "import os" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# GhApi\n", "\n", "> A delightful and complete interface to GitHub's amazing API" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`GhApi` provides 100% always-updated coverage of the entire [GitHub API](https://docs.github.com/en/free-pro-team@latest/rest). Because we automatically convert the [OpenAPI spec](https://docs.github.com/en/free-pro-team@latest/rest/overview/openapi-description) to a Pythonic API, `GhApi` is always up to date with the latest changes to GitHub APIs.\n", "\n", "Using `GhApi`, you can do anything that you can do through the GitHub web interface or through the `git` client, such as:\n", "\n", "- Open, list, comment on, or modify [issues](https://guides.github.com/features/issues/) or [pull requests](https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/about-pull-requests)\n", "- Create, list, or modify [git tags](https://git-scm.com/book/en/v2/Git-Basics-Tagging) or [GitHub releases](https://docs.github.com/en/free-pro-team@latest/github/administering-a-repository/managing-releases-in-a-repository), including uploading release assets\n", "- Configure and run GitHub [Actions](https://github.com/features/actions) and [webhooks](https://docs.github.com/en/free-pro-team@latest/developers/webhooks-and-events/about-webhooks)\n", "- Set up GitHub [users](https://docs.github.com/en/free-pro-team@latest/rest/reference/users) and [organizations](https://docs.github.com/en/free-pro-team@latest/github/setting-up-and-managing-organizations-and-teams/about-organizations)\n", "- Manage your [deployments](https://docs.github.com/en/free-pro-team@latest/rest/guides/delivering-deployments)\n", "- ...and much, much more." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Either `pip install ghapi` or `conda install -c fastai ghapi`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How to use" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Throughout this documentation, you will see code inputs and outputs shown in this format:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We recommend reading the documentation on the [official site](https://ghapi.fast.ai/), rather than on GitHub, since not all the functionality described on this page is available through the GitHub viewer.\n", "\n", "All of the documentation is available directly as Jupyter Notebooks, for instance the current page you're reading is available as a notebook [here](https://github.com/fastai/ghapi/blob/master/index.ipynb). To open any page as an interactive notebook in Google Colab, click the *Colab* badge at the top of the page." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "github_token = os.environ['FASTRELEASE_TOKEN']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To access the GitHub API, first create a `GhApi` object:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "api = GhApi()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Every part of the API includes documentation directly in the `api` object itself. For instance, here's how to explore the groups of functionality provided by the API by displaying the object:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "- [apps](https://docs.github.com/en/free-pro-team@latest/rest/reference/apps)\n", "- [oauth-authorizations](https://docs.github.com/en/free-pro-team@latest/rest/reference/oauth-authorizations)\n", "- [codes-of-conduct](https://docs.github.com/en/free-pro-team@latest/rest/reference/codes-of-conduct)\n", "- [emojis](https://docs.github.com/en/free-pro-team@latest/rest/reference/emojis)\n", "- [enterprise-admin](https://docs.github.com/en/free-pro-team@latest/rest/reference/enterprise-admin)\n", "- [billing](https://docs.github.com/en/free-pro-team@latest/rest/reference/billing)\n", "- [activity](https://docs.github.com/en/free-pro-team@latest/rest/reference/activity)\n", "- [gists](https://docs.github.com/en/free-pro-team@latest/rest/reference/gists)\n", "- [gitignore](https://docs.github.com/en/free-pro-team@latest/rest/reference/gitignore)\n", "- [issues](https://docs.github.com/en/free-pro-team@latest/rest/reference/issues)\n", "- [licenses](https://docs.github.com/en/free-pro-team@latest/rest/reference/licenses)\n", "- [markdown](https://docs.github.com/en/free-pro-team@latest/rest/reference/markdown)\n", "- [meta](https://docs.github.com/en/free-pro-team@latest/rest/reference/meta)\n", "- [orgs](https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs)\n", "- [actions](https://docs.github.com/en/free-pro-team@latest/rest/reference/actions)\n", "- [interactions](https://docs.github.com/en/free-pro-team@latest/rest/reference/interactions)\n", "- [migrations](https://docs.github.com/en/free-pro-team@latest/rest/reference/migrations)\n", "- [projects](https://docs.github.com/en/free-pro-team@latest/rest/reference/projects)\n", "- [repos](https://docs.github.com/en/free-pro-team@latest/rest/reference/repos)\n", "- [teams](https://docs.github.com/en/free-pro-team@latest/rest/reference/teams)\n", "- [reactions](https://docs.github.com/en/free-pro-team@latest/rest/reference/reactions)\n", "- [rate-limit](https://docs.github.com/en/free-pro-team@latest/rest/reference/rate-limit)\n", "- [checks](https://docs.github.com/en/free-pro-team@latest/rest/reference/checks)\n", "- [code-scanning](https://docs.github.com/en/free-pro-team@latest/rest/reference/code-scanning)\n", "- [git](https://docs.github.com/en/free-pro-team@latest/rest/reference/git)\n", "- [pulls](https://docs.github.com/en/free-pro-team@latest/rest/reference/pulls)\n", "- [scim](https://docs.github.com/en/free-pro-team@latest/rest/reference/scim)\n", "- [search](https://docs.github.com/en/free-pro-team@latest/rest/reference/search)\n", "- [users](https://docs.github.com/en/free-pro-team@latest/rest/reference/users)" ], "text/plain": [ "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "api" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we can explore the endpoints provided by the API in each group, e.g. for the `git` group:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "- [create_blob](https://docs.github.com/rest/reference/git#create-a-blob)\n", "- [get_blob](https://docs.github.com/rest/reference/git#get-a-blob)\n", "- [create_commit](https://docs.github.com/rest/reference/git#create-a-commit)\n", "- [get_commit](https://docs.github.com/rest/reference/git#get-a-commit)\n", "- [list_matching_refs](https://docs.github.com/rest/reference/git#list-matching-references)\n", "- [get_ref](https://docs.github.com/rest/reference/git#get-a-reference)\n", "- [create_ref](https://docs.github.com/rest/reference/git#create-a-reference)\n", "- [update_ref](https://docs.github.com/rest/reference/git#update-a-reference)\n", "- [delete_ref](https://docs.github.com/rest/reference/git#delete-a-reference)\n", "- [create_tag](https://docs.github.com/rest/reference/git#create-a-tag-object)\n", "- [get_tag](https://docs.github.com/rest/reference/git#get-a-tag)\n", "- [create_tree](https://docs.github.com/rest/reference/git#create-a-tree)\n", "- [get_tree](https://docs.github.com/rest/reference/git#get-a-tree)" ], "text/plain": [ "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "api.git" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's how to learn about an endpoint you want to use, e.g.:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "[git/get_ref](https://docs.github.com/rest/reference/git#get-a-reference)(ref, repo, owner): *Get a reference*" ], "text/plain": [ "[git/get_ref](https://docs.github.com/rest/reference/git#get-a-reference)(ref, repo, owner): *Get a reference*" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "api.git.get_ref" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Jupyter Notebook full tab completion, parameter lists, etc are provided for all endpoints. Endpoints are called as standard Python methods:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "- ref: refs/heads/master\n", "- node_id: MDM6UmVmMjI1NDYwNTk5OnJlZnMvaGVhZHMvbWFzdGVy\n", "- url: https://api.github.com/repos/fastai/fastcore/git/refs/heads/master\n", "- object: \n", " - sha: 0b188e9a4f200c5ad93c692bb10aa6e7ceb10d4c\n", " - type: commit\n", " - url: https://api.github.com/repos/fastai/fastcore/git/commits/0b188e9a4f200c5ad93c692bb10aa6e7ceb10d4c" ], "text/plain": [ "- ref: refs/heads/master\n", "- node_id: MDM6UmVmMjI1NDYwNTk5OnJlZnMvaGVhZHMvbWFzdGVy\n", "- url: https://api.github.com/repos/fastai/fastcore/git/refs/heads/master\n", "- object: \n", " - sha: 0b188e9a4f200c5ad93c692bb10aa6e7ceb10d4c\n", " - type: commit\n", " - url: https://api.github.com/repos/fastai/fastcore/git/commits/0b188e9a4f200c5ad93c692bb10aa6e7ceb10d4c" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "api.git.get_ref(owner='fastai', repo='fastcore', ref='heads/master')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For access to authenticated endpoints, pass a [GitHub token](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token). You can also pass any parameters you want auto-inserted into relevant methods, such as `owner` and `repo`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "api = GhApi(owner='fastai', repo='fastcore', token=github_token)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now repeat the previous method, but only need to pass `ref`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "- ref: refs/heads/master\n", "- node_id: MDM6UmVmMjI1NDYwNTk5OnJlZnMvaGVhZHMvbWFzdGVy\n", "- url: https://api.github.com/repos/fastai/fastcore/git/refs/heads/master\n", "- object: \n", " - sha: 0b188e9a4f200c5ad93c692bb10aa6e7ceb10d4c\n", " - type: commit\n", " - url: https://api.github.com/repos/fastai/fastcore/git/commits/0b188e9a4f200c5ad93c692bb10aa6e7ceb10d4c" ], "text/plain": [ "- ref: refs/heads/master\n", "- node_id: MDM6UmVmMjI1NDYwNTk5OnJlZnMvaGVhZHMvbWFzdGVy\n", "- url: https://api.github.com/repos/fastai/fastcore/git/refs/heads/master\n", "- object: \n", " - sha: 0b188e9a4f200c5ad93c692bb10aa6e7ceb10d4c\n", " - type: commit\n", " - url: https://api.github.com/repos/fastai/fastcore/git/commits/0b188e9a4f200c5ad93c692bb10aa6e7ceb10d4c" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "api.git.get_ref('heads/master')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we've provided our token, we can use authenticated endpoints such as creating an issue:\n", "\n", "```python\n", "issue = api.issues.create(\"Remember to check out GhApi!\")\n", "```\n", "\n", "Since we've now checked out GhApi, let's close this issue. 😎\n", "\n", "```python\n", "api.issues.update(issue.number, state='closed')\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }