forked from jesusvasquez333/verify-pr-label-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify_pr_lables.py
executable file
·112 lines (89 loc) · 3.93 KB
/
verify_pr_lables.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
import os
import sys
import re
from github import Github
def get_env_var(env_var_name, echo_value=False):
"""Try to get the value from a environmental variable.
If the values is 'None', then a ValueError exception will
be thrown.
Args:
env_var_name (string): The name of the environmental variable.
echo_value (bool): Print the resulting value
Returns:
string: the value from the environmental variable.
"""
value=os.environ.get(env_var_name)
if value == None:
raise ValueError(f'The environmental variable {env_var_name} is empty!')
if echo_value:
print(f"{env_var_name} = {value}")
return value
# Check if the number of input arguments is correct
if len(sys.argv) < 3:
print(sys.argv)
raise ValueError('Invalid number of arguments!')
# Get the GitHub token
token=sys.argv[1]
# Get the list of valid labels
valid_labels=sys.argv[2]
# Post Review
post_review=sys.argv[3]
#Post custom comment
default_comment=f"This pull request does not contain a valid label. Please add one of the following labels: `{valid_labels}`"
post_comment=sys.argv[4] if sys.argv[4] != 'false' else default_comment
print(f'Valid labels are: {valid_labels}')
# Get needed values from the environmental variables
repo_name=get_env_var('GITHUB_REPOSITORY')
github_ref=get_env_var('GITHUB_REF')
github_sha=get_env_var('GITHUB_SHA')
# Create a repository object, using the GitHub token
repo = Github(token).get_repo(repo_name)
# Try to extract the pull request number from the GitHub reference.
try:
pr_number=int(re.search('refs/pull/([0-9]+)/merge', github_ref).group(1))
print(f'Pull request number: {pr_number}')
except AttributeError:
raise ValueError(f'The Pull request number could not be extracted from the GITHUB_REF = {github_ref}')
# Create a pull request object
pr = repo.get_pull(pr_number)
# Get the pull request labels
pr_labels = pr.get_labels()
# This is a list of valid label found in the pull request
pr_valid_labels = []
# Check which of the label in the pull request, are in the
# list of valid labels
for label in pr_labels:
if label.name in valid_labels:
pr_valid_labels.append(label.name)
# Check if there were at least one valid label
# Note: In both cases we exit without an error code and let the check to succeed. This is because GitHub
# workflow will create different checks for different trigger conditions. So, adding a missing label won't
# clear the initial failed check during the PR creation, for example.
# Instead, we will create a pull request review, marked with 'REQUEST_CHANGES' when no valid label was found.
# This will prevent merging the pull request until a valid label is added, which will trigger this check again
# and will create a new pull request review, but in this case marked as 'APPROVE'
if len(pr_valid_labels):
# If there were valid labels, then create a pull request request review, approving it
print(f'Success! This pull request contains the following valid labels: {pr_valid_labels}')
repo.get_commit(sha=github_sha).create_status(
state="success",
target_url="https://amazon.com",
description="This pull request contains the following valid labels",
context="ci/FooCI"
)
if post_review == "true":
pr.create_review(event = 'APPROVE')
else:
# If there were not valid labels, then create a pull request review, requesting changes
print(f'Error! This pull request does not contain any of the valid labels: {valid_labels}')
repo.get_commit(sha=github_sha).create_status(
state="failure",
target_url="https://amazon.com",
description="This pull request does not contain any of the valid labels",
context="ci/FooCI"
)
if post_review == "true":
pr.create_review(body = 'This pull request does not contain a valid label. '
f'{post_comment}',
event = 'REQUEST_CHANGES')