@@ -252,9 +252,38 @@ def lambda_handler(event, context):
252252 if response_code == 204:
253253 print(f"Repository {repo_name} deleted successfully")
254254 except urllib.error.HTTPError as e:
255- print(f"HTTP Error: {e.code} - {e.reason}")
256- print(e.read().decode())
257- raise
255+ if e.code == 404:
256+ # Repository doesn't exist - already deleted
257+ print(f"Repository {repo_name} not found - treating as successful deletion")
258+ cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
259+ return
260+ elif e.code == 403:
261+ # Forbidden - try to archive instead
262+ print(f"HTTP Error deleting repository: 403 - Forbidden")
263+ print(f"Attempting to archive repository instead...")
264+ try:
265+ archive_data = json.dumps({'archived': True}).encode('utf-8')
266+ archive_req = urllib.request.Request(
267+ f"https://api.github.com/repos/{github_owner}/{repo_name}",
268+ data=archive_data,
269+ headers=headers,
270+ method='PATCH'
271+ )
272+ with urllib.request.urlopen(archive_req) as archive_response:
273+ if archive_response.getcode() == 200:
274+ print(f"Repository {repo_name} archived successfully")
275+ cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
276+ return
277+ except Exception as archive_error:
278+ print(f"Failed to archive repository: {str(archive_error)}")
279+ cfnresponse.send(event, context, cfnresponse.FAILED, {})
280+ return
281+ else:
282+ # Other errors - fail
283+ print(f"HTTP Error deleting repository: {e.code} - {e.reason}")
284+ print(e.read().decode())
285+ cfnresponse.send(event, context, cfnresponse.FAILED, {})
286+ return
258287
259288 cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
260289 else:
0 commit comments