5
0
mirror of https://github.com/odoo/runbot.git synced 2025-03-27 13:25:47 +07:00
runbot/runbot_merge/models/crons/cleanup_scratch_branches.py
Xavier Morel 76f4ed3bf6 [ADD] runbot_merge: delete scratch branches when a branch is disabled
If a branch `foo` is disabled, then `tmp.foo` and `staging.foo` become
unnecessary (with  fixed the tmp refs are not used for creating
stagings anymore, but for now they're still used for the "safety
dance" of merging a successful staging into the corresponding
mainline).

Fixes 
2023-08-31 09:07:01 +02:00

33 lines
1.2 KiB
Python

import logging
from odoo import models
_logger = logging.getLogger(__name__)
class BranchCleanup(models.TransientModel):
_name = 'runbot_merge.branch_cleanup'
_description = "cleans up scratch refs for deactivated branches"
def _run(self):
deactivated = self.env['runbot_merge.branch'].search([
('active', '=', False),
('write_date', '>=', self.env.context['lastcall']),
])
_logger.info(
"deleting scratch (tmp and staging) refs for branches %s",
', '.join(b.name for b in deactivated)
)
# loop around the repos first, so we can reuse the gh instance
for r in deactivated.mapped('project_id.repo_ids'):
gh = r.github()
for b in deactivated:
if b.project_id != r.project_id:
continue
res = gh('delete', f'git/refs/heads/tmp.{b.name}', check=False)
if res.status_code != 204:
_logger.info("no tmp branch found for %s:%s", r.name, b.name)
res = gh('delete', f'git/refs/heads/staging.{b.name}', check=False)
if res.status_code != 204:
_logger.info("no staging branch found for %s:%s", res.name, b.name)