Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GitLab API Experiments
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Karl Fogel
GitLab API Experiments
Commits
29869e41
Commit
29869e41
authored
1 month ago
by
Karl Fogel
Browse files
Options
Downloads
Patches
Plain Diff
Implement first two subcommands with argparse
parent
a65e2040
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
gl-sak
+46
-19
46 additions, 19 deletions
gl-sak
run.sh.tmpl
+2
-1
2 additions, 1 deletion
run.sh.tmpl
with
48 additions
and
20 deletions
gl-sak
+
46
−
19
View file @
29869e41
...
...
@@ -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 fur
ther arguments.
Run with
'
--help
'
to see subcommands and
the
i
r 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.
proj
ect
=
None
proj
=
None
try
:
proj
ect
=
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__
'
:
...
...
This diff is collapsed.
Click to expand it.
run.sh.tmpl
+
2
−
1
View file @
29869e41
...
...
@@ -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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment