##// END OF EJS Templates
issues: make issue_prefix optional again...
Thomas De Schampheleire -
r7083:62b7f3d2 default
parent child Browse files
Show More
@@ -1,1266 +1,1266
1 1 # -*- coding: utf-8 -*-
2 2 # This program is free software: you can redistribute it and/or modify
3 3 # it under the terms of the GNU General Public License as published by
4 4 # the Free Software Foundation, either version 3 of the License, or
5 5 # (at your option) any later version.
6 6 #
7 7 # This program is distributed in the hope that it will be useful,
8 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 # GNU General Public License for more details.
11 11 #
12 12 # You should have received a copy of the GNU General Public License
13 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 """
15 15 Helper functions
16 16
17 17 Consists of functions to typically be used within templates, but also
18 18 available to Controllers. This module is available to both as 'h'.
19 19 """
20 20 import hashlib
21 21 import json
22 22 import StringIO
23 23 import logging
24 24 import re
25 25 import urlparse
26 26 import textwrap
27 27
28 28 from beaker.cache import cache_region
29 29 from pygments.formatters.html import HtmlFormatter
30 30 from pygments import highlight as code_highlight
31 31 from tg.i18n import ugettext as _
32 32
33 33 from webhelpers.html import literal, HTML, escape
34 34 from webhelpers.html.tags import checkbox, end_form, hidden, link_to, \
35 35 select, submit, text, password, textarea, radio, form as insecure_form
36 36 from webhelpers.number import format_byte_size
37 37 from webhelpers.pylonslib import Flash as _Flash
38 38 from webhelpers.pylonslib.secure_form import secure_form, authentication_token
39 39 from webhelpers.text import chop_at, truncate, wrap_paragraphs
40 40 from webhelpers.html.tags import _set_input_attrs, _set_id_attr, \
41 41 convert_boolean_attrs, NotGiven, _make_safe_id_component
42 42
43 43 from kallithea.config.routing import url
44 44 from kallithea.lib.annotate import annotate_highlight
45 45 from kallithea.lib.pygmentsutils import get_custom_lexer
46 46 from kallithea.lib.utils2 import str2bool, safe_unicode, safe_str, \
47 47 time_to_datetime, AttributeDict, safe_int, MENTIONS_REGEX
48 48 from kallithea.lib.markup_renderer import url_re
49 49 from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError
50 50 from kallithea.lib.vcs.backends.base import BaseChangeset, EmptyChangeset
51 51
52 52 log = logging.getLogger(__name__)
53 53
54 54
55 55 def canonical_url(*args, **kargs):
56 56 '''Like url(x, qualified=True), but returns url that not only is qualified
57 57 but also canonical, as configured in canonical_url'''
58 58 from kallithea import CONFIG
59 59 try:
60 60 parts = CONFIG.get('canonical_url', '').split('://', 1)
61 61 kargs['host'] = parts[1].split('/', 1)[0]
62 62 kargs['protocol'] = parts[0]
63 63 except IndexError:
64 64 kargs['qualified'] = True
65 65 return url(*args, **kargs)
66 66
67 67
68 68 def canonical_hostname():
69 69 '''Return canonical hostname of system'''
70 70 from kallithea import CONFIG
71 71 try:
72 72 parts = CONFIG.get('canonical_url', '').split('://', 1)
73 73 return parts[1].split('/', 1)[0]
74 74 except IndexError:
75 75 parts = url('home', qualified=True).split('://', 1)
76 76 return parts[1].split('/', 1)[0]
77 77
78 78
79 79 def html_escape(s):
80 80 """Return string with all html escaped.
81 81 This is also safe for javascript in html but not necessarily correct.
82 82 """
83 83 return (s
84 84 .replace('&', '&amp;')
85 85 .replace(">", "&gt;")
86 86 .replace("<", "&lt;")
87 87 .replace('"', "&quot;")
88 88 .replace("'", "&#39;") # some mail readers use HTML 4 and doesn't support &apos;
89 89 )
90 90
91 91 def js(value):
92 92 """Convert Python value to the corresponding JavaScript representation.
93 93
94 94 This is necessary to safely insert arbitrary values into HTML <script>
95 95 sections e.g. using Mako template expression substitution.
96 96
97 97 Note: Rather than using this function, it's preferable to avoid the
98 98 insertion of values into HTML <script> sections altogether. Instead,
99 99 data should (to the extent possible) be passed to JavaScript using
100 100 data attributes or AJAX calls, eliminating the need for JS specific
101 101 escaping.
102 102
103 103 Note: This is not safe for use in attributes (e.g. onclick), because
104 104 quotes are not escaped.
105 105
106 106 Because the rules for parsing <script> varies between XHTML (where
107 107 normal rules apply for any special characters) and HTML (where
108 108 entities are not interpreted, but the literal string "</script>"
109 109 is forbidden), the function ensures that the result never contains
110 110 '&', '<' and '>', thus making it safe in both those contexts (but
111 111 not in attributes).
112 112 """
113 113 return literal(
114 114 ('(' + json.dumps(value) + ')')
115 115 # In JSON, the following can only appear in string literals.
116 116 .replace('&', r'\x26')
117 117 .replace('<', r'\x3c')
118 118 .replace('>', r'\x3e')
119 119 )
120 120
121 121
122 122 def jshtml(val):
123 123 """HTML escapes a string value, then converts the resulting string
124 124 to its corresponding JavaScript representation (see `js`).
125 125
126 126 This is used when a plain-text string (possibly containing special
127 127 HTML characters) will be used by a script in an HTML context (e.g.
128 128 element.innerHTML or jQuery's 'html' method).
129 129
130 130 If in doubt, err on the side of using `jshtml` over `js`, since it's
131 131 better to escape too much than too little.
132 132 """
133 133 return js(escape(val))
134 134
135 135
136 136 def shorter(s, size=20, firstline=False, postfix='...'):
137 137 """Truncate s to size, including the postfix string if truncating.
138 138 If firstline, truncate at newline.
139 139 """
140 140 if firstline:
141 141 s = s.split('\n', 1)[0].rstrip()
142 142 if len(s) > size:
143 143 return s[:size - len(postfix)] + postfix
144 144 return s
145 145
146 146
147 147 def _reset(name, value=None, id=NotGiven, type="reset", **attrs):
148 148 """
149 149 Reset button
150 150 """
151 151 _set_input_attrs(attrs, type, name, value)
152 152 _set_id_attr(attrs, id, name)
153 153 convert_boolean_attrs(attrs, ["disabled"])
154 154 return HTML.input(**attrs)
155 155
156 156
157 157 reset = _reset
158 158 safeid = _make_safe_id_component
159 159
160 160
161 161 def FID(raw_id, path):
162 162 """
163 163 Creates a unique ID for filenode based on it's hash of path and revision
164 164 it's safe to use in urls
165 165
166 166 :param raw_id:
167 167 :param path:
168 168 """
169 169
170 170 return 'C-%s-%s' % (short_id(raw_id), hashlib.md5(safe_str(path)).hexdigest()[:12])
171 171
172 172
173 173 class _FilesBreadCrumbs(object):
174 174
175 175 def __call__(self, repo_name, rev, paths):
176 176 if isinstance(paths, str):
177 177 paths = safe_unicode(paths)
178 178 url_l = [link_to(repo_name, url('files_home',
179 179 repo_name=repo_name,
180 180 revision=rev, f_path=''),
181 181 class_='ypjax-link')]
182 182 paths_l = paths.split('/')
183 183 for cnt, p in enumerate(paths_l):
184 184 if p != '':
185 185 url_l.append(link_to(p,
186 186 url('files_home',
187 187 repo_name=repo_name,
188 188 revision=rev,
189 189 f_path='/'.join(paths_l[:cnt + 1])
190 190 ),
191 191 class_='ypjax-link'
192 192 )
193 193 )
194 194
195 195 return literal('/'.join(url_l))
196 196
197 197
198 198 files_breadcrumbs = _FilesBreadCrumbs()
199 199
200 200
201 201 class CodeHtmlFormatter(HtmlFormatter):
202 202 """
203 203 My code Html Formatter for source codes
204 204 """
205 205
206 206 def wrap(self, source, outfile):
207 207 return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
208 208
209 209 def _wrap_code(self, source):
210 210 for cnt, it in enumerate(source):
211 211 i, t = it
212 212 t = '<span id="L%s">%s</span>' % (cnt + 1, t)
213 213 yield i, t
214 214
215 215 def _wrap_tablelinenos(self, inner):
216 216 dummyoutfile = StringIO.StringIO()
217 217 lncount = 0
218 218 for t, line in inner:
219 219 if t:
220 220 lncount += 1
221 221 dummyoutfile.write(line)
222 222
223 223 fl = self.linenostart
224 224 mw = len(str(lncount + fl - 1))
225 225 sp = self.linenospecial
226 226 st = self.linenostep
227 227 la = self.lineanchors
228 228 aln = self.anchorlinenos
229 229 nocls = self.noclasses
230 230 if sp:
231 231 lines = []
232 232
233 233 for i in range(fl, fl + lncount):
234 234 if i % st == 0:
235 235 if i % sp == 0:
236 236 if aln:
237 237 lines.append('<a href="#%s%d" class="special">%*d</a>' %
238 238 (la, i, mw, i))
239 239 else:
240 240 lines.append('<span class="special">%*d</span>' % (mw, i))
241 241 else:
242 242 if aln:
243 243 lines.append('<a href="#%s%d">%*d</a>' % (la, i, mw, i))
244 244 else:
245 245 lines.append('%*d' % (mw, i))
246 246 else:
247 247 lines.append('')
248 248 ls = '\n'.join(lines)
249 249 else:
250 250 lines = []
251 251 for i in range(fl, fl + lncount):
252 252 if i % st == 0:
253 253 if aln:
254 254 lines.append('<a href="#%s%d">%*d</a>' % (la, i, mw, i))
255 255 else:
256 256 lines.append('%*d' % (mw, i))
257 257 else:
258 258 lines.append('')
259 259 ls = '\n'.join(lines)
260 260
261 261 # in case you wonder about the seemingly redundant <div> here: since the
262 262 # content in the other cell also is wrapped in a div, some browsers in
263 263 # some configurations seem to mess up the formatting...
264 264 if nocls:
265 265 yield 0, ('<table class="%stable">' % self.cssclass +
266 266 '<tr><td><div class="linenodiv">'
267 267 '<pre>' + ls + '</pre></div></td>'
268 268 '<td id="hlcode" class="code">')
269 269 else:
270 270 yield 0, ('<table class="%stable">' % self.cssclass +
271 271 '<tr><td class="linenos"><div class="linenodiv">'
272 272 '<pre>' + ls + '</pre></div></td>'
273 273 '<td id="hlcode" class="code">')
274 274 yield 0, dummyoutfile.getvalue()
275 275 yield 0, '</td></tr></table>'
276 276
277 277
278 278 _whitespace_re = re.compile(r'(\t)|( )(?=\n|</div>)')
279 279
280 280
281 281 def _markup_whitespace(m):
282 282 groups = m.groups()
283 283 if groups[0]:
284 284 return '<u>\t</u>'
285 285 if groups[1]:
286 286 return ' <i></i>'
287 287
288 288
289 289 def markup_whitespace(s):
290 290 return _whitespace_re.sub(_markup_whitespace, s)
291 291
292 292
293 293 def pygmentize(filenode, **kwargs):
294 294 """
295 295 pygmentize function using pygments
296 296
297 297 :param filenode:
298 298 """
299 299 lexer = get_custom_lexer(filenode.extension) or filenode.lexer
300 300 return literal(markup_whitespace(
301 301 code_highlight(filenode.content, lexer, CodeHtmlFormatter(**kwargs))))
302 302
303 303
304 304 def pygmentize_annotation(repo_name, filenode, **kwargs):
305 305 """
306 306 pygmentize function for annotation
307 307
308 308 :param filenode:
309 309 """
310 310
311 311 color_dict = {}
312 312
313 313 def gen_color(n=10000):
314 314 """generator for getting n of evenly distributed colors using
315 315 hsv color and golden ratio. It always return same order of colors
316 316
317 317 :returns: RGB tuple
318 318 """
319 319
320 320 def hsv_to_rgb(h, s, v):
321 321 if s == 0.0:
322 322 return v, v, v
323 323 i = int(h * 6.0) # XXX assume int() truncates!
324 324 f = (h * 6.0) - i
325 325 p = v * (1.0 - s)
326 326 q = v * (1.0 - s * f)
327 327 t = v * (1.0 - s * (1.0 - f))
328 328 i = i % 6
329 329 if i == 0:
330 330 return v, t, p
331 331 if i == 1:
332 332 return q, v, p
333 333 if i == 2:
334 334 return p, v, t
335 335 if i == 3:
336 336 return p, q, v
337 337 if i == 4:
338 338 return t, p, v
339 339 if i == 5:
340 340 return v, p, q
341 341
342 342 golden_ratio = 0.618033988749895
343 343 h = 0.22717784590367374
344 344
345 345 for _unused in xrange(n):
346 346 h += golden_ratio
347 347 h %= 1
348 348 HSV_tuple = [h, 0.95, 0.95]
349 349 RGB_tuple = hsv_to_rgb(*HSV_tuple)
350 350 yield map(lambda x: str(int(x * 256)), RGB_tuple)
351 351
352 352 cgenerator = gen_color()
353 353
354 354 def get_color_string(cs):
355 355 if cs in color_dict:
356 356 col = color_dict[cs]
357 357 else:
358 358 col = color_dict[cs] = cgenerator.next()
359 359 return "color: rgb(%s)! important;" % (', '.join(col))
360 360
361 361 def url_func(repo_name):
362 362
363 363 def _url_func(changeset):
364 364 author = escape(changeset.author)
365 365 date = changeset.date
366 366 message = escape(changeset.message)
367 367 tooltip_html = ("<b>Author:</b> %s<br/>"
368 368 "<b>Date:</b> %s</b><br/>"
369 369 "<b>Message:</b> %s") % (author, date, message)
370 370
371 371 lnk_format = show_id(changeset)
372 372 uri = link_to(
373 373 lnk_format,
374 374 url('changeset_home', repo_name=repo_name,
375 375 revision=changeset.raw_id),
376 376 style=get_color_string(changeset.raw_id),
377 377 **{'data-toggle': 'popover',
378 378 'data-content': tooltip_html}
379 379 )
380 380
381 381 uri += '\n'
382 382 return uri
383 383 return _url_func
384 384
385 385 return literal(markup_whitespace(annotate_highlight(filenode, url_func(repo_name), **kwargs)))
386 386
387 387
388 388 class _Message(object):
389 389 """A message returned by ``Flash.pop_messages()``.
390 390
391 391 Converting the message to a string returns the message text. Instances
392 392 also have the following attributes:
393 393
394 394 * ``message``: the message text.
395 395 * ``category``: the category specified when the message was created.
396 396 """
397 397
398 398 def __init__(self, category, message):
399 399 self.category = category
400 400 self.message = message
401 401
402 402 def __str__(self):
403 403 return self.message
404 404
405 405 __unicode__ = __str__
406 406
407 407 def __html__(self):
408 408 return escape(safe_unicode(self.message))
409 409
410 410
411 411 class Flash(_Flash):
412 412
413 413 def __call__(self, message, category=None, ignore_duplicate=False, logf=None):
414 414 """
415 415 Show a message to the user _and_ log it through the specified function
416 416
417 417 category: notice (default), warning, error, success
418 418 logf: a custom log function - such as log.debug
419 419
420 420 logf defaults to log.info, unless category equals 'success', in which
421 421 case logf defaults to log.debug.
422 422 """
423 423 if logf is None:
424 424 logf = log.info
425 425 if category == 'success':
426 426 logf = log.debug
427 427
428 428 logf('Flash %s: %s', category, message)
429 429
430 430 super(Flash, self).__call__(message, category, ignore_duplicate)
431 431
432 432 def pop_messages(self):
433 433 """Return all accumulated messages and delete them from the session.
434 434
435 435 The return value is a list of ``Message`` objects.
436 436 """
437 437 from tg import session
438 438 messages = session.pop(self.session_key, [])
439 439 session.save()
440 440 return [_Message(*m) for m in messages]
441 441
442 442
443 443 flash = Flash()
444 444
445 445 #==============================================================================
446 446 # SCM FILTERS available via h.
447 447 #==============================================================================
448 448 from kallithea.lib.vcs.utils import author_name, author_email
449 449 from kallithea.lib.utils2 import credentials_filter, age as _age
450 450
451 451 age = lambda x, y=False: _age(x, y)
452 452 capitalize = lambda x: x.capitalize()
453 453 email = author_email
454 454 short_id = lambda x: x[:12]
455 455 hide_credentials = lambda x: ''.join(credentials_filter(x))
456 456
457 457
458 458 def show_id(cs):
459 459 """
460 460 Configurable function that shows ID
461 461 by default it's r123:fffeeefffeee
462 462
463 463 :param cs: changeset instance
464 464 """
465 465 from kallithea import CONFIG
466 466 def_len = safe_int(CONFIG.get('show_sha_length', 12))
467 467 show_rev = str2bool(CONFIG.get('show_revision_number', False))
468 468
469 469 raw_id = cs.raw_id[:def_len]
470 470 if show_rev:
471 471 return 'r%s:%s' % (cs.revision, raw_id)
472 472 else:
473 473 return raw_id
474 474
475 475
476 476 def fmt_date(date):
477 477 if date:
478 478 return date.strftime("%Y-%m-%d %H:%M:%S").decode('utf8')
479 479
480 480 return ""
481 481
482 482
483 483 def is_git(repository):
484 484 if hasattr(repository, 'alias'):
485 485 _type = repository.alias
486 486 elif hasattr(repository, 'repo_type'):
487 487 _type = repository.repo_type
488 488 else:
489 489 _type = repository
490 490 return _type == 'git'
491 491
492 492
493 493 def is_hg(repository):
494 494 if hasattr(repository, 'alias'):
495 495 _type = repository.alias
496 496 elif hasattr(repository, 'repo_type'):
497 497 _type = repository.repo_type
498 498 else:
499 499 _type = repository
500 500 return _type == 'hg'
501 501
502 502
503 503 @cache_region('long_term', 'user_or_none')
504 504 def user_or_none(author):
505 505 """Try to match email part of VCS committer string with a local user - or return None"""
506 506 from kallithea.model.db import User
507 507 email = author_email(author)
508 508 if email:
509 509 return User.get_by_email(email, cache=True) # cache will only use sql_cache_short
510 510 return None
511 511
512 512
513 513 def email_or_none(author):
514 514 """Try to match email part of VCS committer string with a local user.
515 515 Return primary email of user, email part of the specified author name, or None."""
516 516 if not author:
517 517 return None
518 518 user = user_or_none(author)
519 519 if user is not None:
520 520 return user.email # always use main email address - not necessarily the one used to find user
521 521
522 522 # extract email from the commit string
523 523 email = author_email(author)
524 524 if email:
525 525 return email
526 526
527 527 # No valid email, not a valid user in the system, none!
528 528 return None
529 529
530 530
531 531 def person(author, show_attr="username"):
532 532 """Find the user identified by 'author', return one of the users attributes,
533 533 default to the username attribute, None if there is no user"""
534 534 from kallithea.model.db import User
535 535 # attr to return from fetched user
536 536 person_getter = lambda usr: getattr(usr, show_attr)
537 537
538 538 # if author is already an instance use it for extraction
539 539 if isinstance(author, User):
540 540 return person_getter(author)
541 541
542 542 user = user_or_none(author)
543 543 if user is not None:
544 544 return person_getter(user)
545 545
546 546 # Still nothing? Just pass back the author name if any, else the email
547 547 return author_name(author) or email(author)
548 548
549 549
550 550 def person_by_id(id_, show_attr="username"):
551 551 from kallithea.model.db import User
552 552 # attr to return from fetched user
553 553 person_getter = lambda usr: getattr(usr, show_attr)
554 554
555 555 # maybe it's an ID ?
556 556 if str(id_).isdigit() or isinstance(id_, int):
557 557 id_ = int(id_)
558 558 user = User.get(id_)
559 559 if user is not None:
560 560 return person_getter(user)
561 561 return id_
562 562
563 563
564 564 def boolicon(value):
565 565 """Returns boolean value of a value, represented as small html image of true/false
566 566 icons
567 567
568 568 :param value: value
569 569 """
570 570
571 571 if value:
572 572 return HTML.tag('i', class_="icon-ok")
573 573 else:
574 574 return HTML.tag('i', class_="icon-minus-circled")
575 575
576 576
577 577 def action_parser(user_log, feed=False, parse_cs=False):
578 578 """
579 579 This helper will action_map the specified string action into translated
580 580 fancy names with icons and links
581 581
582 582 :param user_log: user log instance
583 583 :param feed: use output for feeds (no html and fancy icons)
584 584 :param parse_cs: parse Changesets into VCS instances
585 585 """
586 586
587 587 action = user_log.action
588 588 action_params = ' '
589 589
590 590 x = action.split(':')
591 591
592 592 if len(x) > 1:
593 593 action, action_params = x
594 594
595 595 def get_cs_links():
596 596 revs_limit = 3 # display this amount always
597 597 revs_top_limit = 50 # show upto this amount of changesets hidden
598 598 revs_ids = action_params.split(',')
599 599 deleted = user_log.repository is None
600 600 if deleted:
601 601 return ','.join(revs_ids)
602 602
603 603 repo_name = user_log.repository.repo_name
604 604
605 605 def lnk(rev, repo_name):
606 606 lazy_cs = False
607 607 title_ = None
608 608 url_ = '#'
609 609 if isinstance(rev, BaseChangeset) or isinstance(rev, AttributeDict):
610 610 if rev.op and rev.ref_name:
611 611 if rev.op == 'delete_branch':
612 612 lbl = _('Deleted branch: %s') % rev.ref_name
613 613 elif rev.op == 'tag':
614 614 lbl = _('Created tag: %s') % rev.ref_name
615 615 else:
616 616 lbl = 'Unknown operation %s' % rev.op
617 617 else:
618 618 lazy_cs = True
619 619 lbl = rev.short_id[:8]
620 620 url_ = url('changeset_home', repo_name=repo_name,
621 621 revision=rev.raw_id)
622 622 else:
623 623 # changeset cannot be found - it might have been stripped or removed
624 624 lbl = rev[:12]
625 625 title_ = _('Changeset %s not found') % lbl
626 626 if parse_cs:
627 627 return link_to(lbl, url_, title=title_, **{'data-toggle': 'tooltip'})
628 628 return link_to(lbl, url_, class_='lazy-cs' if lazy_cs else '',
629 629 **{'data-raw_id': rev.raw_id, 'data-repo_name': repo_name})
630 630
631 631 def _get_op(rev_txt):
632 632 _op = None
633 633 _name = rev_txt
634 634 if len(rev_txt.split('=>')) == 2:
635 635 _op, _name = rev_txt.split('=>')
636 636 return _op, _name
637 637
638 638 revs = []
639 639 if len(filter(lambda v: v != '', revs_ids)) > 0:
640 640 repo = None
641 641 for rev in revs_ids[:revs_top_limit]:
642 642 _op, _name = _get_op(rev)
643 643
644 644 # we want parsed changesets, or new log store format is bad
645 645 if parse_cs:
646 646 try:
647 647 if repo is None:
648 648 repo = user_log.repository.scm_instance
649 649 _rev = repo.get_changeset(rev)
650 650 revs.append(_rev)
651 651 except ChangesetDoesNotExistError:
652 652 log.error('cannot find revision %s in this repo', rev)
653 653 revs.append(rev)
654 654 else:
655 655 _rev = AttributeDict({
656 656 'short_id': rev[:12],
657 657 'raw_id': rev,
658 658 'message': '',
659 659 'op': _op,
660 660 'ref_name': _name
661 661 })
662 662 revs.append(_rev)
663 663 cs_links = [" " + ', '.join(
664 664 [lnk(rev, repo_name) for rev in revs[:revs_limit]]
665 665 )]
666 666 _op1, _name1 = _get_op(revs_ids[0])
667 667 _op2, _name2 = _get_op(revs_ids[-1])
668 668
669 669 _rev = '%s...%s' % (_name1, _name2)
670 670
671 671 compare_view = (
672 672 ' <div class="compare_view" data-toggle="tooltip" title="%s">'
673 673 '<a href="%s">%s</a> </div>' % (
674 674 _('Show all combined changesets %s->%s') % (
675 675 revs_ids[0][:12], revs_ids[-1][:12]
676 676 ),
677 677 url('changeset_home', repo_name=repo_name,
678 678 revision=_rev
679 679 ),
680 680 _('Compare view')
681 681 )
682 682 )
683 683
684 684 # if we have exactly one more than normally displayed
685 685 # just display it, takes less space than displaying
686 686 # "and 1 more revisions"
687 687 if len(revs_ids) == revs_limit + 1:
688 688 cs_links.append(", " + lnk(revs[revs_limit], repo_name))
689 689
690 690 # hidden-by-default ones
691 691 if len(revs_ids) > revs_limit + 1:
692 692 uniq_id = revs_ids[0]
693 693 html_tmpl = (
694 694 '<span> %s <a class="show_more" id="_%s" '
695 695 'href="#more">%s</a> %s</span>'
696 696 )
697 697 if not feed:
698 698 cs_links.append(html_tmpl % (
699 699 _('and'),
700 700 uniq_id, _('%s more') % (len(revs_ids) - revs_limit),
701 701 _('revisions')
702 702 )
703 703 )
704 704
705 705 if not feed:
706 706 html_tmpl = '<span id="%s" style="display:none">, %s </span>'
707 707 else:
708 708 html_tmpl = '<span id="%s"> %s </span>'
709 709
710 710 morelinks = ', '.join(
711 711 [lnk(rev, repo_name) for rev in revs[revs_limit:]]
712 712 )
713 713
714 714 if len(revs_ids) > revs_top_limit:
715 715 morelinks += ', ...'
716 716
717 717 cs_links.append(html_tmpl % (uniq_id, morelinks))
718 718 if len(revs) > 1:
719 719 cs_links.append(compare_view)
720 720 return ''.join(cs_links)
721 721
722 722 def get_fork_name():
723 723 repo_name = action_params
724 724 url_ = url('summary_home', repo_name=repo_name)
725 725 return _('Fork name %s') % link_to(action_params, url_)
726 726
727 727 def get_user_name():
728 728 user_name = action_params
729 729 return user_name
730 730
731 731 def get_users_group():
732 732 group_name = action_params
733 733 return group_name
734 734
735 735 def get_pull_request():
736 736 from kallithea.model.db import PullRequest
737 737 pull_request_id = action_params
738 738 nice_id = PullRequest.make_nice_id(pull_request_id)
739 739
740 740 deleted = user_log.repository is None
741 741 if deleted:
742 742 repo_name = user_log.repository_name
743 743 else:
744 744 repo_name = user_log.repository.repo_name
745 745
746 746 return link_to(_('Pull request %s') % nice_id,
747 747 url('pullrequest_show', repo_name=repo_name,
748 748 pull_request_id=pull_request_id))
749 749
750 750 def get_archive_name():
751 751 archive_name = action_params
752 752 return archive_name
753 753
754 754 # action : translated str, callback(extractor), icon
755 755 action_map = {
756 756 'user_deleted_repo': (_('[deleted] repository'),
757 757 None, 'icon-trashcan'),
758 758 'user_created_repo': (_('[created] repository'),
759 759 None, 'icon-plus'),
760 760 'user_created_fork': (_('[created] repository as fork'),
761 761 None, 'icon-fork'),
762 762 'user_forked_repo': (_('[forked] repository'),
763 763 get_fork_name, 'icon-fork'),
764 764 'user_updated_repo': (_('[updated] repository'),
765 765 None, 'icon-pencil'),
766 766 'user_downloaded_archive': (_('[downloaded] archive from repository'),
767 767 get_archive_name, 'icon-download-cloud'),
768 768 'admin_deleted_repo': (_('[delete] repository'),
769 769 None, 'icon-trashcan'),
770 770 'admin_created_repo': (_('[created] repository'),
771 771 None, 'icon-plus'),
772 772 'admin_forked_repo': (_('[forked] repository'),
773 773 None, 'icon-fork'),
774 774 'admin_updated_repo': (_('[updated] repository'),
775 775 None, 'icon-pencil'),
776 776 'admin_created_user': (_('[created] user'),
777 777 get_user_name, 'icon-user'),
778 778 'admin_updated_user': (_('[updated] user'),
779 779 get_user_name, 'icon-user'),
780 780 'admin_created_users_group': (_('[created] user group'),
781 781 get_users_group, 'icon-pencil'),
782 782 'admin_updated_users_group': (_('[updated] user group'),
783 783 get_users_group, 'icon-pencil'),
784 784 'user_commented_revision': (_('[commented] on revision in repository'),
785 785 get_cs_links, 'icon-comment'),
786 786 'user_commented_pull_request': (_('[commented] on pull request for'),
787 787 get_pull_request, 'icon-comment'),
788 788 'user_closed_pull_request': (_('[closed] pull request for'),
789 789 get_pull_request, 'icon-ok'),
790 790 'push': (_('[pushed] into'),
791 791 get_cs_links, 'icon-move-up'),
792 792 'push_local': (_('[committed via Kallithea] into repository'),
793 793 get_cs_links, 'icon-pencil'),
794 794 'push_remote': (_('[pulled from remote] into repository'),
795 795 get_cs_links, 'icon-move-up'),
796 796 'pull': (_('[pulled] from'),
797 797 None, 'icon-move-down'),
798 798 'started_following_repo': (_('[started following] repository'),
799 799 None, 'icon-heart'),
800 800 'stopped_following_repo': (_('[stopped following] repository'),
801 801 None, 'icon-heart-empty'),
802 802 }
803 803
804 804 action_str = action_map.get(action, action)
805 805 if feed:
806 806 action = action_str[0].replace('[', '').replace(']', '')
807 807 else:
808 808 action = action_str[0] \
809 809 .replace('[', '<b>') \
810 810 .replace(']', '</b>')
811 811
812 812 action_params_func = lambda: ""
813 813
814 814 if callable(action_str[1]):
815 815 action_params_func = action_str[1]
816 816
817 817 def action_parser_icon():
818 818 action = user_log.action
819 819 action_params = None
820 820 x = action.split(':')
821 821
822 822 if len(x) > 1:
823 823 action, action_params = x
824 824
825 825 ico = action_map.get(action, ['', '', ''])[2]
826 826 html = """<i class="%s"></i>""" % ico
827 827 return literal(html)
828 828
829 829 # returned callbacks we need to call to get
830 830 return [lambda: literal(action), action_params_func, action_parser_icon]
831 831
832 832
833 833
834 834 #==============================================================================
835 835 # PERMS
836 836 #==============================================================================
837 837 from kallithea.lib.auth import HasPermissionAny, \
838 838 HasRepoPermissionLevel, HasRepoGroupPermissionLevel
839 839
840 840
841 841 #==============================================================================
842 842 # GRAVATAR URL
843 843 #==============================================================================
844 844 def gravatar_div(email_address, cls='', size=30, **div_attributes):
845 845 """Return an html literal with a span around a gravatar if they are enabled.
846 846 Extra keyword parameters starting with 'div_' will get the prefix removed
847 847 and '_' changed to '-' and be used as attributes on the div. The default
848 848 class is 'gravatar'.
849 849 """
850 850 from tg import tmpl_context as c
851 851 if not c.visual.use_gravatar:
852 852 return ''
853 853 if 'div_class' not in div_attributes:
854 854 div_attributes['div_class'] = "gravatar"
855 855 attributes = []
856 856 for k, v in sorted(div_attributes.items()):
857 857 assert k.startswith('div_'), k
858 858 attributes.append(' %s="%s"' % (k[4:].replace('_', '-'), escape(v)))
859 859 return literal("""<span%s>%s</span>""" %
860 860 (''.join(attributes),
861 861 gravatar(email_address, cls=cls, size=size)))
862 862
863 863
864 864 def gravatar(email_address, cls='', size=30):
865 865 """return html element of the gravatar
866 866
867 867 This method will return an <img> with the resolution double the size (for
868 868 retina screens) of the image. If the url returned from gravatar_url is
869 869 empty then we fallback to using an icon.
870 870
871 871 """
872 872 from tg import tmpl_context as c
873 873 if not c.visual.use_gravatar:
874 874 return ''
875 875
876 876 src = gravatar_url(email_address, size * 2)
877 877
878 878 if src:
879 879 # here it makes sense to use style="width: ..." (instead of, say, a
880 880 # stylesheet) because we using this to generate a high-res (retina) size
881 881 html = ('<i class="icon-gravatar {cls}"'
882 882 ' style="font-size: {size}px;background-size: {size}px;background-image: url(\'{src}\')"'
883 883 '></i>').format(cls=cls, size=size, src=src)
884 884
885 885 else:
886 886 # if src is empty then there was no gravatar, so we use a font icon
887 887 html = ("""<i class="icon-user {cls}" style="font-size: {size}px;"></i>"""
888 888 .format(cls=cls, size=size, src=src))
889 889
890 890 return literal(html)
891 891
892 892
893 893 def gravatar_url(email_address, size=30, default=''):
894 894 # doh, we need to re-import those to mock it later
895 895 from kallithea.config.routing import url
896 896 from kallithea.model.db import User
897 897 from tg import tmpl_context as c
898 898 if not c.visual.use_gravatar:
899 899 return ""
900 900
901 901 _def = 'anonymous@kallithea-scm.org' # default gravatar
902 902 email_address = email_address or _def
903 903
904 904 if email_address == _def:
905 905 return default
906 906
907 907 parsed_url = urlparse.urlparse(url.current(qualified=True))
908 908 url = (c.visual.gravatar_url or User.DEFAULT_GRAVATAR_URL ) \
909 909 .replace('{email}', email_address) \
910 910 .replace('{md5email}', hashlib.md5(safe_str(email_address).lower()).hexdigest()) \
911 911 .replace('{netloc}', parsed_url.netloc) \
912 912 .replace('{scheme}', parsed_url.scheme) \
913 913 .replace('{size}', safe_str(size))
914 914 return url
915 915
916 916
917 917 def changed_tooltip(nodes):
918 918 """
919 919 Generates a html string for changed nodes in changeset page.
920 920 It limits the output to 30 entries
921 921
922 922 :param nodes: LazyNodesGenerator
923 923 """
924 924 if nodes:
925 925 pref = ': <br/> '
926 926 suf = ''
927 927 if len(nodes) > 30:
928 928 suf = '<br/>' + _(' and %s more') % (len(nodes) - 30)
929 929 return literal(pref + '<br/> '.join([safe_unicode(x.path)
930 930 for x in nodes[:30]]) + suf)
931 931 else:
932 932 return ': ' + _('No files')
933 933
934 934
935 935 def fancy_file_stats(stats):
936 936 """
937 937 Displays a fancy two colored bar for number of added/deleted
938 938 lines of code on file
939 939
940 940 :param stats: two element list of added/deleted lines of code
941 941 """
942 942 from kallithea.lib.diffs import NEW_FILENODE, DEL_FILENODE, \
943 943 MOD_FILENODE, RENAMED_FILENODE, CHMOD_FILENODE, BIN_FILENODE
944 944
945 945 a, d = stats['added'], stats['deleted']
946 946 width = 100
947 947
948 948 if stats['binary']:
949 949 # binary mode
950 950 lbl = ''
951 951 bin_op = 1
952 952
953 953 if BIN_FILENODE in stats['ops']:
954 954 lbl = 'bin+'
955 955
956 956 if NEW_FILENODE in stats['ops']:
957 957 lbl += _('new file')
958 958 bin_op = NEW_FILENODE
959 959 elif MOD_FILENODE in stats['ops']:
960 960 lbl += _('mod')
961 961 bin_op = MOD_FILENODE
962 962 elif DEL_FILENODE in stats['ops']:
963 963 lbl += _('del')
964 964 bin_op = DEL_FILENODE
965 965 elif RENAMED_FILENODE in stats['ops']:
966 966 lbl += _('rename')
967 967 bin_op = RENAMED_FILENODE
968 968
969 969 # chmod can go with other operations
970 970 if CHMOD_FILENODE in stats['ops']:
971 971 _org_lbl = _('chmod')
972 972 lbl += _org_lbl if lbl.endswith('+') else '+%s' % _org_lbl
973 973
974 974 #import ipdb;ipdb.set_trace()
975 975 b_d = '<div class="bin bin%s progress-bar" style="width:100%%">%s</div>' % (bin_op, lbl)
976 976 b_a = '<div class="bin bin1" style="width:0%"></div>'
977 977 return literal('<div style="width:%spx" class="progress">%s%s</div>' % (width, b_a, b_d))
978 978
979 979 t = stats['added'] + stats['deleted']
980 980 unit = float(width) / (t or 1)
981 981
982 982 # needs > 9% of width to be visible or 0 to be hidden
983 983 a_p = max(9, unit * a) if a > 0 else 0
984 984 d_p = max(9, unit * d) if d > 0 else 0
985 985 p_sum = a_p + d_p
986 986
987 987 if p_sum > width:
988 988 # adjust the percentage to be == 100% since we adjusted to 9
989 989 if a_p > d_p:
990 990 a_p = a_p - (p_sum - width)
991 991 else:
992 992 d_p = d_p - (p_sum - width)
993 993
994 994 a_v = a if a > 0 else ''
995 995 d_v = d if d > 0 else ''
996 996
997 997 d_a = '<div class="added progress-bar" style="width:%s%%">%s</div>' % (
998 998 a_p, a_v
999 999 )
1000 1000 d_d = '<div class="deleted progress-bar" style="width:%s%%">%s</div>' % (
1001 1001 d_p, d_v
1002 1002 )
1003 1003 return literal('<div class="progress" style="width:%spx">%s%s</div>' % (width, d_a, d_d))
1004 1004
1005 1005
1006 1006 _URLIFY_RE = re.compile(r'''
1007 1007 # URL markup
1008 1008 (?P<url>%s) |
1009 1009 # @mention markup
1010 1010 (?P<mention>%s) |
1011 1011 # Changeset hash markup
1012 1012 (?<!\w|[-_])
1013 1013 (?P<hash>[0-9a-f]{12,40})
1014 1014 (?!\w|[-_]) |
1015 1015 # Markup of *bold text*
1016 1016 (?:
1017 1017 (?:^|(?<=\s))
1018 1018 (?P<bold> [*] (?!\s) [^*\n]* (?<!\s) [*] )
1019 1019 (?![*\w])
1020 1020 ) |
1021 1021 # "Stylize" markup
1022 1022 \[see\ \=&gt;\ *(?P<seen>[a-zA-Z0-9\/\=\?\&\ \:\/\.\-]*)\] |
1023 1023 \[license\ \=&gt;\ *(?P<license>[a-zA-Z0-9\/\=\?\&\ \:\/\.\-]*)\] |
1024 1024 \[(?P<tagtype>requires|recommends|conflicts|base)\ \=&gt;\ *(?P<tagvalue>[a-zA-Z0-9\-\/]*)\] |
1025 1025 \[(?:lang|language)\ \=&gt;\ *(?P<lang>[a-zA-Z\-\/\#\+]*)\] |
1026 1026 \[(?P<tag>[a-z]+)\]
1027 1027 ''' % (url_re.pattern, MENTIONS_REGEX.pattern),
1028 1028 re.VERBOSE | re.MULTILINE | re.IGNORECASE)
1029 1029
1030 1030
1031 1031 def urlify_text(s, repo_name=None, link_=None, truncate=None, stylize=False, truncatef=truncate):
1032 1032 """
1033 1033 Parses given text message and make literal html with markup.
1034 1034 The text will be truncated to the specified length.
1035 1035 Hashes are turned into changeset links to specified repository.
1036 1036 URLs links to what they say.
1037 1037 Issues are linked to given issue-server.
1038 1038 If link_ is provided, all text not already linking somewhere will link there.
1039 1039 """
1040 1040
1041 1041 def _replace(match_obj):
1042 1042 url = match_obj.group('url')
1043 1043 if url is not None:
1044 1044 return '<a href="%(url)s">%(url)s</a>' % {'url': url}
1045 1045 mention = match_obj.group('mention')
1046 1046 if mention is not None:
1047 1047 return '<b>%s</b>' % mention
1048 1048 hash_ = match_obj.group('hash')
1049 1049 if hash_ is not None and repo_name is not None:
1050 1050 from kallithea.config.routing import url # doh, we need to re-import url to mock it later
1051 1051 return '<a class="changeset_hash" href="%(url)s">%(hash)s</a>' % {
1052 1052 'url': url('changeset_home', repo_name=repo_name, revision=hash_),
1053 1053 'hash': hash_,
1054 1054 }
1055 1055 bold = match_obj.group('bold')
1056 1056 if bold is not None:
1057 1057 return '<b>*%s*</b>' % _urlify(bold[1:-1])
1058 1058 if stylize:
1059 1059 seen = match_obj.group('seen')
1060 1060 if seen:
1061 1061 return '<div class="label label-meta" data-tag="see">see =&gt; %s</div>' % seen
1062 1062 license = match_obj.group('license')
1063 1063 if license:
1064 1064 return '<div class="label label-meta" data-tag="license"><a href="http:\/\/www.opensource.org/licenses/%s">%s</a></div>' % (license, license)
1065 1065 tagtype = match_obj.group('tagtype')
1066 1066 if tagtype:
1067 1067 tagvalue = match_obj.group('tagvalue')
1068 1068 return '<div class="label label-meta" data-tag="%s">%s =&gt; <a href="/%s">%s</a></div>' % (tagtype, tagtype, tagvalue, tagvalue)
1069 1069 lang = match_obj.group('lang')
1070 1070 if lang:
1071 1071 return '<div class="label label-meta" data-tag="lang">%s</div>' % lang
1072 1072 tag = match_obj.group('tag')
1073 1073 if tag:
1074 1074 return '<div class="label label-meta" data-tag="%s">%s</div>' % (tag, tag)
1075 1075 return match_obj.group(0)
1076 1076
1077 1077 def _urlify(s):
1078 1078 """
1079 1079 Extract urls from text and make html links out of them
1080 1080 """
1081 1081 return _URLIFY_RE.sub(_replace, s)
1082 1082
1083 1083 if truncate is None:
1084 1084 s = s.rstrip()
1085 1085 else:
1086 1086 s = truncatef(s, truncate, whole_word=True)
1087 1087 s = html_escape(s)
1088 1088 s = _urlify(s)
1089 1089 if repo_name is not None:
1090 1090 s = urlify_issues(s, repo_name)
1091 1091 if link_ is not None:
1092 1092 # make href around everything that isn't a href already
1093 1093 s = linkify_others(s, link_)
1094 1094 s = s.replace('\r\n', '<br/>').replace('\n', '<br/>')
1095 1095 return literal(s)
1096 1096
1097 1097
1098 1098 def linkify_others(t, l):
1099 1099 """Add a default link to html with links.
1100 1100 HTML doesn't allow nesting of links, so the outer link must be broken up
1101 1101 in pieces and give space for other links.
1102 1102 """
1103 1103 urls = re.compile(r'(\<a.*?\<\/a\>)',)
1104 1104 links = []
1105 1105 for e in urls.split(t):
1106 1106 if e.strip() and not urls.match(e):
1107 1107 links.append('<a class="message-link" href="%s">%s</a>' % (l, e))
1108 1108 else:
1109 1109 links.append(e)
1110 1110
1111 1111 return ''.join(links)
1112 1112
1113 1113
1114 1114 # Global variable that will hold the actual urlify_issues function body.
1115 1115 # Will be set on first use when the global configuration has been read.
1116 1116 _urlify_issues_f = None
1117 1117
1118 1118
1119 1119 def urlify_issues(newtext, repo_name):
1120 1120 """Urlify issue references according to .ini configuration"""
1121 1121 global _urlify_issues_f
1122 1122 if _urlify_issues_f is None:
1123 1123 from kallithea import CONFIG
1124 1124 from kallithea.model.db import URL_SEP
1125 1125 assert CONFIG['sqlalchemy.url'] # make sure config has been loaded
1126 1126
1127 1127 # Build chain of urlify functions, starting with not doing any transformation
1128 1128 tmp_urlify_issues_f = lambda s: s
1129 1129
1130 1130 issue_pat_re = re.compile(r'issue_pat(.*)')
1131 1131 for k in CONFIG.keys():
1132 1132 # Find all issue_pat* settings that also have corresponding server_link and prefix configuration
1133 1133 m = issue_pat_re.match(k)
1134 1134 if m is None:
1135 1135 continue
1136 1136 suffix = m.group(1)
1137 1137 issue_pat = CONFIG.get(k)
1138 1138 issue_server_link = CONFIG.get('issue_server_link%s' % suffix)
1139 1139 issue_prefix = CONFIG.get('issue_prefix%s' % suffix)
1140 if issue_pat and issue_server_link and issue_prefix:
1140 if issue_pat and issue_server_link and issue_prefix is not None: # issue_prefix can be empty but should be present
1141 1141 log.debug('issue pattern %r: %r -> %r %r', suffix, issue_pat, issue_server_link, issue_prefix)
1142 1142 else:
1143 1143 log.error('skipping incomplete issue pattern %r: %r -> %r %r', suffix, issue_pat, issue_server_link, issue_prefix)
1144 1144 continue
1145 1145
1146 1146 # Wrap tmp_urlify_issues_f with substitution of this pattern, while making sure all loop variables (and compiled regexpes) are bound
1147 1147 issue_re = re.compile(issue_pat)
1148 1148
1149 1149 def issues_replace(match_obj,
1150 1150 issue_server_link=issue_server_link, issue_prefix=issue_prefix):
1151 1151 leadingspace = ' ' if match_obj.group().startswith(' ') else ''
1152 1152 issue_id = ''.join(match_obj.groups())
1153 1153 issue_url = issue_server_link.replace('{id}', issue_id)
1154 1154 issue_url = issue_url.replace('{repo}', repo_name)
1155 1155 issue_url = issue_url.replace('{repo_name}', repo_name.split(URL_SEP)[-1])
1156 1156 return (
1157 1157 '%(leadingspace)s<a class="issue-tracker-link" href="%(url)s">'
1158 1158 '%(issue-prefix)s%(id-repr)s'
1159 1159 '</a>'
1160 1160 ) % {
1161 1161 'leadingspace': leadingspace,
1162 1162 'url': issue_url,
1163 1163 'id-repr': issue_id,
1164 1164 'issue-prefix': issue_prefix,
1165 1165 'serv': issue_server_link,
1166 1166 }
1167 1167 tmp_urlify_issues_f = (lambda s,
1168 1168 issue_re=issue_re, issues_replace=issues_replace, chain_f=tmp_urlify_issues_f:
1169 1169 issue_re.sub(issues_replace, chain_f(s)))
1170 1170
1171 1171 # Set tmp function globally - atomically
1172 1172 _urlify_issues_f = tmp_urlify_issues_f
1173 1173
1174 1174 return _urlify_issues_f(newtext)
1175 1175
1176 1176
1177 1177 def render_w_mentions(source, repo_name=None):
1178 1178 """
1179 1179 Render plain text with revision hashes and issue references urlified
1180 1180 and with @mention highlighting.
1181 1181 """
1182 1182 s = safe_unicode(source)
1183 1183 s = urlify_text(s, repo_name=repo_name)
1184 1184 return literal('<div class="formatted-fixed">%s</div>' % s)
1185 1185
1186 1186
1187 1187 def short_ref(ref_type, ref_name):
1188 1188 if ref_type == 'rev':
1189 1189 return short_id(ref_name)
1190 1190 return ref_name
1191 1191
1192 1192
1193 1193 def link_to_ref(repo_name, ref_type, ref_name, rev=None):
1194 1194 """
1195 1195 Return full markup for a href to changeset_home for a changeset.
1196 1196 If ref_type is branch it will link to changelog.
1197 1197 ref_name is shortened if ref_type is 'rev'.
1198 1198 if rev is specified show it too, explicitly linking to that revision.
1199 1199 """
1200 1200 txt = short_ref(ref_type, ref_name)
1201 1201 if ref_type == 'branch':
1202 1202 u = url('changelog_home', repo_name=repo_name, branch=ref_name)
1203 1203 else:
1204 1204 u = url('changeset_home', repo_name=repo_name, revision=ref_name)
1205 1205 l = link_to(repo_name + '#' + txt, u)
1206 1206 if rev and ref_type != 'rev':
1207 1207 l = literal('%s (%s)' % (l, link_to(short_id(rev), url('changeset_home', repo_name=repo_name, revision=rev))))
1208 1208 return l
1209 1209
1210 1210
1211 1211 def changeset_status(repo, revision):
1212 1212 from kallithea.model.changeset_status import ChangesetStatusModel
1213 1213 return ChangesetStatusModel().get_status(repo, revision)
1214 1214
1215 1215
1216 1216 def changeset_status_lbl(changeset_status):
1217 1217 from kallithea.model.db import ChangesetStatus
1218 1218 return ChangesetStatus.get_status_lbl(changeset_status)
1219 1219
1220 1220
1221 1221 def get_permission_name(key):
1222 1222 from kallithea.model.db import Permission
1223 1223 return dict(Permission.PERMS).get(key)
1224 1224
1225 1225
1226 1226 def journal_filter_help():
1227 1227 return _(textwrap.dedent('''
1228 1228 Example filter terms:
1229 1229 repository:vcs
1230 1230 username:developer
1231 1231 action:*push*
1232 1232 ip:127.0.0.1
1233 1233 date:20120101
1234 1234 date:[20120101100000 TO 20120102]
1235 1235
1236 1236 Generate wildcards using '*' character:
1237 1237 "repository:vcs*" - search everything starting with 'vcs'
1238 1238 "repository:*vcs*" - search for repository containing 'vcs'
1239 1239
1240 1240 Optional AND / OR operators in queries
1241 1241 "repository:vcs OR repository:test"
1242 1242 "username:test AND repository:test*"
1243 1243 '''))
1244 1244
1245 1245
1246 1246 def not_mapped_error(repo_name):
1247 1247 flash(_('%s repository is not mapped to db perhaps'
1248 1248 ' it was created or renamed from the filesystem'
1249 1249 ' please run the application again'
1250 1250 ' in order to rescan repositories') % repo_name, category='error')
1251 1251
1252 1252
1253 1253 def ip_range(ip_addr):
1254 1254 from kallithea.model.db import UserIpMap
1255 1255 s, e = UserIpMap._get_ip_range(ip_addr)
1256 1256 return '%s - %s' % (s, e)
1257 1257
1258 1258
1259 1259 def form(url, method="post", **attrs):
1260 1260 """Like webhelpers.html.tags.form but automatically using secure_form with
1261 1261 authentication_token for POST. authentication_token is thus never leaked
1262 1262 in the URL."""
1263 1263 if method.lower() == 'get':
1264 1264 return insecure_form(url, method=method, **attrs)
1265 1265 # webhelpers will turn everything but GET into POST
1266 1266 return secure_form(url, method=method, **attrs)
@@ -1,486 +1,486
1 1 # -*- coding: utf-8 -*-
2 2 # This program is free software: you can redistribute it and/or modify
3 3 # it under the terms of the GNU General Public License as published by
4 4 # the Free Software Foundation, either version 3 of the License, or
5 5 # (at your option) any later version.
6 6 #
7 7 # This program is distributed in the hope that it will be useful,
8 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 # GNU General Public License for more details.
11 11 #
12 12 # You should have received a copy of the GNU General Public License
13 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 """
15 15 kallithea.tests.other.test_libs
16 16 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17 17
18 18 Package for testing various lib/helper functions in kallithea
19 19
20 20 This file was forked by the Kallithea project in July 2014.
21 21 Original author and date, and relevant copyright and licensing information is below:
22 22 :created_on: Jun 9, 2011
23 23 :author: marcink
24 24 :copyright: (c) 2013 RhodeCode GmbH, and others.
25 25 :license: GPLv3, see LICENSE.md for more details.
26 26 """
27 27
28 28 import datetime
29 29 import hashlib
30 30 import mock
31 31 from kallithea.tests.base import *
32 32 from kallithea.lib.utils2 import AttributeDict
33 33 from kallithea.model.db import Repository
34 34 from tg.util.webtest import test_context
35 35
36 36 proto = 'http'
37 37 TEST_URLS = [
38 38 ('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
39 39 '%s://127.0.0.1' % proto),
40 40 ('%s://username@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
41 41 '%s://127.0.0.1' % proto),
42 42 ('%s://username:pass@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
43 43 '%s://127.0.0.1' % proto),
44 44 ('%s://127.0.0.1:8080' % proto, ['%s://' % proto, '127.0.0.1', '8080'],
45 45 '%s://127.0.0.1:8080' % proto),
46 46 ('%s://example.com' % proto, ['%s://' % proto, 'example.com'],
47 47 '%s://example.com' % proto),
48 48 ('%s://user:pass@example.com:8080' % proto, ['%s://' % proto, 'example.com',
49 49 '8080'],
50 50 '%s://example.com:8080' % proto),
51 51 ]
52 52
53 53 proto = 'https'
54 54 TEST_URLS += [
55 55 ('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
56 56 '%s://127.0.0.1' % proto),
57 57 ('%s://username@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
58 58 '%s://127.0.0.1' % proto),
59 59 ('%s://username:pass@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
60 60 '%s://127.0.0.1' % proto),
61 61 ('%s://127.0.0.1:8080' % proto, ['%s://' % proto, '127.0.0.1', '8080'],
62 62 '%s://127.0.0.1:8080' % proto),
63 63 ('%s://example.com' % proto, ['%s://' % proto, 'example.com'],
64 64 '%s://example.com' % proto),
65 65 ('%s://user:pass@example.com:8080' % proto, ['%s://' % proto, 'example.com',
66 66 '8080'],
67 67 '%s://example.com:8080' % proto),
68 68 ]
69 69
70 70
71 71 class FakeUrlGenerator(object):
72 72
73 73 def __init__(self, current_url=None, default_route=None, **routes):
74 74 """Initialize using specified 'current' URL template,
75 75 default route template, and all other aguments describing known
76 76 routes (format: route=template)"""
77 77 self.current_url = current_url
78 78 self.default_route = default_route
79 79 self.routes = routes
80 80
81 81 def __call__(self, route_name, *args, **kwargs):
82 82 if route_name in self.routes:
83 83 return self.routes[route_name] % kwargs
84 84
85 85 return self.default_route % kwargs
86 86
87 87 def current(self, *args, **kwargs):
88 88 return self.current_url % kwargs
89 89
90 90
91 91 class TestLibs(TestController):
92 92
93 93 @parametrize('test_url,expected,expected_creds', TEST_URLS)
94 94 def test_uri_filter(self, test_url, expected, expected_creds):
95 95 from kallithea.lib.utils2 import uri_filter
96 96 assert uri_filter(test_url) == expected
97 97
98 98 @parametrize('test_url,expected,expected_creds', TEST_URLS)
99 99 def test_credentials_filter(self, test_url, expected, expected_creds):
100 100 from kallithea.lib.utils2 import credentials_filter
101 101 assert credentials_filter(test_url) == expected_creds
102 102
103 103 @parametrize('str_bool,expected', [
104 104 ('t', True),
105 105 ('true', True),
106 106 ('y', True),
107 107 ('yes', True),
108 108 ('on', True),
109 109 ('1', True),
110 110 ('Y', True),
111 111 ('yeS', True),
112 112 ('Y', True),
113 113 ('TRUE', True),
114 114 ('T', True),
115 115 ('False', False),
116 116 ('F', False),
117 117 ('FALSE', False),
118 118 ('0', False),
119 119 ('-1', False),
120 120 ('', False)
121 121 ])
122 122 def test_str2bool(self, str_bool, expected):
123 123 from kallithea.lib.utils2 import str2bool
124 124 assert str2bool(str_bool) == expected
125 125
126 126 def test_mention_extractor(self):
127 127 from kallithea.lib.utils2 import extract_mentioned_usernames
128 128 sample = (
129 129 "@first hi there @world here's my email username@example.com "
130 130 "@lukaszb check @one_more22 it pls @ ttwelve @D[] @one@two@three "
131 131 "@UPPER @cAmEL @2one_more22 @john please see this http://org.pl "
132 132 "@marian.user just do it @marco-polo and next extract @marco_polo "
133 133 "user.dot hej ! not-needed maril@example.com"
134 134 )
135 135
136 136 expected = set([
137 137 '2one_more22', 'first', 'lukaszb', 'one', 'one_more22', 'UPPER', 'cAmEL', 'john',
138 138 'marian.user', 'marco-polo', 'marco_polo', 'world'])
139 139 assert expected == set(extract_mentioned_usernames(sample))
140 140
141 141 @parametrize('age_args,expected', [
142 142 (dict(), u'just now'),
143 143 (dict(seconds= -1), u'1 second ago'),
144 144 (dict(seconds= -60 * 2), u'2 minutes ago'),
145 145 (dict(hours= -1), u'1 hour ago'),
146 146 (dict(hours= -24), u'1 day ago'),
147 147 (dict(hours= -24 * 5), u'5 days ago'),
148 148 (dict(months= -1), u'1 month ago'),
149 149 (dict(months= -1, days= -2), u'1 month and 2 days ago'),
150 150 (dict(months= -1, days= -20), u'1 month and 19 days ago'),
151 151 (dict(years= -1, months= -1), u'1 year and 1 month ago'),
152 152 (dict(years= -1, months= -10), u'1 year and 10 months ago'),
153 153 (dict(years= -2, months= -4), u'2 years and 4 months ago'),
154 154 (dict(years= -2, months= -11), u'2 years and 11 months ago'),
155 155 (dict(years= -3, months= -2), u'3 years and 2 months ago'),
156 156 ])
157 157 def test_age(self, age_args, expected):
158 158 from kallithea.lib.utils2 import age
159 159 from dateutil import relativedelta
160 160 with test_context(self.app):
161 161 n = datetime.datetime(year=2012, month=5, day=17)
162 162 delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
163 163 assert age(n + delt(**age_args), now=n) == expected
164 164
165 165 @parametrize('age_args,expected', [
166 166 (dict(), u'just now'),
167 167 (dict(seconds= -1), u'1 second ago'),
168 168 (dict(seconds= -60 * 2), u'2 minutes ago'),
169 169 (dict(hours= -1), u'1 hour ago'),
170 170 (dict(hours= -24), u'1 day ago'),
171 171 (dict(hours= -24 * 5), u'5 days ago'),
172 172 (dict(months= -1), u'1 month ago'),
173 173 (dict(months= -1, days= -2), u'1 month ago'),
174 174 (dict(months= -1, days= -20), u'1 month ago'),
175 175 (dict(years= -1, months= -1), u'13 months ago'),
176 176 (dict(years= -1, months= -10), u'22 months ago'),
177 177 (dict(years= -2, months= -4), u'2 years ago'),
178 178 (dict(years= -2, months= -11), u'3 years ago'),
179 179 (dict(years= -3, months= -2), u'3 years ago'),
180 180 (dict(years= -4, months= -8), u'5 years ago'),
181 181 ])
182 182 def test_age_short(self, age_args, expected):
183 183 from kallithea.lib.utils2 import age
184 184 from dateutil import relativedelta
185 185 with test_context(self.app):
186 186 n = datetime.datetime(year=2012, month=5, day=17)
187 187 delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
188 188 assert age(n + delt(**age_args), show_short_version=True, now=n) == expected
189 189
190 190 @parametrize('age_args,expected', [
191 191 (dict(), u'just now'),
192 192 (dict(seconds=1), u'in 1 second'),
193 193 (dict(seconds=60 * 2), u'in 2 minutes'),
194 194 (dict(hours=1), u'in 1 hour'),
195 195 (dict(hours=24), u'in 1 day'),
196 196 (dict(hours=24 * 5), u'in 5 days'),
197 197 (dict(months=1), u'in 1 month'),
198 198 (dict(months=1, days=1), u'in 1 month and 1 day'),
199 199 (dict(years=1, months=1), u'in 1 year and 1 month')
200 200 ])
201 201 def test_age_in_future(self, age_args, expected):
202 202 from kallithea.lib.utils2 import age
203 203 from dateutil import relativedelta
204 204 with test_context(self.app):
205 205 n = datetime.datetime(year=2012, month=5, day=17)
206 206 delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
207 207 assert age(n + delt(**age_args), now=n) == expected
208 208
209 209 def test_tag_extractor(self):
210 210 sample = (
211 211 "hello pta[tag] gog [[]] [[] sda ero[or]d [me =>>< sa]"
212 212 "[requires] [stale] [see<>=>] [see => http://example.com]"
213 213 "[requires => url] [lang => python] [just a tag]"
214 214 "[,d] [ => ULR ] [obsolete] [desc]]"
215 215 )
216 216 from kallithea.lib.helpers import urlify_text
217 217 res = urlify_text(sample, stylize=True)
218 218 assert '<div class="label label-meta" data-tag="tag">tag</div>' in res
219 219 assert '<div class="label label-meta" data-tag="obsolete">obsolete</div>' in res
220 220 assert '<div class="label label-meta" data-tag="stale">stale</div>' in res
221 221 assert '<div class="label label-meta" data-tag="lang">python</div>' in res
222 222 assert '<div class="label label-meta" data-tag="requires">requires =&gt; <a href="/url">url</a></div>' in res
223 223 assert '<div class="label label-meta" data-tag="tag">tag</div>' in res
224 224
225 225 def test_alternative_gravatar(self):
226 226 from kallithea.lib.helpers import gravatar_url
227 227 _md5 = lambda s: hashlib.md5(s).hexdigest()
228 228
229 229 # mock tg.tmpl_context
230 230 def fake_tmpl_context(_url):
231 231 _c = AttributeDict()
232 232 _c.visual = AttributeDict()
233 233 _c.visual.use_gravatar = True
234 234 _c.visual.gravatar_url = _url
235 235
236 236 return _c
237 237
238 238 fake_url = FakeUrlGenerator(current_url='https://example.com')
239 239 with mock.patch('kallithea.config.routing.url', fake_url):
240 240 fake = fake_tmpl_context(_url='http://example.com/{email}')
241 241 with mock.patch('tg.tmpl_context', fake):
242 242 from kallithea.config.routing import url
243 243 assert url.current() == 'https://example.com'
244 244 grav = gravatar_url(email_address='test@example.com', size=24)
245 245 assert grav == 'http://example.com/test@example.com'
246 246
247 247 fake = fake_tmpl_context(_url='http://example.com/{email}')
248 248 with mock.patch('tg.tmpl_context', fake):
249 249 grav = gravatar_url(email_address='test@example.com', size=24)
250 250 assert grav == 'http://example.com/test@example.com'
251 251
252 252 fake = fake_tmpl_context(_url='http://example.com/{md5email}')
253 253 with mock.patch('tg.tmpl_context', fake):
254 254 em = 'test@example.com'
255 255 grav = gravatar_url(email_address=em, size=24)
256 256 assert grav == 'http://example.com/%s' % (_md5(em))
257 257
258 258 fake = fake_tmpl_context(_url='http://example.com/{md5email}/{size}')
259 259 with mock.patch('tg.tmpl_context', fake):
260 260 em = 'test@example.com'
261 261 grav = gravatar_url(email_address=em, size=24)
262 262 assert grav == 'http://example.com/%s/%s' % (_md5(em), 24)
263 263
264 264 fake = fake_tmpl_context(_url='{scheme}://{netloc}/{md5email}/{size}')
265 265 with mock.patch('tg.tmpl_context', fake):
266 266 em = 'test@example.com'
267 267 grav = gravatar_url(email_address=em, size=24)
268 268 assert grav == 'https://example.com/%s/%s' % (_md5(em), 24)
269 269
270 270 @parametrize('tmpl,repo_name,overrides,prefix,expected', [
271 271 (Repository.DEFAULT_CLONE_URI, 'group/repo1', {}, '', 'http://vps1:8000/group/repo1'),
272 272 (Repository.DEFAULT_CLONE_URI, 'group/repo1', {'user': 'username'}, '', 'http://username@vps1:8000/group/repo1'),
273 273 (Repository.DEFAULT_CLONE_URI, 'group/repo1', {}, '/prefix', 'http://vps1:8000/prefix/group/repo1'),
274 274 (Repository.DEFAULT_CLONE_URI, 'group/repo1', {'user': 'user'}, '/prefix', 'http://user@vps1:8000/prefix/group/repo1'),
275 275 (Repository.DEFAULT_CLONE_URI, 'group/repo1', {'user': 'username'}, '/prefix', 'http://username@vps1:8000/prefix/group/repo1'),
276 276 (Repository.DEFAULT_CLONE_URI, 'group/repo1', {'user': 'user'}, '/prefix/', 'http://user@vps1:8000/prefix/group/repo1'),
277 277 (Repository.DEFAULT_CLONE_URI, 'group/repo1', {'user': 'username'}, '/prefix/', 'http://username@vps1:8000/prefix/group/repo1'),
278 278 ('{scheme}://{user}@{netloc}/_{repoid}', 'group/repo1', {}, '', 'http://vps1:8000/_23'),
279 279 ('{scheme}://{user}@{netloc}/_{repoid}', 'group/repo1', {'user': 'username'}, '', 'http://username@vps1:8000/_23'),
280 280 ('http://{user}@{netloc}/_{repoid}', 'group/repo1', {'user': 'username'}, '', 'http://username@vps1:8000/_23'),
281 281 ('http://{netloc}/_{repoid}', 'group/repo1', {'user': 'username'}, '', 'http://vps1:8000/_23'),
282 282 ('https://{user}@proxy1.example.com/{repo}', 'group/repo1', {'user': 'username'}, '', 'https://username@proxy1.example.com/group/repo1'),
283 283 ('https://{user}@proxy1.example.com/{repo}', 'group/repo1', {}, '', 'https://proxy1.example.com/group/repo1'),
284 284 ('https://proxy1.example.com/{user}/{repo}', 'group/repo1', {'user': 'username'}, '', 'https://proxy1.example.com/username/group/repo1'),
285 285 ])
286 286 def test_clone_url_generator(self, tmpl, repo_name, overrides, prefix, expected):
287 287 from kallithea.lib.utils2 import get_clone_url
288 288 clone_url = get_clone_url(uri_tmpl=tmpl, qualified_home_url='http://vps1:8000'+prefix,
289 289 repo_name=repo_name, repo_id=23, **overrides)
290 290 assert clone_url == expected
291 291
292 292 def _quick_url(self, text, tmpl="""<a class="changeset_hash" href="%s">%s</a>""", url_=None):
293 293 """
294 294 Changes `some text url[foo]` => `some text <a href="/">foo</a>
295 295
296 296 :param text:
297 297 """
298 298 import re
299 299 # quickly change expected url[] into a link
300 300 url_pattern = re.compile(r'(?:url\[)(.+?)(?:\])')
301 301
302 302 def url_func(match_obj):
303 303 _url = match_obj.groups()[0]
304 304 return tmpl % (url_ or '/repo_name/changeset/%s' % _url, _url)
305 305 return url_pattern.sub(url_func, text)
306 306
307 307 @parametrize('sample,expected', [
308 308 ("",
309 309 ""),
310 310 ("git-svn-id: https://svn.apache.org/repos/asf/libcloud/trunk@1441655 13f79535-47bb-0310-9956-ffa450edef68",
311 311 """git-svn-id: <a href="https://svn.apache.org/repos/asf/libcloud/trunk@1441655">https://svn.apache.org/repos/asf/libcloud/trunk@1441655</a> 13f79535-47bb-0310-9956-ffa450edef68"""),
312 312 ("from rev 000000000000",
313 313 """from rev url[000000000000]"""),
314 314 ("from rev 000000000000123123 also rev 000000000000",
315 315 """from rev url[000000000000123123] also rev url[000000000000]"""),
316 316 ("this should-000 00",
317 317 """this should-000 00"""),
318 318 ("longtextffffffffff rev 123123123123",
319 319 """longtextffffffffff rev url[123123123123]"""),
320 320 ("rev ffffffffffffffffffffffffffffffffffffffffffffffffff",
321 321 """rev ffffffffffffffffffffffffffffffffffffffffffffffffff"""),
322 322 ("ffffffffffff some text traalaa",
323 323 """url[ffffffffffff] some text traalaa"""),
324 324 ("""Multi line
325 325 123123123123
326 326 some text 123123123123
327 327 sometimes !
328 328 """,
329 329 """Multi line<br/>"""
330 330 """ url[123123123123]<br/>"""
331 331 """ some text url[123123123123]<br/>"""
332 332 """ sometimes !"""),
333 333 ])
334 334 def test_urlify_text(self, sample, expected):
335 335 expected = self._quick_url(expected)
336 336 fake_url = FakeUrlGenerator(changeset_home='/%(repo_name)s/changeset/%(revision)s')
337 337 with mock.patch('kallithea.config.routing.url', fake_url):
338 338 from kallithea.lib.helpers import urlify_text
339 339 assert urlify_text(sample, 'repo_name') == expected
340 340
341 341 @parametrize('sample,expected,url_', [
342 342 ("",
343 343 "",
344 344 ""),
345 345 ("https://svn.apache.org/repos",
346 346 """url[https://svn.apache.org/repos]""",
347 347 "https://svn.apache.org/repos"),
348 348 ("http://svn.apache.org/repos",
349 349 """url[http://svn.apache.org/repos]""",
350 350 "http://svn.apache.org/repos"),
351 351 ("from rev a also rev http://google.com",
352 352 """from rev a also rev url[http://google.com]""",
353 353 "http://google.com"),
354 354 ("http://imgur.com/foo.gif inline http://imgur.com/foo.gif ending http://imgur.com/foo.gif",
355 355 """url[http://imgur.com/foo.gif] inline url[http://imgur.com/foo.gif] ending url[http://imgur.com/foo.gif]""",
356 356 "http://imgur.com/foo.gif"),
357 357 ("""Multi line
358 358 https://foo.bar.example.com
359 359 some text lalala""",
360 360 """Multi line<br/>"""
361 361 """ url[https://foo.bar.example.com]<br/>"""
362 362 """ some text lalala""",
363 363 "https://foo.bar.example.com"),
364 364 ("@mention @someone",
365 365 """<b>@mention</b> <b>@someone</b>""",
366 366 ""),
367 367 ("deadbeefcafe 123412341234",
368 368 """<a class="changeset_hash" href="/repo_name/changeset/deadbeefcafe">deadbeefcafe</a> <a class="changeset_hash" href="/repo_name/changeset/123412341234">123412341234</a>""",
369 369 ""),
370 370 ("We support * markup for *bold* markup of *single or multiple* words, "
371 371 "*a bit @like http://slack.com*. "
372 372 "The first * must come after whitespace and not be followed by whitespace, "
373 373 "contain anything but * and newline until the next *, "
374 374 "which must not come after whitespace "
375 375 "and not be followed by * or alphanumerical *characters*.",
376 376 """We support * markup for <b>*bold*</b> markup of <b>*single or multiple*</b> words, """
377 377 """<b>*a bit <b>@like</b> <a href="http://slack.com">http://slack.com</a>*</b>. """
378 378 """The first * must come after whitespace and not be followed by whitespace, """
379 379 """contain anything but * and newline until the next *, """
380 380 """which must not come after whitespace """
381 381 """and not be followed by * or alphanumerical <b>*characters*</b>.""",
382 382 "-"),
383 383 # tags are covered by test_tag_extractor
384 384 ])
385 385 def test_urlify_test(self, sample, expected, url_):
386 386 expected = self._quick_url(expected,
387 387 tmpl="""<a href="%s">%s</a>""", url_=url_)
388 388 fake_url = FakeUrlGenerator(changeset_home='/%(repo_name)s/changeset/%(revision)s')
389 389 with mock.patch('kallithea.config.routing.url', fake_url):
390 390 from kallithea.lib.helpers import urlify_text
391 391 assert urlify_text(sample, 'repo_name', stylize=True) == expected
392 392
393 393 @parametrize('sample,expected', [
394 394 ("deadbeefcafe @mention, and http://foo.bar/ yo",
395 395 """<a class="changeset_hash" href="/repo_name/changeset/deadbeefcafe">deadbeefcafe</a>"""
396 396 """<a class="message-link" href="#the-link"> <b>@mention</b>, and </a>"""
397 397 """<a href="http://foo.bar/">http://foo.bar/</a>"""
398 398 """<a class="message-link" href="#the-link"> yo</a>"""),
399 399 ])
400 400 def test_urlify_link(self, sample, expected):
401 401 fake_url = FakeUrlGenerator(changeset_home='/%(repo_name)s/changeset/%(revision)s')
402 402 with mock.patch('kallithea.config.routing.url', fake_url):
403 403 from kallithea.lib.helpers import urlify_text
404 404 assert urlify_text(sample, 'repo_name', link_='#the-link') == expected
405 405
406 406 @parametrize('issue_pat,issue_server,issue_prefix,sample,expected', [
407 407 (r'#(\d+)', 'http://foo/{repo}/issue/{id}', '#',
408 408 'issue #123', 'issue <a class="issue-tracker-link" href="http://foo/repo_name/issue/123">#123</a>'),
409 409 (r'#(\d+)', 'http://foo/{repo}/issue/{id}', '#',
410 410 'issue#456', 'issue<a class="issue-tracker-link" href="http://foo/repo_name/issue/456">#456</a>'),
411 411 (r'#(\d+)', 'http://foo/{repo}/issue/{id}', 'PR',
412 412 'interesting issue #123', 'interesting issue <a class="issue-tracker-link" href="http://foo/repo_name/issue/123">PR123</a>'),
413 413 (r'BUG\d{5}', 'https://bar/{repo}/{id}', 'BUG',
414 414 'silly me, I did not parenthesize the {id}, BUG12345.', 'silly me, I did not parenthesize the {id}, <a class="issue-tracker-link" href="https://bar/repo_name/">BUG</a>.'),
415 415 (r'BUG(\d{5})', 'https://bar/{repo}/', 'BUG',
416 416 'silly me, the URL does not contain {id}, BUG12345.', 'silly me, the URL does not contain {id}, <a class="issue-tracker-link" href="https://bar/repo_name/">BUG12345</a>.'),
417 417 (r'(PR-\d+)', 'http://foo/{repo}/issue/{id}', '',
418 'interesting issue #123, err PR-56', 'interesting issue #123, err PR-56'), # no match because empty prefix
418 'interesting issue #123, err PR-56', 'interesting issue #123, err <a class="issue-tracker-link" href="http://foo/repo_name/issue/PR-56">PR-56</a>'),
419 419 ])
420 420 def test_urlify_issues(self, issue_pat, issue_server, issue_prefix, sample, expected):
421 421 from kallithea.lib.helpers import urlify_issues
422 422 config_stub = {
423 423 'sqlalchemy.url': 'foo',
424 424 'issue_pat': issue_pat,
425 425 'issue_server_link': issue_server,
426 426 'issue_prefix': issue_prefix,
427 427 }
428 428 # force recreation of lazy function
429 429 with mock.patch('kallithea.lib.helpers._urlify_issues_f', None):
430 430 with mock.patch('kallithea.CONFIG', config_stub):
431 431 assert urlify_issues(sample, 'repo_name') == expected
432 432
433 433 @parametrize('sample,expected', [
434 434 ('abc X5', 'abc <a class="issue-tracker-link" href="http://main/repo_name/main/5/">#5</a>'),
435 435 ('abc pullrequest #6 xyz', 'abc <a class="issue-tracker-link" href="http://pr/repo_name/pr/6">PR#6</a> xyz'),
436 436 ('pull request7 #', '<a class="issue-tracker-link" href="http://pr/repo_name/pr/7">PR#7</a> #'),
437 437 ('look PR9 and pr #11', 'look <a class="issue-tracker-link" href="http://pr/repo_name/pr/9">PR#9</a> and <a class="issue-tracker-link" href="http://pr/repo_name/pr/11">PR#11</a>'),
438 438 ('pullrequest#10 solves issue 9', '<a class="issue-tracker-link" href="http://pr/repo_name/pr/10">PR#10</a> solves <a class="issue-tracker-link" href="http://bug/repo_name/bug/9">bug#9</a>'),
439 ('issue FAIL67', 'issue FAIL67'), # no match because empty prefix
439 ('issue FAIL67', 'issue <a class="issue-tracker-link" href="http://fail/repo_name/67">67</a>'),
440 440 ('issue FAILMORE89', 'issue FAILMORE89'), # no match because absent prefix
441 441 ])
442 442 def test_urlify_issues_multiple_issue_patterns(self, sample, expected):
443 443 from kallithea.lib.helpers import urlify_issues
444 444 config_stub = {
445 445 'sqlalchemy.url': 'foo',
446 446 'issue_pat': 'X(\d+)',
447 447 'issue_server_link': 'http://main/{repo}/main/{id}/',
448 448 'issue_prefix': '#',
449 449 'issue_pat_pr': '(?:pullrequest|pull request|PR|pr) ?#?(\d+)',
450 450 'issue_server_link_pr': 'http://pr/{repo}/pr/{id}',
451 451 'issue_prefix_pr': 'PR#',
452 452 'issue_pat_bug': '(?:BUG|bug|issue) ?#?(\d+)',
453 453 'issue_server_link_bug': 'http://bug/{repo}/bug/{id}',
454 454 'issue_prefix_bug': 'bug#',
455 455 'issue_pat_empty_prefix': 'FAIL(\d+)',
456 456 'issue_server_link_empty_prefix': 'http://fail/{repo}/{id}',
457 457 'issue_prefix_empty_prefix': '',
458 458 'issue_pat_absent_prefix': 'FAILMORE(\d+)',
459 459 'issue_server_link_absent_prefix': 'http://failmore/{repo}/{id}',
460 460 }
461 461 # force recreation of lazy function
462 462 with mock.patch('kallithea.lib.helpers._urlify_issues_f', None):
463 463 with mock.patch('kallithea.CONFIG', config_stub):
464 464 assert urlify_issues(sample, 'repo_name') == expected
465 465
466 466 @parametrize('test,expected', [
467 467 ("", None),
468 468 ("/_2", '2'),
469 469 ("_2", '2'),
470 470 ("/_2/", '2'),
471 471 ("_2/", '2'),
472 472
473 473 ("/_21", '21'),
474 474 ("_21", '21'),
475 475 ("/_21/", '21'),
476 476 ("_21/", '21'),
477 477
478 478 ("/_21/foobar", '21'),
479 479 ("_21/121", '21'),
480 480 ("/_21/_12", '21'),
481 481 ("_21/prefix/foo", '21'),
482 482 ])
483 483 def test_get_repo_by_id(self, test, expected):
484 484 from kallithea.lib.utils import _extract_id_from_repo_name
485 485 _test = _extract_id_from_repo_name(test)
486 486 assert _test == expected, 'url:%s, got:`%s` expected: `%s`' % (test, _test, expected)
General Comments 0
You need to be logged in to leave comments. Login now