|
9 | 9 | import gitlab
|
10 | 10 | from gitlab.exceptions import GitlabCreateError, GitlabGetError
|
11 | 11 |
|
12 |
| -import json |
| 12 | +import json, time |
13 | 13 |
|
14 | 14 | @register_target('lab', 'gitlab')
|
15 | 15 | class GitlabService(RepositoryService):
|
@@ -66,6 +66,66 @@ def delete(self, repo, user=None):
|
66 | 66 | except Exception as err:
|
67 | 67 | raise ResourceError("Unhandled exception: {}".format(err)) from err
|
68 | 68 |
|
| 69 | + def list(self, user, _long=False): |
| 70 | + import shutil, sys |
| 71 | + from datetime import datetime |
| 72 | + term_width = shutil.get_terminal_size((80, 20)).columns |
| 73 | + def col_print(lines, indent=0, pad=2): |
| 74 | + # prints a list of items in a fashion similar to the dir command |
| 75 | + # borrowed from https://gist.github.com/critiqjo/2ca84db26daaeb1715e1 |
| 76 | + n_lines = len(lines) |
| 77 | + if n_lines == 0: |
| 78 | + return |
| 79 | + col_width = max(len(line) for line in lines) |
| 80 | + n_cols = int((term_width + pad - indent)/(col_width + pad)) |
| 81 | + n_cols = min(n_lines, max(1, n_cols)) |
| 82 | + col_len = int(n_lines/n_cols) + (0 if n_lines % n_cols == 0 else 1) |
| 83 | + if (n_cols - 1) * col_len >= n_lines: |
| 84 | + n_cols -= 1 |
| 85 | + cols = [lines[i*col_len : i*col_len + col_len] for i in range(n_cols)] |
| 86 | + rows = list(zip(*cols)) |
| 87 | + rows_missed = zip(*[col[len(rows):] for col in cols[:-1]]) |
| 88 | + rows.extend(rows_missed) |
| 89 | + for row in rows: |
| 90 | + print(" "*indent + (" "*pad).join(line.ljust(col_width) for line in row)) |
| 91 | + |
| 92 | + if not self.gl.users.search(user): |
| 93 | + raise ResourceNotFoundError("User {} does not exists.".format(user)) |
| 94 | + |
| 95 | + repositories = self.gl.projects.list(author=user) |
| 96 | + if not _long: |
| 97 | + repositories = list(repositories) |
| 98 | + col_print([repo.path_with_namespace for repo in repositories]) |
| 99 | + else: |
| 100 | + print('Status\tCommits\tReqs\tIssues\tForks\tCoders\tWatch\tLikes\tLang\tModif\t\tName', file=sys.stderr) |
| 101 | + for repo in repositories: |
| 102 | + time.sleep(0.5) |
| 103 | + # if repo.last_activity_at.year < datetime.now().year: |
| 104 | + # date_fmt = "%b %d %Y" |
| 105 | + # else: |
| 106 | + # date_fmt = "%b %d %H:%M" |
| 107 | + |
| 108 | + status = ''.join([ |
| 109 | + 'F' if False else ' ', # is a fork? |
| 110 | + 'P' if repo.visibility_level == 0 else ' ', # is private? |
| 111 | + ]) |
| 112 | + print('\t'.join([ |
| 113 | + # status |
| 114 | + status, |
| 115 | + # stats |
| 116 | + str(len(list(repo.commits.list()))), # number of commits |
| 117 | + str(len(list(repo.mergerequests.list()))), # number of pulls |
| 118 | + str(len(list(repo.issues.list()))), # number of issues |
| 119 | + str(repo.forks_count), # number of forks |
| 120 | + str(len(list(repo.members.list()))), # number of contributors |
| 121 | + 'N.A.', # number of subscribers |
| 122 | + str(repo.star_count), # number of ♥ |
| 123 | + # info |
| 124 | + 'N.A.', # language |
| 125 | + repo.last_activity_at, # date |
| 126 | + repo.name_with_namespace, # name |
| 127 | + ])) |
| 128 | + |
69 | 129 | def get_repository(self, user, repo):
|
70 | 130 | try:
|
71 | 131 | return self.gl.projects.get('{}/{}'.format(user, repo))
|
|
0 commit comments