Skip to content

Commit a0e4d78

Browse files
committed
updated code
1 parent f840e31 commit a0e4d78

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

functions-python/tasks_executor/src/tasks/licenses/populate_license_rules.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,39 @@ def populate_license_rules_task(dry_run, db_session):
3434

3535
try:
3636
logging.info(f"Downloading rules from {RULES_JSON_URL}")
37-
response = requests.get(RULES_JSON_URL)
37+
response = requests.get(RULES_JSON_URL, timeout=10)
3838
response.raise_for_status()
39-
rules_data = response.json()
40-
logging.info(f"Rules data downloaded: {rules_data}")
41-
logging.info(f"Successfully downloaded {len(rules_data)} rules.")
39+
rules_json = response.json()
40+
41+
# Combine all rule lists from the three categories
42+
rules_data = []
43+
for rule_type, rule_list in rules_json.items():
44+
for rule_data in rule_list:
45+
# Attach the category/type info to each rule
46+
rule_data["type"] = rule_type
47+
rules_data.append(rule_data)
48+
49+
logging.info(
50+
f"Loaded {len(rules_data)} rules from {len(rules_json)} categories."
51+
)
4252

4353
if dry_run:
44-
logging.info("Dry run enabled. No changes will be made to the database.")
45-
logging.info(f"Would attempt to upsert {len(rules_data)} rules.")
54+
logging.info(f"Dry run: would insert/update {len(rules_data)} rules.")
4655
else:
47-
logging.info("Populating license rules in the database...")
48-
4956
for rule_data in rules_data:
50-
# Create a Rule ORM object from the downloaded data
5157
rule_object = Rule(
5258
name=rule_data.get("name"),
5359
label=rule_data.get("label"),
5460
description=rule_data.get("description"),
5561
type=rule_data.get("type"),
5662
)
57-
# Merge the object into the session.
58-
# If a rule with the same primary key (name) exists, it will be updated.
59-
# If not, a new one will be inserted.
6063
db_session.merge(rule_object)
6164

6265
db_session.commit()
6366
logging.info(
64-
f"License rules populated successfully. {len(rules_data)} rules were upserted."
67+
f"Successfully upserted {len(rules_data)} rules into the database."
6568
)
69+
6670
except requests.exceptions.RequestException as e:
6771
logging.error(f"Failed to download rules JSON file: {e}")
6872
raise

0 commit comments

Comments
 (0)