{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#default_exp cli" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "from fastcore.utils import *\n", "import ghapi.core as gh,inspect\n", "from ghapi.core import *\n", "from collections import defaultdict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Command line interface\n", "> Access to the GitHub API from the command line" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use `GhApi` via the command line, and can access nearly everything in the [GitHub API](https://docs.github.com/en/free-pro-team@latest/rest).\n", "\n", "There are three commands provided. For most people, `ghapi` will be the easiest to use. All three commands take the following parameters:\n", "\n", "- `--help`: list all parameters to this command and endpoint\n", "- `--headers`: A list of extra headers to pass, JSON-encoded\n", "- `--token`: A GitHub authentation token\n", "- `--debug`: Print requests before sending them\n", "\n", "If you have an environment variable `GITHUB_TOKEN` defined, then it will be used for the token if `--token` is not passed. If neither is provided, then the commands will not be able to acces any authentication functionality.\n", "\n", "We'll now look at each of the three commands in turn." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The `ghapi` command" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get started with the `ghapi` command, first find the name of the operation you wish to perform. If you've used the Python API, then you already know the operation names - they're whatever you type in Python after \"`api.`\". To find the name of the operation you need, search the [full API reference](https://ghapi.fast.ai/fullapi.html) which contains information about every endpoint in the entire GitHub API. For instance, if you want to work with GitHub Issues, then searching for \"issues\" on the full reference page will take you to [this section](https://ghapi.fast.ai/fullapi.html#issues).\n", "\n", "The first operation listed there is [issues.list](https://docs.github.com/en/free-pro-team@latest/rest/reference/issues#list-issues-assigned-to-the-authenticated-user), shown with a link to the official GitHub documentation, and a list of parameters that the command accepts. You'll see the official docs list some parameters, such as \"`accept`\", which aren't listed in the GhApi reference - that's because GhApi automatically set some parameters for you.\n", "\n", "You'll also see that the parameter list on the official docs includes a column marked \"**in**\", which can be \"header\", \"query\", \"path\", or \"body\". The `ghapi` command handles that distinction for you, so you can ignore it for now." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def _parse_args(a):\n", " \"Extract positional and keyword arguments from `a`=`sys.argv`\"\n", " pos,kw = [],{}\n", " i=1\n", " while i\")\n", " call = f(pos, api)\n", " return call if kw.get('help', None) else call(*pos, **kw)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def _ghapi(arg, api):\n", " for part in arg.pop(0).split('.'): api = getattr(api,part)\n", " return api" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "#export\n", "def ghapi():\n", " \"Python backend for the `ghapi` command, which calls an endpoint by operation name\"\n", " res = _call_api(_ghapi)\n", " if isinstance(res, (gh._GhObj,dict)): print(res)\n", " elif res: print(inspect.signature(res))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To use `ghapi`, pass the method name (exactly the same as you'd use in the Python API) as the first parameter, followed by any positional parameters required, and then keyword arguments with \"`--`\" before each parameter name.\n", "\n", "For instance, [git.get_ref](https://ghapi.fast.ai/fullapi.html#git) takes three parameters: `owner`, `repo`, and `ref`. If we wish to pass the first two as positional parameters, and the last as a named argument, then we'd call:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "- ref: refs/heads/master\r\n", "- node_id: MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy\r\n", "- url: https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master\r\n", "- object: \r\n", " - sha: f086d17cc9ec8e99f466c613cab44f98abad1db6\r\n", " - type: commit\r\n", " - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/f086d17cc9ec8e99f466c613cab44f98abad1db6\r\n" ] } ], "source": [ "! ghapi git.get_ref fastai ghapi-test --ref heads/master" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(NB: The \"`!`\" before the examples such as the one above is only needed when calling the commands from Jupyer Notebook. In your terminal, do not include the \"`!`\" prefix.)\n", "\n", "If you pass just `--help` after the operation name, you'll see a full list of all parameters accepted, and a link to the official GitHub documentation." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "git.get_ref(owner, repo, ref)\r\n", "https://docs.github.com/rest/reference/git#get-a-reference\r\n" ] } ], "source": [ "! ghapi git.get_ref --help" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Helper methods are also supported, e.g:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(tag_name, branch='master', name=None, body='', draft=False, prerelease=False, files=None)\r\n" ] } ], "source": [ "! ghapi create_release --help" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The `ghpath` command" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you find the endpoint you want in the GitHub docs, rather than the GhApi reference, you'll see a `path` and a `verb` instead of an operation name. For example, here's the GitHub documentation for [git.getref](https://docs.github.com/en/free-pro-team@latest/rest/reference/git#get-a-reference). The white text on blue background section shows that the verb required is \"`GET`\". Next to that is the path, which is listed as \"`/repos/{owner}/{repo}/git/ref/{ref}`\".\n", "\n", "To call an endpoint when you have this information from the GitHub docs, use the `ghpath` command. The arguments are exactly the same as `ghapi`, except that instead of an operation name, you provide the path (which you can paste directly from the GitHub docs) and verb." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def _ghpath(arg, api): return api[arg.pop(0),arg.pop(0)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "#export\n", "def ghpath():\n", " \"Python backend for the `ghpath` command, which calls an endpoint by path\"\n", " print(_call_api(_ghpath) or '')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just like with `ghapi`, `ghpath` handles header, query, path and body parameters for you automatically.\n", "\n", "For instance, the previous `git.get_ref` command using `ghpath` is:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "- ref: refs/heads/master\r\n", "- node_id: MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy\r\n", "- url: https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master\r\n", "- object: \r\n", " - sha: f086d17cc9ec8e99f466c613cab44f98abad1db6\r\n", " - type: commit\r\n", " - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/f086d17cc9ec8e99f466c613cab44f98abad1db6\r\n" ] } ], "source": [ "! ghpath '/repos/{owner}/{repo}/git/ref/{ref}' get fastai ghapi-test --ref heads/master" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The `ghraw` command" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "#export\n", "def ghraw():\n", " \"Python backend for the `ghraw` command, which calls a fully-specified endpoint\"\n", " cmd,api,pos,kw = _api()\n", " if not pos: return print(f\"Usage: `{cmd}` operation \")\n", " print(api(*pos, **kw))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes, you just want to call an exact path directly, handling the path and query parameters yourself. The `ghraw` command provides that functionality. Like with `ghpath`, you pass the path and verb as the first two arguments, but now you have to fill in all the path parameters yourself, plus construct the query string yourself (if one is required). If you need to pass body parameters, use the `--data` parameter, which requires that you encode the request body appropriately (which is JSON-encoded for most GitHub endpoints).\n", "\n", "For instance, the `git.git_ref` example show in the previous two sections does not have any header, body, or query parameters, so we need only insert the path parameters, which results in this call:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "- ref: refs/heads/master\r\n", "- node_id: MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy\r\n", "- url: https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master\r\n", "- object: \r\n", " - sha: f086d17cc9ec8e99f466c613cab44f98abad1db6\r\n", " - type: commit\r\n", " - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/f086d17cc9ec8e99f466c613cab44f98abad1db6\r\n" ] } ], "source": [ "! ghraw /repos/fastai/ghapi-test/git/ref/heads/master get" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Shell completions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Shell completions are provided for `ghapi` behind the scenes by the `completion-ghapi` command (which is implemented with the `completion_ghapi` function). You probably won't need to call this yourself - it will be called for you as part by your shell." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "#export\n", "_TAB_COMPLETION=\"\"\"\n", "_do_ghapi_completions()\n", "{\n", " COMP=\"$(completion-ghapi \"${COMP_WORDS[1]}\")\"\n", " COMPREPLY=($COMP)\n", "}\n", "\n", "complete -F _do_ghapi_completions ghapi\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "#export\n", "def completion_ghapi():\n", " \"Python backend for `completion-ghapi` command\"\n", " if len(sys.argv) == 2 and sys.argv[1] == '--install':\n", " print(_TAB_COMPLETION)\n", " return\n", " *parts,final = (sys.argv[1] if len(sys.argv)>1 else '').split('.')\n", " call = GhApi()\n", " for part in parts: call = getattr(call,part)\n", " if hasattr(call,final): res = [final]\n", " else: res = [o for o in dir(call) if o.startswith(final) and not o.startswith('_')]\n", " pre = '.'.join(parts+[final])\n", " print(' '.join(pre + remove_prefix(o, final) for o in res))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "git\r\n" ] } ], "source": [ "! completion-ghapi git" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gists git gitignore\r\n" ] } ], "source": [ "! completion-ghapi gi" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "git.create_blob git.create_commit git.create_ref git.create_tag git.create_tree git.delete_ref git.get_blob git.get_commit git.get_ref git.get_tag git.get_tree git.list_matching_refs git.name git.update_ref git.verbs\r\n" ] } ], "source": [ "! completion-ghapi git." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "git.get_blob git.get_commit git.get_ref git.get_tag git.get_tree\r\n" ] } ], "source": [ "! completion-ghapi git.g" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tab completion" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can enable tab completion for `ghapi` by placing the following command at the end of your `~/.bashrc` or `~/.zshrc` file:\n", "\n", "```bash\n", "eval \"$(completion-ghapi --install)\"\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\r\n", "_do_ghapi_completions()\r\n", "{\r\n", " COMP=\"$(completion-ghapi \"${COMP_WORDS[1]}\")\"\r\n", " COMPREPLY=($COMP)\r\n", "}\r\n", "\r\n", "complete -F _do_ghapi_completions ghapi\r\n", "\r\n" ] } ], "source": [ "#hide\n", "! completion-ghapi --install" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Export -" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Converted 00_core.ipynb.\n", "Converted 01_cli.ipynb.\n", "Converted 50_fullapi.ipynb.\n", "Converted 90_build_lib.ipynb.\n", "Converted Enable Pages.ipynb.\n", "Converted Untitled.ipynb.\n", "Converted index.ipynb.\n" ] } ], "source": [ "#hide\n", "from nbdev.export import notebook2script\n", "notebook2script()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 4 }