Skip to content

Commit 6941400

Browse files
authored
Readme instructions and event improvements 🔥 (#13)
* helm-charts repo * merge event * readme updated * debug
1 parent 0487731 commit 6941400

File tree

11 files changed

+114
-59
lines changed

11 files changed

+114
-59
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.env
22
.vscode
3-
*.yaml
43
_config
54
.DS_Store
65
_metrics

README.md

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![screen](screen.webp)
44

5-
### Features
5+
## Features
66
- rule for title
77
- rule for approvals
88
- rule for approvers
@@ -11,29 +11,85 @@
1111
- delete stale branches
1212

1313

14+
## Table of Contents
15+
16+
- [Installation](#installation)
17+
- [Gitlab Cloud](#gitlab-cloud)
18+
- [Docker-compose](#Docker-compose)
19+
- [Helm](#helm)
20+
- [CLI](#cli)
21+
- [Config file](#config-file)
22+
- [Example](#example)
23+
- [Demo project on gitlab](https://gitlab.com/Gasoid/sugar-test)
24+
- [Required bot permissions](#required-bot-permissions)
25+
26+
### Demo repo
27+
28+
https://gitlab.com/Gasoid/sugar-test
29+
1430
### Commands
1531
- !merge
1632
- !check
1733
- !update
1834

19-
## Self-hosted or Cloud
35+
## Installation
2036
The Bot could be run within your infrastructure as container.
2137
In case you want to test the bot you can use gitlab cloud bot.
2238

23-
### Self-hosted
39+
40+
### Gitlab Cloud
41+
1. Invite bot ([@mergeapprovebot](https://gitlab.com/mergeapprovebot)) in your repository as **maintainer** (you can revoke permissions from usual developers in order to prevent merging)
42+
2. Add webhook `https://mergebot.tools/mergebot/webhook/gitlab/your_username_or_company_name/repo-name/` (Comments and merge request events)
43+
3. PROFIT: now you can create MR, leave commands: !check and then !merge (comment in MR)
44+
45+
You can test bot on gitlab public repo: https://gitlab.com/Gasoid/sugar-test
46+
47+
### Docker-compose
2448

2549
1. bot.env:
2650
```
2751
GITLAB_TOKEN="your_token"
2852
#TLS_ENABLED="false"
2953
#TLS_DOMAIN="domain.your-example.com"
54+
#GITLAB_URL=""
3055
```
3156

3257
2. run docker-compose
3358
```
3459
docker-compose up -d
3560
```
3661

62+
63+
### Helm
64+
65+
[Helm](https://helm.sh) must be installed to use the charts. Please refer to
66+
Helm's [documentation](https://helm.sh/docs) to get started.
67+
68+
Once Helm has been set up correctly, add the repo as follows:
69+
70+
helm repo add merge-bot https://gasoid.github.io/helm-charts
71+
72+
If you had already added this repo earlier, run `helm repo update` to retrieve
73+
the latest versions of the packages. You can then run `helm search repo merge-bot` to see the charts.
74+
75+
To install the merge-bot chart:
76+
77+
helm install my-merge-bot merge-bot/merge-bot
78+
79+
To uninstall the chart:
80+
81+
helm uninstall my-merge-bot
82+
83+
### CLI
84+
85+
Create personal/repo/org token in gitlab, copy it and set as env variable
86+
```bash
87+
export GITLAB_TOKEN="your_token"
88+
export GITLAB_URL="" # if it is not public gitlab cloud
89+
export TLS_ENABLED="true"
90+
export TLS_DOMAIN="bot.domain.com"
91+
```
92+
3793
you can configure bot using cli args as well:
3894
```bash
3995
Usage of merge-bot:
@@ -49,32 +105,11 @@ Usage of merge-bot:
49105
whether tls enabled or not, bot will use Letsencrypt, default is false (also via TLS_ENABLED)
50106
```
51107
52-
### Setup for Gitlab Cloud
53-
1. Invite bot ([@mergeapprovebot](https://gitlab.com/mergeapprovebot)) in your repository as **maintainer** (you can revoke permissions from usual developers in order to prevent merging)
54-
2. Add webhook `https://mergebot.tools/mergebot/webhook/gitlab/your_username_or_company_name/repo-name/` (Comments and merge request events)
55-
3. PROFIT: now you can create MR, leave commands: !check and then !merge (comment in MR)
56-
57-
### Quickstart on your env
58-
59-
Create personal/repo/org token in gitlab, copy it and set as env variable
60-
```bash
61-
export GITLAB_TOKEN="your_token"
62-
export GITLAB_URL="" # if it is not public gitlab cloud
63-
export TLS_ENABLED="true"
64-
export TLS_DOMAIN="bot.domain.com"
65-
```
66-
67108
Run bot
68109
```
69110
go run ./
70111
```
71112
72-
### Build
73-
```
74-
go build ./
75-
```
76-
77-
78113
79114
## Config file
80115
@@ -102,7 +137,7 @@ stale_branches_deletion:
102137
days: 90 # branch is staled after int days, default is 90
103138
```
104139
105-
Example:
140+
#### Example:
106141
107142
```yaml
108143
approvers:
@@ -123,3 +158,7 @@ stale_branches_deletion:
123158
```
124159
125160
place it in root of your repo and name it `.mrbot.yaml`
161+
162+
### Required bot permissions
163+
- Bot must have __Maintainer__ role in order to comment, merge and delete branches
164+
- Access Token must have following permissions: api, read_repository, write_repository

bot.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,17 @@ func Handler(c echo.Context) error {
8181
go func() {
8282
command, err := handlers.New(providerName)
8383
if err != nil {
84-
slog.Error("can't initialize provider", "provider", providerName, "command", command, "err", err)
84+
slog.Error("can't initialize provider", "provider", providerName, "event", hook.Event, "err", err)
8585
return
8686
}
8787

88+
// if err := command.LoadInfoAndConfig(hook.GetProjectID(), hook.GetID()); err != nil {
89+
// slog.Error("can't load repo config", "provider", providerName, "command", command, "err", err)
90+
// return
91+
// }
92+
8893
if err := f(command, hook); err != nil {
89-
slog.Error("handlerFunc returns err", "provider", providerName, "command", command, "err", err)
94+
slog.Error("handlerFunc returns err", "provider", providerName, "event", hook.Event, "err", err)
9095
}
9196
}()
9297
}

bot_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,13 @@ import (
1111
)
1212

1313
type testWebhookProvider struct {
14-
isNew bool
1514
isValid bool
1615
id int
1716
projectID int
1817
cmd string
1918
err error
2019
}
2120

22-
func (p *testWebhookProvider) IsNew() bool {
23-
return p.isNew
24-
}
25-
2621
func (p *testWebhookProvider) IsValid() bool {
2722
return p.isValid
2823
}

commands.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func init() {
1212
handle("!check", CheckCmd)
1313
handle("!update", UpdateBranchCmd)
1414
handle(webhook.OnNewMR, NewMR)
15+
handle(webhook.OnMerge, MergeEvent)
1516
}
1617

1718
func UpdateBranchCmd(command *handlers.Request, hook *webhook.Webhook) error {
@@ -55,3 +56,11 @@ func NewMR(command *handlers.Request, hook *webhook.Webhook) error {
5556

5657
return nil
5758
}
59+
60+
func MergeEvent(command *handlers.Request, hook *webhook.Webhook) error {
61+
if err := command.DeleteStaleBranches(hook.GetProjectID(), hook.GetID()); err != nil {
62+
return fmt.Errorf("command.MergeEvent returns err: %w", err)
63+
}
64+
65+
return nil
66+
}

handlers/checkers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ var (
5151
checkFunc: checkDescription,
5252
},
5353
{
54-
text: "Number of approvals",
54+
text: "Number of approvals (mr author is ignored)",
5555
checkFunc: checkApprovals,
5656
},
5757
{
58-
text: "Required approvers",
58+
text: "Required approvers (mr author is ignored)",
5959
checkFunc: checkApprovers,
6060
},
6161
{

handlers/gitlab/gitlab.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (g *GitlabProvider) IsValid(projectId, mergeId int) (bool, error) {
144144
}
145145

146146
if g.mr.State != "opened" {
147-
return false, handlers.StatusError
147+
return false, nil
148148
}
149149

150150
return !g.mr.HasConflicts, nil

handlers/request.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ func (r *Request) Greetings(projectId, id int) error {
113113
return r.LeaveComment(projectId, id, buf.String())
114114
}
115115

116+
func (r *Request) DeleteStaleBranches(projectId, id int) error {
117+
if err := r.LoadInfoAndConfig(projectId, id); err != nil {
118+
return err
119+
}
120+
121+
if r.config.StaleBranchesDeletion.Enabled {
122+
return r.cleanStaleBranches(projectId)
123+
}
124+
125+
return nil
126+
}
127+
116128
func (r *Request) Merge(projectId, id int) (bool, string, error) {
117129
if err := r.LoadInfoAndConfig(projectId, id); err != nil {
118130
return false, "", err
@@ -125,10 +137,6 @@ func (r *Request) Merge(projectId, id int) (bool, string, error) {
125137
}
126138
}
127139

128-
if r.config.StaleBranchesDeletion.Enabled {
129-
defer r.cleanStaleBranches(projectId)
130-
}
131-
132140
if ok, text, err := r.IsValid(projectId, id); ok {
133141
if err := r.provider.Merge(projectId, id, fmt.Sprintf("%s\nMerged by MergeApproveBot", r.info.Title)); err != nil {
134142
return false, "", err

handlers/stalebranches.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package handlers
22

33
import (
4+
"fmt"
45
"log/slog"
56
"time"
67
)
@@ -10,11 +11,12 @@ type Branch struct {
1011
LastUpdated time.Time
1112
}
1213

13-
func (r *Request) cleanStaleBranches(projectId int) {
14+
func (r *Request) cleanStaleBranches(projectId int) error {
15+
slog.Debug("deletion of stale branches has been run")
16+
1417
candidates, err := r.provider.ListBranches(projectId)
1518
if err != nil {
16-
slog.Error("ListBranches returns error", "err", err)
17-
return
19+
return fmt.Errorf("ListBranches returns error: %w", err)
1820
}
1921

2022
days := r.config.StaleBranchesDeletion.Days
@@ -26,8 +28,9 @@ func (r *Request) cleanStaleBranches(projectId int) {
2628
// delete branch
2729
slog.Debug("branch info", "name", b.Name, "createdAt", b.LastUpdated.String())
2830
if err := r.provider.DeleteBranch(projectId, b.Name); err != nil {
29-
slog.Error("DeleteBranch returns error", "branch", b.Name, "err", err)
31+
return fmt.Errorf("DeleteBranch returns error: %w", err)
3032
}
3133
}
3234
}
35+
return nil
3336
}

webhook/gitlab/gitlab.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,23 @@ func (g *GitlabProvider) ParseRequest(request *http.Request) error {
6767
}
6868

6969
func (g *GitlabProvider) GetCmd() string {
70+
slog.Debug("getCmd", "action", g.action)
71+
72+
if g.action == "merge" {
73+
return webhook.OnMerge
74+
}
75+
76+
if g.action == "open" {
77+
return webhook.OnNewMR
78+
}
79+
7080
slog.Debug("getCmd", "note", g.note)
7181
if strings.HasPrefix(g.note, "!") {
7282
return g.note
7383
}
7484
return ""
7585
}
7686

77-
func (g *GitlabProvider) IsNew() bool {
78-
return g.action == "open"
79-
}
80-
8187
func (g *GitlabProvider) GetID() int {
8288
return g.id
8389
}

0 commit comments

Comments
 (0)