Skip to content

Commit bcdbd74

Browse files
committed
Fixes #2005
1 parent ba5d08d commit bcdbd74

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

cli/housekeeper.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
#
32
# sonar-tools
43
# Copyright (C) 2019-2025 Olivier Korach
@@ -124,6 +123,12 @@ def _parse_arguments() -> object:
124123
default=_DEFAULT_BRANCH_OBSOLESCENCE,
125124
help=f"Deletes branches not to be kept and not analyzed since a given number of days, by default {_DEFAULT_BRANCH_OBSOLESCENCE} days",
126125
)
126+
parser.add_argument(
127+
"--keepWhenInactive",
128+
required=False,
129+
type=str,
130+
help=f"Regexp of branches to keep when inactive, overrides the SonarQube default sonar.dbcleaner.branchesToKeepWhenInactive value",
131+
)
127132
parser.add_argument(
128133
"-R",
129134
"--pullrequestsMaxAge",
@@ -195,12 +200,13 @@ def main() -> None:
195200
sq.verify_connection()
196201
sq.set_user_agent(f"{TOOL_NAME} {version.PACKAGE_VERSION}")
197202

198-
mode, proj_age, branch_age, pr_age, token_age = (
203+
mode, proj_age, branch_age, pr_age, token_age, keep_regexp = (
199204
kwargs["mode"],
200205
kwargs["projectsMaxAge"],
201206
kwargs["branchesMaxAge"],
202207
kwargs["pullrequestsMaxAge"],
203208
kwargs["tokensMaxAge"],
209+
kwargs.get("keepWhenInactive", None),
204210
)
205211
settings = {
206212
"audit.tokens.maxAge": token_age,
@@ -209,6 +215,7 @@ def main() -> None:
209215
PROJ_MAX_AGE: proj_age,
210216
"audit.projects.branches.maxLastAnalysisAge": branch_age,
211217
"audit.projects.pullRequests.maxLastAnalysisAge": pr_age,
218+
"audit.projects.branches.keepWhenInactive": keep_regexp,
212219
c.AUDIT_MODE_PARAM: "housekeeper",
213220
options.NBR_THREADS: kwargs[options.NBR_THREADS],
214221
}

sonar/branches.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,20 @@ def __audit_never_analyzed(self) -> list[Problem]:
361361

362362
def __audit_last_analysis(self, audit_settings: types.ConfigSettings) -> list[Problem]:
363363
if self.is_main():
364-
log.debug("%s is main (not purgeable)", str(self))
364+
log.info("%s is main (not purgeable)", str(self))
365365
return []
366366
if (age := util.age(self.last_analysis())) is None:
367367
log.debug("%s last analysis audit is disabled, skipped...", str(self))
368368
return []
369369
max_age = audit_settings.get("audit.projects.branches.maxLastAnalysisAge", 30)
370+
preserved = audit_settings.get("audit.projects.branches.keepWhenInactive", None)
370371
problems = []
371-
if self.is_kept_when_inactive():
372-
log.debug("%s is kept when inactive (not purgeable)", str(self))
372+
log.info("EVALUATING %s age %d greater than %d days and not matches '%s'", str(self), age, max_age, preserved)
373+
if preserved is not None and age > max_age and not re.match(rf"^{preserved}$", self.name):
374+
log.info("%s age %d greater than %d days and not matches '%s'", str(self), age, max_age, preserved)
375+
problems.append(Problem(get_rule(RuleId.BRANCH_LAST_ANALYSIS), self, str(self), age))
376+
elif self.is_kept_when_inactive():
377+
log.info("%s is kept when inactive (not purgeable)", str(self))
373378
elif age > max_age:
374379
problems.append(Problem(get_rule(RuleId.BRANCH_LAST_ANALYSIS), self, str(self), age))
375380
else:

0 commit comments

Comments
 (0)