|
| 1 | +# Copyright 2026 Dixmit |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import re |
| 6 | + |
| 7 | +import yaml |
| 8 | + |
| 9 | +from odoo import fields, models |
| 10 | + |
| 11 | +_logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class VcpRule(models.Model): |
| 15 | + _inherit = "vcp.rule" |
| 16 | + |
| 17 | + rule_type = fields.Selection( |
| 18 | + selection_add=[("psc", "PSC Analysis")], |
| 19 | + ondelete={"psc": "cascade"}, |
| 20 | + ) |
| 21 | + |
| 22 | + def _process_rule_psc(self, record, parameters=None): |
| 23 | + """ |
| 24 | + Process a PSC rule for a given repository. |
| 25 | +
|
| 26 | + :param repository: The repository to process the rule for. |
| 27 | + :param rule: The PSC rule to process. |
| 28 | + """ |
| 29 | + if record._name != "vcp.repository.branch": |
| 30 | + # It doesn't make sense to process this kind of rules outside |
| 31 | + # of a repository branch, as they need the code to be downloaded |
| 32 | + # and analyzed. |
| 33 | + return |
| 34 | + record._download_code() |
| 35 | + _logger.info("Downloaded code for repository branch '%s'", record.display_name) |
| 36 | + for conf in self.paths.splitlines(): |
| 37 | + conf = conf.strip() |
| 38 | + if not conf or conf.startswith("#"): |
| 39 | + continue |
| 40 | + conf = record.local_path + "/" + conf |
| 41 | + if not os.path.exists(conf): |
| 42 | + _logger.warning( |
| 43 | + "Path '%s' does not exist for repository branch '%s'", |
| 44 | + conf, |
| 45 | + record.display_name, |
| 46 | + ) |
| 47 | + continue |
| 48 | + _logger.info(f"Found {conf}") |
| 49 | + for psc_file in os.listdir(os.path.join(record.local_path, conf, "psc")): |
| 50 | + if re.match(r"^.*\.yml$", psc_file): |
| 51 | + psc_path = os.path.join(record.local_path, conf, "psc", psc_file) |
| 52 | + with open(psc_path) as f: |
| 53 | + try: |
| 54 | + psc_data = yaml.safe_load(f) |
| 55 | + except yaml.YAMLError as e: |
| 56 | + _logger.error(f"Error parsing PSC file '{psc_path}': {e}") |
| 57 | + continue |
| 58 | + self._process_psc_data(record, psc_data) |
| 59 | + for psc_repo_file in os.listdir( |
| 60 | + os.path.join(record.local_path, conf, "repo") |
| 61 | + ): |
| 62 | + if re.match(r"^.*\.yml$", psc_repo_file): |
| 63 | + psc_repo_path = os.path.join( |
| 64 | + record.local_path, conf, "repo", psc_repo_file |
| 65 | + ) |
| 66 | + with open(psc_repo_path) as f: |
| 67 | + try: |
| 68 | + psc_repo_data = yaml.safe_load(f) |
| 69 | + except yaml.YAMLError as e: |
| 70 | + _logger.error( |
| 71 | + f"Error parsing PSC file '{psc_repo_path}': {e}" |
| 72 | + ) |
| 73 | + continue |
| 74 | + self._process_psc_repo_data(record, psc_repo_data) |
| 75 | + |
| 76 | + def _process_psc_data(self, record, psc_data): |
| 77 | + """ |
| 78 | + Process the PSC data. |
| 79 | +
|
| 80 | + :param record: The repository branch record to process the PSC data for. |
| 81 | + :param psc_data: The PSC data to process. |
| 82 | + """ |
| 83 | + if not psc_data: |
| 84 | + _logger.warning( |
| 85 | + "No PSC data found for repository branch '%s'", record.display_name |
| 86 | + ) |
| 87 | + return |
| 88 | + for psc_name in psc_data: |
| 89 | + psc_record = self.env["vcp.platform.psc"].search( |
| 90 | + [ |
| 91 | + ("platform_id", "=", record.platform_id.id), |
| 92 | + ("key", "=", psc_name), |
| 93 | + ], |
| 94 | + limit=1, |
| 95 | + ) |
| 96 | + if not psc_record: |
| 97 | + psc_record = ( |
| 98 | + self.env["vcp.platform.psc"] |
| 99 | + .sudo() |
| 100 | + .create( |
| 101 | + { |
| 102 | + "platform_id": record.platform_id.id, |
| 103 | + "key": psc_name, |
| 104 | + "name": psc_data[psc_name].get("name", psc_name), |
| 105 | + } |
| 106 | + ) |
| 107 | + ) |
| 108 | + else: |
| 109 | + psc_record.sudo().write( |
| 110 | + { |
| 111 | + "name": psc_data[psc_name].get("name", psc_name), |
| 112 | + } |
| 113 | + ) |
| 114 | + member_logins = psc_data[psc_name].get("members", []) + psc_data[ |
| 115 | + psc_name |
| 116 | + ].get("representatives", []) |
| 117 | + members = [] |
| 118 | + for member in member_logins: |
| 119 | + members.append(record.platform_id.host_id._get_user(member)) |
| 120 | + psc_record.sudo().member_ids = self.env["vcp.user"].browse(members) |
| 121 | + |
| 122 | + def _process_psc_repo_data(self, record, psc_repo_data): |
| 123 | + """ |
| 124 | + Process the PSC Repository data |
| 125 | +
|
| 126 | + :param record: The repository branch record to process the PSC data for. |
| 127 | + :param psc_repo_data: The PSC Repository data to process. |
| 128 | + """ |
| 129 | + if not psc_repo_data: |
| 130 | + _logger.warning( |
| 131 | + "No data found for repository branch '%s'", record.display_name |
| 132 | + ) |
| 133 | + return |
| 134 | + for repo_name in psc_repo_data: |
| 135 | + psc_name = psc_repo_data[repo_name].get("psc") |
| 136 | + psc_record = self.env["vcp.platform.psc"].search( |
| 137 | + [ |
| 138 | + ("platform_id", "=", record.platform_id.id), |
| 139 | + ("key", "=", psc_name), |
| 140 | + ], |
| 141 | + limit=1, |
| 142 | + ) |
| 143 | + if not psc_record: |
| 144 | + continue |
| 145 | + repo_record = ( |
| 146 | + self.env["vcp.repository"] |
| 147 | + .sudo() |
| 148 | + .search( |
| 149 | + [ |
| 150 | + ("platform_id", "=", record.platform_id.id), |
| 151 | + ("name", "=", repo_name), |
| 152 | + ], |
| 153 | + limit=1, |
| 154 | + ) |
| 155 | + ) |
| 156 | + if repo_record: |
| 157 | + repo_record.sudo().write( |
| 158 | + { |
| 159 | + "psc_id": psc_record.id, |
| 160 | + } |
| 161 | + ) |
0 commit comments