Table of Contents
gitlab package)gitlab command)python-gitlab only supports GitLab API v4.
gitlab.Gitlab class¶To connect to GitLab.com or another GitLab instance, create agitlab.Gitlab object:
importgitlab# anonymous read-only access for public resources (GitLab.com)gl=gitlab.Gitlab()# anonymous read-only access for public resources (self-hosted GitLab instance)gl=gitlab.Gitlab('https://gitlab.example.com')# private token or personal token authentication (GitLab.com)gl=gitlab.Gitlab(private_token='JVNSESs8EwWRx5yDxM5q')# private token or personal token authentication (self-hosted GitLab instance)gl=gitlab.Gitlab(url='https://gitlab.example.com',private_token='JVNSESs8EwWRx5yDxM5q')# oauth token authenticationgl=gitlab.Gitlab('https://gitlab.example.com',oauth_token='my_long_token_here')# job token authentication (to be used in CI)# bear in mind the limitations of the API endpoints it supports:# https://docs.gitlab.com/ee/ci/jobs/ci_job_token.htmlimportosgl=gitlab.Gitlab('https://gitlab.example.com',job_token=os.environ['CI_JOB_TOKEN'])# Define your own custom user agent for requestsgl=gitlab.Gitlab('https://gitlab.example.com',user_agent='my-package/1.0.0')# make an API request to create the gl.user object. This is mandatory if you# use the username/password authentication - not required for token authentication,# and will not work with job tokens.gl.auth()
You can also use configuration files to creategitlab.Gitlab objects:
gl=gitlab.Gitlab.from_config('somewhere',['/tmp/gl.cfg'])
See theConfiguration section for more information aboutconfiguration files.
Warning
Note that a url that results in 301/302 redirects will raise an error,so it is highly recommended to use the final destination in theurl field.For example, if the GitLab server you are using redirects requests from httpto https, make sure to use thehttps:// protocol in the URL definition.
A URL that redirects using 301/302 (rather than 307/308) will most likelycause malformed POST and PUT requests.
python-gitlab will therefore raise aRedirectionError when it encountersa redirect which it believes will cause such an error, to avoid confusionbetween successful GET and failing POST/PUT requests on the same instance.
The/session API endpoint used for username/password authentication hasbeen removed from GitLab in version 10.2, and is not available on gitlab.comanymore. Personal token authentication is the preferred authentication method.
If you need username/password authentication, you can use cookie-basedauthentication. You can use the web UI form to authenticate, retrieve cookies,and then use a customrequests.Session object to connect to the GitLab API.The following code snippet demonstrates how to automate this:https://gist.github.com/gpocentek/bd4c3fbf8a6ce226ebddc4aad6b46c0a.
Seeissue 380for a detailed discussion.
Thegitlab.Gitlab class provides managers to access the GitLab resources.Each manager provides a set of methods to act on the resources. The availablemethods depend on the resource type.
Examples:
# list all the projectsprojects=gl.projects.list()forprojectinprojects:print(project)# get the group with id == 2group=gl.groups.get(2)forprojectingroup.projects.list():print(project)# create a new useruser_data={'email':'jen@foo.com','username':'jen','name':'Jen'}user=gl.users.create(user_data)print(user)
You can list the mandatory and optional attributes for object creation andupdate with the manager’sget_create_attrs() andget_update_attrs()methods. They return 2 tuples, the first one is the list of mandatoryattributes, the second one is the list of optional attribute:
# v4 onlyprint(gl.projects.get_create_attrs())(('name',),('path','namespace_id',...))
The attributes of objects are defined upon object creation, and depend on theGitLab API itself. To list the available information associated with an objectuse theattributes attribute:
project=gl.projects.get(1)print(project.attributes)
Some objects also provide managers to access related GitLab resources:
# list the issues for a projectproject=gl.projects.get(1)issues=project.issues.list()
python-gitlab allows to send any data to the GitLab server when making queries.In case of invalid or missing arguments python-gitlab will raise an exceptionwith the GitLab server error message:
>>>gl.projects.list(sort='invalid value')...GitlabListError: 400: sort does not have a valid value
You can use thequery_parameters argument to send arguments that wouldconflict with python or python-gitlab when using them as kwargs:
gl.user_activities.list(from='2019-01-01')## invalidgl.user_activities.list(query_parameters={'from':'2019-01-01'})# OK
You can update or delete a remote object when it exists locally:
# update the attributes of a resourceproject=gl.projects.get(1)project.wall_enabled=False# don't forget to apply your changes on the server:project.save()# delete the resourceproject.delete()
Some classes provide additional methods, allowing more actions on the GitLabresources. For example:
# star a git repositoryproject=gl.projects.get(1)project.star()
Thegitlab package provides some base types.
gitlab.Gitlab is the primary class, handling the HTTP requests. It holdsthe GitLab URL and authentication information.
gitlab.base.RESTObject is the base class for all the GitLab v4 objects.These objects provide an abstraction for GitLab resources (projects, groups,and so on).
gitlab.base.RESTManager is the base class for v4 objects managers,providing the API to manipulate the resources and their attributes.
To avoid useless API calls to the server you can create lazy objects. Theseobjects are created locally using a known ID, and give access to other managersand methods.
The following example will only make one API call to the GitLab server to stara project (the previous example used 2 API calls):
# star a git repositoryproject=gl.projects.get(1,lazy=True)# no API callproject.star()# API call
You can use pagination to iterate over long lists. All the Gitlab objectslisting methods support thepage andper_page parameters:
ten_first_groups=gl.groups.list(page=1,per_page=10)
Warning
The first page is page 1, not page 0.
By default GitLab does not return the complete list of items. Use theallparameter to get all the items when using listing methods:
all_groups=gl.groups.list(all=True)all_owned_projects=gl.projects.list(owned=True,all=True)
You can define theper_page value globally to avoid passing it to everylist() method call:
gl=gitlab.Gitlab(url,token,per_page=50)
Gitlab allows to also use keyset pagination. You can supply it to your project listing,but you can also do so globally. Be aware that GitLab then also requires you to only use supportedorder options. At the time of writing, onlyorder_by="id" works.
gl=gitlab.Gitlab(url,token,pagination="keyset",order_by="id",per_page=100)gl.projects.list()
Reference:https://docs.gitlab.com/ce/api/README.html#keyset-based-pagination
list() methods can also return a generator object which will handle thenext calls to the API when required. This is the recommended way to iteratethrough a large number of items:
items=gl.groups.list(as_list=False)foriteminitems:print(item.attributes)
The generator exposes extra listing information as received from the server:
current_page: current page number (first page is 1)
prev_page: ifNone the current page is the first one
next_page: ifNone the current page is the last one
per_page: number of items per page
total_pages: total number of pages available. This may be aNone value.
total: total number of items in the list. This may be aNone value.
Note
For performance reasons, if a query returns more than 10,000 records, GitLabdoes not return thetotal_pages ortotal headers. In this case,total_pages andtotal will have a value ofNone.
For more information see:https://docs.gitlab.com/ee/user/gitlab_com/index.html#pagination-response-headers
If you have the administrator status, you can usesudo to act as anotheruser. For example:
p=gl.projects.create({'name':'awesome_project'},sudo='user1')
python-gitlab relies onrequestsSession objects to perform all theHTTP requests to the Gitlab servers.
You can provide your ownSession object with custom configuration whenyou create aGitlab object.
You can useGitlab objects as context managers. This makes sure that therequests.Session object associated with aGitlab instance is alwaysproperly closed when you exit awith block:
withgitlab.Gitlab(host,token)asgl:gl.projects.list()
Warning
The context manager will also close the customSession object you mighthave used to build theGitlab instance.
The following sample illustrates how to define a proxy configuration when usingpython-gitlab:
importgitlabimportrequestssession=requests.Session()session.proxies={'https':os.environ.get('https_proxy'),'http':os.environ.get('http_proxy'),}gl=gitlab.gitlab(url,token,api_version=4,session=session)
Reference:https://2.python-requests.org/en/master/user/advanced/#proxies
python-gitlab relies on the CA certificate bundle in thecertifi packagethat comes with the requests library.
If you need python-gitlab to use your system CA store instead, you can providethe path to the CA bundle in theREQUESTS_CA_BUNDLE environment variable.
Reference:https://2.python-requests.org/en/master/user/advanced/#ssl-cert-verification
The following sample illustrates how to use a client-side certificate:
importgitlabimportrequestssession=requests.Session()session.cert=('/path/to/client.cert','/path/to/client.key')gl=gitlab.gitlab(url,token,api_version=4,session=session)
Reference:https://2.python-requests.org/en/master/user/advanced/#client-side-certificates
python-gitlab obeys the rate limit of the GitLab server by default. Onreceiving a 429 response (Too Many Requests), python-gitlab sleeps for theamount of time in the Retry-After header that GitLab sends back. If GitLabdoes not return a response with the Retry-After header, python-gitlab willperform an exponential backoff.
If you don’t want to wait, you can disable the rate-limiting feature, bysupplying theobey_rate_limit argument.
importgitlabimportrequestsgl=gitlab.gitlab(url,token,api_version=4)gl.projects.list(all=True,obey_rate_limit=False)
If you do not disable the rate-limiting feature, you can supply a custom valueformax_retries; by default, this is set to 10. To retry without bound whenthrottled, you can set this parameter to -1. This parameter is ignored ifobey_rate_limit is set toFalse.
importgitlabimportrequestsgl=gitlab.gitlab(url,token,api_version=4)gl.projects.list(all=True,max_retries=12)
Warning
You will get an Exception, if you then go over the rate limit of your GitLab instance.
GitLab server can sometimes return a transient HTTP error.python-gitlab can automatically retry in such case, whenretry_transient_errors argument is set toTrue. When enabled,HTTP error codes 500 (Internal Server Error), 502 (502 Bad Gateway),503 (Service Unavailable), and 504 (Gateway Timeout) are retried. Bydefault an exception is raised for these errors.
importgitlabimportrequestsgl=gitlab.gitlab(url,token,api_version=4)gl.projects.list(all=True,retry_transient_errors=True)
The defaultretry_transient_errors can also be set on theGitlab objectand overridden by individual API calls.
importgitlabimportrequestsgl=gitlab.gitlab(url,token,api_version=4,retry_transient_errors=True)gl.projects.list(all=True)# retries due to default valuegl.projects.list(all=True,retry_transient_errors=False)# does not retry
python-gitlab will by default use thetimeout option from it’s configurationfor all requests. This is passed downwards to therequests module at thetime of making the HTTP request. However if you would like to override theglobal timeout parameter for a particular call, you can provide thetimeoutparameter to that API invocation:
importgitlabgl=gitlab.gitlab(url,token,api_version=4)gl.projects.import_github(ACCESS_TOKEN,123456,"root",timeout=120.0)
When methods manipulate an existing object, such as withrefresh() andsave(),the object will only have attributes that were returned by the server. In some cases,such as when the initial request fetches attributes that are needed later for additionalprocessing, this may not be desired:
project=gl.projects.get(1,statistics=True)project.statisticsproject.refresh()project.statistics# AttributeError
To avoid this, either copy the object/attributes before callingrefresh()/save()or subsequently perform anotherget() call as needed, to fetch the attributes you want.