Skip to content

Commit e6054d9

Browse files
sandrobonazzolakwk
andcommitted
add markdown format output
and enable it for bugzilla, github and gitlab. Signed-off-by: Sandro Bonazzola <[email protected]> Co-authored-by: Konrad Kleine <[email protected]>
1 parent a3a49fa commit e6054d9

File tree

7 files changed

+63
-29
lines changed

7 files changed

+63
-29
lines changed

README.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ The default output is plain text of maximum width 79 characters.
8686
This can be adjusted using the ``--width`` parameter. To disable
8787
shortening altogether use ``--width=0``. The default width value
8888
can be saved in the config file as well. Use ``--format=wiki`` to
89-
enable simple MoinMoin wiki syntax. For stats which support them,
89+
enable simple MoinMoin wiki syntax or ``--format=wiki`` to enable
90+
markdown syntax. For stats which support them,
9091
``--brief`` and ``--verbose`` can be used to specify a different
9192
level of detail to be shown.
9293

93-
--format=FMT
94-
Output style, possible values: text (default) or wiki
94+
--format {text,md,wiki}
95+
Output style, default: text
9596

9697
--width=WIDTH
9798
Maximum width of the report output (default: 79)

did/cli.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ def __init__(self, arguments=None):
7777
# Formating options
7878
group = self.parser.add_argument_group("Format")
7979
group.add_argument(
80-
"--format", default="text",
81-
help="Output style, possible values: text (default) or wiki")
80+
"--format", default="text", choices=["text", "md", "wiki"],
81+
help="Output style, default: text")
8282
group.add_argument(
8383
"--width", default=width, type=int,
8484
help="Maximum width of the report output (default: %(default)s)")
@@ -149,8 +149,17 @@ def parse(self):
149149
raise RuntimeError(
150150
"Invalid date range ({0} to {1})".format(
151151
opt.since, opt.until.date - delta(days=1)))
152-
header = "Status report for {0} ({1} to {2}).".format(
152+
153+
header = "Status report for {0} ({1} to {2})".format(
153154
period, opt.since, opt.until.date - delta(days=1))
155+
if opt.format == "md":
156+
# in markdown first line must be an header
157+
# using alternate syntax allowing to use did's
158+
# output in commit messages as well
159+
header = f"{header}\n{'=' * len(header)}"
160+
else:
161+
# in markdown no trailing punctuation is allowed in headings
162+
header = header + "."
154163

155164
# Finito
156165
log.debug("Gathered options:")

did/plugins/bugzilla.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ def __str__(self):
140140
""" Consistent identifier and summary for displaying """
141141
if self.options.format == "wiki":
142142
return "<<Bug({0})>> - {1}".format(self.id, self.summary)
143+
elif self.options.format == "md":
144+
link = self.parent.url.replace("xmlrpc.cgi", "show_bug.cgi?id=")
145+
return "[{0}#{1}]({2}{1}) - {3}".format(
146+
self.prefix, str(self.id), link, self.summary)
143147
else:
144148
return "{0}#{1} - {2}".format(
145149
self.prefix, str(self.id).rjust(7, "0"), self.summary)

did/plugins/github.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,26 @@ def search(self, query):
110110
class Issue(object):
111111
""" GitHub Issue """
112112

113-
def __init__(self, data):
113+
def __init__(self, data, parent):
114114
self.data = data
115115
self.title = data["title"]
116116
matched = re.search(
117117
r"/repos/([^/]+)/([^/]+)/issues/(\d+)", data["url"])
118118
self.owner = matched.groups()[0]
119119
self.project = matched.groups()[1]
120120
self.id = matched.groups()[2]
121+
self.options = parent.options
121122

122123
def __str__(self):
123124
""" String representation """
124-
return "{0}/{1}#{2} - {3}".format(
125-
self.owner, self.project,
126-
str(self.id).zfill(PADDING), self.data["title"])
125+
if self.options.format == "md":
126+
return "[{0}/{1}#{2}](https://github.com/{0}/{1}/issues/{2}) - {3}".format(
127+
self.owner, self.project,
128+
str(self.id), self.data["title"].strip())
129+
else:
130+
return "{0}/{1}#{2} - {3}".format(
131+
self.owner, self.project,
132+
str(self.id).zfill(PADDING), self.data["title"])
127133

128134

129135
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -139,7 +145,7 @@ def fetch(self):
139145
self.user.login, self.options.since, self.options.until)
140146
query += "+type:issue"
141147
self.stats = [
142-
Issue(issue) for issue in self.parent.github.search(query)]
148+
Issue(issue, self.parent) for issue in self.parent.github.search(query)]
143149

144150

145151
class IssuesClosed(Stats):
@@ -151,7 +157,7 @@ def fetch(self):
151157
self.user.login, self.options.since, self.options.until)
152158
query += "+type:issue"
153159
self.stats = [
154-
Issue(issue) for issue in self.parent.github.search(query)]
160+
Issue(issue, self.parent) for issue in self.parent.github.search(query)]
155161

156162

157163
class IssueCommented(Stats):
@@ -163,7 +169,7 @@ def fetch(self):
163169
self.user.login, self.options.since, self.options.until)
164170
query += "+type:issue"
165171
self.stats = [
166-
Issue(issue) for issue in self.parent.github.search(query)]
172+
Issue(issue, self.parent) for issue in self.parent.github.search(query)]
167173

168174

169175
class PullRequestsCreated(Stats):
@@ -176,7 +182,7 @@ def fetch(self):
176182
self.user.login, self.options.since, self.options.until)
177183
query += "+type:pr"
178184
self.stats = [
179-
Issue(issue) for issue in self.parent.github.search(query)]
185+
Issue(issue, self.parent) for issue in self.parent.github.search(query)]
180186

181187

182188
class PullRequestsCommented(Stats):
@@ -189,7 +195,7 @@ def fetch(self):
189195
self.user.login, self.options.since, self.options.until)
190196
query += "+type:pr"
191197
self.stats = [
192-
Issue(issue) for issue in self.parent.github.search(query)]
198+
Issue(issue, self.parent) for issue in self.parent.github.search(query)]
193199

194200

195201
class PullRequestsClosed(Stats):
@@ -202,7 +208,7 @@ def fetch(self):
202208
self.user.login, self.options.since, self.options.until)
203209
query += "+type:pr"
204210
self.stats = [
205-
Issue(issue) for issue in self.parent.github.search(query)]
211+
Issue(issue, self.parent) for issue in self.parent.github.search(query)]
206212

207213

208214
class PullRequestsReviewed(Stats):
@@ -215,7 +221,7 @@ def fetch(self):
215221
self.user.login, self.options.since, self.options.until)
216222
query += "+type:pr"
217223
self.stats = [
218-
Issue(issue) for issue in self.parent.github.search(query)]
224+
Issue(issue, self.parent) for issue in self.parent.github.search(query)]
219225

220226

221227
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

did/plugins/gitlab.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ def search(self, user, since, until, target_type, action_name):
163163
class Issue(object):
164164
""" GitLab Issue """
165165

166-
def __init__(self, data, gitlabapi):
166+
def __init__(self, data, gitlabapi, parent):
167+
self.parent = parent
167168
self.data = data
168169
self.gitlabapi = gitlabapi
169170
self.project = self.gitlabapi.get_project(data['project_id'])
@@ -176,9 +177,18 @@ def iid(self):
176177

177178
def __str__(self):
178179
""" String representation """
179-
return "{0}#{1} - {2}".format(
180-
self.project['path_with_namespace'],
181-
str(self.id).zfill(PADDING), self.title)
180+
if self.parent.options.format == "md":
181+
return "[{1}#{3}]({0}/{1}/-/{2}/{3}) - {4}".format(
182+
self.gitlabapi.url,
183+
self.project['path_with_namespace'],
184+
("issues"
185+
if self.data['target_type'] == 'Issue'
186+
else "merge_requests"),
187+
str(self.id), self.title)
188+
else:
189+
return "{0}#{1} - {2}".format(
190+
self.project['path_with_namespace'],
191+
str(self.id).zfill(PADDING), self.title)
182192

183193

184194
class MergeRequest(Issue):
@@ -223,7 +233,7 @@ def fetch(self):
223233
self.user.login, self.options.since, self.options.until,
224234
'Issue', 'opened')
225235
self.stats = [
226-
Issue(issue, self.parent.gitlab)
236+
Issue(issue, self.parent.gitlab, self.parent)
227237
for issue in results]
228238

229239

@@ -237,7 +247,7 @@ def fetch(self):
237247
self.user.login, self.options.since, self.options.until,
238248
'Note', 'commented on')
239249
self.stats = [
240-
Note(issue, self.parent.gitlab)
250+
Note(issue, self.parent.gitlab, self.parent)
241251
for issue in results
242252
if issue['note']['noteable_type'] == 'Issue']
243253

@@ -252,7 +262,7 @@ def fetch(self):
252262
self.user.login, self.options.since, self.options.until,
253263
'Issue', 'closed')
254264
self.stats = [
255-
Issue(issue, self.parent.gitlab)
265+
Issue(issue, self.parent.gitlab, self.parent)
256266
for issue in results]
257267

258268

@@ -266,7 +276,7 @@ def fetch(self):
266276
self.user.login, self.options.since, self.options.until,
267277
'MergeRequest', 'opened')
268278
self.stats = [
269-
MergeRequest(mr, self.parent.gitlab)
279+
MergeRequest(mr, self.parent.gitlab, self.parent)
270280
for mr in results]
271281

272282

@@ -280,7 +290,7 @@ def fetch(self):
280290
self.user.login, self.options.since, self.options.until,
281291
'Note', 'commented on')
282292
self.stats = [
283-
Note(issue, self.parent.gitlab)
293+
Note(issue, self.parent.gitlab, self.parent)
284294
for issue in results
285295
if issue['note']['noteable_type'] == 'MergeRequest']
286296

@@ -295,7 +305,7 @@ def fetch(self):
295305
self.user.login, self.options.since, self.options.until,
296306
'MergeRequest', 'accepted')
297307
self.stats = [
298-
MergeRequest(mr, self.parent.gitlab)
308+
MergeRequest(mr, self.parent.gitlab, self.parent)
299309
for mr in results]
300310

301311

@@ -309,7 +319,7 @@ def fetch(self):
309319
self.user.login, self.options.since, self.options.until,
310320
'MergeRequest', 'approved')
311321
self.stats = [
312-
MergeRequest(mr, self.parent.gitlab)
322+
MergeRequest(mr, self.parent.gitlab, self.parent)
313323
for mr in results]
314324

315325

did/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ def item(text, level=0, options=None):
186186
return
187187
# Four space for each level, additional space for wiki format
188188
indent = level * 4
189+
if options.format == "md":
190+
indent = level * 2
189191
if options.format == "wiki" and level == 0:
190192
indent = 1
191193
# Shorten the text if necessary to match the desired maximum width

docs/examples.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ corresponding option groups for each of them::
9494
--footer All above
9595

9696
Format:
97-
--format FORMAT Output style, possible values: text (default) or wiki
97+
Format:
98+
--format {text,md,wiki}
99+
Output style, default: text
98100
--width WIDTH Maximum width of the report output (default: 79)
99101
--brief Show brief summary only, do not list individual items
100102
--verbose Include more details (like modified git directories)

0 commit comments

Comments
 (0)