Skip to content

Commit cd124c8

Browse files
authored
feat: push event (#65)
* feat: push event * fix: obsolete code deleted
1 parent ffbfa21 commit cd124c8

File tree

9 files changed

+37
-65
lines changed

9 files changed

+37
-65
lines changed

commands.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"path"
88
"strconv"
99
"strings"
10-
"time"
1110

1211
"github.com/gasoid/merge-bot/handlers"
1312
"github.com/gasoid/merge-bot/logger"
@@ -22,6 +21,7 @@ func init() {
2221
handle(webhook.OnNewMR, NewMR)
2322
handle(webhook.OnMerge, MergeEvent)
2423
handle(webhook.OnUpdate, UpdateEvent)
24+
handle(webhook.OnCommit, PushEvent)
2525
}
2626

2727
const success = "You can merge, LGTM :D"
@@ -123,12 +123,11 @@ func UpdateEvent(command *handlers.Request, args string) error {
123123
}
124124
}
125125

126-
parsedTime, err := time.Parse("2006-01-02 15:04:05 UTC", args)
127-
if err != nil {
128-
return fmt.Errorf("time.Parse returns err: %w", err)
129-
}
126+
return nil
127+
}
130128

131-
if err := command.ResetApprovals(parsedTime); err != nil {
129+
func PushEvent(command *handlers.Request, args string) error {
130+
if err := command.ResetApprovals(); err != nil {
132131
return fmt.Errorf("command.ResetApprovals returns err: %w", err)
133132
}
134133

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/gasoid/merge-bot
33
go 1.25.1
44

55
require (
6-
github.com/codeGROOVE-dev/retry v1.3.0
76
github.com/dustin/go-humanize v1.0.1
87
github.com/getsentry/sentry-go v0.33.0
98
github.com/getsentry/sentry-go/echo v0.33.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
22
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
33
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
44
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
5-
github.com/codeGROOVE-dev/retry v1.3.0 h1:/+ipAWRJLL6y1R1vprYo0FSjSBvH6fE5j9LKXjpD54g=
6-
github.com/codeGROOVE-dev/retry v1.3.0/go.mod h1:8OgefgV1XP7lzX2PdKlCXILsYKuz6b4ZpHa/20iLi8E=
75
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
86
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
97
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=

handlers/gitlab/gitlab.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"net/http"
88
"slices"
9-
"strings"
109
"sync"
1110
"time"
1211

@@ -556,7 +555,7 @@ func (g GitlabProvider) getToken(projectId int, name string) (string, error) {
556555
return resultToken.Token, nil
557556
}
558557

559-
func (g GitlabProvider) ResetApprovals(projectId, mergeId int, updatedAt time.Time, config handlers.ResetApprovalsOnPush) error {
558+
func (g GitlabProvider) ResetApprovals(projectId, mergeId int, config handlers.ResetApprovalsOnPush) error {
560559
logger.Debug("gitlab resetApprovals", "mergeId", mergeId)
561560

562561
if config.IssueToken {
@@ -568,30 +567,16 @@ func (g GitlabProvider) ResetApprovals(projectId, mergeId int, updatedAt time.Ti
568567
g.client = newGitlabClient(token, gitlabURL)
569568
}
570569

571-
for note := range g.listMergeRequestNotes(projectId, mergeId, getApprovalsSize, sortDesc) {
572-
if !note.System {
573-
continue
574-
}
575-
576-
if note.UpdatedAt.After(updatedAt) {
577-
continue
578-
}
579-
580-
if strings.Contains(note.Body, "commit") {
581-
_, err := g.client.MergeRequestApprovals.ResetApprovalsOfMergeRequest(projectId, mergeId)
582-
if err != nil {
583-
return err
584-
}
585-
586-
if err := g.LeaveComment(projectId, mergeId, approvalsResetMessage); err != nil {
587-
return err
588-
}
570+
_, err := g.client.MergeRequestApprovals.ResetApprovalsOfMergeRequest(projectId, mergeId)
571+
if err != nil {
572+
return err
573+
}
589574

590-
return nil
591-
}
575+
if err := g.LeaveComment(projectId, mergeId, approvalsResetMessage); err != nil {
576+
return err
592577
}
593578

594-
return handlers.CommitNotFoundError
579+
return nil
595580
}
596581

597582
func newGitlabClient(token, instanceUrl string) *gitlab.Client {

handlers/provider.go

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

33
import (
44
"sync"
5-
"time"
65
)
76

87
const (
@@ -72,7 +71,7 @@ type MergeRequest interface {
7271
FindMergeRequests(projectId int, targetBranch, label string) ([]MR, error)
7372
UpdateFromMaster(projectId, mergeId int) error
7473
AssignLabel(projectId, mergeId int, name, color string) error
75-
ResetApprovals(projectId, mergeId int, updatedAt time.Time, config ResetApprovalsOnPush) error
74+
ResetApprovals(projectId, mergeId int, config ResetApprovalsOnPush) error
7675
}
7776

7877
type Project interface {

handlers/request.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ package handlers
22

33
import (
44
"bytes"
5-
"errors"
65
"fmt"
76
"html/template"
87
"strings"
9-
"time"
108

11-
"github.com/codeGROOVE-dev/retry"
129
"github.com/gasoid/merge-bot/logger"
1310
"github.com/gasoid/merge-bot/metrics"
1411
"github.com/gasoid/merge-bot/semaphore"
@@ -251,8 +248,8 @@ func (r Request) RerunPipeline(pipelineId int) (string, error) {
251248
return r.provider.RerunPipeline(r.info.ProjectId, pipelineId, r.info.SourceBranch)
252249
}
253250

254-
func (r Request) ResetApprovals(updatedAt time.Time) error {
255-
logger.Debug("resetApprovals", "updatedAt", updatedAt)
251+
func (r Request) ResetApprovals() error {
252+
logger.Debug("resetApprovals")
256253

257254
if !r.config.Rules.ResetApprovalsOnPush.Enabled {
258255
return nil
@@ -262,20 +259,10 @@ func (r Request) ResetApprovals(updatedAt time.Time) error {
262259
return nil
263260
}
264261

265-
err := retry.Do(
266-
func() error {
267-
return r.provider.ResetApprovals(
268-
r.info.ProjectId,
269-
r.info.Id, updatedAt,
270-
r.config.Rules.ResetApprovalsOnPush)
271-
},
272-
retry.Attempts(3),
273-
)
274-
if !errors.Is(err, CommitNotFoundError) {
275-
return err
276-
}
277-
278-
return nil
262+
return r.provider.ResetApprovals(
263+
r.info.ProjectId,
264+
r.info.Id,
265+
r.config.Rules.ResetApprovalsOnPush)
279266
}
280267

281268
func (r Request) ValidateSecret(secret string) bool {

handlers/request_test.go

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

33
import (
44
"testing"
5-
"time"
65

76
"github.com/stretchr/testify/assert"
87
)
@@ -100,7 +99,7 @@ func (p *testProvider) RerunPipeline(projectId, pipelineId int, ref string) (str
10099
return "", p.err
101100
}
102101

103-
func (p *testProvider) ResetApprovals(projectId, mergeId int, updatedAt time.Time, config ResetApprovalsOnPush) error {
102+
func (p *testProvider) ResetApprovals(projectId, mergeId int, config ResetApprovalsOnPush) error {
104103
return p.err
105104
}
106105

webhook/gitlab/gitlab.go

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

33
import (
4-
"fmt"
54
"io"
65
"net/http"
76
"strings"
@@ -15,6 +14,7 @@ const (
1514
mergeAction = "merge"
1615
openAction = "open"
1716
updateAction = "update"
17+
pushAction = "push"
1818
)
1919

2020
func init() {
@@ -76,7 +76,13 @@ func (g *GitlabProvider) ParseRequest(request *http.Request) error {
7676
if mr, ok = event.(*gitlab.MergeEvent); ok {
7777
g.projectId = mr.Project.ID
7878
g.id = mr.ObjectAttributes.IID
79-
g.action = mr.ObjectAttributes.Action
79+
80+
if mr.ObjectAttributes.OldRev != "" {
81+
g.action = pushAction
82+
} else {
83+
g.action = mr.ObjectAttributes.Action
84+
}
85+
8086
g.updatedAt = mr.ObjectAttributes.UpdatedAt
8187
}
8288

@@ -86,16 +92,15 @@ func (g *GitlabProvider) ParseRequest(request *http.Request) error {
8692
func (g *GitlabProvider) GetCmd() string {
8793
logger.Debug("getCmd", "action", g.action)
8894

89-
if g.action == mergeAction {
95+
switch g.action {
96+
case mergeAction:
9097
return webhook.OnMerge
91-
}
92-
93-
if g.action == openAction {
98+
case openAction:
9499
return webhook.OnNewMR
95-
}
96-
97-
if g.action == updateAction {
98-
return fmt.Sprintf("%s %s", webhook.OnUpdate, g.updatedAt)
100+
case updateAction:
101+
return webhook.OnUpdate
102+
case pushAction:
103+
return webhook.OnCommit
99104
}
100105

101106
logger.Debug("getCmd", "note", g.note)

webhook/webhook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const (
1010
OnNewMR = "\anewMREvent"
1111
OnMerge = "\amergeEvent"
1212
OnUpdate = "\aupdateEvent"
13+
OnCommit = "\acommitEvent"
1314
spaceSymbol = " "
1415
)
1516

0 commit comments

Comments
 (0)