This page looks best with JavaScript enabled

Using PyGithub to Create Labels Automatically

 ·  ☕ 2 min read

1. About Labels

In an earlier document, How to Create GitLab Labels Automatically with python-gitlab, I already explained that Labels can be used for simple project management.

A team usually has more than one code repository. To reduce the cost of communication and learning, besides using a consistent toolchain, it should also reach a certain basic consensus. This basic consensus pushes everyone toward the best practices. One such item is that under the same large project, all sub-projects use the same set of Labels.

2. How to Copy Labels from Other Projects

In the open source community, there are projects with considerable influence in their field. Participating in and following the community’s practices is a good choice. We can create a Token on the settings/tokens page to sync labels from other projects. There is no permission requirement for this Token, so no option needs to be checked.

  • Install PyGithub
1
pip install PyGithub==1.50
  • Run the script below, replacing the relevant variables
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# -*- coding: utf-8 -*-
from github import Github

g = Github("xxxTokenxxx")

u_repo = g.get_repo("kubernetes/kubernetes")
my_repo = g.get_repo("shaowenchen/test")

labels = u_repo.get_labels()

for label in labels:
    try:
        my_repo.create_label(label.name, label.color)
    except Exception as e:
        print(e)

3. References


WeChat Official Account
WRITTEN BY
WeChat Official Account