ADD clone command to anubis cli
@@ -68,7 +68,7 @@ def init_conf():
})
-def _make_request(path, request_func, **kwargs):
+def _make_request(path, request_func, **kwargs) -> requests.Response:
if 'params' in kwargs and kwargs['params'] is None:
kwargs['params'] = {}
@@ -88,7 +88,7 @@ def _make_request(path, request_func, **kwargs):
return r
-def post_json(path: str, data: dict, params=None):
+def post_json(path: str, data: dict, params=None) -> requests.Response:
"""
Do a POST request to the API with a json object.
@@ -98,7 +98,7 @@ def post_json(path: str, data: dict, params=None):
return _make_request(path, requests.post, json=data, params=params)
-def get_json(path: str, params=None):
+def get_json(path: str, params=None) -> requests.Response:
"""
GET something from the api, expecting a json response.
@@ -237,5 +237,38 @@ def build(path, push):
assignment_meta['assignment']['pipeline_image'])) == 0
+@assignment.command()
+def ls():
+ assignments = get_json('/admin/assignments/list').json()['data'].get('assignments', [])
+ click.echo(json.dumps(assignments, indent=2))
+
+
+@assignment.command()
+@click.argument('assignment-name')
+def clone(assignment_name):
+ assignments = get_json('/admin/assignments/list').json()['data'].get('assignments', [])
+
+ for assignment_ in assignments:
+ if assignment_.get('name', None) == assignment_name:
+ assignment_id = assignment_['id']
+ break
+ else:
+ click.echo('Unable to find an assignment with that name.')
+ click.echo('List assignments with:')
+ click.echo(' $ anubis assignment ls')
+ return 1
+
+ repos = get_json('/admin/assignments/repos/{}'.format(assignment_id)).json()['data'].get('repos', [])
+
+ r = requests.post('http://localhost:5001/clone', json={
+ 'assignment_name': assignment_name,
+ 'path': os.getcwd(),
+ 'repos': repos,
+ })
+
+ click.echo(r.text)
+ return r.status_code == 200
+
+
if __name__ == "__main__":
sys.exit(main()) # pragma: no cover
