Skip to content

Commit 2b70735

Browse files
committed
Improved the classification of some github related external resources. Tightened validation of new resource values. Commit ready to merge.
- Legacy-Id: 18378
1 parent 2af4246 commit 2b70735

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright The IETF Trust 2020, All Rights Reserved
2+
3+
from urllib.parse import urlparse
4+
from django.db import migrations
5+
6+
def categorize(url):
7+
# This will categorize a few urls pointing into files in a repo as a repo, but that's better than calling them an org
8+
element_count = len(urlparse(url).path.strip('/').split('/'))
9+
if element_count < 1:
10+
print("Bad github resource:",url)
11+
return 'github_org' if element_count == 1 else 'github_repo'
12+
13+
def forward(apps, schema_editor):
14+
DocExtResource = apps.get_model('doc','DocExtResource')
15+
16+
for resource in DocExtResource.objects.filter(name__slug__in=('github_org','github_repo')):
17+
category = categorize(resource.value)
18+
if resource.name_id != category:
19+
resource.name_id = category
20+
resource.save()
21+
22+
def reverse(apps, schema_editor):
23+
# Intentionally don't try to return to former worse state
24+
pass
25+
26+
class Migration(migrations.Migration):
27+
28+
dependencies = [
29+
('doc', '0035_populate_docextresources'),
30+
]
31+
32+
operations = [
33+
migrations.RunPython(forward, reverse),
34+
]

ietf/doc/tests_draft.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,7 @@ def test_edit_doc_extresources(self):
11411141

11421142
badlines = (
11431143
'github_repo https://github3.com/some/repo',
1144+
'github_org https://github.com/not/an_org',
11441145
'github_notify badaddr',
11451146
'website /not/a/good/url',
11461147
'notavalidtag blahblahblah',
@@ -1154,6 +1155,7 @@ def test_edit_doc_extresources(self):
11541155

11551156
goodlines = """
11561157
github_repo https://github.com/some/repo Some display text
1158+
github_org https://github.com/totally_some_org
11571159
github_username githubuser
11581160
webpage http://example.com/http/is/fine
11591161
"""
@@ -1163,7 +1165,7 @@ def test_edit_doc_extresources(self):
11631165
doc = Document.objects.get(name=self.docname)
11641166
self.assertEqual(doc.latest_event(DocEvent,type="changed_document").desc[:35], 'Changed document external resources')
11651167
self.assertIn('github_username githubuser', doc.latest_event(DocEvent,type="changed_document").desc)
1166-
self.assertEqual(doc.docextresource_set.count(), 3)
1168+
self.assertEqual(doc.docextresource_set.count(), 4)
11671169
self.assertEqual(doc.docextresource_set.get(name__slug='github_repo').display_name, 'Some display text')
11681170
self.assertIn(doc.docextresource_set.first().name.slug,str(doc.docextresource_set.first()))
11691171

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright The IETF Trust 2020, All Rights Reserved
2+
3+
from urllib.parse import urlparse
4+
from django.db import migrations
5+
6+
def categorize(url):
7+
# This will categorize a few urls pointing into files in a repo as a repo, but that's better than calling them an org
8+
element_count = len(urlparse(url).path.strip('/').split('/'))
9+
if element_count < 1:
10+
print("Bad github resource:",url)
11+
return 'github_org' if element_count == 1 else 'github_repo'
12+
13+
def forward(apps, schema_editor):
14+
GroupExtResource = apps.get_model('group','GroupExtResource')
15+
16+
for resource in GroupExtResource.objects.filter(name_id__in=('github_org','github_repo',)):
17+
category = categorize(resource.value)
18+
if resource.name_id != category:
19+
resource.name_id = category
20+
resource.save()
21+
22+
def reverse(apps, schema_editor):
23+
# Intentionally don't try to return to former worse state
24+
pass
25+
26+
class Migration(migrations.Migration):
27+
28+
dependencies = [
29+
('group', '0035_add_research_area_groups'),
30+
]
31+
32+
operations = [
33+
migrations.RunPython(forward, reverse),
34+
]

ietf/utils/validators.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,16 @@ def __call__(self, value):
170170
def validate_external_resource_value(name, value):
171171
""" validate a resource value using its name's properties """
172172

173-
if name.type.slug == 'url':
173+
if name.type_id == 'url':
174174

175175
if name.slug in ( 'github_org', 'github_repo' ):
176176
validate_http_url(value)
177-
hostname = urlparse(value).netloc.lower()
177+
parsed_url = urlparse(value)
178+
hostname = parsed_url.netloc.lower()
178179
if not any([ hostname.endswith(x) for x in ('github.com','github.io' ) ]):
179180
raise ValidationError('URL must be a github url')
181+
if name.slug == 'github_org' and len(parsed_url.path.strip('/').split('/'))!=1:
182+
raise ValidationError('github path has too many components to be an organization URL')
180183
elif name.slug == 'jabber_room':
181184
validate_xmpp(value)
182185
else:

0 commit comments

Comments
 (0)