1010import github
1111import requests
1212
13- import git_portfolio .config_manager as config_manager
13+ import git_portfolio .config_manager as cm
1414import git_portfolio .prompt as prompt
1515
1616# starting log
2020LOGGER = logging .getLogger (__name__ )
2121
2222
23- class PortfolioManager :
24- """Portfolio manager class."""
23+ class GithubManager :
24+ """Github manager class."""
2525
26- def __init__ (self ) -> None :
26+ def __init__ (self , config : cm . Config ) -> None :
2727 """Constructor."""
28- self .config_manager = config_manager .ConfigManager ()
29- self .configs = self .config_manager .load_configs ()
30- if self .configs .github_access_token :
31- self .github_setup ()
28+ self .config = config
29+ if config .github_access_token :
30+ self ._github_setup ()
3231 else :
3332 self .config_ini ()
3433
@@ -37,15 +36,15 @@ def create_issues(
3736 ) -> None :
3837 """Create issues."""
3938 if not issue :
40- issue = prompt .create_issues (self .configs .github_selected_repos )
39+ issue = prompt .create_issues (self .config .github_selected_repos )
4140 labels = (
4241 [label .strip () for label in issue .labels .split ("," )] if issue .labels else []
4342 )
4443
4544 if github_repo :
4645 self ._create_issue_from_repo (github_repo , issue , labels )
4746 else :
48- for github_repo in self .configs .github_selected_repos :
47+ for github_repo in self .config .github_selected_repos :
4948 self ._create_issue_from_repo (github_repo , issue , labels )
5049
5150 def _create_issue_from_repo (
@@ -72,12 +71,12 @@ def create_pull_requests(
7271 ) -> None :
7372 """Create pull requests."""
7473 if not pr :
75- pr = prompt .create_pull_requests (self .configs .github_selected_repos )
74+ pr = prompt .create_pull_requests (self .config .github_selected_repos )
7675
7776 if github_repo :
7877 self ._create_pull_request_from_repo (github_repo , pr )
7978 else :
80- for github_repo in self .configs .github_selected_repos :
79+ for github_repo in self .config .github_selected_repos :
8180 self ._create_pull_request_from_repo (github_repo , pr )
8281
8382 def _create_pull_request_from_repo (self , github_repo : str , pr : Any ) -> None :
@@ -127,7 +126,7 @@ def merge_pull_requests(
127126 """Merge pull requests."""
128127 if not pr_merge :
129128 pr_merge = prompt .merge_pull_requests (
130- self .github_username , self .configs .github_selected_repos
129+ self .github_username , self .config .github_selected_repos
131130 )
132131 # Important note: base and head arguments have different import formats.
133132 # https://developer.github.com/v3/pulls/#list-pull-requests
@@ -138,7 +137,7 @@ def merge_pull_requests(
138137 if github_repo :
139138 self ._merge_pull_request_from_repo (github_repo , head , pr_merge , state )
140139 else :
141- for github_repo in self .configs .github_selected_repos :
140+ for github_repo in self .config .github_selected_repos :
142141 self ._merge_pull_request_from_repo (github_repo , head , pr_merge , state )
143142
144143 def _merge_pull_request_from_repo (
@@ -175,12 +174,12 @@ def _merge_pull_request_from_repo(
175174 def delete_branches (self , branch : str = "" , github_repo : str = "" ) -> None :
176175 """Delete branches."""
177176 if not branch :
178- branch = prompt .delete_branches (self .configs .github_selected_repos )
177+ branch = prompt .delete_branches (self .config .github_selected_repos )
179178
180179 if github_repo :
181180 self ._delete_branch_from_repo (github_repo , branch )
182181 else :
183- for github_repo in self .configs .github_selected_repos :
182+ for github_repo in self .config .github_selected_repos :
184183 self ._delete_branch_from_repo (github_repo , branch )
185184
186185 def _delete_branch_from_repo (self , github_repo : str , branch : str ) -> None :
@@ -196,13 +195,13 @@ def _delete_branch_from_repo(self, github_repo: str, branch: str) -> None:
196195 def get_github_connection (self ) -> github .Github :
197196 """Get Github connection."""
198197 # GitHub Enterprise
199- if self .configs .github_hostname :
200- base_url = f"https://{ self .configs .github_hostname } /api/v3"
198+ if self .config .github_hostname :
199+ base_url = f"https://{ self .config .github_hostname } /api/v3"
201200 return github .Github (
202- base_url = base_url , login_or_token = self .configs .github_access_token
201+ base_url = base_url , login_or_token = self .config .github_access_token
203202 )
204203 # GitHub.com
205- return github .Github (self .configs .github_access_token )
204+ return github .Github (self .config .github_access_token )
206205
207206 def get_github_username (
208207 self ,
@@ -216,9 +215,14 @@ def get_github_username(
216215 except (github .BadCredentialsException , github .GithubException ):
217216 return ""
218217 except requests .exceptions .ConnectionError :
219- sys .exit ("Unable to reach server. Please check you network." )
218+ sys .exit (
219+ (
220+ "Unable to reach server. Please check you network and credentials "
221+ "and try again."
222+ )
223+ )
220224
221- def get_github_repos (
225+ def _get_github_repos (
222226 self ,
223227 user : Union [
224228 github .AuthenticatedUser .AuthenticatedUser , github .NamedUser .NamedUser
@@ -227,42 +231,36 @@ def get_github_repos(
227231 """Get Github repos from user."""
228232 return user .get_repos ()
229233
230- def config_repos (self ) -> None :
234+ def config_ini (self ) -> None :
235+ """Initialize app configuration."""
236+ # only config if gets a valid connection
237+ valid = False
238+ while not valid :
239+ answers = prompt .connect_github (self .config .github_access_token )
240+ self .config .github_access_token = answers .github_access_token .strip ()
241+ self .config .github_hostname = answers .github_hostname
242+ valid = self ._github_setup ()
243+ if not valid :
244+ print ("Wrong GitHub token/permissions. Please try again." )
245+ self .config_repos ()
246+
247+ def config_repos (self ) -> Optional [cm .Config ]:
231248 """Configure repos in use."""
232- if self .configs .github_selected_repos :
233- print ("\n The configured repos will be used:" )
234- for repo in self .configs .github_selected_repos :
235- print (" *" , repo )
236- new_repos = prompt .new_repos ()
249+ if self .config .github_selected_repos :
250+ new_repos = prompt .new_repos (self .config .github_selected_repos )
237251 if not new_repos :
238- print ("gitp successfully configured." )
239- return
252+ return None
240253
241254 repo_names = [repo .full_name for repo in self .github_repos ]
255+ self .config .github_selected_repos = prompt .select_repos (repo_names )
256+ return self .config
242257
243- self .configs .github_selected_repos = prompt .select_repos (repo_names )
244- self .config_manager .save_configs (self .configs )
245- print ("gitp successfully configured." )
246-
247- def github_setup (self ) -> bool :
258+ def _github_setup (self ) -> bool :
248259 """Setup Github connection properties."""
249260 self .github_connection = self .get_github_connection ()
250261 user = self .github_connection .get_user ()
251262 self .github_username = self .get_github_username (user )
252263 if not self .github_username :
253264 return False
254- self .github_repos = self .get_github_repos (user )
265+ self .github_repos = self ._get_github_repos (user )
255266 return True
256-
257- def config_ini (self ) -> None :
258- """Initialize app configuration."""
259- # only config if gets a valid connection
260- valid = False
261- while not valid :
262- answers = prompt .connect_github (self .configs .github_access_token )
263- self .configs .github_access_token = answers .github_access_token .strip ()
264- self .configs .github_hostname = answers .github_hostname
265- valid = self .github_setup ()
266- if not valid :
267- print ("Wrong GitHub token/permissions. Please try again." )
268- self .config_repos ()
0 commit comments