Skip to content

Commit

Permalink
fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Nov 26, 2020
1 parent 0e17f28 commit 3ad2f07
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 20 deletions.
92 changes: 82 additions & 10 deletions 00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
"- [users](https://docs.github.com/en/free-pro-team@latest/rest/reference/users)"
],
"text/plain": [
"<__main__.GhApi at 0x7f5648450b90>"
"<__main__.GhApi at 0x7fce9813eb50>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -339,7 +339,7 @@
"- [get_tree](https://docs.github.com/rest/reference/git#get-a-tree)"
],
"text/plain": [
"<__main__._GhVerbGroup at 0x7f5648384ad0>"
"<__main__._GhVerbGroup at 0x7fce981c7090>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -466,7 +466,7 @@
"def _upload_file(self:GhApi, url:str, fn:str):\n",
" \"Upload `fn` to endpoint `url`\"\n",
" mime = mimetypes.guess_type(fn, False)[0] or 'application/octet-stream'\n",
" headers = {**api.headers, 'Content-Type':mime}\n",
" headers = {**self.headers, 'Content-Type':mime}\n",
" data = Path(fn).read_bytes()\n",
" return urlsend(url, 'POST', headers=headers, query = {'name':fn}, data=data)"
]
Expand All @@ -481,9 +481,9 @@
"@patch\n",
"def create_release(self:GhApi, tag_name, branch='master', name=None, body='',\n",
" draft=False, prerelease=False, files=None):\n",
" \"Wrapper for `api.repos.create_release` which also uploads `files`\"\n",
" \"Wrapper for `GhApi.repos.create_release` which also uploads `files`\"\n",
" if name is None: name = 'v'+tag_name\n",
" rel = api.repos.create_release(tag_name, target_commitish=branch, name=name, body=body,\n",
" rel = self.repos.create_release(tag_name, target_commitish=branch, name=name, body=body,\n",
" draft=draft, prerelease=prerelease)\n",
" url = rel.upload_url.replace('{?name,label}','')\n",
" for file in listify(files): self._upload_file(url, file)\n",
Expand All @@ -494,7 +494,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Creating a release and attaching files to it is normally a multi-stage process, so `create_release` wraps this up for you. It takes the same arguments as [`api.repos.create_release`](https://docs.github.com/rest/reference/repos#create-a-release), along with `files`, which can contain a single file name, or a list of file names to upload to your release:"
"Creating a release and attaching files to it is normally a multi-stage process, so `create_release` wraps this up for you. It takes the same arguments as [`repos.create_release`](https://docs.github.com/rest/reference/repos#create-a-release), along with `files`, which can contain a single file name, or a list of file names to upload to your release:"
]
},
{
Expand Down Expand Up @@ -527,6 +527,80 @@
"test_eq(assets[0].content_type, 'text/markdown')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"@patch\n",
"def list_tags(self:GhApi, prefix:str=''):\n",
" \"List all tags, optionally filtered to those starting with `prefix`\"\n",
" return self.git.list_matching_refs(f'tags/{prefix}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With no `prefix`, all tags are listed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_eq(len(api.list_tags()), 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using the full tag name will return just that tag."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_eq(len(api.list_tags(rel.tag_name)), 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"@patch\n",
"def list_branches(self:GhApi, prefix:str=''):\n",
" \"List all branches, optionally filtered to those starting with `prefix`\"\n",
" return self.git.list_matching_refs(f'heads/{prefix}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Branches can be listed in the exactly the same way as tags."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_eq(len(api.list_branches('master')), 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -537,10 +611,8 @@
"@patch\n",
"def delete_release(self:GhApi, release):\n",
" \"Delete a release and its associated tag\"\n",
" ref = f'tags/{release.tag_name}'\n",
" tag = api.git.list_matching_refs(ref)\n",
" api.repos.delete_release(rel.id)\n",
" api.git.delete_ref(ref)"
" self.repos.delete_release(rel.id)\n",
" self.git.delete_ref(ref)"
]
},
{
Expand Down
142 changes: 139 additions & 3 deletions docs/core.html
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ <h3 id="Convenience-methods">Convenience methods<a class="anchor-link" href="#Co
<div class="output_markdown rendered_html output_subarea ">
<h4 id="GhApi.create_release" class="doc_header"><code>GhApi.create_release</code><a href="https://github.com/fastai/ghapi/tree/master/ghapi/core.py#L90" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>GhApi.create_release</code>(<strong><code>tag_name</code></strong>, <strong><code>branch</code></strong>=<em><code>'master'</code></em>, <strong><code>name</code></strong>=<em><code>None</code></em>, <strong><code>body</code></strong>=<em><code>''</code></em>, <strong><code>draft</code></strong>=<em><code>False</code></em>, <strong><code>prerelease</code></strong>=<em><code>False</code></em>, <strong><code>files</code></strong>=<em><code>None</code></em>)</p>
</blockquote>
<p>Wrapper for <code>api.repos.create_release</code> which also uploads <code>files</code></p>
<p>Wrapper for <a href="/core.html#GhApi.repos.create_release"><code>GhApi.repos.create_release</code></a> which also uploads <code>files</code></p>

</div>

Expand All @@ -532,7 +532,7 @@ <h4 id="GhApi.create_release" class="doc_header"><code>GhApi.create_release</cod

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Creating a release and attaching files to it is normally a multi-stage process, so <code>create_release</code> wraps this up for you. It takes the same arguments as <code>api.repos.create_release</code>, along with <code>files</code>, which can contain a single file name, or a list of file names to upload to your release:</p>
<p>Creating a release and attaching files to it is normally a multi-stage process, so <code>create_release</code> wraps this up for you. It takes the same arguments as <code>repos.create_release</code>, along with <code>files</code>, which can contain a single file name, or a list of file names to upload to your release:</p>

</div>
</div>
Expand Down Expand Up @@ -594,7 +594,143 @@ <h4 id="GhApi.create_release" class="doc_header"><code>GhApi.create_release</cod


<div class="output_markdown rendered_html output_subarea ">
<h4 id="GhApi.delete_release" class="doc_header"><code>GhApi.delete_release</code><a href="https://github.com/fastai/ghapi/tree/master/ghapi/core.py#L102" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>GhApi.delete_release</code>(<strong><code>release</code></strong>)</p>
<h4 id="GhApi.list_tags" class="doc_header"><code>GhApi.list_tags</code><a href="https://github.com/fastai/ghapi/tree/master/ghapi/core.py#L102" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>GhApi.list_tags</code>(<strong><code>prefix</code></strong>:<code>str</code>=<em><code>''</code></em>)</p>
</blockquote>
<p>List all tags, optionally filtered to those starting with <code>prefix</code></p>

</div>

</div>

</div>
</div>

</div>
{% endraw %}

{% raw %}

<div class="cell border-box-sizing code_cell rendered">

</div>
{% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>With no <code>prefix</code>, all tags are listed.</p>

</div>
</div>
</div>
{% raw %}

<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">test_eq</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">list_tags</span><span class="p">()),</span> <span class="mi">1</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

</div>
{% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Using the full tag name will return just that tag.</p>

</div>
</div>
</div>
{% raw %}

<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">test_eq</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">list_tags</span><span class="p">(</span><span class="n">rel</span><span class="o">.</span><span class="n">tag_name</span><span class="p">)),</span> <span class="mi">1</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

</div>
{% endraw %}

{% raw %}

<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="GhApi.list_branches" class="doc_header"><code>GhApi.list_branches</code><a href="https://github.com/fastai/ghapi/tree/master/ghapi/core.py#L108" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>GhApi.list_branches</code>(<strong><code>prefix</code></strong>:<code>str</code>=<em><code>''</code></em>)</p>
</blockquote>
<p>List all branches, optionally filtered to those starting with <code>prefix</code></p>

</div>

</div>

</div>
</div>

</div>
{% endraw %}

{% raw %}

<div class="cell border-box-sizing code_cell rendered">

</div>
{% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Branches can be listed in the exactly the same way as tags.</p>

</div>
</div>
</div>
{% raw %}

<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">test_eq</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">api</span><span class="o">.</span><span class="n">list_branches</span><span class="p">(</span><span class="s1">&#39;master&#39;</span><span class="p">)),</span> <span class="mi">1</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

</div>
{% endraw %}

{% raw %}

<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="GhApi.delete_release" class="doc_header"><code>GhApi.delete_release</code><a href="https://github.com/fastai/ghapi/tree/master/ghapi/core.py#L114" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>GhApi.delete_release</code>(<strong><code>release</code></strong>)</p>
</blockquote>
<p>Delete a release and its associated tag</p>

Expand Down
2 changes: 2 additions & 0 deletions ghapi/_nbdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
index = {"GH_HOST": "00_core.ipynb",
"GhApi": "00_core.ipynb",
"GhApi.create_release": "00_core.ipynb",
"GhApi.list_tags": "00_core.ipynb",
"GhApi.list_branches": "00_core.ipynb",
"GhApi.delete_release": "00_core.ipynb",
"GH_OPENAPI_URL": "90_build_lib.ipynb",
"build_funcs": "90_build_lib.ipynb",
Expand Down
24 changes: 17 additions & 7 deletions ghapi/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,37 @@ def __getattr__(self,k): return self._fs[k] if k in self._fs else stop(Attribute
def _upload_file(self:GhApi, url:str, fn:str):
"Upload `fn` to endpoint `url`"
mime = mimetypes.guess_type(fn, False)[0] or 'application/octet-stream'
headers = {**api.headers, 'Content-Type':mime}
headers = {**self.headers, 'Content-Type':mime}
data = Path(fn).read_bytes()
return urlsend(url, 'POST', headers=headers, query = {'name':fn}, data=data)

# Cell
@patch
def create_release(self:GhApi, tag_name, branch='master', name=None, body='',
draft=False, prerelease=False, files=None):
"Wrapper for `api.repos.create_release` which also uploads `files`"
"Wrapper for `GhApi.repos.create_release` which also uploads `files`"
if name is None: name = 'v'+tag_name
rel = api.repos.create_release(tag_name, target_commitish=branch, name=name, body=body,
rel = self.repos.create_release(tag_name, target_commitish=branch, name=name, body=body,
draft=draft, prerelease=prerelease)
url = rel.upload_url.replace('{?name,label}','')
for file in listify(files): self._upload_file(url, file)
return rel

# Cell
@patch
def list_tags(self:GhApi, prefix:str=''):
"List all tags, optionally filtered to those starting with `prefix`"
return self.git.list_matching_refs(f'tags/{prefix}')

# Cell
@patch
def list_branches(self:GhApi, prefix:str=''):
"List all branches, optionally filtered to those starting with `prefix`"
return self.git.list_matching_refs(f'heads/{prefix}')

# Cell
@patch
def delete_release(self:GhApi, release):
"Delete a release and its associated tag"
ref = f'tags/{release.tag_name}'
tag = api.git.list_matching_refs(ref)
api.repos.delete_release(rel.id)
api.git.delete_ref(ref)
self.repos.delete_release(rel.id)
self.git.delete_ref(ref)

0 comments on commit 3ad2f07

Please sign in to comment.