Skip to content

Commit d696f89

Browse files
committed
Add support to protect branches
1 parent 659869b commit d696f89

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

branchmgr/main.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import functools
2-
import json
32
import os
43
import pprint
54
import sys
@@ -71,10 +70,10 @@ async def _set_branch_protection(self, owner, reponame, branch, status):
7170
Given a branch on a repository, sets the branch protection status to
7271
status. Status must be a dictionary.
7372
"""
74-
encoded_status = json.dumps(status)
7573
return await self._gh.put(
7674
f"/repos/{owner}/{reponame}/branches/{branch}/protection",
77-
accept=accept_format(version="loki-preview")
75+
data=status,
76+
accept=accept_format(version="loki-preview"),
7877
)
7978

8079
async def branch_requires_review(self, owner, reponame, branch):
@@ -96,7 +95,7 @@ async def protect_branch(self, owner, reponame, branch):
9695
Protects a given branch.
9796
"""
9897
protection_status = protection_data()
99-
self._set_branch_protection(owner, reponame, branch, protection_status)
98+
await self._set_branch_protection(owner, reponame, branch, protection_status)
10099

101100

102101
@click.group()
@@ -105,17 +104,33 @@ def cli():
105104

106105

107106
@cli.command()
107+
@click.argument('organisation')
108108
@click.argument('repo')
109109
@click.argument('branch')
110110
@synchronize
111-
async def protection(repo, branch):
111+
async def protection(organisation, repo, branch):
112112
"""
113113
Query the protection status of a branch.
114114
"""
115115
client = APIClient()
116-
organisation, reponame = repo.split('/', 1)
117116

118-
if await client.branch_requires_review(organisation, reponame, branch):
119-
print(f"{organisation}/{reponame}@{branch} requires review")
117+
if await client.branch_requires_review(organisation, repo, branch):
118+
print(f"{organisation}/{repo}@{branch} requires review")
120119
else:
121-
print(f"{organisation}/{reponame}@{branch} does not require review")
120+
print(f"{organisation}/{repo}@{branch} does not require review")
121+
122+
123+
@cli.command()
124+
@click.argument('organisation')
125+
@click.argument('repo')
126+
@click.argument('branch')
127+
@synchronize
128+
async def protect(organisation, repo, branch):
129+
"""
130+
Protect a branch.
131+
132+
This command sets a branch as protected using the default settings that
133+
branchmgr provides.
134+
"""
135+
client = APIClient()
136+
await client.protect_branch(organisation, repo, branch)

0 commit comments

Comments
 (0)