From c399f9f96cc6b7228b8a4b0ab7e1c4e5127e3877 Mon Sep 17 00:00:00 2001 From: Enrique Martin Zabalza Date: Sat, 2 Feb 2019 01:35:10 +0100 Subject: [PATCH] [11.0][FIX] github_connector keyerror 'blog' (#34) [FIX] github_connector: keyerror 'blog' Description of the issue/feature this PR addresses: On new database error when syncing new github organization (example: OCA). Error traceback: KeyError: 'blog' Current behavior before PR: Synchronization gets blog key on `get_odoo_data_from_github` but blog field does not exist. When it tries to match the key with model attribute we get key error. Desired behavior after PR is merged: Get blog key from github data and show it on form view. --- github_connector/README.rst | 1 + github_connector/__manifest__.py | 2 +- .../models/abstract_github_model.py | 29 ++++++++++++++----- .../models/github_organization.py | 16 +++++++--- github_connector/models/github_repository.py | 15 +++++++--- github_connector/models/github_team.py | 14 +++++++-- github_connector/models/res_partner.py | 16 ++++++++-- 7 files changed, 70 insertions(+), 23 deletions(-) diff --git a/github_connector/README.rst b/github_connector/README.rst index 7491a714..5082a458 100644 --- a/github_connector/README.rst +++ b/github_connector/README.rst @@ -242,6 +242,7 @@ Contributors * Sébastien BEAU (sebastien.beau@akretion.com) * Benoît GUILLOT (benoit.guillot@akretion.com) * Vicent Cubells (vicent.cubells@tecnativa.com) +* Enrique Martín (enriquemartin@digital5.es) Maintainer ---------- diff --git a/github_connector/__manifest__.py b/github_connector/__manifest__.py index 0505d222..25a490cb 100644 --- a/github_connector/__manifest__.py +++ b/github_connector/__manifest__.py @@ -5,7 +5,7 @@ { 'name': 'Github Connector', 'summary': 'Synchronize information from Github repositories', - 'version': '11.0.1.1.1', + 'version': '11.0.1.1.2', 'category': 'Connector', 'license': 'AGPL-3', 'author': diff --git a/github_connector/models/abstract_github_model.py b/github_connector/models/abstract_github_model.py index 0e4648e2..29bd8b8a 100644 --- a/github_connector/models/abstract_github_model.py +++ b/github_connector/models/abstract_github_model.py @@ -63,17 +63,30 @@ def github_login_field(self): return self._github_login_field @api.model - def get_odoo_data_from_github(self, data): - """Prepare function that map Github data to create in Odoo""" + def get_conversion_dict(self): + """ + Prepare function that map Github fields to Odoo fields + :return: Dictionary {odoo_field: github_field} + """ return { - 'github_id_external': data['id'], - 'github_url': data.get('html_url', False), - 'github_login': data.get(self.github_login_field(), False), - 'github_create_date': data.get('created_at', False), - 'github_write_date': data.get('updated_at', False), - 'github_last_sync_date': fields.Datetime.now(), + 'github_id_external': 'id', + 'github_url': 'html_url', + 'github_login': self.github_login_field(), + 'github_create_date': 'created_at', + 'github_write_date': 'updated_at', } + @api.model + def get_odoo_data_from_github(self, data): + """Prepare function that map Github data to create in Odoo""" + map_dict = self.get_conversion_dict() + res = {} + for k, v in map_dict.items(): + if hasattr(self, k) and data.get(v, False): + res.update({k: data[v]}) + res.update({'github_last_sync_date': fields.Datetime.now()}) + return res + @api.multi def get_github_data_from_odoo(self): """Prepare function that map Odoo data to create in Github. diff --git a/github_connector/models/github_organization.py b/github_connector/models/github_organization.py index 1b42dc63..4c21e0c2 100644 --- a/github_connector/models/github_organization.py +++ b/github_connector/models/github_organization.py @@ -74,13 +74,21 @@ class GithubOrganization(models.Model): ci_url_pattern = fields.Char(string='CI URL Pattern') # Overloadable Section + @api.model + def get_conversion_dict(self): + res = super(GithubOrganization, self).get_conversion_dict() + res.update({ + 'name': 'name', + 'description': 'description', + 'location': 'location', + 'email': 'email', + 'website_url': 'blog', + }) + return res + @api.model def get_odoo_data_from_github(self, data): res = super(GithubOrganization, self).get_odoo_data_from_github(data) - keys = ['name', 'description', 'location', 'blog', 'email'] - for key in keys: - if key in data: - res.update({key: data[key]}) if 'avatar_url' in data: res.update({ 'image': self.get_base64_image_from_github(data['avatar_url']), diff --git a/github_connector/models/github_repository.py b/github_connector/models/github_repository.py index e225e385..6dba98fe 100644 --- a/github_connector/models/github_repository.py +++ b/github_connector/models/github_repository.py @@ -91,16 +91,23 @@ def _compute_repository_branch_qty(self): len(repository.repository_branch_ids) # Overloadable Section + @api.model + def get_conversion_dict(self): + res = super(GithubRepository, self).get_conversion_dict() + res.update({ + 'name': 'name', + 'github_url': 'url', + 'description': 'description', + 'website': 'homepage', + }) + return res + @api.model def get_odoo_data_from_github(self, data): organization_obj = self.env['github.organization'] res = super(GithubRepository, self).get_odoo_data_from_github(data) organization = organization_obj.get_from_id_or_create(data['owner']) res.update({ - 'name': data['name'], - 'github_url': data['url'], - 'description': data['description'], - 'website': data['homepage'], 'organization_id': organization.id, }) return res diff --git a/github_connector/models/github_team.py b/github_connector/models/github_team.py index 061176d8..0b8ef4f7 100644 --- a/github_connector/models/github_team.py +++ b/github_connector/models/github_team.py @@ -93,6 +93,17 @@ def _compute_repository_qty(self): team.repository_qty = len(team.repository_ids) # Overloadable Section + @api.model + def get_conversion_dict(self): + res = super(GithubTeam, self).get_conversion_dict() + res.update({ + 'name': 'name', + 'description': 'description', + 'privacy': 'privacy', + }) + return res + + @api.model def get_odoo_data_from_github(self, data): organization_obj = self.env['github.organization'] res = super(GithubTeam, self).get_odoo_data_from_github(data) @@ -102,9 +113,6 @@ def get_odoo_data_from_github(self, data): else: organization_id = False res.update({ - 'name': data['name'], - 'description': data['description'], - 'privacy': data['privacy'], 'organization_id': organization_id, }) return res diff --git a/github_connector/models/res_partner.py b/github_connector/models/res_partner.py index 729c3c14..beae0bea 100644 --- a/github_connector/models/res_partner.py +++ b/github_connector/models/res_partner.py @@ -67,6 +67,15 @@ def _compute_github_team_qty(self): partner.github_team_qty = len(partner.github_team_ids) # Custom Section + @api.model + def get_conversion_dict(self): + res = super(ResPartner, self).get_conversion_dict() + res.update({ + 'website': 'blog', + 'email': 'email', + }) + return res + @api.model def get_odoo_data_from_github(self, data): res = super(ResPartner, self).get_odoo_data_from_github(data) @@ -74,8 +83,9 @@ def get_odoo_data_from_github(self, data): 'name': data['name'] and data['name'] or '%s (Github)' % data['login'], - 'website': data['blog'], - 'email': data['email'], - 'image': self.get_base64_image_from_github(data['avatar_url']), }) + if 'avatar_url' in data: + res.update({ + 'image': self.get_base64_image_from_github(data['avatar_url']), + }) return res