Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ auto_master_merge: false # Auto-update branch from target branch
stale_branches_deletion:
enabled: false # Clean up stale branches after merge
days: 90 # Consider branches stale after N days
batch_size: 5 # Number of branches can be deleted at once
```

#### Example Configuration
Expand Down Expand Up @@ -186,6 +187,7 @@ auto_master_merge: true
stale_branches_deletion:
enabled: true
days: 30
batch_size: 2
```

## Features
Expand Down
7 changes: 5 additions & 2 deletions handlers/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,19 +227,22 @@ func (g *GitlabProvider) GetVar(projectId int, varName string) (string, error) {
return secretVar.Value, nil
}

func (g *GitlabProvider) ListBranches(projectId int) ([]handlers.Branch, error) {
func (g *GitlabProvider) ListBranches(projectId, size int) ([]handlers.Branch, error) {
branches, _, err := g.client.Branches.ListBranches(projectId, &gitlab.ListBranchesOptions{})
if err != nil {
return nil, err
}

staleBranches := []handlers.Branch{}
staleBranches := make([]handlers.Branch, 0, size)
for _, b := range branches {
if b.Default || b.Protected {
continue
}

staleBranches = append(staleBranches, handlers.Branch{Name: b.Name, LastUpdated: *b.Commit.CreatedAt})
if len(staleBranches) == size {
break
}
}
return staleBranches, nil
}
Expand Down
7 changes: 4 additions & 3 deletions handlers/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type RequestProvider interface {
LeaveComment(projectId, mergeId int, message string) error
GetMRInfo(projectId, mergeId int, path string) (*MrInfo, error)
UpdateFromMaster(projectId, mergeId int) error
ListBranches(projectId int) ([]Branch, error)
ListBranches(projectId, size int) ([]Branch, error)
DeleteBranch(projectId int, name string) error
GetVar(projectId int, varName string) (string, error)
}
Expand All @@ -71,8 +71,9 @@ type Config struct {
AutoMasterMerge bool `yaml:"auto_master_merge"`

StaleBranchesDeletion struct {
Enabled bool `yaml:"enabled"`
Days int `yaml:"days"`
Enabled bool `yaml:"enabled"`
Days int `yaml:"days"`
BatchSize int `yaml:"batch_size"`
} `yaml:"stale_branches_deletion"`
}

Expand Down
10 changes: 6 additions & 4 deletions handlers/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ func (r *Request) ParseConfig(content string) (*Config, error) {
},
AutoMasterMerge: false,
StaleBranchesDeletion: struct {
Enabled bool `yaml:"enabled"`
Days int `yaml:"days"`
Enabled bool `yaml:"enabled"`
Days int `yaml:"days"`
BatchSize int `yaml:"batch_size"`
}{
Enabled: false,
Days: 90,
Enabled: false,
Days: 90,
BatchSize: 5,
},
}

Expand Down
2 changes: 1 addition & 1 deletion handlers/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (p *testProvider) Merge(projectId, id int, message string) error {
return p.err
}

func (p *testProvider) ListBranches(projectId int) ([]Branch, error) {
func (p *testProvider) ListBranches(projectId, size int) ([]Branch, error) {
return nil, nil
}

Expand Down
10 changes: 9 additions & 1 deletion handlers/stalebranches.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ package handlers

import (
"fmt"
"sync"
"time"

"github.com/Gasoid/mergebot/logger"
)

var (
cleanStaleBranchesLock sync.Mutex
)

type Branch struct {
Name string
LastUpdated time.Time
}

func (r *Request) cleanStaleBranches(projectId int) error {
cleanStaleBranchesLock.Lock()
defer cleanStaleBranchesLock.Unlock()

logger.Debug("deletion of stale branches has been run")

candidates, err := r.provider.ListBranches(projectId)
candidates, err := r.provider.ListBranches(projectId, r.config.StaleBranchesDeletion.BatchSize)
if err != nil {
return fmt.Errorf("ListBranches returns error: %w", err)
}
Expand Down
Loading