Skip to content

Commit 386e7c2

Browse files
committed
🚧 added repository list feature for bitbucket
Signed-off-by: Guyzmo <[email protected]>
1 parent 4beb0d9 commit 386e7c2

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

git_repo/services/ext/bitbucket.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,67 @@ def delete(self, repo, user=None):
7070
raise ResourceNotFoundError("Cannot delete: repository {}/{} does not exists.".format(user, repo)) from err
7171
raise ResourceError("Couldn't complete deletion: {}".format(err)) from err
7272

73+
def list(self, user, _long=False):
74+
import shutil, sys
75+
from datetime import datetime
76+
term_width = shutil.get_terminal_size((80, 20)).columns
77+
def col_print(lines, indent=0, pad=2):
78+
# prints a list of items in a fashion similar to the dir command
79+
# borrowed from https://gist.github.com/critiqjo/2ca84db26daaeb1715e1
80+
n_lines = len(lines)
81+
if n_lines == 0:
82+
return
83+
col_width = max(len(line) for line in lines)
84+
n_cols = int((term_width + pad - indent)/(col_width + pad))
85+
n_cols = min(n_lines, max(1, n_cols))
86+
col_len = int(n_lines/n_cols) + (0 if n_lines % n_cols == 0 else 1)
87+
if (n_cols - 1) * col_len >= n_lines:
88+
n_cols -= 1
89+
cols = [lines[i*col_len : i*col_len + col_len] for i in range(n_cols)]
90+
rows = list(zip(*cols))
91+
rows_missed = zip(*[col[len(rows):] for col in cols[:-1]])
92+
rows.extend(rows_missed)
93+
for row in rows:
94+
print(" "*indent + (" "*pad).join(line.ljust(col_width) for line in row))
95+
96+
try:
97+
user = User.find_user_by_username(user)
98+
except HTTPError as err:
99+
raise ResourceNotFoundError("User {} does not exists.".format(user)) from err
100+
101+
repositories = user.repositories()
102+
if not _long:
103+
repositories = list(repositories)
104+
col_print(["/".join([user.username, repo.name]) for repo in repositories])
105+
else:
106+
print('Status\tCommits\tReqs\tIssues\tForks\tCoders\tWatch\tLikes\tLang\tModif\t\tName', file=sys.stderr)
107+
for repo in repositories:
108+
# if repo.updated_at.year < datetime.now().year:
109+
# date_fmt = "%b %d %Y"
110+
# else:
111+
# date_fmt = "%b %d %H:%M"
112+
113+
status = ''.join([
114+
'F' if getattr(repo, 'parent', None) else ' ', # is a fork?
115+
'P' if repo.is_private else ' ', # is private?
116+
])
117+
print('\t'.join([
118+
# status
119+
status,
120+
# stats
121+
str(len(list(repo.commits()))), # number of commits
122+
str(len(list(repo.pullrequests()))), # number of pulls
123+
str('N.A.'), # number of issues
124+
str(len(list(repo.forks()))), # number of forks
125+
str('N.A.'), # number of contributors
126+
str(len(list(repo.watchers()))), # number of subscribers
127+
str('N.A.'), # number of ♥
128+
# info
129+
repo.language or '?', # language
130+
repo.updated_on, # date
131+
'/'.join([user.username, repo.name]), # name
132+
]))
133+
73134
def get_repository(self, user, repo):
74135
try:
75136
return next(self.bb.repositoryByOwnerAndRepositoryName(owner=user, repository_name=repo))

0 commit comments

Comments
 (0)