3
3
import sys
4
4
import typing
5
5
6
+ import attr
6
7
import click
7
8
from requests .auth import HTTPBasicAuth
8
9
9
- from labels import __version__
10
+ from labels import __version__ , utils
10
11
from labels .exceptions import LabelsException
11
- from labels .github import Client , Label
12
+ from labels .github import Client , Label , Repository
12
13
from labels .io import write_labels , read_labels
13
14
from labels .log import create_logger
14
15
15
16
Labels_Dict = typing .Dict [str , Label ]
16
17
17
18
19
+ @attr .s (auto_attribs = True )
20
+ class LabelsContext :
21
+ """Extra context for label CLI commands."""
22
+
23
+ client : Client
24
+ repository : typing .Optional [Repository ] = None
25
+
26
+
18
27
@click .group ()
19
28
@click .pass_context
20
29
@click .version_option (__version__ , "-V" , "--version" , prog_name = "labels" )
@@ -45,13 +54,49 @@ def labels(ctx, username: str, token: str, verbose: bool) -> None:
45
54
else :
46
55
logger .setLevel (logging .INFO )
47
56
48
- ctx .obj = Client (HTTPBasicAuth (username , token ))
57
+ ctx .obj = LabelsContext (Client (HTTPBasicAuth (username , token )))
58
+
59
+
60
+ @click .pass_obj
61
+ def default_owner (labels_context : LabelsContext ) -> str :
62
+ """Load repository owner information from the local working tree."""
63
+ if labels_context .repository is None :
64
+ repository = utils .load_repository_info ()
65
+ if repository is None :
66
+ raise click .BadParameter ("Unable to load repository information." )
67
+ labels_context .repository = repository
68
+ return labels_context .repository .owner
69
+
70
+
71
+ @click .pass_obj
72
+ def default_repo (labels_context : LabelsContext ) -> str :
73
+ """Load repository name information from the local working tree."""
74
+ if labels_context .repository is None :
75
+ repository = utils .load_repository_info ()
76
+ if repository is None :
77
+ raise click .BadParameter ("Unable to load repository information." )
78
+ labels_context .repository = repository
79
+ return labels_context .repository .name
49
80
50
81
51
82
@labels .command ("fetch" )
52
83
@click .pass_obj
53
- @click .option ("-o" , "--owner" , help = "GitHub owner name" , type = str , required = True )
54
- @click .option ("-r" , "--repo" , help = "GitHub repository name" , type = str , required = True )
84
+ @click .option (
85
+ "-o" ,
86
+ "--owner" ,
87
+ help = "GitHub owner name" ,
88
+ type = str ,
89
+ default = default_owner ,
90
+ required = True ,
91
+ )
92
+ @click .option (
93
+ "-r" ,
94
+ "--repo" ,
95
+ help = "GitHub repository name" ,
96
+ type = str ,
97
+ default = default_repo ,
98
+ required = True ,
99
+ )
55
100
@click .option (
56
101
"-f" ,
57
102
"--filename" ,
@@ -60,13 +105,16 @@ def labels(ctx, username: str, token: str, verbose: bool) -> None:
60
105
type = click .Path (),
61
106
required = True ,
62
107
)
63
- def fetch_cmd (client : Client , owner : str , repo : str , filename : str ) -> None :
108
+ def fetch_cmd (context : LabelsContext , owner : str , repo : str , filename : str ) -> None :
64
109
"""Fetch labels for a GitHub repository.
65
110
66
111
This will write the labels information to disk to the specified filename.
67
112
"""
113
+
114
+ repository = Repository (owner , repo )
115
+
68
116
try :
69
- labels = client .list_labels (owner , repo )
117
+ labels = context . client .list_labels (repository )
70
118
except LabelsException as exc :
71
119
click .echo (str (exc ))
72
120
sys .exit (1 )
@@ -79,8 +127,22 @@ def fetch_cmd(client: Client, owner: str, repo: str, filename: str) -> None:
79
127
80
128
@labels .command ("sync" )
81
129
@click .pass_obj
82
- @click .option ("-o" , "--owner" , help = "GitHub owner name" , type = str , required = True )
83
- @click .option ("-r" , "--repo" , help = "GitHub repository name" , type = str , required = True )
130
+ @click .option (
131
+ "-o" ,
132
+ "--owner" ,
133
+ help = "GitHub owner name" ,
134
+ type = str ,
135
+ default = default_owner ,
136
+ required = True ,
137
+ )
138
+ @click .option (
139
+ "-r" ,
140
+ "--repo" ,
141
+ help = "GitHub repository name" ,
142
+ type = str ,
143
+ default = default_repo ,
144
+ required = True ,
145
+ )
84
146
@click .option ("-n" , "--dryrun" , help = "Do not modify remote labels" , is_flag = True )
85
147
@click .option (
86
148
"-f" ,
@@ -91,7 +153,7 @@ def fetch_cmd(client: Client, owner: str, repo: str, filename: str) -> None:
91
153
required = True ,
92
154
)
93
155
def sync_cmd (
94
- client : Client , owner : str , repo : str , filename : str , dryrun : bool
156
+ context : LabelsContext , owner : str , repo : str , filename : str , dryrun : bool
95
157
) -> None :
96
158
"""Sync labels with a GitHub repository.
97
159
@@ -105,8 +167,10 @@ def sync_cmd(
105
167
106
168
local_labels = read_labels (filename )
107
169
170
+ repository = Repository (owner , repo )
171
+
108
172
try :
109
- remote_labels = {l .name : l for l in client .list_labels (owner , repo )}
173
+ remote_labels = {l .name : l for l in context . client .list_labels (repository )}
110
174
except LabelsException as exc :
111
175
click .echo (str (exc ), err = True )
112
176
sys .exit (1 )
@@ -152,21 +216,21 @@ def sync_cmd(
152
216
153
217
for name in labels_to_delete .keys ():
154
218
try :
155
- client .delete_label (owner , repo , name = name )
219
+ context . client .delete_label (repository , name = name )
156
220
except LabelsException as exc :
157
221
click .echo (str (exc ), err = True )
158
222
failures .append (name )
159
223
160
224
for name , label in labels_to_update .items ():
161
225
try :
162
- client .edit_label (owner , repo , name = name , label = label )
226
+ context . client .edit_label (repository , name = name , label = label )
163
227
except LabelsException as exc :
164
228
click .echo (str (exc ), err = True )
165
229
failures .append (name )
166
230
167
231
for name , label in labels_to_create .items ():
168
232
try :
169
- client .create_label (owner , repo , label = label )
233
+ context . client .create_label (repository , label = label )
170
234
except LabelsException as exc :
171
235
click .echo (str (exc ), err = True )
172
236
failures .append (name )
0 commit comments