Repository navigation
Expand file tree
/
Copy pathcommands.go
More file actions
106 lines (87 loc) · 2.75 KB
/
Copy pathcommands.go
File metadata and controls
106 lines (87 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"errors"
"fmt"
"net/url"
"path"
"strconv"
"strings"
"github.com/gasoid/merge-bot/handlers"
"github.com/gasoid/merge-bot/logger"
"github.com/gasoid/merge-bot/webhook"
)
func init() {
handle("!merge", MergeCmd)
handle("!check", CheckCmd)
handle("!update", UpdateBranchCmd)
handle("!rerun", RerunPipeline)
handle(webhook.OnNewMR, NewMR)
handle(webhook.OnMerge, MergeEvent)
}
func UpdateBranchCmd(command *handlers.Request, args string) error {
if err := command.UpdateFromMaster(); err != nil {
logger.Error("command.UpdateFromMaster failed", "error", err)
return command.LeaveComment("❌ i couldn't update branch from master")
}
return nil
}
func MergeCmd(command *handlers.Request, args string) error {
ok, text, err := command.Merge()
if err != nil {
return fmt.Errorf("command.Merge returns err: %w", err)
}
if !ok && len(text) > 0 {
return command.LeaveComment(text)
}
return err
}
func CheckCmd(command *handlers.Request, args string) error {
ok, text, err := command.IsValid()
if err != nil {
return fmt.Errorf("command.IsValid returns err: %w", err)
}
if !ok && len(text) > 0 {
return command.LeaveComment(text)
} else {
return command.LeaveComment("You can merge, LGTM :D")
}
}
func NewMR(command *handlers.Request, args string) error {
if err := command.Greetings(); err != nil {
return fmt.Errorf("command.Greetings returns err: %w", err)
}
return nil
}
func MergeEvent(command *handlers.Request, args string) error {
if err := command.CreateLabels(); err != nil {
return fmt.Errorf("command.CreateLabels returns err: %w", err)
}
if err := command.UpdateBranches(); err != nil {
return fmt.Errorf("command.UpdateBranchesWithLabel returns err: %w", err)
}
if err := command.DeleteStaleBranches(); err != nil {
return fmt.Errorf("command.DeleteStaleBranches returns err: %w", err)
}
return nil
}
func RerunPipeline(command *handlers.Request, args string) error {
arg := strings.TrimPrefix(args, "#")
pipelineId, err := strconv.Atoi(arg)
if err != nil {
logger.Debug("rerun", "args", args, "arg", arg)
return command.LeaveComment("> [!important]\n> Pipeline ID is invalid or wrong")
}
logger.Debug("rerun", "args", args, "arg", arg)
pipelineURL, err := command.RerunPipeline(pipelineId)
if err != nil {
if errors.Is(err, handlers.NotFoundError) {
return command.LeaveComment("> [!important]\n> Provided pipeline was not found")
}
return command.LeaveComment("> [!important]\n> Validate your pipeline syntax")
}
parsedUrl, err := url.Parse(pipelineURL)
if err != nil {
return command.LeaveComment("> [!important]\n> pipeline created, but provided url is wrong")
}
return command.LeaveComment(fmt.Sprintf("🤖 pipeline created: [%s](%s)", path.Base(parsedUrl.Path), pipelineURL))
}