| # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| """Utility for parsing Gerrit URLs into CL numbers.""" |
| |
| from __future__ import absolute_import |
| from __future__ import division |
| from __future__ import print_function |
| |
| import re |
| |
| from chromite.lib import clactions |
| |
| |
| _URL_PREFIX = r'(?:https?://)?' |
| |
| _EXTERNAL_PATTERNS = tuple( |
| re.compile(_URL_PREFIX + url_pattern) for url_pattern in ( |
| r'crrev\.com/c.*/(\d+)', |
| r'crosreview\.com/[^i].*/(\d+)', |
| r'crosreview\.com/(\d+)', |
| r'chromium-review\.googlesource\.com.*/(\d+)') |
| ) |
| |
| _INTERNAL_PATTERNS = tuple( |
| re.compile(_URL_PREFIX + url_pattern) for url_pattern in ( |
| r'crrev.com/i.*/(\d+)', |
| r'crosreview.com/i.*/(\d+)', |
| r'chrome-internal-review\.googlesource\.com.*/(\d+)') |
| ) |
| |
| |
| def CLFromBlameURL(blame_url): |
| """Parses an annotation's blame url into a GerritChangeTuple. |
| |
| External URLs are of the form (possibly prefixed by http(s?)://) |
| crrev.com/c/.../<gerrit CL number> |
| crosreview.com/.../<gerrit CL number> |
| chromium-review.googlesource.com/.../<gerrit CL number> |
| |
| Internal URLs are of the form: |
| crrev.com/i/.../<gerrit CL number> |
| crosreview.com/i/.../<gerrit CL number> |
| chrome-internal-review.googlesource.com/.../<gerrit CL number> |
| |
| E.g. _CLFromBlameURL('crrev.com/i/anything/12345') == '*12345' |
| _CLFromBlameURL('crrev.com/anything/54321') == '54321' |
| |
| Args: |
| blame_url: The URL of the CL |
| |
| Returns: |
| The matched CL, or None. |
| """ |
| for pattern in _EXTERNAL_PATTERNS: |
| m = pattern.match(blame_url) |
| if m: |
| return clactions.GerritChangeTuple(int(m.group(1)), internal=False) |
| for pattern in _INTERNAL_PATTERNS: |
| m = pattern.match(blame_url) |
| if m: |
| return clactions.GerritChangeTuple(int(m.group(1)), internal=True) |
| raise ValueError("Malformed Blame URL; can't parse a gerrit CL.") |