Skip to content

Commit f706c32

Browse files
committed
Commit on '2024-05-27 18:04:08'
1 parent 210bbbb commit f706c32

File tree

22 files changed

+362
-416
lines changed

22 files changed

+362
-416
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ test:
44
cd src/; go test
55

66
run:
7-
go run ./cmd/main.go
7+
CONFIG_FILE=config.yaml go run ./cmd/main.go
88

99
build-image:
1010
export DOCKER_BUILDKIT=1

cmd/main.go

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,15 @@ import (
88
"gitbot/internal/event"
99
"gitbot/internal/event/provider"
1010
"gitbot/internal/event/queue"
11-
"gitbot/internal/event/worker"
1211

1312
"log/slog"
1413
"net/http"
1514
"os"
1615
"os/signal"
1716
"syscall"
1817
"time"
19-
20-
"k8s.io/client-go/kubernetes"
21-
"k8s.io/client-go/rest"
22-
"k8s.io/client-go/tools/clientcmd"
23-
)
24-
25-
const (
26-
kubeconfig = "/home/mlr/Documents/Code/gitbot/kubeconfig"
2718
)
2819

29-
func NewKubernetes() *kubernetes.Clientset {
30-
config, err := func() (*rest.Config, error) {
31-
_, exists := os.LookupEnv("KUBERNETES_SERVICE_HOST")
32-
if exists {
33-
return rest.InClusterConfig()
34-
} else {
35-
return clientcmd.BuildConfigFromFlags("", kubeconfig)
36-
}
37-
}()
38-
if err != nil {
39-
panic(err)
40-
}
41-
42-
clientset, err := kubernetes.NewForConfig(config)
43-
if err != nil {
44-
panic(err)
45-
}
46-
47-
return clientset
48-
}
49-
5020
type Server struct {
5121
}
5222

@@ -55,29 +25,27 @@ func main() {
5525
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
5626

5727
// Load config
58-
//config := config.Load(config.Get("CONFIG_FILE"))
59-
60-
// Configure kubernetes
61-
//clientset := NewKubernetes()
28+
c := config.Load()
6229

63-
// Configure webhook queue
30+
/* Events */
31+
// Service
6432
queue := queue.NewMemoryQueue[event.QueueItem]()
65-
worker := worker.NewWorker(queue)
66-
33+
eService := event.NewService(queue, c.SecurityRules)
34+
// Worker
35+
worker := event.NewWorker(eService)
6736
// Handlers
68-
bitbucketProvider := provider.NewBitbucketProvider(config.Get("BITBUCKET_BEARER_TOKEN"))
69-
bitbucketHandler := event.NewHandler(queue, bitbucketProvider)
37+
bitbucket := provider.NewBitbucketProvider(c.BitbucketBearerToken)
38+
bitbucketHandler := event.NewHandler(eService, bitbucket)
7039

71-
// Routes
40+
/* Routes */
7241
router := http.NewServeMux()
73-
7442
router.HandleFunc("GET /status", status)
7543
router.HandleFunc("POST /api/v1/webhook/bitbucket", bitbucketHandler.Handle())
7644

7745
// Starting Http Server
78-
srv := &http.Server{Addr: ":" + config.Get("HTTP_PORT"), Handler: router}
46+
srv := &http.Server{Addr: ":" + c.HttpPort, Handler: router}
7947
go func() {
80-
slog.Info("Starting server in port :" + config.Get("HTTP_PORT"))
48+
slog.Info("Starting server in port :" + c.HttpPort)
8149
err := srv.ListenAndServe()
8250

8351
if err != nil && !errors.Is(err, http.ErrServerClosed) {

env.local.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
HTTP_PORT=8080
22
BITBUCKET_BEARER_TOKEN=token
3+
CONFIG_FILE=config.yaml
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package types
1+
package app
22

33
type Application struct {
44
Name string

internal/config/config.go

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
package config
22

3-
import "fmt"
3+
import (
4+
"fmt"
45

5-
func (c iYamlConfigFile) validate() error {
6+
"gitbot/internal/event"
7+
)
8+
9+
type ConfigFile struct {
10+
Security struct {
11+
Groups []struct {
12+
Name string `yaml:"name"`
13+
Users []string `yaml:"users"`
14+
} `yaml:"groups"`
15+
Rules []struct {
16+
Respository string `yaml:"repository"`
17+
FilePatternList []string `yaml:"filepattern"`
18+
ActionList []string `yaml:"action"`
19+
GroupList []string `yaml:"group"`
20+
UserList []string `yaml:"user"`
21+
} `yaml:"rules"`
22+
} `yaml:"security"`
23+
}
24+
25+
func (c ConfigFile) validate() error {
626

727
// Group has any user
828
if err := c.hasEmptyGroup(); err != nil {
@@ -26,7 +46,7 @@ func (c iYamlConfigFile) validate() error {
2646
return nil
2747
}
2848

29-
func (c iYamlConfigFile) hasEmptyGroup() error {
49+
func (c ConfigFile) hasEmptyGroup() error {
3050
for _, g := range c.Security.Groups {
3151
if len(g.Users) == 0 {
3252
return fmt.Errorf("Group '%s' is empty", g.Name)
@@ -36,7 +56,7 @@ func (c iYamlConfigFile) hasEmptyGroup() error {
3656
return nil
3757
}
3858

39-
func (c iYamlConfigFile) hasRuleGroupThatExists() error {
59+
func (c ConfigFile) hasRuleGroupThatExists() error {
4060
for _, rule := range c.Security.Rules {
4161
for _, group := range rule.GroupList {
4262
exists := false
@@ -53,7 +73,7 @@ func (c iYamlConfigFile) hasRuleGroupThatExists() error {
5373
return nil
5474
}
5575

56-
func (c iYamlConfigFile) hasRuleCorrectActions() error {
76+
func (c ConfigFile) hasRuleCorrectActions() error {
5777
for _, rule := range c.Security.Rules {
5878
for _, action := range rule.ActionList {
5979
if !(action == "lock" || action == "unlock") {
@@ -64,7 +84,7 @@ func (c iYamlConfigFile) hasRuleCorrectActions() error {
6484
return nil
6585
}
6686

67-
func (c iYamlConfigFile) hasRuleFieldEmpty() error {
87+
func (c ConfigFile) hasRuleFieldEmpty() error {
6888
for _, rule := range c.Security.Rules {
6989
if len(rule.ActionList) == 0 {
7090
return fmt.Errorf("The field action is empty")
@@ -78,3 +98,30 @@ func (c iYamlConfigFile) hasRuleFieldEmpty() error {
7898
}
7999
return nil
80100
}
101+
102+
func (c ConfigFile) seRules() []event.SecurityRule {
103+
var result []event.SecurityRule
104+
105+
for _, r := range c.Security.Rules {
106+
// Add users from group to userlist
107+
userList := r.UserList
108+
for _, group := range r.GroupList {
109+
for _, g := range c.Security.Groups {
110+
if group != g.Name {
111+
continue
112+
}
113+
userList = append(userList, g.Users...)
114+
}
115+
}
116+
117+
// create final securityrule
118+
result = append(result, event.SecurityRule{
119+
Repository: r.Respository,
120+
FilePatterns: r.FilePatternList,
121+
Actions: r.ActionList,
122+
Users: userList,
123+
})
124+
}
125+
126+
return result
127+
}

internal/config/file/manager.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

internal/config/file/mapper.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

internal/config/file/types.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)