Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit f46e8aa

Browse files
author
staticdev
committed
Black
1 parent b6f19b0 commit f46e8aa

File tree

2 files changed

+197
-54
lines changed

2 files changed

+197
-54
lines changed

src/__main__.py

Lines changed: 197 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def _ignore_if_not_confirmed(answers: Dict[str, Any]) -> bool:
3535
return not answers["confirmation"]
3636

3737

38-
class Manager():
38+
class Manager:
3939
def __init__(self):
4040
self.github_repos = []
4141
self.github_connection = None
@@ -44,60 +44,134 @@ def __init__(self):
4444

4545
def create_issues(self) -> None:
4646
questions = [
47-
inquirer.Text('title', message='Write an issue title', validate=_not_empty_validation),
48-
inquirer.Text('body', message='Write an issue body [optional]'),
49-
inquirer.Text('labels', message='Write issue labels [optional, separated by comma]'),
50-
inquirer.Confirm('correct', message='Confirm creation of issue for the project(s) {}. Continue?'.format(self.configs.github_selected_repos), default=False),
47+
inquirer.Text(
48+
"title", message="Write an issue title", validate=_not_empty_validation
49+
),
50+
inquirer.Text("body", message="Write an issue body [optional]"),
51+
inquirer.Text(
52+
"labels", message="Write issue labels [optional, separated by comma]"
53+
),
54+
inquirer.Confirm(
55+
"correct",
56+
message="Confirm creation of issue for the project(s) {}. Continue?".format(
57+
self.configs.github_selected_repos
58+
),
59+
default=False,
60+
),
5161
]
5262
answers = inquirer.prompt(questions)
53-
labels = [label.strip() for label in answers['labels'].split(",")] if answers['labels'] else []
54-
if answers['correct']:
63+
labels = (
64+
[label.strip() for label in answers["labels"].split(",")]
65+
if answers["labels"]
66+
else []
67+
)
68+
if answers["correct"]:
5569
for github_repo in self.configs.github_selected_repos:
5670
repo = self.github_connection.get_repo(github_repo)
5771
try:
58-
repo.create_issue(title=answers['title'], body=answers['body'], labels=labels)
72+
repo.create_issue(
73+
title=answers["title"], body=answers["body"], labels=labels
74+
)
5975
print("{}: issue created successfully.".format(github_repo))
6076
except github.GithubException as github_exception:
61-
if github_exception.data["message"] == "Issues are disabled for this repo":
62-
print("{}: {}. It may be a fork.".format(github_repo, github_exception.data["message"]))
77+
if (
78+
github_exception.data["message"]
79+
== "Issues are disabled for this repo"
80+
):
81+
print(
82+
"{}: {}. It may be a fork.".format(
83+
github_repo, github_exception.data["message"]
84+
)
85+
)
6386
else:
64-
print("{}: {}.".format(github_repo, github_exception.data["message"]))
87+
print(
88+
"{}: {}.".format(
89+
github_repo, github_exception.data["message"]
90+
)
91+
)
6592

6693
def create_pull_requests(self) -> None:
6794
# TODO create issue for github to add labels to their API
6895
questions = [
69-
inquirer.Text('base', message='Write base branch name (destination)', default="master", validate=_not_empty_validation),
70-
inquirer.Text('head', message='Write the head branch name (source)', validate=_not_empty_validation),
71-
inquirer.Text('title', message='Write a PR title', validate=_not_empty_validation),
72-
inquirer.Text('body', message='Write an PR body [optional]'),
73-
inquirer.Confirm('draft', message='Do you want to create a draft PR?', default=False),
74-
inquirer.Text('labels', message='Write PR labels [optional, separated by comma]'),
75-
inquirer.Confirm('confirmation', message='Do you want to link pull request to issues by title?', default=False),
76-
inquirer.Text('link', message='Write issue title (or part of it)', validate=_not_empty_validation, ignore=_ignore_if_not_confirmed),
77-
inquirer.Confirm('inherit_labels', message='Do you want to add labels inherited from the issues?', default=True, ignore=_ignore_if_not_confirmed),
78-
inquirer.Confirm('correct', message='Confirm creation of pull request(s) for the project(s) {}. Continue?'.format(self.configs.github_selected_repos), default=False)
96+
inquirer.Text(
97+
"base",
98+
message="Write base branch name (destination)",
99+
default="master",
100+
validate=_not_empty_validation,
101+
),
102+
inquirer.Text(
103+
"head",
104+
message="Write the head branch name (source)",
105+
validate=_not_empty_validation,
106+
),
107+
inquirer.Text(
108+
"title", message="Write a PR title", validate=_not_empty_validation
109+
),
110+
inquirer.Text("body", message="Write an PR body [optional]"),
111+
inquirer.Confirm(
112+
"draft", message="Do you want to create a draft PR?", default=False
113+
),
114+
inquirer.Text(
115+
"labels", message="Write PR labels [optional, separated by comma]"
116+
),
117+
inquirer.Confirm(
118+
"confirmation",
119+
message="Do you want to link pull request to issues by title?",
120+
default=False,
121+
),
122+
inquirer.Text(
123+
"link",
124+
message="Write issue title (or part of it)",
125+
validate=_not_empty_validation,
126+
ignore=_ignore_if_not_confirmed,
127+
),
128+
inquirer.Confirm(
129+
"inherit_labels",
130+
message="Do you want to add labels inherited from the issues?",
131+
default=True,
132+
ignore=_ignore_if_not_confirmed,
133+
),
134+
inquirer.Confirm(
135+
"correct",
136+
message="Confirm creation of pull request(s) for the project(s) {}. Continue?".format(
137+
self.configs.github_selected_repos
138+
),
139+
default=False,
140+
),
79141
]
80142
answers = inquirer.prompt(questions)
81-
labels = set(label.strip() for label in answers['labels'].split(",")) if answers['labels'] else set()
82-
if answers['correct']:
83-
body = answers['body']
143+
labels = (
144+
set(label.strip() for label in answers["labels"].split(","))
145+
if answers["labels"]
146+
else set()
147+
)
148+
if answers["correct"]:
149+
body = answers["body"]
84150
for github_repo in self.configs.github_selected_repos:
85151
repo = self.github_connection.get_repo(github_repo)
86152
# link issues
87-
if answers['confirmation']:
88-
issues = repo.get_issues(state='open')
153+
if answers["confirmation"]:
154+
issues = repo.get_issues(state="open")
89155
closes = ""
90156
for issue in issues:
91-
if answers['link'] in issue.title:
157+
if answers["link"] in issue.title:
92158
closes += "#{} ".format(issue.number)
93-
if answers['inherit_labels']:
94-
issue_labels = [label.name for label in issue.get_labels()]
159+
if answers["inherit_labels"]:
160+
issue_labels = [
161+
label.name for label in issue.get_labels()
162+
]
95163
labels.update(issue_labels)
96164
closes = closes.strip()
97165
if closes:
98166
body += "\n\nCloses {}".format(closes)
99167
try:
100-
pr = repo.create_pull(title=answers['title'], body=body, head=answers['head'], base=answers['base'], draft=answers['draft'])
168+
pr = repo.create_pull(
169+
title=answers["title"],
170+
body=body,
171+
head=answers["head"],
172+
base=answers["base"],
173+
draft=answers["draft"],
174+
)
101175
print("{}: PR created successfully.".format(github_repo))
102176
# PyGithub does not support a list of strings for adding (only one str)
103177
for label in labels:
@@ -109,68 +183,126 @@ def create_pull_requests(self) -> None:
109183
extra += "{} ".format(error["message"])
110184
else:
111185
extra += "Invalid field {}. ".format(error["field"])
112-
print("{}: {}. {}".format(github_repo, github_exception.data["message"], extra))
186+
print(
187+
"{}: {}. {}".format(
188+
github_repo, github_exception.data["message"], extra
189+
)
190+
)
113191

114192
def merge_pull_requests(self) -> None:
115193
"""Merge pull request."""
116194
state = "open"
117195
questions = [
118-
inquirer.Text('base', message='Write base branch name (destination)', default="master", validate=_not_empty_validation),
119-
inquirer.Text('head', message='Write the head branch name (source)', validate=_not_empty_validation),
120-
inquirer.Text('prefix', message='Write base user or organization name from PR head', default=self.github_username, validate=_not_empty_validation),
121-
inquirer.Confirm('correct', message='Confirm merging of pull request(s) for the project(s) {}. Continue?'.format(self.configs.github_selected_repos), default=False)
196+
inquirer.Text(
197+
"base",
198+
message="Write base branch name (destination)",
199+
default="master",
200+
validate=_not_empty_validation,
201+
),
202+
inquirer.Text(
203+
"head",
204+
message="Write the head branch name (source)",
205+
validate=_not_empty_validation,
206+
),
207+
inquirer.Text(
208+
"prefix",
209+
message="Write base user or organization name from PR head",
210+
default=self.github_username,
211+
validate=_not_empty_validation,
212+
),
213+
inquirer.Confirm(
214+
"correct",
215+
message="Confirm merging of pull request(s) for the project(s) {}. Continue?".format(
216+
self.configs.github_selected_repos
217+
),
218+
default=False,
219+
),
122220
]
123221
answers = inquirer.prompt(questions)
124222
# Important note: base and head arguments have different import formats.
125223
# https://developer.github.com/v3/pulls/#list-pull-requests
126224
# head needs format "user/org:branch"
127225
head = "{}:{}".format(answers["prefix"], answers["head"])
128226

129-
if answers['correct']:
227+
if answers["correct"]:
130228
for github_repo in self.configs.github_selected_repos:
131229
repo = self.github_connection.get_repo(github_repo)
132-
pulls = repo.get_pulls(state=state, base=answers['base'], head=head)
230+
pulls = repo.get_pulls(state=state, base=answers["base"], head=head)
133231
if pulls.totalCount == 1:
134232
pull = pulls[0]
135233
if pull.mergeable:
136234
try:
137235
pull.merge()
138236
print("{}: PR merged successfully.".format(github_repo))
139237
except github.GithubException as github_exception:
140-
print("{}: {}.".format(github_repo, github_exception.data["message"]))
238+
print(
239+
"{}: {}.".format(
240+
github_repo, github_exception.data["message"]
241+
)
242+
)
141243
else:
142-
print("{}: PR not mergeable, GitHub checks may be running.".format(github_repo))
244+
print(
245+
"{}: PR not mergeable, GitHub checks may be running.".format(
246+
github_repo
247+
)
248+
)
143249
else:
144-
print("{}: no open PR found for {}:{}.".format(github_repo, answers['base'], answers['head']))
250+
print(
251+
"{}: no open PR found for {}:{}.".format(
252+
github_repo, answers["base"], answers["head"]
253+
)
254+
)
145255

146256
def delete_branches(self):
147257
questions = [
148-
inquirer.Text('branch', message='Write the branch name', validate=_not_empty_validation),
149-
inquirer.Confirm('correct', message='Confirm deleting of branch(es) for the project(s) {}. Continue?'.format(self.configs.github_selected_repos), default=False)
258+
inquirer.Text(
259+
"branch",
260+
message="Write the branch name",
261+
validate=_not_empty_validation,
262+
),
263+
inquirer.Confirm(
264+
"correct",
265+
message="Confirm deleting of branch(es) for the project(s) {}. Continue?".format(
266+
self.configs.github_selected_repos
267+
),
268+
default=False,
269+
),
150270
]
151271
answers = inquirer.prompt(questions)
152-
if answers['correct']:
272+
if answers["correct"]:
153273
for github_repo in self.configs.github_selected_repos:
154274
repo = self.github_connection.get_repo(github_repo)
155275
try:
156-
branch = repo.get_branch(branch=answers['branch'])
276+
branch = repo.get_branch(branch=answers["branch"])
157277

158278
print("{}: branch deleted successfully.".format(github_repo))
159279
except github.GithubException as github_exception:
160-
print("{}: {}.".format(github_repo, github_exception.data["message"]))
280+
print(
281+
"{}: {}.".format(github_repo, github_exception.data["message"])
282+
)
161283

162284
def connect_github(self) -> None:
163285
questions = [
164286
# inquirer.Text('github_username', message='GitHub username', default=self.github_username, validate=_not_empty_validation),
165-
inquirer.Password('github_access_token', message='GitHub access token', validate=_not_empty_validation, default=self.configs.github_access_token),
166-
inquirer.Text('github_hostname', message='GitHub hostname (change ONLY if you use GitHub Enterprise)'),
287+
inquirer.Password(
288+
"github_access_token",
289+
message="GitHub access token",
290+
validate=_not_empty_validation,
291+
default=self.configs.github_access_token,
292+
),
293+
inquirer.Text(
294+
"github_hostname",
295+
message="GitHub hostname (change ONLY if you use GitHub Enterprise)",
296+
),
167297
]
168298
answers = inquirer.prompt(questions)
169299
self.configs.github_access_token = answers["github_access_token"].strip()
170300
# GitHub Enterprise
171301
if answers["github_hostname"]:
172302
base_url = "https://{}/api/v3".format(answers["github_hostname"])
173-
self.github_connection = github.Github(base_url=base_url, login_or_token=self.configs.github_access_token)
303+
self.github_connection = github.Github(
304+
base_url=base_url, login_or_token=self.configs.github_access_token
305+
)
174306
# GitHub.com
175307
else:
176308
self.github_connection = github.Github(self.configs.github_access_token)
@@ -190,21 +322,32 @@ def select_github_repos(self) -> None:
190322
print("\nThe configured repos will be used:")
191323
for repo in self.configs.github_selected_repos:
192324
print(" *", repo)
193-
answer = inquirer.prompt([inquirer.Confirm('', message="Deseja selecionar novas áreas de projeto?")])['']
325+
answer = inquirer.prompt(
326+
[
327+
inquirer.Confirm(
328+
"", message="Deseja selecionar novas áreas de projeto?"
329+
)
330+
]
331+
)[""]
194332
if not answer:
195333
print("gitp successfully configured.")
196-
return self.create_pull_requests()
334+
return self.merge_pull_requests()
197335

198336
try:
199337
repo_names = [repo.full_name for repo in self.github_repos]
200338
except Exception as ex:
201339
print(ex)
202340

203341
while True:
204-
selected = inquirer.prompt([inquirer.Checkbox("github_repos",
205-
message="Which repos are you working on? (Select pressing space)",
206-
choices=repo_names,
207-
)])["github_repos"]
342+
selected = inquirer.prompt(
343+
[
344+
inquirer.Checkbox(
345+
"github_repos",
346+
message="Which repos are you working on? (Select pressing space)",
347+
choices=repo_names,
348+
)
349+
]
350+
)["github_repos"]
208351
if len(selected) == 0:
209352
print("Please select with `space` at least one repo.\n")
210353
else:
1.54 KB
Binary file not shown.

0 commit comments

Comments
 (0)