This page looks best with JavaScript enabled

How to Create GitLab Labels Automatically with python-gitlab

 ·  ☕ 2 min read

Using GitLab issues for project management is a very Dev-friendly thing. Entering issues, opening Merge Requests, creating milestones — these all advance dynamically during development. But creating a pile of Labels for every new project is a headache. This article is mainly about solving that problem.

1. GitLab Label

Before creating GitLab Labels, we first need to standardize the Label format.

GitLab Labels are mainly used for categorized management and filtered viewing of issues. A recommended usage is to
adopt the “{type}/{value}” label rather than “{value}”. Such two-dimensional labels can express more information.

1.1 Label Types

  • type/feature
    A request for a new feature

  • type/enhancement
    Improvements, enhancements, and refactoring of a feature.

  • type/bug
    Minor issues that do not match expectations

  • type/question
    More serious issues

  • type/test
    Issues related to testing

1.2 Priority: priority

Priority labels specify the priority at which an issue should be handled.

  • priority/critical
    This issue should be fixed now

  • priority/high
    This issue should be resolved as soon as possible

  • priority/low
    This issue is not a high-priority issue and can be handled later. This label allows recording an issue without handling it immediately

1.3 Value

Value labels describe who benefits from the issue. This helps to schedule issues better.

  • value/client
    This issue will benefit the customer

  • value/admin
    This issue will benefit administrator users. Sometimes administrator users are not necessarily clients

  • value/developer
    This issue will benefit developers

1.4 Change

Change labels give a rough estimate of the change involved.

  • change/minor
    This issue usually takes a few hours or less.

  • change/medium
    This issue took less than a day, but it is not a quick fix.

  • change/major
    This issue involves significant changes and takes more than 1 day.

1.5 Miscellaneous

These are very important labels and should be used as needed.

  • others/needs-discussion
    This issue needs further discussion before it can be resolved.

We will query everything with this label as often as possible and discuss it with the relevant parties. Once we finish the discussion, we usually remove the label, but sometimes we cannot decide what we want to decide, so we leave the label there and push the item to the next meeting.

  • others/in-progress
    This issue is in progress. This label tells the person working on the issue (the one assigned the label) and prevents others from starting to work on the same issue.

  • others/duplicate
    This issue duplicates another feature request or bug report.

2. gitlab-python

python-gitlab is a Python package that provides access to the GitLab server API. It supports GitLab’s v3 and v4 APIs and provides a CLI tool.

python-gitlab is compatible with Python 2.7 and Python 3.4 and above.

2.1 Installing python-gitlab

1
pip install python-gitlab -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

2.2 Configuring Access

Here the configuration file is placed in the $HOME directory.

1
vim ~/.python-gitlab.cfg

python-gitlab also supports a system-level access configuration file, specified at startup. For the specific configuration, please refer to here.

1
2
3
4
5
6
7
8
9
[global]
default = gitlab-yourdomain
ssl_verify = false
timeout = 5

[gitlab-yourdomain]
url = https://gitlab.yourdomain.com
private_token = your-token
api_version = 4

This private_token can be generated on your GitLab homepage, http://gitlab.yourdomain.com/profile/personal_access_tokens. As shown below:

2.3 Common Commands

  • Get the first page of projects in the Console
1
gitlab project list
  • Python script

To access the GitLab API in a Python script, you first need to obtain a gitlab operation instance.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import gitlab

gitlab_url = 'http://gitlab.yourdomain.com'
gitlab_token = 'your-token'

# 登录
gl = gitlab.Gitlab(gitlab_url, gitlab_token)
# 获取第一页 project
projects = gl.projects.list()
# 获取所有的project
projects = gl.projects.list(all=True)

Here is the Python script for creating the Labels a project needs.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
import gitlab

gitlab_url = 'http://gitlab.yourdomain.com'
gitlab_token = 'your-token'

# 登录
gl = gitlab.Gitlab(gitlab_url, gitlab_token)

# 通过 "空间名/项目""名获取 project 对象
project = gl.projects.get('namespace/project_name')

# 获取所有 label
labels = project.labels.list()

# 删除全部 label
for label in labels:
    label.delete()

# 创建项目的 label
# 类型 type
# 优先级 priority
# 价值 value
# 更改 changeA
# 杂项 others
all_label_list = {
    'type': [('feature', '#52BE80'), ('enhancement', '#5DADE2'), ('bug', '#E59866'), ('question', '#D35400'), ('test', '#A9CCE3')],
    'priority': [('critical', '#E74C3C'), ('high', '#F4D03F'), ('low', '#F5CBA7')],
    'value': [('client', '#AED6F1'), ('admin', '#D6DBDF'), ('developer', '#3498DB')],
    'change': [('minor', '#EBDEF0'), ('medium', '#BB8FCE'), ('major', '#8E44AD')],
    'others': [('needs-discussion', '#2E86C1'), ('in-progress', '#D68910'), ('duplicate', '#85929E')]}

for key in all_label_list:
    for label in all_label_list[key]:
        project.labels.create(
            {'name': ''.join([key, '/', label[0]]), 'color': label[1]})

# 创建一个 tag
# tag = project.tags.create({'tag_name':'V1.0.0', 'ref':'master'})

# 获取所有 commit info
# commits = project.commits.list()
# for c in commits:
#     print c.author_name, c.message, c.title

# 获取指定 commit 的 info
# commit = project.commits.get('2f597633')

#  创建一个 merge request
# mr = project.mergerequests.create({'source_branch':'bugfix',
#                                    'target_branch':'master',
#                                    'title':'fix db coding', })
# 更新一个 merge request 的描述
# mr.description = 'some description'
# mr.save()

After running it, the result looks like this:

3. References


微信公众号
WRITTEN BY
微信公众号