Skip to content

Commit 23dcaeb

Browse files
author
grenston
committed
exception handled for branch not found
1 parent d39fc66 commit 23dcaeb

File tree

2 files changed

+51
-29
lines changed

2 files changed

+51
-29
lines changed

automate-branch-rules.py

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from github import Github
2+
from github import GithubException
3+
import sys
24
from stdiomask import getpass
35
from config import branches
46
from config import branch_rules
@@ -10,51 +12,71 @@ def add_all(pat):
1012
print("")
1113
for repo in git.get_organization(org_name).get_repos():
1214
for branch_name in branches:
13-
branch = repo.get_branch(branch_name)
14-
if(add_codeowners_file):
15-
codeowners.add(org_name,pat,repo.name,branch_name)
16-
branch.edit_protection(**branch_rules)
17-
if(signed_commit):
18-
branch.add_required_signatures()
15+
try:
16+
repo.get_branch(branch_name)
17+
except GithubException:
18+
print("Error:",repo.name,",",branch_name,"-->",sys.exc_info()[1])
1919
else:
20-
branch.remove_required_signatures()
21-
print("Edited the branch protection rules for: " + repo.name + "," + branch_name)
20+
branch = repo.get_branch(branch_name)
21+
if(add_codeowners_file):
22+
codeowners.add(org_name,pat,repo.name,branch_name)
23+
branch.edit_protection(**branch_rules)
24+
if(signed_commit):
25+
branch.add_required_signatures()
26+
else:
27+
branch.remove_required_signatures()
28+
print("Edited the branch protection rules for: " + repo.name + "," + branch_name)
2229

2330
def add_one(pat):
2431
repo_name = input("\nRepository: ")
2532
repo = git.get_repo(org_name+"/"+repo_name)
2633
for branch_name in branches:
27-
branch = repo.get_branch(branch_name)
28-
if(add_codeowners_file):
29-
codeowners.add(org_name,pat,repo_name,branch_name)
30-
branch.edit_protection(**branch_rules)
31-
if(signed_commit):
32-
branch.add_required_signatures()
34+
try:
35+
repo.get_branch(branch_name)
36+
except GithubException:
37+
print("Error:",repo.name,",",branch_name,"-->",sys.exc_info()[1])
3338
else:
34-
branch.remove_required_signatures()
35-
print("Edited the branch protection rules for: " + repo.name + "," + branch_name)
39+
branch=repo.get_branch(branch_name)
40+
if(add_codeowners_file):
41+
codeowners.add(org_name,pat,repo_name,branch_name)
42+
branch.edit_protection(**branch_rules)
43+
if(signed_commit):
44+
branch.add_required_signatures()
45+
else:
46+
branch.remove_required_signatures()
47+
print("Edited the branch protection rules for: " + repo.name + "," + branch_name)
3648

3749
def remove_one():
3850
repo_name = input("\nRepository: ")
3951
repo = git.get_repo(org_name+"/"+repo_name)
4052
for branch_name in branches:
41-
branch = repo.get_branch(branch_name)
42-
if (branch.protected):
43-
branch.remove_protection()
44-
print("Removed branch protection rules for: " + repo.name + "," + branch_name)
53+
try:
54+
repo.get_branch(branch_name)
55+
except GithubException:
56+
print("Error:",repo.name,",",branch_name,"-->",sys.exc_info()[1])
4557
else:
46-
print("No branch protection rules for: " + repo.name + "," + branch.name)
58+
branch = repo.get_branch(branch_name)
59+
if (branch.protected):
60+
branch.remove_protection()
61+
print("Removed branch protection rules for: " + repo.name + "," + branch_name)
62+
else:
63+
print("No branch protection rules for: " + repo.name + "," + branch.name)
4764

4865
def remove_all():
4966
print("")
5067
for repo in git.get_organization(org_name).get_repos():
5168
for branch_name in branches:
52-
branch = repo.get_branch(branch_name)
53-
if (branch.protected):
54-
branch.remove_protection()
55-
print("Removed branch protection rules for: " + repo.name + "," + branch_name)
69+
try:
70+
repo.get_branch(branch_name)
71+
except GithubException:
72+
print("Error:",repo.name,",",branch_name,"-->",sys.exc_info()[1])
5673
else:
57-
print("No branch protection rules for: " + repo.name + "," + branch.name)
74+
branch = repo.get_branch(branch_name)
75+
if (branch.protected):
76+
branch.remove_protection()
77+
print("Removed branch protection rules for: " + repo.name + "," + branch_name)
78+
else:
79+
print("No branch protection rules for: " + repo.name + "," + branch.name)
5880

5981
print(" ___ _ _ ______ _ ______ _ ")
6082
print(" / _ \ | | | | | ___ \ | | | ___ \ | | ")

config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
branches = ("master","dev")
2-
add_codeowners_file = False
1+
branches = ("dev","master")
2+
add_codeowners_file = True
33
signed_commit = False
44
branch_rules = { "required_approving_review_count":1,
55
"require_code_owner_reviews":True,
66
"contexts":["CodeQL"],
77
"strict":True
8-
}
8+
}

0 commit comments

Comments
 (0)