10000 🚧 Support for listing of repositories by guyzmo · Pull Request #78 · guyzmo/git-repo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

🚧 Support for listing of repositories #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion git_repo/services/ext/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import gitlab
from gitlab.exceptions import GitlabCreateError, GitlabGetError

import json
import json, time

@register_target('lab', 'gitlab')
class GitlabService(RepositoryService):
Expand Down Expand Up @@ -66,6 +66,66 @@ def delete(self, repo, user=None):
except Exception as err:
raise ResourceError("Unhandled exception: {}".format(err)) from err

def list(self, user, _long=False):
import shutil, sys
from datetime import datetime
term_width = shutil.get_terminal_size((80, 20)).columns
def col_print(lines, indent=0, pad=2):
# prints a list of items in a fashion similar to the dir command
# borrowed from https://gist.github.com/critiqjo/2ca84db26daaeb1715e1
n_lines = len(lines)
if n_lines == 0:
return
col_width = max(len(line) for line in lines)
n_cols = int((term_width + pad - indent)/(col_width + pad))
n_cols = min(n_lines, max(1, n_cols))
col_len = int(n_lines/n_cols) + (0 if n_lines % n_cols == 0 else 1)
if (n_cols - 1) * col_len >= n_lines:
n_cols -= 1
cols = [lines[i*col_len : i*col_len + col_len] for i in range(n_cols)]
rows = list(zip(*cols))
rows_missed = zip(*[col[len(rows):] for col in cols[:-1]])
rows.extend(rows_missed)
for row in rows:
print(" "*indent + (" "*pad).join(line.ljust(col_width) for line in row))

if not self.gl.users.search(user):
raise ResourceNotFoundError("User {} does not exists.".format(user))

repositories = self.gl.projects.list(author=user)
if not _long:
repositories = list(repositories)
col_print([repo.path_with_namespace for repo in repositories])
else:
print('Status\tCommits\tReqs\tIssues\tForks\tCoders\tWatch\tLikes\tLang\tModif\t\tName', file=sys.stderr)
for repo in repositories:
time.sleep(0.5)
# if repo.last_activity_at.year < datetime.now().year:
# date_fmt = "%b %d %Y"
# else:
# date_fmt = "%b %d %H:%M"

status = ''.join([
'F' if False else ' ', # is a fork?
'P' if repo.visibility_level == 0 else ' ', # is private?
])
print('\t'.join([
# status
status,
# stats
str(len(list(repo.commits.list()))), # number of commits
str(len(list(repo.mergerequests.list()))), # number of pulls
str(len(list(repo.issues.list()))), # number of issues
str(repo.forks_count), # number of forks
str(len(list(repo.members.list()))), # number of contributors
'N.A.', # number of subscribers
str(repo.star_count), # number of ♥
# info
'N.A.', # language
repo.last_activity_at, # date
repo.name_with_namespace, # name
]))

def get_repository(self, user, repo):
try:
return self.gl.projects.get('{}/{}'.format(user, repo))
Expand Down
10 changes: 10 additions & 0 deletions git_repo/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,16 @@ def delete(self, repo, user=None): #pragma: no cover
'''
raise NotImplementedError

def list(self, user, _long=False):
'''List an user's repositories on the service

:param user: name of the user
:param _long: format of the listing

Meant to be implemented by subclasses
'''
raise NotImplementedError

def create(self, user, repo, add=False): #pragma: no cover
'''Create a new remote repository on the service

Expand Down
0