|
| 1 | +import functools |
1 | 2 | import json
|
2 | 3 | import os
|
3 | 4 | import pprint
|
4 | 5 | import sys
|
5 | 6 |
|
| 7 | +import click |
6 | 8 | import gidgethub
|
7 | 9 |
|
8 | 10 | from twisted.internet import reactor, defer, task
|
|
13 | 15 | USER_AGENT = "Lukasa"
|
14 | 16 |
|
15 | 17 |
|
| 18 | +def synchronize(f): |
| 19 | + """ |
| 20 | + A function decorator that takes an async function and wraps it in a |
| 21 | + function that can invoke it synchronously using twisted's task.react. This |
| 22 | + is done to bridge between Twisted and click. |
| 23 | + """ |
| 24 | + @functools.wraps(f) |
| 25 | + def inner(*args, **kwargs): |
| 26 | + d = defer.ensureDeferred(f(*args, **kwargs)) |
| 27 | + task.react(lambda *args: d) |
| 28 | + |
| 29 | + return inner |
| 30 | + |
| 31 | + |
16 | 32 | def protection_data():
|
17 | 33 | """
|
18 | 34 | Get a dictionary representing the JSON required to protect a branch.
|
@@ -83,23 +99,23 @@ async def protect_branch(self, owner, reponame, branch):
|
83 | 99 | self._set_branch_protection(owner, reponame, branch, protection_status)
|
84 | 100 |
|
85 | 101 |
|
86 |
| -async def async_main(): |
| 102 | +@click.group() |
| 103 | +def cli(): |
| 104 | + pass |
| 105 | + |
| 106 | + |
| 107 | +@cli.command() |
| 108 | +@click.argument('repo') |
| 109 | +@click.argument('branch') |
| 110 | +@synchronize |
| 111 | +async def protection(repo, branch): |
| 112 | + """ |
| 113 | + Query the protection status of a branch. |
| 114 | + """ |
87 | 115 | client = APIClient()
|
88 |
| - organisation, reponame = sys.argv[1].split('/', 1) |
89 |
| - try: |
90 |
| - branch = sys.argv[2] |
91 |
| - except IndexError: |
92 |
| - branch = sys.argv[2] |
| 116 | + organisation, reponame = repo.split('/', 1) |
93 | 117 |
|
94 | 118 | if await client.branch_requires_review(organisation, reponame, branch):
|
95 | 119 | print(f"{organisation}/{reponame}@{branch} requires review")
|
96 | 120 | else:
|
97 | 121 | print(f"{organisation}/{reponame}@{branch} does not require review")
|
98 |
| - |
99 |
| - |
100 |
| -def sync_main(reactor, *args): |
101 |
| - return defer.ensureDeferred(async_main(*args)) |
102 |
| - |
103 |
| - |
104 |
| -def main(): |
105 |
| - task.react(sync_main) |
|
0 commit comments