OLD | NEW |
1 # Copyright 2017 The Chromium Authors. All rights reserved. | 1 # Copyright 2017 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 from dashboard.common import namespaced_stored_object | 5 from dashboard.common import namespaced_stored_object |
6 | 6 |
7 | 7 |
8 _REPOSITORIES_KEY = 'repositories' | 8 _REPOSITORIES_KEY = 'repositories' |
9 _URLS_TO_NAMES_KEY = 'repository_urls_to_names' | 9 _URLS_TO_NAMES_KEY = 'repository_urls_to_names' |
10 | 10 |
11 | 11 |
12 def RepositoryUrl(name): | 12 def RepositoryUrl(name): |
| 13 """Returns the URL of a repository, given its short name. |
| 14 |
| 15 If a repository moved locations or has multiple locations, a repository can |
| 16 have multiple URLs. The returned URL should be the current canonical one. |
| 17 |
| 18 Args: |
| 19 name: The short name of the repository. |
| 20 |
| 21 Returns: |
| 22 A URL string, not including '.git'. |
| 23 """ |
13 repositories = namespaced_stored_object.Get(_REPOSITORIES_KEY) | 24 repositories = namespaced_stored_object.Get(_REPOSITORIES_KEY) |
| 25 # We have the 'repository_url' key in case we want to add more fields later. |
14 return repositories[name]['repository_url'] | 26 return repositories[name]['repository_url'] |
15 | 27 |
16 | 28 |
17 def Repository(url, add_if_missing=False): | 29 def Repository(url, add_if_missing=False): |
| 30 """Returns the short repository name, given its URL. |
| 31 |
| 32 By default, the short repository name is the last part of the URL. |
| 33 E.g. "https://chromium.googlesource.com/v8/v8": "v8" |
| 34 In some cases this is ambiguous, so the names can be manually adjusted. |
| 35 E.g. "../chromium/src": "chromium" and "../breakpad/breakpad/src": "breakpad" |
| 36 |
| 37 If a repository moved locations or has multiple locations, multiple URLs can |
| 38 map to the same name. This should only be done if they are exact mirrors and |
| 39 have the same git hashes. |
| 40 "https://webrtc.googlesource.com/src": "webrtc" |
| 41 "https://webrtc.googlesource.com/src/webrtc": "old_webrtc" |
| 42 "https://chromium.googlesource.com/external/webrtc/trunk/webrtc": "old_webrtc" |
| 43 |
| 44 Internally, all repositories are stored by short name, which always maps to |
| 45 the current canonical URL, so old URLs are automatically "upconverted". |
| 46 |
| 47 Args: |
| 48 url: The repository URL. |
| 49 add_if_missing: If True, also attempts to add the URL to the database with |
| 50 the default name mapping. Throws an exception if there's a name collision. |
| 51 |
| 52 Returns: |
| 53 The short name as a string. |
| 54 |
| 55 Raises: |
| 56 AssertionError: add_if_missing is True and there's a name collision. |
| 57 """ |
18 if url.endswith('.git'): | 58 if url.endswith('.git'): |
19 url = url[:-4] | 59 url = url[:-4] |
20 | 60 |
21 urls_to_names = namespaced_stored_object.Get(_URLS_TO_NAMES_KEY) | 61 urls_to_names = namespaced_stored_object.Get(_URLS_TO_NAMES_KEY) |
22 try: | 62 try: |
23 return urls_to_names[url] | 63 return urls_to_names[url] |
24 except KeyError: | 64 except KeyError: |
25 if add_if_missing: | 65 if add_if_missing: |
26 return _AddRepository(url) | 66 return _AddRepository(url) |
27 raise | 67 raise |
28 | 68 |
29 | 69 |
30 def _AddRepository(url): | 70 def _AddRepository(url): |
| 71 """Add a repository URL to the database with the default name mapping. |
| 72 |
| 73 The default short repository name is the last part of the URL. |
| 74 |
| 75 Returns: |
| 76 The short repository name. |
| 77 |
| 78 Raises: |
| 79 AssertionError: The default name is already in the database. |
| 80 """ |
31 name = url.split('/')[-1] | 81 name = url.split('/')[-1] |
32 | 82 |
33 # Add to main repositories dict. | 83 # Add to main repositories dict. |
34 repositories = namespaced_stored_object.Get(_REPOSITORIES_KEY) | 84 repositories = namespaced_stored_object.Get(_REPOSITORIES_KEY) |
35 if name in repositories: | 85 if name in repositories: |
36 raise AssertionError("Attempted to add a repository that's already in the " | 86 raise AssertionError("Attempted to add a repository that's already in the " |
37 'Datastore: %s: %s' % (name, url)) | 87 'Datastore: %s: %s' % (name, url)) |
38 repositories[name] = {'repository_url': url} | 88 repositories[name] = {'repository_url': url} |
39 namespaced_stored_object.Set(_REPOSITORIES_KEY, repositories) | 89 namespaced_stored_object.Set(_REPOSITORIES_KEY, repositories) |
40 | 90 |
41 # Add to URL -> name mapping dict. | 91 # Add to URL -> name mapping dict. |
42 urls_to_names = namespaced_stored_object.Get(_URLS_TO_NAMES_KEY) | 92 urls_to_names = namespaced_stored_object.Get(_URLS_TO_NAMES_KEY) |
43 urls_to_names[url] = name | 93 urls_to_names[url] = name |
44 namespaced_stored_object.Set(_URLS_TO_NAMES_KEY, urls_to_names) | 94 namespaced_stored_object.Set(_URLS_TO_NAMES_KEY, urls_to_names) |
45 | 95 |
46 return name | 96 return name |
OLD | NEW |