File: push_labels.py

package info (click to toggle)
python-azure 20181112%2Bgit-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 407,300 kB
  • sloc: python: 717,190; makefile: 201; sh: 76
file content (39 lines) | stat: -rw-r--r-- 1,028 bytes parent folder | download
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
import os
import sys

from github import Github, GithubException

LABEL_COLOUR = "e99695"


def get_repo(repo_name):
    con = Github(os.environ["GH_TOKEN"])
    repo = con.get_repo(repo_name)
    repo.name  # Force checking if repo exists, otherwise "get_repo" does nothing
    return repo


def create_label(repo, label):
    print(f"Adding label {label}")
    try:
        repo.create_label(label, LABEL_COLOUR)
        print(f"-> Created label {label}")
    except GithubException as err:
        err_code = err.data['errors'][0].get('code', '')
        if err.status == 422 and err_code == "already_exists":
            print(f"-> Label {label} already exists")
            return
        raise

def do(repo_name, label_filepath):
    print(f"Getting repo {repo_name}")
    repo = get_repo(repo_name)

    print("Adding labels to repo")
    with open(label_filepath, "r") as fd:
        for label in fd.read().splitlines():
            create_label(repo, label)


if __name__ == "__main__":
    do(sys.argv[1], sys.argv[2])