Skip to content

Commit c5b43b3

Browse files
committed
fixes #213
1 parent e016777 commit c5b43b3

20 files changed

Lines changed: 136 additions & 134 deletions

ghapi/actions.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
Docs: https://ghapi.fast.ai/actions.html.md"""
44

5-
# AUTOGENERATED! DO NOT EDIT! File to edit: ../01_actions.ipynb.
5+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_actions.ipynb.
66

77
# %% auto #0
88
__all__ = ['contexts', 'env_github', 'Event', 'def_pipinst', 'user_repo', 'create_workflow_files', 'fill_workflow_templates',
@@ -11,7 +11,7 @@
1111
'context_github', 'context_env', 'context_job', 'context_steps', 'context_runner', 'context_secrets',
1212
'context_strategy', 'context_matrix', 'context_needs']
1313

14-
# %% ../01_actions.ipynb #e4187aeb
14+
# %% ../nbs/01_actions.ipynb #e4187aeb
1515
from fastcore.all import *
1616
from .core import *
1717
from .templates import *
@@ -20,7 +20,7 @@
2020
from contextlib import contextmanager
2121
from enum import Enum
2222

23-
# %% ../01_actions.ipynb #fb69ba90
23+
# %% ../nbs/01_actions.ipynb #fb69ba90
2424
# So we can run this outside of GitHub actions too, read from file if needed
2525
for a,b in (('CONTEXT_GITHUB',context_example), ('CONTEXT_NEEDS',needs_example), ('GITHUB_REPOSITORY','octocat/Hello-World')):
2626
if a not in os.environ: os.environ[a] = b
@@ -29,18 +29,18 @@
2929
for context in contexts:
3030
globals()[f'context_{context}'] = dict2obj(loads(os.getenv(f"CONTEXT_{context.upper()}", "{}")))
3131

32-
# %% ../01_actions.ipynb #393990e1
32+
# %% ../nbs/01_actions.ipynb #393990e1
3333
_all_ = ['context_github', 'context_env', 'context_job', 'context_steps', 'context_runner', 'context_secrets', 'context_strategy', 'context_matrix', 'context_needs']
3434

35-
# %% ../01_actions.ipynb #02ca0806
35+
# %% ../nbs/01_actions.ipynb #02ca0806
3636
env_github = dict2obj({k[7:].lower():v for k,v in os.environ.items() if k.startswith('GITHUB_')})
3737

38-
# %% ../01_actions.ipynb #3170b688
38+
# %% ../nbs/01_actions.ipynb #3170b688
3939
def user_repo():
4040
"List of `user,repo` from `env_github.repository"
4141
return env_github.repository.split('/')
4242

43-
# %% ../01_actions.ipynb #deb389d7
43+
# %% ../nbs/01_actions.ipynb #deb389d7
4444
Event = str_enum('Event',
4545
'page_build','content_reference','repository_import','create','workflow_run','delete','organization','sponsorship',
4646
'project_column','push','context','milestone','project_card','project','package','pull_request','repository_dispatch',
@@ -50,14 +50,14 @@ def user_repo():
5050
'installation','release','issues','repository','gollum','membership','deployment','deploy_key','issue_comment','ping',
5151
'deployment_status','fork','schedule')
5252

53-
# %% ../01_actions.ipynb #bf0b5888
53+
# %% ../nbs/01_actions.ipynb #bf0b5888
5454
def _create_file(path:Path, fname:str, contents):
5555
if contents and not (path/fname).exists(): (path/fname).write_text(contents)
5656

5757
def _replace(s:str, find, repl, i:int=0, suf:str=''):
5858
return s.replace(find, textwrap.indent(repl, ' '*i)+suf)
5959

60-
# %% ../01_actions.ipynb #26eb64e5
60+
# %% ../nbs/01_actions.ipynb #26eb64e5
6161
def create_workflow_files(fname:str, workflow:str, build_script:str, prebuild:bool=False):
6262
"Create workflow and script files in suitable places in `github` folder"
6363
if not os.path.exists('.git'): return print('This does not appear to be the root of a git repo')
@@ -69,7 +69,7 @@ def create_workflow_files(fname:str, workflow:str, build_script:str, prebuild:bo
6969
_create_file(scr_path, f'build-{fname}.py', build_script)
7070
if prebuild: _create_file(scr_path, f'prebuild-{fname}.py', build_script)
7171

72-
# %% ../01_actions.ipynb #3cf450a0
72+
# %% ../nbs/01_actions.ipynb #3cf450a0
7373
def fill_workflow_templates(name:str, event, run, context, script, opersys='ubuntu', prebuild=False):
7474
"Function to create a simple Ubuntu workflow that calls a Python `ghapi` script"
7575
c = wf_tmpl
@@ -80,23 +80,23 @@ def fill_workflow_templates(name:str, event, run, context, script, opersys='ubun
8080
c = _replace(c, f'${find}', str(repl), i)
8181
create_workflow_files(name, c, script, prebuild=prebuild)
8282

83-
# %% ../01_actions.ipynb #bc2f80a5
83+
# %% ../nbs/01_actions.ipynb #bc2f80a5
8484
def env_contexts(contexts):
8585
"Create a suitable `env:` line for a workflow to make a context available in the environment"
8686
contexts = uniqueify(['github'] + listify(contexts))
8787
return "\n".join("CONTEXT_" + o.upper() + ": ${{ toJson(" + o.lower() + ") }}" for o in contexts)
8888

89-
# %% ../01_actions.ipynb #561125fa
89+
# %% ../nbs/01_actions.ipynb #561125fa
9090
def_pipinst = 'pip install -Uq ghapi'
9191

92-
# %% ../01_actions.ipynb #39c5d429
92+
# %% ../nbs/01_actions.ipynb #39c5d429
9393
def create_workflow(name:str, event:Event, contexts:list=None, opersys='ubuntu', prebuild=False):
9494
"Function to create a simple Ubuntu workflow that calls a Python `ghapi` script"
9595
script = "from fastcore.all import *\nfrom ghapi import *"
9696
fill_workflow_templates(name, f'{event}:', def_pipinst, env_contexts(contexts),
9797
script=script, opersys=opersys, prebuild=prebuild)
9898

99-
# %% ../01_actions.ipynb #e8482286
99+
# %% ../nbs/01_actions.ipynb #e8482286
100100
@call_parse
101101
def gh_create_workflow(
102102
name:str, # Name of the workflow file
@@ -106,53 +106,53 @@ def gh_create_workflow(
106106
"Supports `gh-create-workflow`, a CLI wrapper for `create_workflow`."
107107
create_workflow(name, Event[event], contexts.split())
108108

109-
# %% ../01_actions.ipynb #a85ea1f8
109+
# %% ../nbs/01_actions.ipynb #a85ea1f8
110110
_example_url = 'https://raw.githubusercontent.com/fastai/ghapi/master/examples/{}.json'
111111

112-
# %% ../01_actions.ipynb #47cfc8b9
112+
# %% ../nbs/01_actions.ipynb #47cfc8b9
113113
def example_payload(event):
114114
"Get an example of a JSON payload for `event`"
115115
return dict2obj(urljson(_example_url.format(event)))
116116

117-
# %% ../01_actions.ipynb #778d7cc7
117+
# %% ../nbs/01_actions.ipynb #778d7cc7
118118
def github_token():
119119
"Get GitHub token from `GITHUB_TOKEN` env var if available, or from `github` context"
120120
return os.getenv('GITHUB_TOKEN', context_github.get('token', None))
121121

122-
# %% ../01_actions.ipynb #88716812
122+
# %% ../nbs/01_actions.ipynb #88716812
123123
def actions_output(name, value):
124124
"Print the special GitHub Actions `::set-output` line for `name::value`"
125125
print(f"::set-output name={name}::{value}")
126126

127-
# %% ../01_actions.ipynb #50cad4c2
127+
# %% ../nbs/01_actions.ipynb #50cad4c2
128128
def actions_debug(message):
129129
"Print the special `::debug` line for `message`"
130130
print(f"::debug::{message}")
131131

132-
# %% ../01_actions.ipynb #5b6d15ab
132+
# %% ../nbs/01_actions.ipynb #5b6d15ab
133133
def actions_warn(message, details=''):
134134
"Print the special `::warning` line for `message`"
135135
print(f"::warning {details}::{message}")
136136

137-
# %% ../01_actions.ipynb #e97ba982
137+
# %% ../nbs/01_actions.ipynb #e97ba982
138138
def actions_error(message, details=''):
139139
"Print the special `::error` line for `message`"
140140
print(f"::error {details}::{message}")
141141

142-
# %% ../01_actions.ipynb #a7ff989d
142+
# %% ../nbs/01_actions.ipynb #a7ff989d
143143
@contextmanager
144144
def actions_group(title):
145145
"Context manager to print the special `::group`/`::endgroup` lines for `title`"
146146
print(f"::group::{title}")
147147
yield
148148
print(f"::endgroup::")
149149

150-
# %% ../01_actions.ipynb #3405cb7f
150+
# %% ../nbs/01_actions.ipynb #3405cb7f
151151
def actions_mask(value):
152152
"Print the special `::add-mask` line for `value`"
153153
print(f"::add-mask::{value}")
154154

155-
# %% ../01_actions.ipynb #8682ff46
155+
# %% ../nbs/01_actions.ipynb #8682ff46
156156
def set_git_user(api=None):
157157
"Set git user name/email to authenticated user (if `api`) or GitHub Actions bot (otherwise)"
158158
if api:

ghapi/auth.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,38 @@
22
33
Docs: https://ghapi.fast.ai/auth.html.md"""
44

5-
# AUTOGENERATED! DO NOT EDIT! File to edit: ../02_auth.ipynb.
5+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_auth.ipynb.
66

77
# %% auto #0
88
__all__ = ['Scope', 'scope_str', 'GhDeviceAuth', 'github_auth_device']
99

10-
# %% ../02_auth.ipynb #e1ccb974
10+
# %% ../nbs/02_auth.ipynb #e1ccb974
1111
from fastcore.all import *
1212
from .core import *
1313

1414
import webbrowser,time
1515
from urllib.parse import parse_qs,urlsplit
1616

17-
# %% ../02_auth.ipynb #46620407
17+
# %% ../nbs/02_auth.ipynb #46620407
1818
_scopes =(
1919
'repo','repo:status','repo_deployment','public_repo','repo:invite','security_events','admin:repo_hook','write:repo_hook',
2020
'read:repo_hook','admin:org','write:org','read:org','admin:public_key','write:public_key','read:public_key','admin:org_hook',
2121
'gist','notifications','user','read:user','user:email','user:follow','delete_repo','write:discussion','read:discussion',
2222
'write:packages','read:packages','delete:packages','admin:gpg_key','write:gpg_key','read:gpg_key','workflow'
2323
)
2424

25-
# %% ../02_auth.ipynb #0cbc503b
25+
# %% ../nbs/02_auth.ipynb #0cbc503b
2626
Scope = AttrDict({o.replace(':','_'):o for o in _scopes})
2727

28-
# %% ../02_auth.ipynb #404b7d4f
28+
# %% ../nbs/02_auth.ipynb #404b7d4f
2929
def scope_str(*scopes)->str:
3030
"Convert `scopes` into a comma-separated string"
3131
return ','.join(str(o) for o in scopes if o)
3232

33-
# %% ../02_auth.ipynb #7b224a0a
33+
# %% ../nbs/02_auth.ipynb #7b224a0a
3434
_def_clientid = '71604a89b882ab8c8634'
3535

36-
# %% ../02_auth.ipynb #2a614e72
36+
# %% ../nbs/02_auth.ipynb #2a614e72
3737
class GhDeviceAuth(GetAttrBase):
3838
"Get an oauth token using the GitHub API device flow"
3939
_attr="params"
@@ -44,20 +44,20 @@ def __init__(self, client_id=_def_clientid, *scopes):
4444

4545
def _getattr(self,v): return v[0]
4646

47-
# %% ../02_auth.ipynb #d3764621
47+
# %% ../nbs/02_auth.ipynb #d3764621
4848
@patch
4949
def url_docs(self:GhDeviceAuth)->str:
5050
"Default instructions on how to authenticate"
5151
return f"""First copy your one-time code: {self.user_code}
5252
Then visit {self.verification_uri} in your browser, and paste the code when prompted."""
5353

54-
# %% ../02_auth.ipynb #255b299c
54+
# %% ../nbs/02_auth.ipynb #255b299c
5555
@patch
5656
def open_browser(self:GhDeviceAuth):
5757
"Open a web browser with the verification URL"
5858
webbrowser.open(self.verification_uri)
5959

60-
# %% ../02_auth.ipynb #5091b2c8
60+
# %% ../nbs/02_auth.ipynb #5091b2c8
6161
@patch
6262
def auth(self:GhDeviceAuth)->str:
6363
"Return token if authentication complete, or `None` otherwise"
@@ -70,7 +70,7 @@ def auth(self:GhDeviceAuth)->str:
7070
if err: raise Exception(resp['error_description'][0])
7171
return resp['access_token'][0]
7272

73-
# %% ../02_auth.ipynb #a7d55794
73+
# %% ../nbs/02_auth.ipynb #a7d55794
7474
@patch
7575
def wait(self:GhDeviceAuth, cb:callable=None, n_polls=9999)->str:
7676
"Wait up to `n_polls` times for authentication to complete, calling `cb` after each poll, if passed"
@@ -82,7 +82,7 @@ def wait(self:GhDeviceAuth, cb:callable=None, n_polls=9999)->str:
8282
if cb: cb()
8383
time.sleep(interval)
8484

85-
# %% ../02_auth.ipynb #e329a3d2
85+
# %% ../nbs/02_auth.ipynb #e329a3d2
8686
def github_auth_device(wb='', n_polls=9999):
8787
"Authenticate with GitHub, polling up to `n_polls` times to wait for completion"
8888
auth = GhDeviceAuth()

ghapi/build_lib.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
"""Docs: https://ghapi.fast.ai/build_lib.html.md"""
22

3-
# AUTOGENERATED! DO NOT EDIT! File to edit: ../90_build_lib.ipynb.
3+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/90_build_lib.ipynb.
44

55
# %% auto #0
66
__all__ = ['GH_OPENAPI_URL', 'GhMeta', 'build_funcs']
77

8-
# %% ../90_build_lib.ipynb #cdbcf258
8+
# %% ../nbs/90_build_lib.ipynb #cdbcf258
99
from fastcore.all import *
1010

1111
import pprint
1212
# from json import loads
1313
from jsonref import loads
1414
from collections import namedtuple
1515

16-
# %% ../90_build_lib.ipynb #c215ed8d
16+
# %% ../nbs/90_build_lib.ipynb #c215ed8d
1717
GH_OPENAPI_URL = 'https://github.com/github/rest-api-description/raw/main/descriptions/api.github.com/api.github.com.json?raw=true'
1818
_DOC_URL = 'https://docs.github.com/'
1919

20-
# %% ../90_build_lib.ipynb #ce33036d
20+
# %% ../nbs/90_build_lib.ipynb #ce33036d
2121
_lu_type = dict(zip(
2222
'NA string object array boolean number integer'.split(),
2323
map(PrettyString,'object str dict list bool int int'.split())
@@ -36,7 +36,7 @@ def _find_data(d):
3636
if 'properties' in o: return o['properties']
3737
return {}
3838

39-
# %% ../90_build_lib.ipynb #cf7204d9
39+
# %% ../nbs/90_build_lib.ipynb #cf7204d9
4040
def build_funcs(nm='ghapi/metadata.py', url=GH_OPENAPI_URL, docurl=_DOC_URL):
4141
"Build module metadata.py from an Open API spec and optionally filter by a path `pre`"
4242
def _get_detls(o):
@@ -55,5 +55,5 @@ def _get_detls(o):
5555
if 'externalDocs' in detls]
5656
Path(nm).write_text("funcs = " + pprint.pformat(_funcs, width=360))
5757

58-
# %% ../90_build_lib.ipynb #9d2307fc
58+
# %% ../nbs/90_build_lib.ipynb #9d2307fc
5959
GhMeta = namedtuple('GhMeta', 'path verb oper_id summary doc_url params data preview'.split())

ghapi/cli.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
33
Docs: https://ghapi.fast.ai/cli.html.md"""
44

5-
# AUTOGENERATED! DO NOT EDIT! File to edit: ../10_cli.ipynb.
5+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/10_cli.ipynb.
66

77
# %% auto #0
88
__all__ = ['ghapi', 'ghpath', 'ghraw', 'completion_ghapi']
99

10-
# %% ../10_cli.ipynb #e10a0628
10+
# %% ../nbs/10_cli.ipynb #e10a0628
1111
from fastcore.all import *
1212
import ghapi.core as gh,inspect
1313
from .core import *
1414
from collections import defaultdict
1515

16-
# %% ../10_cli.ipynb #44ebb54f
16+
# %% ../nbs/10_cli.ipynb #44ebb54f
1717
def _parse_args(a):
1818
"Extract positional and keyword arguments from `a`=`sys.argv`"
1919
pos,kw = [],{}
@@ -45,34 +45,34 @@ def _call_api(f):
4545
call = f(pos, api)
4646
return call if kw.get('help', None) else call(*pos, **kw)
4747

48-
# %% ../10_cli.ipynb #55f9002f
48+
# %% ../nbs/10_cli.ipynb #55f9002f
4949
def _ghapi(arg, api):
5050
for part in arg.pop(0).split('.'): api = getattr(api,part)
5151
return api
5252

53-
# %% ../10_cli.ipynb #7557c309
53+
# %% ../nbs/10_cli.ipynb #7557c309
5454
def ghapi():
5555
"Python backend for the `ghapi` command, which calls an endpoint by operation name"
5656
res = _call_api(_ghapi)
5757
if isinstance(res, (gh._GhObj,dict,L)): print(res)
5858
elif res: print(inspect.signature(res))
5959

60-
# %% ../10_cli.ipynb #524623ba
60+
# %% ../nbs/10_cli.ipynb #524623ba
6161
def _ghpath(arg, api): return api[arg.pop(0),arg.pop(0)]
6262

63-
# %% ../10_cli.ipynb #723624b0
63+
# %% ../nbs/10_cli.ipynb #723624b0
6464
def ghpath():
6565
"Python backend for the `ghpath` command, which calls an endpoint by path"
6666
print(_call_api(_ghpath) or '')
6767

68-
# %% ../10_cli.ipynb #93dbad3e
68+
# %% ../nbs/10_cli.ipynb #93dbad3e
6969
def ghraw():
7070
"Python backend for the `ghraw` command, which calls a fully-specified endpoint"
7171
cmd,api,pos,kw = _api()
7272
if not pos: return print(f"Usage: `{cmd}` operation <params>")
7373
print(api(*pos, **kw))
7474

75-
# %% ../10_cli.ipynb #21033494
75+
# %% ../nbs/10_cli.ipynb #21033494
7676
_TAB_COMPLETION="""
7777
_do_ghapi_completions()
7878
{
@@ -83,7 +83,7 @@ def ghraw():
8383
complete -F _do_ghapi_completions ghapi
8484
"""
8585

86-
# %% ../10_cli.ipynb #9fe362ab
86+
# %% ../nbs/10_cli.ipynb #9fe362ab
8787
def completion_ghapi():
8888
"Python backend for `completion-ghapi` command"
8989
if len(sys.argv) == 2 and sys.argv[1] == '--install':

0 commit comments

Comments
 (0)