build: clean up upload-index-json error handling (#42657)
This commit is contained in:
		
					parent
					
						
							
								a8baa6f1ad
							
						
					
				
			
			
				commit
				
					
						607b9b8f11
					
				
			
		
					 1 changed files with 36 additions and 22 deletions
				
			
		| 
						 | 
					@ -15,8 +15,7 @@ OUT_DIR     = get_out_dir()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
BASE_URL = 'https://electron-metadumper.herokuapp.com/?version='
 | 
					BASE_URL = 'https://electron-metadumper.herokuapp.com/?version='
 | 
				
			||||||
 | 
					
 | 
				
			||||||
version = sys.argv[1]
 | 
					AUTH_TOKEN = os.getenv('META_DUMPER_AUTH_HEADER')
 | 
				
			||||||
authToken = os.getenv('META_DUMPER_AUTH_HEADER')
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def is_json(myjson):
 | 
					def is_json(myjson):
 | 
				
			||||||
  try:
 | 
					  try:
 | 
				
			||||||
| 
						 | 
					@ -25,40 +24,55 @@ def is_json(myjson):
 | 
				
			||||||
    return False
 | 
					    return False
 | 
				
			||||||
  return True
 | 
					  return True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_content(retry_count = 5):
 | 
					def get_content(version, retry_count=5):
 | 
				
			||||||
  try:
 | 
					  for attempt in range(retry_count):
 | 
				
			||||||
    request = Request(
 | 
					    try:
 | 
				
			||||||
      BASE_URL + version,
 | 
					      request = Request(
 | 
				
			||||||
      headers={"Authorization" : authToken}
 | 
					        BASE_URL + version,
 | 
				
			||||||
    )
 | 
					        headers={"Authorization": AUTH_TOKEN}
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    with urlopen(request) as resp:
 | 
					      with urlopen(request) as resp:
 | 
				
			||||||
      proposed_content = resp.read()
 | 
					        proposed_content = resp.read()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if is_json(proposed_content):
 | 
					      if is_json(proposed_content):
 | 
				
			||||||
      return proposed_content
 | 
					        return proposed_content
 | 
				
			||||||
    print("bad attempt")
 | 
					
 | 
				
			||||||
    raise Exception("Failed to fetch valid JSON from the metadumper service")
 | 
					      print("Received content is not valid JSON.")
 | 
				
			||||||
  except Exception as e:
 | 
					      if attempt == retry_count - 1:
 | 
				
			||||||
    if retry_count == 0:
 | 
					        return None
 | 
				
			||||||
      raise e
 | 
					
 | 
				
			||||||
    return get_content(retry_count - 1)
 | 
					    except Exception as e:
 | 
				
			||||||
 | 
					      print(f"Attempt {attempt + 1} failed: {e}")
 | 
				
			||||||
 | 
					      if attempt == retry_count - 1:
 | 
				
			||||||
 | 
					        return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if attempt < retry_count - 1:
 | 
				
			||||||
 | 
					      print("Retrying...")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def main():
 | 
					def main():
 | 
				
			||||||
  if not authToken or authToken == "":
 | 
					  if not AUTH_TOKEN or AUTH_TOKEN == "":
 | 
				
			||||||
    raise Exception("Please set META_DUMPER_AUTH_HEADER")
 | 
					    raise Exception("Please set META_DUMPER_AUTH_HEADER")
 | 
				
			||||||
  # Upload the index.json.
 | 
					
 | 
				
			||||||
 | 
					  if len(sys.argv) < 2 or not sys.argv[1]:
 | 
				
			||||||
 | 
					    raise Exception("Version is required")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  version = sys.argv[1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  with scoped_cwd(ELECTRON_DIR):
 | 
					  with scoped_cwd(ELECTRON_DIR):
 | 
				
			||||||
    safe_mkdir(OUT_DIR)
 | 
					    safe_mkdir(OUT_DIR)
 | 
				
			||||||
    index_json = os.path.relpath(os.path.join(OUT_DIR, 'index.json'))
 | 
					    index_json = os.path.relpath(os.path.join(OUT_DIR, 'index.json'))
 | 
				
			||||||
 | 
					    new_content = get_content(version)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    new_content = get_content()
 | 
					    if new_content is None:
 | 
				
			||||||
 | 
					      raise Exception("Failed to fetch valid JSON after maximum retries.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    with open(index_json, "wb") as f:
 | 
					    with open(index_json, "wb") as f:
 | 
				
			||||||
      f.write(new_content)
 | 
					      f.write(new_content)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    store_artifact(OUT_DIR, 'headers/dist', [index_json])
 | 
					    store_artifact(OUT_DIR, 'headers/dist', [index_json])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
  sys.exit(main())
 | 
					  sys.exit(main())
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue