cilium
84 строки · 2.6 Кб
1#!/usr/bin/env python3
2
3"""
4This script requires PyGithub to be installed
5`pip install pygithub`
6
7GITHUB_TOKEN env variable is used to access GH API
8"""
9
10import argparse
11import os
12import sys
13
14try:
15from github import Github
16except ImportError:
17print("pygithub not found you can install it by running 'pip3 install --user PyGithub'")
18sys.exit(-1)
19
20parser = argparse.ArgumentParser()
21parser.add_argument('pr_number', type=int)
22actions = ["pending", "done"]
23parser.add_argument('action', type=str, choices=actions)
24parser.add_argument('version', type=str, default="1.0", nargs='?')
25
26args = parser.parse_args()
27
28token = os.environ["GITHUB_TOKEN"]
29pr_number = args.pr_number
30action = args.action
31version = args.version
32
33g = Github(token)
34cilium = g.get_repo("cilium/cilium")
35pr = cilium.get_pull(pr_number)
36pr_labels = list(pr.get_labels())
37old_label_len = len(pr_labels)
38
39cilium_labels = cilium.get_labels()
40
41# After the introduction of the "Update labels of backported PRs" GH
42# workflow and all the workflows in the stable branches that calls it,
43# there is no need to use this script to update the backported PRs label.
44# Specifically, this script shouldn't be used with the `action` parameter
45# set to `done` anymore.
46# However, since there might still be in-flight backporting PRs relying on
47# the old backporting workflow (based on this script to update the labels),
48# we leave the code here.
49#
50# This can be updated once all the new workflows will be in place and the
51# "old" backport PRs will have their labels updated.
52
53print("Setting labels for PR {}... ".format(pr_number), end="")
54if action == "pending":
55pr_labels = [l for l in pr_labels
56if l.name != "needs-backport/"+version]
57if old_label_len - 1 != len(pr_labels):
58print("needs-backport/"+version+" label not found in PR, exiting")
59sys.exit(1)
60
61pr_labels.append(
62[l for l in cilium_labels if l.name == "backport-pending/"+version][0])
63
64if old_label_len != len(pr_labels):
65print("error adding backport-pending/"+version+" label to PR, exiting")
66sys.exit(2)
67pr.set_labels(*pr_labels)
68
69if action == "done":
70pr_labels = [l for l in pr_labels
71if l.name != "backport-pending/"+version]
72if old_label_len - 1 != len(pr_labels):
73print("backport-pending/"+version+" label not found in PR, exiting")
74sys.exit(1)
75
76pr_labels.append(
77[l for l in cilium_labels if l.name == "backport-done/"+version][0])
78
79if old_label_len != len(pr_labels):
80print("error adding backport-done/"+version+" label to PR, exiting")
81sys.exit(2)
82pr.set_labels(*pr_labels)
83
84print("✓")
85