You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
7.0KB

  1. # Copyright (C) 2016-Today: Odoo Community Association (OCA)
  2. # Copyright 2020 Tecnativa - Víctor Martínez
  3. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import api, fields, models
  6. class GithubOrganization(models.Model):
  7. _name = "github.organization"
  8. _inherit = ["abstract.github.model"]
  9. _order = "name"
  10. _description = "Github organization"
  11. _github_type = "organization"
  12. _github_login_field = "login"
  13. # Columns Section
  14. name = fields.Char(string="Organization Name", required=True, readonly=True)
  15. image = fields.Image(string="Image", readonly=True)
  16. description = fields.Char(string="Description", readonly=True)
  17. email = fields.Char(string="Email", readonly=True)
  18. website_url = fields.Char(string="Website URL", readonly=True)
  19. location = fields.Char(string="Location", readonly=True)
  20. ignored_repository_names = fields.Text(
  21. string="Ignored Repositories",
  22. help="Set here repository names"
  23. " you want to ignore. One repository per line."
  24. " If set, the repositories will be created, but branches"
  25. " synchronization and source code download will be disabled."
  26. " Exemple:\n"
  27. "purchase-workflow\nOCB\nOpenUpgrade\n",
  28. )
  29. member_ids = fields.Many2many(
  30. string="Members",
  31. comodel_name="res.partner",
  32. relation="github_organization_partner_rel",
  33. column1="organization_id",
  34. column2="partner_id",
  35. readonly=True,
  36. )
  37. member_qty = fields.Integer(
  38. string="Number of Members", compute="_compute_member_qty", store=True
  39. )
  40. repository_ids = fields.One2many(
  41. string="Repositories",
  42. comodel_name="github.repository",
  43. # ondelete="cascade",
  44. inverse_name="organization_id",
  45. readonly=True,
  46. )
  47. repository_qty = fields.Integer(
  48. string="Number of Repositories", compute="_compute_repository_qty", store=True
  49. )
  50. team_ids = fields.One2many(
  51. string="Teams",
  52. comodel_name="github.team",
  53. inverse_name="organization_id",
  54. readonly=True,
  55. )
  56. team_qty = fields.Integer(
  57. string="Number of Teams", compute="_compute_team_qty", store=True
  58. )
  59. organization_serie_ids = fields.One2many(
  60. string="Organization Series",
  61. comodel_name="github.organization.serie",
  62. inverse_name="organization_id",
  63. )
  64. organization_serie_qty = fields.Integer(
  65. string="Number of Series", store=True, compute="_compute_organization_serie_qty"
  66. )
  67. coverage_url_pattern = fields.Char(string="Coverage URL Pattern")
  68. ci_url_pattern = fields.Char(string="CI URL Pattern")
  69. analysis_rule_ids = fields.Many2many(
  70. string="Analysis Rules", comodel_name="github.analysis.rule"
  71. )
  72. # Overloadable Section
  73. @api.model
  74. def get_conversion_dict(self):
  75. res = super().get_conversion_dict()
  76. res.update(
  77. {
  78. "name": "name",
  79. "description": "description",
  80. "location": "location",
  81. "email": "email",
  82. "website_url": "blog",
  83. }
  84. )
  85. return res
  86. @api.model
  87. def get_odoo_data_from_github(self, data):
  88. res = super().get_odoo_data_from_github(data)
  89. if "avatar_url" in data:
  90. res.update({"image": self.get_base64_image_from_github(data["avatar_url"])})
  91. return res
  92. def full_update(self):
  93. self.button_sync_member()
  94. self.button_sync_repository()
  95. self.button_sync_team()
  96. @api.model
  97. def cron_update_organization_team(self):
  98. organizations = self.search([])
  99. organizations.full_update()
  100. organizations.mapped("team_ids").full_update()
  101. return True
  102. # Compute Section
  103. @api.depends("member_ids", "member_ids.organization_ids")
  104. def _compute_member_qty(self):
  105. for organization in self:
  106. organization.member_qty = len(organization.member_ids)
  107. @api.depends("repository_ids.organization_id")
  108. def _compute_repository_qty(self):
  109. for organization in self:
  110. organization.repository_qty = len(organization.repository_ids)
  111. @api.depends("team_ids.organization_id")
  112. def _compute_team_qty(self):
  113. for organization in self:
  114. organization.team_qty = len(organization.team_ids)
  115. @api.depends("organization_serie_ids.organization_id")
  116. def _compute_organization_serie_qty(self):
  117. for organization in self:
  118. organization.organization_serie_qty = len(
  119. organization.organization_serie_ids
  120. )
  121. # Action section
  122. def button_sync_member(self):
  123. github_member = self.get_github_connector("organization_members")
  124. partner_obj = self.env["res.partner"]
  125. for organization in self:
  126. member_ids = []
  127. for data in github_member.list([organization.github_login]):
  128. partner = partner_obj.get_from_id_or_create(data)
  129. member_ids.append(partner.id)
  130. organization.member_ids = member_ids
  131. def button_sync_repository(self):
  132. repository_obj = self.env["github.repository"]
  133. github_repo = self.get_github_connector("organization_repositories")
  134. for organization in self:
  135. repository_ids = []
  136. for data in github_repo.list([organization.github_login]):
  137. repository = repository_obj.get_from_id_or_create(data)
  138. repository_ids.append(repository.id)
  139. organization.repository_ids = repository_ids
  140. def button_sync_team(self):
  141. team_obj = self.env["github.team"]
  142. github_team = self.get_github_connector("organization_teams")
  143. for organization in self:
  144. team_ids = []
  145. for data in github_team.list([organization.github_login]):
  146. team = team_obj.get_from_id_or_create(
  147. data, {"organization_id": organization.id}
  148. )
  149. team_ids.append(team.id)
  150. organization.team_ids = team_ids
  151. def action_github_repository(self):
  152. self.ensure_one()
  153. action = self.env.ref("github_connector.action_github_repository").read()[0]
  154. action["context"] = dict(self.env.context)
  155. action["context"].pop("group_by", None)
  156. action["context"]["search_default_organization_id"] = self.id
  157. return action
  158. def action_github_team(self):
  159. self.ensure_one()
  160. action = self.env.ref("github_connector.action_github_team").read()[0]
  161. action["context"] = dict(self.env.context)
  162. action["context"].pop("group_by", None)
  163. action["context"]["search_default_organization_id"] = self.id
  164. return action
  165. def action_res_partner(self):
  166. self.ensure_one()
  167. action = self.env.ref("github_connector.action_res_partner").read()[0]
  168. action["context"] = dict(self.env.context)
  169. action["context"].pop("group_by", None)
  170. action["context"]["search_default_organization_ids"] = self.id
  171. return action
上海开阖软件有限公司 沪ICP备12045867号-1