Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of gitlab library in python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of the use of the gitlab library in python, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value, I believe you will gain something after reading this python gitlab library article, let's take a look at it.

Installation

First you need to install the python-gitlab library

Pip installation sudo pip install-- upgrade python-gitlab source code installation git clone https://github.com/python-gitlab/python-gitlabcd python-gitlabsudo python setup.py install usage CLI usage

You need to configure the environment before you can use cli. You need to provide a configuration file that specifies the gitlab server information and connection parameters. The configuration file format is INI, as shown in the following example:

[global] default = somewheressl_verify = truetimeout = 5 [somewhere] url = https://some.whe.reprivate_token = vTbFeqJYCY3sibBP7BZMapi_version = 4 [elsewhere] url = http://else.whe.re:8080private_token = CkqsjqcQSFH5FQKDccu4timeout = 1

The global part must be provided, mainly the parameters for connecting the gitlab.

Other parts are optional, and default is used by default when there is no configuration.

You can specify which section to use with-g, such as gitlab-g somewhere project list

The configuration files used in this article are as follows:

[global] ssl_verify = truetimeout = 5 [gitlab] url = https://gitlab-russellgo.cnprivate_token = xxxxxxapi_version = 4

Configuration files can take effect in the following ways:

Configure PYTHON_GITLAB_CFG through environment variables

Put it under system configuration / etc/python-gitlab.cfg

Put it in the current user's home directory ~ / .python-gitlab.cfg

Specify-c or-- config-file from the command line

The configuration file for this article is placed under home.

When the environment is configured, it can be used happily.

List all project (paged back)

# A gitlab group is defined above, so you can specify gitlab-g gitlab project list with-g during execution

List all project

Gitlab-g gitlab project list-- all

Here is a question: how do you know which commands gitlab currently supports?

Gitlab-g gitlab # is the output usage: gitlab [- h] [--version] [- v] [- d] [- c CONFIG_FILE] [- g GITLAB] [- o {json,legacy,yaml}] [- f FIELDS] {application-settings,audit-event,broadcast-message,current-user,current-user-email,current-user-gp-gkey,current-user-key,current-user-status,deploy-key,dockerfile,event,feature Geo-node,gitignore,gitlabciyml,group,group-access-request,group-badge,group-board,group-board-list,group-cluster,group-custom-attribute,group-epic,group-epic-issue,group-epic-resource-label-event,group-issue,group-label,group-member,group-merge-request,group-milestone,group-notification-settings,group-project,group-subgroup,group-variable,hook,issue,l-da-pgroup,license,merge-request,namespace,notification-settings,pages-domain,project Project-access-request,project-additional-statistics,project-approval,project-approval-rule,project-badge,project-board,project-board-list,project-branch,project-cluster,project-commit,project-commit-comment,project-commit-discussion,project-commit-discussion-note,project-commit-status,project-custom-attribute,project-deployment,project-environment,project-event,project-export,project-file,project-fork,project-hook,project-import,project-issue,project-issue-award-emoji,project-issue-discussion Project-issue-discussion-note,project-issue-link,project-issue-note,project-issue-note-award-emoji,project-issue-resource-label-event,project-issues-statistics,project-job,project-key,project-label,project-member,project-merge-request,project-merge-request-approval,project-merge-request-award-emoji,project-merge-request-diff,project-merge-request-discussion,project-merge-request-discussion-note,project-merge-request-note,project-merge-request-note-award-emoji Project-merge-request-resource-label-event,project-milestone,project-note,project-notification-settings,project-pages-domain,project-pipeline,project-pipeline-job,project-pipeline-schedule,project-pipeline-schedule-variable,project-pipeline-variable,project-protected-branch,project-protected-tag,project-push-rules,project-registry-repository,project-registry-tag,project-release,project-runner,project-service,project-snippet,project-snippet-award-emoji,project-snippet-discussion,project-snippet-discussion-note Project-snippet-note,project-snippet-note-award-emoji,project-tag,project-trigger,project-user,project-variable,project-wiki,runner,runner-job,snippet,todo,user,user-activities,user-custom-attribute,user-email,user-event,user-gp-gkey,user-impersonation-token,user-key,user-project,user-status}

In this way, you can list the resources currently supported by gitlab. If you know the resources supported, how do you know which operations are supported by certain resources? take project as an example.

Gitlab-g gitlab project # is the output usage: gitlab project [- h] {list,get,create,update,delete,repository-blob,repository-contributors,delete-merged-branches,share,archive,repository-compare,create-fork-relation,languages,mirror-pull,unarchive,star,search,artifact,trigger-pipeline,repository-archive,delete-fork-relation,repository-raw-blob,repository-tree,unstar,housekeeping,unshare,upload,snapshot,update-submodule Transfer-project}... gitlab project: error: too few arguments

In this way, you can know what operations gitlab supports on which resources, and then you can know the specific parameters through-- help, such as

Gitlab-g gitlab project list-- help # is the output usage: gitlab project list [- h] [--sudo SUDO] [--search SEARCH] [--owned OWNED] [--starred STARRED] [--archived ARCHIVED] [--visibility VISIBILITY] [--order-by ORDER_BY] [ -- sort SORT] [--simple SIMPLE] [--membership MEMBERSHIP] [--statistics STATISTICS] [--with-issues-enabled WITH_ISSUES_ENABLED] [--with-merge-requests-enabled WITH_MERGE_REQUESTS_ENABLED] [- with-custom-attributes WITH_CUSTOM_ATTRIBUTES] [--page PAGE] [--per-page PER_PAGE] [--all] optional arguments:-h -help show this help message and exit-- sudo SUDO-- search SEARCH-- owned OWNED-- starred STARRED-- archived ARCHIVED-- visibility VISIBILITY-- order-by ORDER_BY-- sort SORT-- simple SIMPLE-- membership MEMBERSHIP-- statistics STATISTICS-- with-issues-enabled WITH_ISSUES_ENABLED-- with-merge-requests-enabled WITH_MERGE_REQUESTS_ENABLED-- with-custom-attributes WITH_CUSTOM_ATTRIBUTES-- Page PAGE-per-page PER_PAGE-all

In this way, it is convenient to operate on gitlab.

Programming usage

In addition to operating gitlab from the command line, you can integrate programmatically, a common scenario in which you download a file from gitlab

Basic usage #! / usr/bin/env python# coding=utf-8from _ _ future__ import print_functionimport gitlab# instantiates a gitlab object url = "https://gitlab.russellgao.cn"private_token =" xxxxxxxx "gl = gitlab.Gitlab ('https://gitlab.russellgao.cn', Private_token=private_token) # list all items projects = gl.projects.list () for project in projects: print (project) # get listgroup = gl.groups.get (2) for project in group.projects.list (): print (project) # create a user user_data = {'email':' jen@foo.com', 'username':' jen' 'name':' Jen'} user = gl.users.create (user_data) print (user) # list parameters needed for create and update # parameters needed for get_create_attrs () creation # parameters needed for get_update_attrs () update parameters print (gl.projects.get_create_attrs ()) (('name',), (' path', 'namespace_id',...)) # returns two tuples The first required parameter and the second optional parameter # get the properties of the object, such as projectproject = gl.projects.get (1) print (project.attributes) # some objects provide gitlab-related resource properties project = gl.projects.get (1) issues = project.issues.list () # python-gitlab allows any data to be sent to gitlab An exception gl.projects.list (sort='invalid value') #... # GitlabListError: 400: sort does not have a valid value# is thrown when sending illegal data or missing relevant parameters. When the parameter conflicts with the python keyword, gl.user_activities.list (from='2019-01-01') # # invalidgl.user_activities.list (query_parameters= {'from':' 2019-01-01'}) # OK function wrapper example

Download files through gitlab raw url

Def download_gitlab_file (url, filename, private_token): "" download file from gitlab: param url: gitlab raw url: param filename: file name saved locally: param private_token:: return: "import gitlab import codecs def writeLinesToFile (filename, lines, append=False Encoding=None): if (append = = True): file_mode = "a" else: file_mode = "w" encoding=encoding or 'utf-8' with codecs.open (filename, file_mode, encoding=encoding) as fp: for line in lines: print (unicode (line), file=fp) url_patterns = url.split ("/") if len (url_patterns)

< 8 : raise ValueError("url: `{}` 参数不合法,以 / 分隔之后长度必须大于8".format(url)) baseurl = "{}//{}".format(url_patterns[0], url_patterns[2]) namespace = url_patterns[3] project_name = url_patterns[4] branch = url_patterns[6] url_filename = "/".join(url_patterns[7:]) if url_patterns[5] == "-" : branch = url_patterns[7] url_filename = "/".join(url_patterns[8:]) gl = gitlab.Gitlab(str(baseurl), private_token) projects = gl.projects.list(search=project_name) projects = filter(lambda x : x.namespace.get("full_path") == namespace, projects ) if len(projects) == 0 : raise ValueError("根据url 没有找到相应的 project ,请检查当前用户是否有权限或者 url 是否正确 ") project = projects[0] raw_content = project.files.raw(file_path=url_filename, ref=branch) writeLinesToFile(filename, [raw_content]) return raw_content源码解析 源码地址: https://github.com/python-gitlab/python-gitlab/ 从 setup.py#L31:5 中可以看出 from setuptools import setupfrom setuptools import find_packages...setup( name="python-gitlab", ... entry_points={"console_scripts": ["gitlab = gitlab.cli:main"]}, ....) python-gitlab 采用 setuptools 进行打包,打成的包有两个作用: 当作 python 库使用 (默认) entry_points={"console_scripts": ["gitlab = gitlab.cli:main"]} 说明可以当作 cli 使用,指令是 gitlab ,真正调用的是 gitlab.cli:main 函数 在看一下 cli.py 这个入口文件,从入口文件可以看到 cli.py#L182:14 def main(): import gitlab.v4.cli... # 可以跳转到这个函数中查看 parser = _get_base_parser(add_help=False)...def _get_base_parser(add_help: bool = True) ->

Argparse.ArgumentParser: parser = argparse.ArgumentParser (add_help=add_help, description= "GitLab API Command Line Interface") parser.add_argument ("- version", help= "Display the version.", action= "store_true") parser.add_argument ("- v", "- verbose", "- fancy", help= "Verbose mode (legacy format only)", action= "store_true",)..

Here, the cli parsing library uses argparse to parse command-line parameters.

As can be seen from GitlabCLI class cli.py#L29:7

Class GitlabCLI (object): def _ init__ (self, gl, what, action, args): self.cls_name = cli.what_to_cls (what) self.cls = gitlab.v4.objects.__dict__ [self.cls _ name] self.what = what.replace ("-" "_" self.action = action.lower () self.gl = gl self.args = args self.mgr_cls = getattr (gitlab.v4.objects, self.cls.__name__ + "Manager") # We could do something smart, like splitting the manager name to find # parents, build the chain of managers to get to the final object. # Instead we do something ugly and efficient: interpolate variables in # the class _ path attribute, and replace the value with the result. Self.mgr_cls._path = self.mgr_cls._path% self.args self.mgr = self.mgr_cls (gl) if self.mgr_cls._types: for attr_name Type_cls in self.mgr_cls._types.items (): if attr_name in self.args.keys (): obj = type_cls () obj.set_from_cli (self.args [attr _ name]) self.args [attr _ name] = obj.get ()

The basic format of cli is gitlab what action args, which is the parameter corresponding to the operation of the resources supported by gitlab mentioned in the cli section above.

By reading the client.py client.py#L446:9 file, you can see

Def http_request (self, verb: str, path: str, query_data: Optional [Dict [str, Any]] = None, post_data: Optional [Dict [str, Any]] = None, streamed: bool = False, files: Optional [Dict [str, Any]] = None, * * kwargs: Any,)-> requests.Response: "" Make an HTTP request to the Gitlab server. Args: verb (str): The HTTP method to call ('get',' post', 'put' Delete') path (str): Path or full URL to query ('/ projects' or 'http://whatever/v4/api/projecs') query_data (dict): Data to send as query parameters post_data (dict): Data to send in the body (will be converted to) Json) streamed (bool): Whether the data should be streamed files (dict): The files to send to the server * * kwargs: Extra options to send to the server (e.g. Sudo) Returns: A requests result object. Raises: GitlabHttpError: When the return code is not 2xx "" query_data = query_data or {} url = self._build_url (path) params: Dict [str, Any] = {} utils.copy_dict (params, query_data) # Deal with kwargs: by default a user uses kwargs to send data to the # gitlab server But this generates problems (python keyword conflicts # and python-gitlab/gitlab conflicts). # So we provide a `query_ parameters` key: if it's there we use its dict # value as arguments for the gitlab server, and ignore the other # arguments, except pagination ones (per_page and page) if "query_parameters" in kwargs: utils.copy_dict (params, kwargs ["query_parameters"]) for arg in ("per_page") "page"): if arg in kwargs: params [arg] = kwargs [arg] else: utils.copy_dict (params, kwargs) opts = self._get_session_opts (content_type= "application/json") verify = opts.pop ("verify") timeout = opts.pop ("timeout") # If timeout was passed into kwargs Allow it to override the default timeout = kwargs.get ("timeout" Timeout) # We need to deal with json vs. Data when uploading files if files: json = None if post_data is None: post_data = {} post_data ["file"] = files.get ("file") post_data ["avatar"] = files.get ("avatar") data = MultipartEncoder (post_data) Opts ["headers"] ["Content-type"] = data.content_type else: json = post_data data = None # Requests assumes that `. `should not be encoded as% 2e and will make # changes to urls using this encoding. Using a prepped request we can # get the desired behavior. # The Requests behavior is right but it seems that web servers don't # always agree with this decision (this is the case with a default # gitlab installation) req = requests.Request (verb, url, json=json, data=data, params=params, * * opts) prepped = self.session.prepare_request (req) prepped.url = utils.sanitized_url (prepped.url) settings = self.session.merge_environment_settings (prepped.url, {}, streamed Verify, None) # obey the rate limit by default obey_rate_limit = kwargs.get ("obey_rate_limit", True) # do not retry transient errors by default retry_transient_errors = kwargs.get ("retry_transient_errors", False) # set max_retries to 10 by default, disable by setting it to-1 max_retries = kwargs.get ("max_retries", 10) cur_retries = 0. This is the end of the article on "what is the use of gitlab libraries in python?" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the use of gitlab library in python". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report