runbot/runbot_merge/models/crons/cleanup_scratch_branches.py
Xavier Morel 78cc8835ce [IMP] rubnbot_merge: avoid triggering every cron on every test
Since every cron runs on a fresh database, on the first `run_crons`
every single cron in the db will run even though almost none of them
is relevant.

Aside from the slight inefficiency, this creates unnecessary extra
garbage in the test logs.

By setting the `nextcall` of all crons to infinity in the template we
avoid this issue, only triggered crons (or the crons whose nextcall we
set ourselves) will trigger during calls.

This requires adjusting the branch cleanup cron slightly: it didn't
correctly handle the initial run (`lastcall` being false).
2024-08-05 08:03:56 +02:00

34 lines
1.3 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):
domain = [('active', '=', False)]
if lastcall := self.env.context['lastcall']:
domain.append(('write_date', '>=', lastcall))
deactivated = self.env['runbot_merge.branch'].search(domain)
_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", r.name, b.name)