安装
我们可以使用 python-gitlab 库来操作 gitlab
pip install python-gitlab
gitlab issue 查询的 api:https://docs.gitlab.com/ee/api/issues.html#list-issues
gitlab issue 查询的 api:https://docs.gitlab.com/ee/api/issues.html#list-issues
创建令牌
我们需要令牌进行访问
基本使用
上面创建好了 token,我们就可以使用了,先创建两个项目
import gitlab url = "http://10.6.215.220/" # gitlab 地址 private_token = "shdjb9NEcinM16bAefjy" # gitlab 生成的 token 令牌 gl = gitlab.Gitlab(url=url, private_token=private_token) # 实例化一个 gitlab 对象 # 列出所有的项目 projects = gl.projects.list() for project in projects: print(project)
结果
<class 'gitlab.v4.objects.projects.Project'> => {'id': 3, 'description': '我是项目2的描述', 'name': '项目2', 'name_with_namespace': 'Administrator / 项目2', 'path': '2', 'path_with_namespace': 'root/2', 'created_at': '2022-06-22T02:11:37.957Z', 'default_branch': 'main', 'tag_list': [], 'topics': [], 'ssh_url_to_repo': 'ssh://git@10.6.215.220:222/root/2.git', 'http_url_to_repo': 'http://10.6.215.220/root/2.git', 'web_url': 'http://10.6.215.220/root/2', 'readme_url': 'http://10.6.215.220/root/2/-/blob/main/README.md', 'avatar_url': None, 'forks_count': 0, 'star_count': 0, 'last_activity_at': '2022-06-22T02:11:37.957Z', 'namespace': {'id': 1, 'name': 'Administrator', 'path': 'root', 'kind': 'user', 'full_path': 'root', 'parent_id': None, 'avatar_url': 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', 'web_url': 'http://10.6.215.220/root'}, '_links': {'self': 'http://10.6.215.220/api/v4/projects/3', 'issues': 'http://10.6.215.220/api/v4/projects/3/issues', 'merge_requests': 'http://10.6.215.220/api/v4/projects/3/merge_requests', 'repo_branches': 'http://10.6.215.220/api/v4/projects/3/repository/branches', 'labels': 'http://10.6.215.220/api/v4/projects/3/labels', 'events': 'http://10.6.215.220/api/v4/projects/3/events', 'members': 'http://10.6.215.220/api/v4/projects/3/members'}, 'packages_enabled': True, 'empty_repo': False, 'archived': False, 'visibility': 'private', 'owner': {'id': 1, 'username': 'root', 'name': 'Administrator', 'state': 'active', 'avatar_url': 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', 'web_url': 'http://10.6.215.220/root'}, 'resolve_outdated_diff_discussions': False, 'container_expiration_policy': {'cadence': '1d', 'enabled': False, 'keep_n': 10, 'older_than': '90d', 'name_regex': '.*', 'name_regex_keep': None, 'next_run_at': '2022-06-23T02:11:38.103Z'}, 'issues_enabled': True, 'merge_requests_enabled': True, 'wiki_enabled': True, 'jobs_enabled': True, 'snippets_enabled': True, 'container_registry_enabled': True, 'service_desk_enabled': False, 'service_desk_address': None, 'can_create_merge_request_in': True, 'issues_access_level': 'enabled', 'repository_access_level': 'enabled', 'merge_requests_access_level': 'enabled', 'forking_access_level': 'enabled', 'wiki_access_level': 'enabled', 'builds_access_level': 'enabled', 'snippets_access_level': 'enabled', 'pages_access_level': 'private', 'operations_access_level': 'enabled', 'analytics_access_level': 'enabled', 'container_registry_access_level': 'enabled', 'emails_disabled': None, 'shared_runners_enabled': True, 'lfs_enabled': True, 'creator_id': 1, 'import_status': 'none', 'open_issues_count': 0, 'ci_default_git_depth': 20, 'ci_forward_deployment_enabled': True, 'ci_job_token_scope_enabled': False, 'public_jobs': True, 'build_timeout': 3600, 'auto_cancel_pending_pipelines': 'enabled', 'build_coverage_regex': None, 'ci_config_path': None, 'shared_with_groups': [], 'only_allow_merge_if_pipeline_succeeds': False, 'allow_merge_on_skipped_pipeline': None, 'restrict_user_defined_variables': False, 'request_access_enabled': True, 'only_allow_merge_if_all_discussions_are_resolved': False, 'remove_source_branch_after_merge': True, 'printing_merge_request_link_enabled': True, 'merge_method': 'merge', 'squash_option': 'default_off', 'suggestion_commit_message': None, 'merge_commit_template': None, 'squash_commit_template': None, 'auto_devops_enabled': True, 'auto_devops_deploy_strategy': 'continuous', 'autoclose_referenced_issues': True, 'repository_storage': 'default', 'keep_latest_artifact': True, 'runner_token_expiration_interval': None, 'permissions': {'project_access': {'access_level': 40, 'notification_level': 3}, 'group_access': None}} <class 'gitlab.v4.objects.projects.Project'> => {'id': 2, 'description': '我是项目1的描述', 'name': '项目1', 'name_with_namespace': 'Administrator / 项目1', 'path': '1', 'path_with_namespace': 'root/1', 'created_at': '2022-06-22T02:06:43.510Z', 'default_branch': 'main', 'tag_list': [], 'topics': [], 'ssh_url_to_repo': 'ssh://git@10.6.215.220:222/root/1.git', 'http_url_to_repo': 'http://10.6.215.220/root/1.git', 'web_url': 'http://10.6.215.220/root/1', 'readme_url': 'http://10.6.215.220/root/1/-/blob/main/README.md', 'avatar_url': None, 'forks_count': 0, 'star_count': 0, 'last_activity_at': '2022-06-22T02:06:43.510Z', 'namespace': {'id': 1, 'name': 'Administrator', 'path': 'root', 'kind': 'user', 'full_path': 'root', 'parent_id': None, 'avatar_url': 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', 'web_url': 'http://10.6.215.220/root'}, '_links': {'self': 'http://10.6.215.220/api/v4/projects/2', 'issues': 'http://10.6.215.220/api/v4/projects/2/issues', 'merge_requests': 'http://10.6.215.220/api/v4/projects/2/merge_requests', 'repo_branches': 'http://10.6.215.220/api/v4/projects/2/repository/branches', 'labels': 'http://10.6.215.220/api/v4/projects/2/labels', 'events': 'http://10.6.215.220/api/v4/projects/2/events', 'members': 'http://10.6.215.220/api/v4/projects/2/members'}, 'packages_enabled': True, 'empty_repo': False, 'archived': False, 'visibility': 'public', 'owner': {'id': 1, 'username': 'root', 'name': 'Administrator', 'state': 'active', 'avatar_url': 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', 'web_url': 'http://10.6.215.220/root'}, 'resolve_outdated_diff_discussions': False, 'container_expiration_policy': {'cadence': '1d', 'enabled': False, 'keep_n': 10, 'older_than': '90d', 'name_regex': '.*', 'name_regex_keep': None, 'next_run_at': '2022-06-23T02:06:44.657Z'}, 'issues_enabled': True, 'merge_requests_enabled': True, 'wiki_enabled': True, 'jobs_enabled': True, 'snippets_enabled': True, 'container_registry_enabled': True, 'service_desk_enabled': False, 'service_desk_address': None, 'can_create_merge_request_in': True, 'issues_access_level': 'enabled', 'repository_access_level': 'enabled', 'merge_requests_access_level': 'enabled', 'forking_access_level': 'enabled', 'wiki_access_level': 'enabled', 'builds_access_level': 'enabled', 'snippets_access_level': 'enabled', 'pages_access_level': 'enabled', 'operations_access_level': 'enabled', 'analytics_access_level': 'enabled', 'container_registry_access_level': 'enabled', 'emails_disabled': None, 'shared_runners_enabled': True, 'lfs_enabled': True, 'creator_id': 1, 'import_status': 'none', 'open_issues_count': 0, 'ci_default_git_depth': 20, 'ci_forward_deployment_enabled': True, 'ci_job_token_scope_enabled': False, 'public_jobs': True, 'build_timeout': 3600, 'auto_cancel_pending_pipelines': 'enabled', 'build_coverage_regex': None, 'ci_config_path': None, 'shared_with_groups': [], 'only_allow_merge_if_pipeline_succeeds': False, 'allow_merge_on_skipped_pipeline': None, 'restrict_user_defined_variables': False, 'request_access_enabled': True, 'only_allow_merge_if_all_discussions_are_resolved': False, 'remove_source_branch_after_merge': True, 'printing_merge_request_link_enabled': True, 'merge_method': 'merge', 'squash_option': 'default_off', 'suggestion_commit_message': None, 'merge_commit_template': None, 'squash_commit_template': None, 'auto_devops_enabled': True, 'auto_devops_deploy_strategy': 'continuous', 'autoclose_referenced_issues': True, 'repository_storage': 'default', 'keep_latest_artifact': True, 'runner_token_expiration_interval': None, 'permissions': {'project_access': {'access_level': 40, 'notification_level': 3}, 'group_access': None}}
实例化的几种方式
import gitlab # 对公共资源的匿名只读访问(GitLab.com)。 gl = gitlab.Gitlab() # 对公共资源的匿名只读访问(自我托管的GitLab实例)。 gl = gitlab.Gitlab('https://gitlab.example.com') # 私人令牌或个人令牌认证(GitLab.com) gl = gitlab.Gitlab(private_token='JVNSESs8EwWRx5yDxM5q') # 私人令牌或个人令牌认证(自我托管的GitLab实例) gl = gitlab.Gitlab(url='https://gitlab.example.com', private_token='JVNSESs8EwWRx5yDxM5q') # oauth令牌认证 gl = gitlab.Gitlab('https://gitlab.example.com', oauth_token='my_long_token_here') # 工作令牌认证(将在CI中使用) # https://docs.gitlab.com/ee/ci/jobs/ci_job_token.html import os gl = gitlab.Gitlab('https://gitlab.example.com', job_token=os.environ['CI_JOB_TOKEN']) # 定义你自己的自定义请求的用户代理 gl = gitlab.Gitlab('https://gitlab.example.com', user_agent='my-package/1.0.0') # 发出一个API请求来创建gl.user对象。这是必须的,如果你使用用户名/密码认证 - 不需要使用令牌认证。 gl.auth()
创建用户
import gitlab url = "http://10.6.215.220/" # gitlab 地址 private_token = "shdjb9NEcinM16bAefjy" # gitlab 生成的 token 令牌 gl = gitlab.Gitlab(url=url, private_token=private_token) # 实例化一个 gitlab 对象 user_data = {"email": "8u93u2e9u239@163.com", "username": "liu.fujia", "name": "邹邹", "password": "12345678"} user = gl.users.create(user_data) # 创建用户 print(user)
结果
<class 'gitlab.v4.objects.users.User'> => {'id': 5, 'username': 'liu.fujia', 'name': '邹邹', 'state': 'active', 'avatar_url': 'https://www.gravatar.com/avatar/430e8e8b16e62967f6b0a5363b158e9c?s=80&d=identicon', 'web_url': 'http://10.6.215.220/liu.fujia', 'created_at': '2022-06-23T12:12:07.895Z', 'bio': '', 'location': None, 'public_email': None, 'skype': '', 'linkedin': '', 'twitter': '', 'website_url': '', 'organization': None, 'job_title': '', 'pronouns': None, 'bot': False, 'work_information': None, 'followers': 0, 'following': 0, 'local_time': None, 'last_sign_in_at': None, 'confirmed_at': None, 'last_activity_on': None, 'email': '8u93u2e9u239@163.com', 'theme_id': 1, 'color_scheme_id': 1, 'projects_limit': 100000, 'current_sign_in_at': None, 'identities': [], 'can_create_group': True, 'can_create_project': True, 'two_factor_enabled': False, 'external': False, 'private_profile': False, 'commit_email': '8u93u2e9u239@163.com', 'is_admin': False, 'note': None}
获取项目信息
import gitlab url = "http://10.6.215.220/" # gitlab 地址 private_token = "shdjb9NEcinM16bAefjy" # gitlab 生成的 token 令牌 gl = gitlab.Gitlab(url=url, private_token=private_token) # 实例化一个 gitlab 对象 project = gl.projects.get(2) print(project) print(project.attributes) print(project.attributes["id"]) print(project.attributes["name"])
结果
{'id': 2, 'description': '我是项目1的描述', 'name': '项目1', 'name_with_namespace': 'Administrator / 项目1', 'path': '1', 'path_with_namespace': 'root/1', 'created_at': '2022-06-22T02:06:43.510Z', 'default_branch': 'main', 'tag_list': [], 'topics': [], 'ssh_url_to_repo': 'ssh://git@10.6.215.220:222/root/1.git', 'http_url_to_repo': 'http://10.6.215.220/root/1.git', 'web_url': 'http://10.6.215.220/root/1', 'readme_url': 'http://10.6.215.220/root/1/-/blob/main/README.md', 'avatar_url': None, 'forks_count': 0, 'star_count': 0, 'last_activity_at': '2022-06-23T12:19:38.686Z', 'namespace': {'id': 1, 'name': 'Administrator', 'path': 'root', 'kind': 'user', 'full_path': 'root', 'parent_id': None, 'avatar_url': 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', 'web_url': 'http://10.6.215.220/root'}, '_links': {'self': 'http://10.6.215.220/api/v4/projects/2', 'issues': 'http://10.6.215.220/api/v4/projects/2/issues', 'merge_requests': 'http://10.6.215.220/api/v4/projects/2/merge_requests', 'repo_branches': 'http://10.6.215.220/api/v4/projects/2/repository/branches', 'labels': 'http://10.6.215.220/api/v4/projects/2/labels', 'events': 'http://10.6.215.220/api/v4/projects/2/events', 'members': 'http://10.6.215.220/api/v4/projects/2/members'}, 'packages_enabled': True, 'empty_repo': False, 'archived': False, 'visibility': 'public', 'owner': {'id': 1, 'username': 'root', 'name': 'Administrator', 'state': 'active', 'avatar_url': 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', 'web_url': 'http://10.6.215.220/root'}, 'resolve_outdated_diff_discussions': False, 'container_expiration_policy': {'cadence': '1d', 'enabled': False, 'keep_n': 10, 'older_than': '90d', 'name_regex': '.*', 'name_regex_keep': None, 'next_run_at': '2022-06-23T02:06:44.657Z'}, 'issues_enabled': True, 'merge_requests_enabled': True, 'wiki_enabled': True, 'jobs_enabled': True, 'snippets_enabled': True, 'container_registry_enabled': True, 'service_desk_enabled': False, 'service_desk_address': None, 'can_create_merge_request_in': True, 'issues_access_level': 'enabled', 'repository_access_level': 'enabled', 'merge_requests_access_level': 'enabled', 'forking_access_level': 'enabled', 'wiki_access_level': 'enabled', 'builds_access_level': 'enabled', 'snippets_access_level': 'enabled', 'pages_access_level': 'enabled', 'operations_access_level': 'enabled', 'analytics_access_level': 'enabled', 'container_registry_access_level': 'enabled', 'emails_disabled': None, 'shared_runners_enabled': True, 'lfs_enabled': True, 'creator_id': 1, 'import_status': 'none', 'import_error': None, 'open_issues_count': 2, 'runners_token': 'GR1348941-6ZJ-rTXJJfYdoz1BgjE', 'ci_default_git_depth': 20, 'ci_forward_deployment_enabled': True, 'ci_job_token_scope_enabled': False, 'public_jobs': True, 'build_git_strategy': 'fetch', 'build_timeout': 3600, 'auto_cancel_pending_pipelines': 'enabled', 'build_coverage_regex': None, 'ci_config_path': None, 'shared_with_groups': [], 'only_allow_merge_if_pipeline_succeeds': False, 'allow_merge_on_skipped_pipeline': None, 'restrict_user_defined_variables': False, 'request_access_enabled': True, 'only_allow_merge_if_all_discussions_are_resolved': False, 'remove_source_branch_after_merge': True, 'printing_merge_request_link_enabled': True, 'merge_method': 'merge', 'squash_option': 'default_off', 'suggestion_commit_message': None, 'merge_commit_template': None, 'squash_commit_template': None, 'auto_devops_enabled': True, 'auto_devops_deploy_strategy': 'continuous', 'autoclose_referenced_issues': True, 'repository_storage': 'default', 'keep_latest_artifact': True, 'runner_token_expiration_interval': None, 'permissions': {'project_access': {'access_level': 40, 'notification_level': 3}, 'group_access': None}} 2 项目1
获取项目的bug
import gitlab url = "http://10.6.215.220/" # gitlab 地址 private_token = "shdjb9NEcinM16bAefjy" # gitlab 生成的 token 令牌 gl = gitlab.Gitlab(url=url, private_token=private_token) # 实例化一个 gitlab 对象 project = gl.projects.get(2) # 获取项目 id 为 2 的 issues = project.issues.list() # 获取所有的 issue for i in issues: print(i)
结果
<class 'gitlab.v4.objects.issues.ProjectIssue'> => {'id': 2, 'iid': 2, 'project_id': 2, 'title': 'bug2', 'description': '我是一个bug', 'state': 'opened', 'created_at': '2022-06-23T12:20:37.581Z', 'updated_at': '2022-06-23T12:25:40.522Z', 'closed_at': None, 'closed_by': None, 'labels': ['bug'], 'milestone': None, 'assignees': [], 'author': {'id': 1, 'username': 'root', 'name': 'Administrator', 'state': 'active', 'avatar_url': 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', 'web_url': 'http://10.6.215.220/root'}, 'type': 'ISSUE', 'assignee': None, 'user_notes_count': 0, 'merge_requests_count': 0, 'upvotes': 0, 'downvotes': 0, 'due_date': None, 'confidential': False, 'discussion_locked': None, 'issue_type': 'issue', 'web_url': 'http://10.6.215.220/root/1/-/issues/2', 'time_stats': {'time_estimate': 0, 'total_time_spent': 0, 'human_time_estimate': None, 'human_total_time_spent': None}, 'task_completion_status': {'count': 0, 'completed_count': 0}, 'has_tasks': False, '_links': {'self': 'http://10.6.215.220/api/v4/projects/2/issues/2', 'notes': 'http://10.6.215.220/api/v4/projects/2/issues/2/notes', 'award_emoji': 'http://10.6.215.220/api/v4/projects/2/issues/2/award_emoji', 'project': 'http://10.6.215.220/api/v4/projects/2'}, 'references': {'short': '#2', 'relative': '#2', 'full': 'root/1#2'}, 'moved_to_id': None, 'service_desk_reply_to': None} <class 'gitlab.v4.objects.issues.ProjectIssue'> => {'id': 1, 'iid': 1, 'project_id': 2, 'title': 'bug1', 'description': '', 'state': 'opened', 'created_at': '2022-06-23T12:19:38.305Z', 'updated_at': '2022-06-23T12:19:38.305Z', 'closed_at': None, 'closed_by': None, 'labels': ['bug'], 'milestone': None, 'assignees': [{'id': 1, 'username': 'root', 'name': 'Administrator', 'state': 'active', 'avatar_url': 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', 'web_url': 'http://10.6.215.220/root'}], 'author': {'id': 1, 'username': 'root', 'name': 'Administrator', 'state': 'active', 'avatar_url': 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', 'web_url': 'http://10.6.215.220/root'}, 'type': 'ISSUE', 'assignee': {'id': 1, 'username': 'root', 'name': 'Administrator', 'state': 'active', 'avatar_url': 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', 'web_url': 'http://10.6.215.220/root'}, 'user_notes_count': 0, 'merge_requests_count': 0, 'upvotes': 0, 'downvotes': 0, 'due_date': None, 'confidential': False, 'discussion_locked': None, 'issue_type': 'issue', 'web_url': 'http://10.6.215.220/root/1/-/issues/1', 'time_stats': {'time_estimate': 0, 'total_time_spent': 0, 'human_time_estimate': None, 'human_total_time_spent': None}, 'task_completion_status': {'count': 0, 'completed_count': 0}, 'has_tasks': False, '_links': {'self': 'http://10.6.215.220/api/v4/projects/2/issues/1', 'notes': 'http://10.6.215.220/api/v4/projects/2/issues/1/notes', 'award_emoji': 'http://10.6.215.220/api/v4/projects/2/issues/1/award_emoji', 'project': 'http://10.6.215.220/api/v4/projects/2'}, 'references': {'short': '#1', 'relative': '#1', 'full': 'root/1#1'}, 'moved_to_id': None, 'service_desk_reply_to': None}
通过 attributes 就可以取到 issue
import gitlab url = "http://10.6.215.220/" # gitlab 地址 private_token = "shdjb9NEcinM16bAefjy" # gitlab 生成的 token 令牌 gl = gitlab.Gitlab(url=url, private_token=private_token) # 实例化一个 gitlab 对象 project = gl.projects.get(2) # 获取项目 id 为 2 的 issues = project.issues.list() # 获取所有的 issue for i in issues: print(i.attributes)
删除项目
import gitlab url = "http://10.6.215.220/" # gitlab 地址 private_token = "shdjb9NEcinM16bAefjy" # gitlab 生成的 token 令牌 gl = gitlab.Gitlab(url=url, private_token=private_token) # 实例化一个 gitlab 对象 project = gl.projects.get(3) project.delete() # 删除项目
格式化打印项目信息
import gitlab url = "http://10.6.215.220/" # gitlab 地址 private_token = "shdjb9NEcinM16bAefjy" # gitlab 生成的 token 令牌 gl = gitlab.Gitlab(url=url, private_token=private_token) # 实例化一个 gitlab 对象 project = gl.projects.get(2) # print(project) # 不格式化打印 project.pprint() # 格式化打印 # print(project.pformat()) # 格式化打印
结果