Skip to content

Commit e15eb37

Browse files
authored
fix: optimized loops and memory leak (#49)
* fix: optimized loops and memory leak fixed * build version * logger after version * feat: readme * fix: lower case of module
1 parent 6fa1a82 commit e15eb37

File tree

17 files changed

+144
-49
lines changed

17 files changed

+144
-49
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
FROM golang:1.24.3-bookworm AS builder
2+
ARG MERGE_BOT_VERSION=dev
23
WORKDIR /code
34
ADD go.mod /code/
45
ADD go.sum /code/
56
RUN go mod download
67
ADD ./ /code/
7-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /tmp/bot .
8+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-X 'main.Version=$MERGE_BOT_VERSION' -X 'main.BuildTime=$(date)'" -a -o /tmp/bot .
89

910
FROM alpine:3.21
1011
EXPOSE 8080 443

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ go run ./
119119
Enable TLS with Let's Encrypt (also via TLS_ENABLED)
120120
-sentry-enabled
121121
Enable Sentry error reporting (default: true, also via SENTRY_ENABLED)
122+
-version
123+
Shows version and build time
122124
```
123125

124126
## Configuration

bot.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"path"
66
"sync"
77

8-
"github.com/Gasoid/merge-bot/config"
9-
"github.com/Gasoid/merge-bot/handlers"
10-
"github.com/Gasoid/merge-bot/logger"
11-
"github.com/Gasoid/merge-bot/webhook"
8+
"github.com/gasoid/merge-bot/config"
9+
"github.com/gasoid/merge-bot/handlers"
10+
"github.com/gasoid/merge-bot/logger"
11+
"github.com/gasoid/merge-bot/webhook"
1212

1313
"net/http"
1414

bot_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strings"
88
"testing"
99

10-
"github.com/Gasoid/merge-bot/webhook"
10+
"github.com/gasoid/merge-bot/webhook"
1111

1212
"github.com/labstack/echo/v4"
1313
"github.com/stretchr/testify/assert"

commands.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"strconv"
99
"strings"
1010

11-
"github.com/Gasoid/merge-bot/handlers"
12-
"github.com/Gasoid/merge-bot/logger"
13-
"github.com/Gasoid/merge-bot/webhook"
11+
"github.com/gasoid/merge-bot/handlers"
12+
"github.com/gasoid/merge-bot/logger"
13+
"github.com/gasoid/merge-bot/webhook"
1414
)
1515

1616
func init() {

commands_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package main
33
import (
44
"testing"
55

6-
"github.com/Gasoid/merge-bot/handlers"
7-
"github.com/Gasoid/merge-bot/webhook"
6+
"github.com/gasoid/merge-bot/handlers"
7+
"github.com/gasoid/merge-bot/webhook"
88
"github.com/stretchr/testify/assert"
99
)
1010

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/Gasoid/merge-bot
1+
module github.com/gasoid/merge-bot
22

33
go 1.24.1
44

handlers/gitlab/gitlab.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"net/http"
77
"slices"
88

9-
"github.com/Gasoid/merge-bot/config"
10-
"github.com/Gasoid/merge-bot/handlers"
11-
"github.com/Gasoid/merge-bot/logger"
9+
"github.com/gasoid/merge-bot/config"
10+
"github.com/gasoid/merge-bot/handlers"
11+
"github.com/gasoid/merge-bot/logger"
1212
gitlab "gitlab.com/gitlab-org/api/client-go"
1313

1414
"github.com/dustin/go-humanize"
@@ -31,6 +31,7 @@ var (
3131
const (
3232
tokenUsername = "oauth2"
3333
gitlabTrue = true
34+
findMRSize = 10
3435
)
3536

3637
type GitlabProvider struct {
@@ -104,6 +105,7 @@ func (g *GitlabProvider) Merge(projectId, mergeId int, message string) error {
104105
func (g *GitlabProvider) GetApprovals(projectId, mergeId int) (map[string]struct{}, error) {
105106
page := 1
106107
approvals := map[string]struct{}{}
108+
107109
for {
108110
notes, resp, err := g.client.Notes.ListMergeRequestNotes(
109111
projectId,
@@ -237,13 +239,9 @@ func (g GitlabProvider) GetVar(projectId int, varName string) (string, error) {
237239
}
238240

239241
func (g GitlabProvider) ListBranches(projectId, size int) ([]handlers.StaleBranch, error) {
240-
branches, _, err := g.client.Branches.ListBranches(projectId, &gitlab.ListBranchesOptions{})
241-
if err != nil {
242-
return nil, err
243-
}
244-
245242
staleBranches := make([]handlers.StaleBranch, 0, size)
246-
for _, b := range branches {
243+
244+
for b := range g.listBranches(projectId, size) {
247245
if b.Default || b.Protected {
248246
continue
249247
}
@@ -277,14 +275,14 @@ func (g *GitlabProvider) DeleteBranch(projectId int, name string) error {
277275
}
278276

279277
func (g GitlabProvider) ListMergeRequests(projectId, size int) ([]handlers.MR, error) {
280-
listMr, _, err := g.client.MergeRequests.ListProjectMergeRequests(projectId,
281-
&gitlab.ListProjectMergeRequestsOptions{State: gitlab.Ptr("opened")})
282-
if err != nil {
283-
return nil, err
284-
}
285-
286278
staleMRS := make([]handlers.MR, 0, size)
287-
for _, mr := range listMr {
279+
280+
listMr := g.listMergeRequests(projectId, size,
281+
&gitlab.ListProjectMergeRequestsOptions{
282+
State: gitlab.Ptr("opened"),
283+
})
284+
285+
for mr := range listMr {
288286
staleMRS = append(staleMRS, handlers.MR{
289287
Id: mr.IID,
290288
Labels: mr.Labels,
@@ -301,18 +299,16 @@ func (g GitlabProvider) ListMergeRequests(projectId, size int) ([]handlers.MR, e
301299
}
302300

303301
func (g GitlabProvider) FindMergeRequests(projectId int, targetBranch, label string) ([]handlers.MR, error) {
304-
listMr, _, err := g.client.MergeRequests.ListProjectMergeRequests(projectId,
302+
mrs := make([]handlers.MR, 0)
303+
304+
listMr := g.listMergeRequests(projectId, findMRSize,
305305
&gitlab.ListProjectMergeRequestsOptions{
306306
State: gitlab.Ptr("opened"),
307307
Labels: &gitlab.LabelOptions{label},
308308
TargetBranch: &targetBranch,
309309
})
310-
if err != nil {
311-
return nil, err
312-
}
313310

314-
mrs := make([]handlers.MR, 0)
315-
for _, mr := range listMr {
311+
for mr := range listMr {
316312
mrs = append(mrs, handlers.MR{
317313
Id: mr.IID,
318314
Labels: mr.Labels,

handlers/gitlab/pagination.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package gitlab
2+
3+
import (
4+
"iter"
5+
6+
"github.com/gasoid/merge-bot/logger"
7+
gitlab "gitlab.com/gitlab-org/api/client-go"
8+
)
9+
10+
func (g GitlabProvider) listBranches(projectId, size int) iter.Seq[*gitlab.Branch] {
11+
page := 1
12+
13+
return func(yield func(b *gitlab.Branch) bool) {
14+
for {
15+
branches, resp, err := g.client.Branches.ListBranches(projectId, &gitlab.ListBranchesOptions{
16+
ListOptions: gitlab.ListOptions{
17+
Page: page,
18+
PerPage: size * 2,
19+
},
20+
})
21+
if err != nil {
22+
logger.Error("listBranches", "err", err)
23+
return
24+
}
25+
26+
for _, branch := range branches {
27+
if !yield(branch) {
28+
return
29+
}
30+
}
31+
32+
if resp.NextPage == 0 {
33+
return
34+
}
35+
36+
page = resp.NextPage
37+
}
38+
}
39+
}
40+
41+
func (g GitlabProvider) listMergeRequests(projectId, size int, options *gitlab.ListProjectMergeRequestsOptions) iter.Seq[*gitlab.BasicMergeRequest] {
42+
page := 1
43+
44+
return func(yield func(*gitlab.BasicMergeRequest) bool) {
45+
for {
46+
options.ListOptions = gitlab.ListOptions{
47+
Page: page,
48+
PerPage: size * 2,
49+
}
50+
51+
mrs, resp, err := g.client.MergeRequests.ListProjectMergeRequests(projectId, options)
52+
if err != nil {
53+
logger.Error("listMergeRequests", "err", err)
54+
return
55+
}
56+
57+
for _, mergeRequest := range mrs {
58+
if !yield(mergeRequest) {
59+
return
60+
}
61+
}
62+
63+
if resp.NextPage == 0 {
64+
return
65+
}
66+
67+
page = resp.NextPage
68+
}
69+
}
70+
}

handlers/merge.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"net/url"
66
"os"
77

8-
"github.com/Gasoid/merge-bot/logger"
8+
"github.com/gasoid/merge-bot/logger"
99

1010
"github.com/ldez/go-git-cmd-wrapper/v2/checkout"
1111
"github.com/ldez/go-git-cmd-wrapper/v2/clone"
@@ -65,8 +65,8 @@ func MergeMaster(username, password, repoUrl, branchName, master string) error {
6565
if output, err := git.Merge(workingDir, merge.Commits(master), merge.M(fmt.Sprintf("✨ merged %s", master))); err != nil {
6666
logger.Debug("git merge error", "output", output)
6767
if output, err := git.Merge(workingDir, merge.NoFf, merge.Commits(master), merge.M(fmt.Sprintf("✨ merged %s", master))); err != nil {
68-
logger.Debug("git merge --noff error", "output", output)
69-
return fmt.Errorf("git merge --noff error: %w, output: %s", err, output)
68+
logger.Debug("git merge --no-ff error", "output", output)
69+
return fmt.Errorf("git merge --no-ff error: %w, output: %s", err, output)
7070
}
7171
}
7272

0 commit comments

Comments
 (0)