Skip to content
Snippets Groups Projects
Commit 29869e41 authored by Karl Fogel's avatar Karl Fogel
Browse files

Implement first two subcommands with argparse

parent a65e2040
No related branches found
No related tags found
No related merge requests found
......@@ -11,9 +11,9 @@ Usage
E.g.,
$ ./gl-sak --gitlab=https://code.librehq.com --project foo/bar/myproj baz qux quux
$ ./gl-sak --authn-token-file=./seekrit --gitlab=https://code.librehq.com --project foo/bar/myproj get-issue 6
TBD: Document some commands, some of which take further arguments.
Run with '--help' to see subcommands and their arguments.
GitLab authentication and authorization
---------------------------------------
......@@ -31,15 +31,18 @@ You could also pass the token directly via the command line with the
$ ./gl-sak --authn-token TOKEN [...]
However, be careful about that: the token could then leak via 'ps'
listings, shell history, etc. Only you can decide whether using
'--authn-token' is a reasonable choice given the security properties
of your runtime environment, but if you are in any doubt, just use
'--authn-token-file' instead.
However, be careful about using the latter way: the token could then
leak via 'ps' listings, shell history, etc. Only you can decide
whether using '--authn-token' is a reasonable choice given the
security properties of your runtime environment; if you are in any
doubt, choose '--authn-token-file' instead.
"""
import argparse
import json
import sys
try:
import gitlab
except ImportError:
......@@ -68,18 +71,18 @@ for more about the Python GitLab API package.""")
# Read https://docs.python.org/dev/library/argparse.html#sub-commands
# when it's time for subcommands.
def list_instance(gl):
def list_instance(args):
"""Print general information about GitLab server GL.
This is not really for practical usage; it's more meant to help
a developer explore what's available via the API."""
projects = gl.projects.list(iterator=True)
projects = args.gitlab.projects.list(iterator=True)
for project in projects:
print(f"*** {project.name_with_namespace} ({project.id}):")
issues = project.issues.list(get_all=True)
# for issue in issues:
# print(f"{issue}")
print("")
groups = gl.groups.list(iterator=True)
groups = args.gitlab.groups.list(iterator=True)
for group in groups:
print(f"{group}")
# This isn't quite right yet:
......@@ -88,6 +91,16 @@ def list_instance(gl):
# for board in boards:
# print(f"*** {board} ({board.id}):")
def get_issue(args):
"""TBD: doc string for get_issue()"""
issue = args.project.issues.get(vars(args)['issue-id'])
print(f"{json.dumps(vars(issue)['_attrs'], indent=2)}")
# issues = args.project.issues.list(get_all=True)
# for issue in issues:
# print(f"{issue}\n")
def main():
# Set up options and arguments
arg_parser = argparse.ArgumentParser(
......@@ -111,6 +124,20 @@ def main():
'-F', '--authn-token-file',
help="File containing the GitLab access token to use for this operation")
# Set up subcommands.
subcommand_parsers = arg_parser.add_subparsers(title="subcommands")
get_issue_parser = subcommand_parsers.add_parser(
'get-issue', help=get_issue.__doc__)
get_issue_parser.set_defaults(func=get_issue)
get_issue_parser.add_argument('issue-id', type=int)
list_instance_parser = subcommand_parsers.add_parser(
'list-instance', help=list_instance.__doc__)
list_instance_parser.set_defaults(func=list_instance)
# Parse command line.
args = arg_parser.parse_args()
if args.authn_token_file is not None:
......@@ -119,27 +146,27 @@ def main():
# Set up the GitLab instance.
gl = gitlab.Gitlab(url=args.gitlab,
private_token=args.authn_token)
private_token=args.authn_token)
gl.auth()
## WARNING: Turning on debugging can cause credentials and other
## sensitive data to be printed on the console.
# gl.enable_debug()
## Uncomment for exploration.
# list_instance(gl)
# Instantiate specified project, if any.
project = None
proj = None
try:
project = gl.projects.get(args.project, None)
proj = gl.projects.get(args.project, None)
except gitlab.exceptions.GitlabGetError as e:
sys.stderr.write(f"ERROR: No such project '{args.project}'\n")
sys.exit(1)
issues = project.issues.list(get_all=True)
for issue in issues:
print(f"{issue}\n")
# Replace identifiers with now-instantiated objects.
args.gitlab = gl
args.project = proj
# Call the subcommand.
args.func(args)
if __name__ == '__main__':
......
......@@ -2,4 +2,5 @@
./gl-sak --authn-token-file="XXX" \
--gitlab="https://code.librehq.com" \
--project="ots/clients/YYY/ZZZ"
--project="ots/clients/YYY/ZZZ" \
get-issue 6
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment