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.

207 lines
6.6KB

  1. # Copyright (C) 2016-Today: Odoo Community Association (OCA)
  2. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import logging
  5. from odoo import api, fields, models
  6. _logger = logging.getLogger(__name__)
  7. class GithubRepository(models.Model):
  8. _name = "github.repository"
  9. _inherit = ["abstract.github.model"]
  10. _order = "organization_id, name"
  11. _description = "Github Repository"
  12. _github_type = "repository"
  13. _github_login_field = "full_name"
  14. # Column Section
  15. organization_id = fields.Many2one(
  16. comodel_name="github.organization",
  17. string="Organization",
  18. required=True,
  19. index=True,
  20. readonly=True,
  21. ondelete="cascade",
  22. )
  23. name = fields.Char(string="Name", index=True, required=True, readonly=True)
  24. complete_name = fields.Char(
  25. string="Complete Name",
  26. readonly=True,
  27. compute="_compute_complete_name",
  28. store=True,
  29. )
  30. description = fields.Char(string="Description", readonly=True)
  31. website = fields.Char(string="Website", readonly=True)
  32. repository_branch_ids = fields.One2many(
  33. comodel_name="github.repository.branch",
  34. inverse_name="repository_id",
  35. string="Branches",
  36. readonly=True,
  37. )
  38. repository_branch_qty = fields.Integer(
  39. string="Number of Branches",
  40. compute="_compute_repository_branch_qty",
  41. store=True,
  42. )
  43. team_ids = fields.One2many(
  44. string="Teams",
  45. comodel_name="github.team.repository",
  46. inverse_name="repository_id",
  47. readonly=True,
  48. )
  49. team_qty = fields.Integer(
  50. string="Number of Teams", compute="_compute_team_qty", store=True
  51. )
  52. is_ignored = fields.Boolean(
  53. string="Is Ignored",
  54. compute="_compute_ignore",
  55. # multi="ignore",
  56. help="If checked, the branches will not be synchronized, and the"
  57. " code source will this way not be downloaded and analyzed. To ignore"
  58. " a repository, go to the organization and add the file"
  59. " 'Ignored Repositories'.",
  60. )
  61. color = fields.Integer(
  62. string="Color Index",
  63. # multi="ignore",
  64. compute="_compute_ignore"
  65. )
  66. analysis_rule_ids = fields.Many2many(
  67. string="Analysis Rules", comodel_name="github.analysis.rule"
  68. )
  69. # Compute Section
  70. @api.depends("organization_id.ignored_repository_names")
  71. def _compute_ignore(self):
  72. for repository in self:
  73. ignored_txt = repository.organization_id.ignored_repository_names
  74. repository.is_ignored = (
  75. ignored_txt and repository.name in ignored_txt.split("\n")
  76. )
  77. repository.color = repository.is_ignored and 1 or 0
  78. @api.depends("team_ids")
  79. def _compute_team_qty(self):
  80. for repository in self:
  81. repository.team_qty = len(repository.team_ids)
  82. @api.depends("name", "organization_id.github_login")
  83. def _compute_complete_name(self):
  84. for repository in self:
  85. repository.complete_name = "%(login)s/%(rep_name)s" % (
  86. {
  87. "login": repository.organization_id.github_login,
  88. "rep_name": repository.name or "",
  89. }
  90. )
  91. @api.depends("repository_branch_ids.repository_id")
  92. def _compute_repository_branch_qty(self):
  93. for repository in self:
  94. repository.repository_branch_qty = len(repository.repository_branch_ids)
  95. # Overloadable Section
  96. @api.model
  97. def get_conversion_dict(self):
  98. res = super().get_conversion_dict()
  99. res.update(
  100. {
  101. "name": "name",
  102. "github_url": "url",
  103. "description": "description",
  104. "website": "homepage",
  105. }
  106. )
  107. return res
  108. @api.model
  109. def get_odoo_data_from_github(self, data):
  110. organization_obj = self.env["github.organization"]
  111. res = super().get_odoo_data_from_github(data)
  112. organization = organization_obj.get_from_id_or_create(data["owner"])
  113. res.update({"organization_id": organization.id})
  114. return res
  115. def get_github_data_from_odoo(self):
  116. self.ensure_one()
  117. return {
  118. "name": self.name,
  119. "description": self.description or "",
  120. "homepage": self.website,
  121. }
  122. def get_github_args_for_creation(self):
  123. self.ensure_one()
  124. return [self.organization_id.github_login]
  125. def full_update(self):
  126. self.button_sync_branch()
  127. @api.model
  128. def cron_update_branch_list(self):
  129. branches = self.search([])
  130. branches.button_sync_branch()
  131. return True
  132. def button_sync_branch(self):
  133. github_branch = self.get_github_connector("repository_branches")
  134. branch_obj = self.env["github.repository.branch"]
  135. for repository in self:
  136. branch_ids = []
  137. correct_series = repository.organization_id.organization_serie_ids.mapped(
  138. "name"
  139. )
  140. for data in github_branch.list([repository.github_login]):
  141. if repository.is_ignored:
  142. pass
  143. elif data["name"] in correct_series:
  144. # We don't use get_from_id_or_create because repository
  145. # branches does not have any ids. (very basic object in the
  146. # Github API)
  147. branch = branch_obj.create_or_update_from_name(
  148. repository.id, data["name"]
  149. )
  150. branch_ids.append(branch.id)
  151. else:
  152. _logger.warning(
  153. "the branch '%s'/'%s' has been ignored.",
  154. repository.name,
  155. data["name"],
  156. )
  157. repository.branch_ids = branch_ids
  158. def action_github_team_repository_from_repository(self):
  159. self.ensure_one()
  160. action = self.env.ref(
  161. "github_connector.action_github_team_repository_from_repository"
  162. ).read()[0]
  163. action["context"] = dict(self.env.context)
  164. action["context"].pop("group_by", None)
  165. action["context"]["search_default_repository_id"] = self.id
  166. return action
  167. def action_github_repository_branch(self):
  168. self.ensure_one()
  169. action = self.env.ref(
  170. "github_connector.action_github_repository_branch"
  171. ).read()[0]
  172. action["context"] = dict(self.env.context)
  173. action["context"].pop("group_by", None)
  174. action["context"]["search_default_repository_id"] = self.id
  175. return action
上海开阖软件有限公司 沪ICP备12045867号-1