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
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
FROM golang:1.24.3-bookworm AS builder
ARG MERGE_BOT_VERSION=dev
WORKDIR /code
ADD go.mod /code/
ADD go.sum /code/
RUN go mod download
ADD ./ /code/
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /tmp/bot .
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 .

FROM alpine:3.21
EXPOSE 8080 443
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ go run ./
Enable TLS with Let's Encrypt (also via TLS_ENABLED)
-sentry-enabled
Enable Sentry error reporting (default: true, also via SENTRY_ENABLED)
-version
Shows version and build time
```

## Configuration
Expand Down
8 changes: 4 additions & 4 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"path"
"sync"

"github.com/Gasoid/merge-bot/config"
"github.com/Gasoid/merge-bot/handlers"
"github.com/Gasoid/merge-bot/logger"
"github.com/Gasoid/merge-bot/webhook"
"github.com/gasoid/merge-bot/config"
"github.com/gasoid/merge-bot/handlers"
"github.com/gasoid/merge-bot/logger"
"github.com/gasoid/merge-bot/webhook"

"net/http"

Expand Down
2 changes: 1 addition & 1 deletion bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"testing"

"github.com/Gasoid/merge-bot/webhook"
"github.com/gasoid/merge-bot/webhook"

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
Expand Down
6 changes: 3 additions & 3 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"strconv"
"strings"

"github.com/Gasoid/merge-bot/handlers"
"github.com/Gasoid/merge-bot/logger"
"github.com/Gasoid/merge-bot/webhook"
"github.com/gasoid/merge-bot/handlers"
"github.com/gasoid/merge-bot/logger"
"github.com/gasoid/merge-bot/webhook"
)

func init() {
Expand Down
4 changes: 2 additions & 2 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"testing"

"github.com/Gasoid/merge-bot/handlers"
"github.com/Gasoid/merge-bot/webhook"
"github.com/gasoid/merge-bot/handlers"
"github.com/gasoid/merge-bot/webhook"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/Gasoid/merge-bot
module github.com/gasoid/merge-bot

go 1.24.1

Expand Down
40 changes: 18 additions & 22 deletions handlers/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"net/http"
"slices"

"github.com/Gasoid/merge-bot/config"
"github.com/Gasoid/merge-bot/handlers"
"github.com/Gasoid/merge-bot/logger"
"github.com/gasoid/merge-bot/config"
"github.com/gasoid/merge-bot/handlers"
"github.com/gasoid/merge-bot/logger"
gitlab "gitlab.com/gitlab-org/api/client-go"

"github.com/dustin/go-humanize"
Expand All @@ -31,6 +31,7 @@ var (
const (
tokenUsername = "oauth2"
gitlabTrue = true
findMRSize = 10
)

type GitlabProvider struct {
Expand Down Expand Up @@ -104,6 +105,7 @@ func (g *GitlabProvider) Merge(projectId, mergeId int, message string) error {
func (g *GitlabProvider) GetApprovals(projectId, mergeId int) (map[string]struct{}, error) {
page := 1
approvals := map[string]struct{}{}

for {
notes, resp, err := g.client.Notes.ListMergeRequestNotes(
projectId,
Expand Down Expand Up @@ -237,13 +239,9 @@ func (g GitlabProvider) GetVar(projectId int, varName string) (string, error) {
}

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

staleBranches := make([]handlers.StaleBranch, 0, size)
for _, b := range branches {

for b := range g.listBranches(projectId, size) {
if b.Default || b.Protected {
continue
}
Expand Down Expand Up @@ -277,14 +275,14 @@ func (g *GitlabProvider) DeleteBranch(projectId int, name string) error {
}

func (g GitlabProvider) ListMergeRequests(projectId, size int) ([]handlers.MR, error) {
listMr, _, err := g.client.MergeRequests.ListProjectMergeRequests(projectId,
&gitlab.ListProjectMergeRequestsOptions{State: gitlab.Ptr("opened")})
if err != nil {
return nil, err
}

staleMRS := make([]handlers.MR, 0, size)
for _, mr := range listMr {

listMr := g.listMergeRequests(projectId, size,
&gitlab.ListProjectMergeRequestsOptions{
State: gitlab.Ptr("opened"),
})

for mr := range listMr {
staleMRS = append(staleMRS, handlers.MR{
Id: mr.IID,
Labels: mr.Labels,
Expand All @@ -301,18 +299,16 @@ func (g GitlabProvider) ListMergeRequests(projectId, size int) ([]handlers.MR, e
}

func (g GitlabProvider) FindMergeRequests(projectId int, targetBranch, label string) ([]handlers.MR, error) {
listMr, _, err := g.client.MergeRequests.ListProjectMergeRequests(projectId,
mrs := make([]handlers.MR, 0)

listMr := g.listMergeRequests(projectId, findMRSize,
&gitlab.ListProjectMergeRequestsOptions{
State: gitlab.Ptr("opened"),
Labels: &gitlab.LabelOptions{label},
TargetBranch: &targetBranch,
})
if err != nil {
return nil, err
}

mrs := make([]handlers.MR, 0)
for _, mr := range listMr {
for mr := range listMr {
mrs = append(mrs, handlers.MR{
Id: mr.IID,
Labels: mr.Labels,
Expand Down
70 changes: 70 additions & 0 deletions handlers/gitlab/pagination.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package gitlab

import (
"iter"

"github.com/gasoid/merge-bot/logger"
gitlab "gitlab.com/gitlab-org/api/client-go"
)

func (g GitlabProvider) listBranches(projectId, size int) iter.Seq[*gitlab.Branch] {
page := 1

return func(yield func(b *gitlab.Branch) bool) {
for {
branches, resp, err := g.client.Branches.ListBranches(projectId, &gitlab.ListBranchesOptions{
ListOptions: gitlab.ListOptions{
Page: page,
PerPage: size * 2,
},
})
if err != nil {
logger.Error("listBranches", "err", err)
return
}

for _, branch := range branches {
if !yield(branch) {
return
}
}

if resp.NextPage == 0 {
return
}

page = resp.NextPage
}
}
}

func (g GitlabProvider) listMergeRequests(projectId, size int, options *gitlab.ListProjectMergeRequestsOptions) iter.Seq[*gitlab.BasicMergeRequest] {
page := 1

return func(yield func(*gitlab.BasicMergeRequest) bool) {
for {
options.ListOptions = gitlab.ListOptions{
Page: page,
PerPage: size * 2,
}

mrs, resp, err := g.client.MergeRequests.ListProjectMergeRequests(projectId, options)
if err != nil {
logger.Error("listMergeRequests", "err", err)
return
}

for _, mergeRequest := range mrs {
if !yield(mergeRequest) {
return
}
}

if resp.NextPage == 0 {
return
}

page = resp.NextPage
}
}
}
6 changes: 3 additions & 3 deletions handlers/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/url"
"os"

"github.com/Gasoid/merge-bot/logger"
"github.com/gasoid/merge-bot/logger"

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

Expand Down
4 changes: 2 additions & 2 deletions handlers/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"html/template"
"strings"

"github.com/Gasoid/merge-bot/logger"
"github.com/Gasoid/merge-bot/semaphore"
"github.com/gasoid/merge-bot/logger"
"github.com/gasoid/merge-bot/semaphore"

"gopkg.in/yaml.v3"
)
Expand Down
2 changes: 1 addition & 1 deletion handlers/stalebranches.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"time"

"github.com/Gasoid/merge-bot/logger"
"github.com/gasoid/merge-bot/logger"
)

type StaleBranch struct {
Expand Down
4 changes: 2 additions & 2 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"testing"
"time"

"github.com/Gasoid/merge-bot/handlers"
"github.com/Gasoid/merge-bot/webhook"
"github.com/gasoid/merge-bot/handlers"
"github.com/gasoid/merge-bot/webhook"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log/slog"

"github.com/Gasoid/merge-bot/config"
"github.com/gasoid/merge-bot/config"

"github.com/getsentry/sentry-go"
sentryslog "github.com/getsentry/sentry-go/slog"
Expand Down
13 changes: 9 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package main

import (
"github.com/Gasoid/merge-bot/config"
_ "github.com/Gasoid/merge-bot/handlers/gitlab"
"github.com/Gasoid/merge-bot/logger"
_ "github.com/Gasoid/merge-bot/webhook/gitlab"
"github.com/gasoid/merge-bot/config"
_ "github.com/gasoid/merge-bot/handlers/gitlab"
"github.com/gasoid/merge-bot/logger"
_ "github.com/gasoid/merge-bot/webhook/gitlab"
)

func main() {
config.Parse()

if showVersion {
PrintVersion()
return
}

logger.New()

start()
Expand Down
21 changes: 21 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"

"github.com/gasoid/merge-bot/config"
)

var (
Version = "dev"
BuildTime = "now"
showVersion bool
)

func init() {
config.BoolVar(&showVersion, "version", false, "Shows version and build time")
}

func PrintVersion() {
fmt.Printf("Version: %s\nBuildTime: %s\n", Version, BuildTime)
}
4 changes: 2 additions & 2 deletions webhook/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"net/http"
"strings"

"github.com/Gasoid/merge-bot/logger"
"github.com/Gasoid/merge-bot/webhook"
"github.com/gasoid/merge-bot/logger"
"github.com/gasoid/merge-bot/webhook"
gitlab "gitlab.com/gitlab-org/api/client-go"
)

Expand Down
Loading