build: ensure index.json is actually valid JSON before uploading (backport: 3-0-x) (#16749)

* build: ensure index.json is actually valid JSON before uploading

* chore: fix py linting for validation of index.json
This commit is contained in:
trop[bot] 2019-02-05 15:00:49 -08:00 committed by Samuel Attard
parent 60596b47f5
commit 021d856006

View file

@ -1,5 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
import json
import os import os
import sys import sys
import urllib2 import urllib2
@ -15,6 +16,13 @@ BASE_URL = 'https://electron-metadumper.herokuapp.com/?version='
version = sys.argv[1] version = sys.argv[1]
authToken = os.getenv('META_DUMPER_AUTH_HEADER') authToken = os.getenv('META_DUMPER_AUTH_HEADER')
def is_json(myjson):
try:
json.loads(myjson)
except ValueError:
return False
return True
def get_content(retry_count = 5): def get_content(retry_count = 5):
try: try:
request = urllib2.Request( request = urllib2.Request(
@ -22,9 +30,14 @@ def get_content(retry_count = 5):
headers={"Authorization" : authToken} headers={"Authorization" : authToken}
) )
return urllib2.urlopen( proposed_content = urllib2.urlopen(
request request
).read() ).read()
if is_json(proposed_content):
return proposed_content
print("bad attempt")
raise Exception("Failed to fetch valid JSON from the metadumper service")
except Exception as e: except Exception as e:
if retry_count == 0: if retry_count == 0:
raise e raise e