id: stale-git-branch-report
namespace: company.team
description: |
List branches in a Git repo with no commits in N days and post the cleanup
list to Slack, so dead branches stop piling up.
triggers:
- id: weekly_stale_scan
type: io.kestra.plugin.core.trigger.Schedule
description: Weekly pass so the stale list is a standing agenda item, not a
quarterly surprise.
cron: "0 7 * * 3"
disabled: true
inputs:
- id: repo_url
type: STRING
defaults: "https://github.com/owner/repo.git"
description: Repo to scan (public or with credentials in the URL).
- id: stale_days
type: INT
defaults: 30
description: Branches with no commit newer than this are stale.
tasks:
- id: find_stale
type: io.kestra.plugin.scripts.shell.Commands
description: Clone with --filter=blob:none for speed, then list remote branches
whose last commit is older than the threshold. Emits counts and the branch
list via the stdout outputs protocol.
containerImage: alpine/git:2.45.0
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
commands:
- |
apk add --no-cache python3 >/dev/null 2>&1
git clone --filter=blob:none --quiet "{{ inputs.repo_url }}" repo
cat > stale.py <<'PYEOF'
import os, json, subprocess, time
days = int(os.environ.get("STALE_DAYS", "30"))
cutoff = time.time() - days * 86400
branches = []
out = subprocess.run(["git", "-C", "repo", "branch", "-r", "--format=%(refname:short) %(committerdate:unix)"], capture_output=True, text=True).stdout
for line in out.splitlines():
parts = line.rsplit(" ", 1)
if len(parts) != 2:
continue
name, ts = parts[0], parts[1]
if name.endswith("/HEAD"):
continue
try:
if int(ts) < cutoff:
branches.append(name)
except ValueError:
pass
print(f"{len(branches)} stale branch(es) older than {days} days")
print("::" + json.dumps({"outputs": {"stale_count": len(branches), "branches": ", ".join(sorted(branches))}}) + "::")
PYEOF
python3 stale.py
env:
STALE_DAYS: "{{ inputs.stale_days }}"
- id: stale_found
type: io.kestra.plugin.core.flow.If
description: Only alert when something is stale; a clean repo just logs.
condition: "{{ outputs.find_stale.vars.stale_count > 0 }}"
then:
- id: alert_stale
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: List the stale branches so the cleanup decision starts there.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": ":broom: {{ outputs.find_stale.vars.stale_count }} branch(es) in {{ inputs.repo_url }} have had no commits in {{ inputs.stale_days }}+ days: {{ outputs.find_stale.vars.branches }}"
}
else:
- id: log_clean
type: io.kestra.plugin.core.log.Log
description: Record the clean sweep.
message: "No stale branches older than {{ inputs.stale_days }} days."
errors:
- id: alert_scan_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alert when the scan itself fails - a dead clone must not read as
zero stale branches.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "Stale branch scan FAILED in flow {{ flow.id }} (execution {{ execution.id }}). Check the repo URL and credentials."
}
outputs:
- id: stale_branches
type: STRING
description: 'Comma-joined stale branch names, e.g. "origin/feat-old, origin/fix-2019".'
value: "{{ outputs.find_stale.vars.branches }}"