Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhangtian
committed
Oct 30, 2022
1 parent
f64eae2
commit ceee8fe
Showing
14 changed files
with
640 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
release/ | ||
bin/ | ||
log/ | ||
data/ | ||
*.assets/ | ||
*.json | ||
*.yaml | ||
upgrade/ | ||
example/ | ||
test/ | ||
*.tar.gz | ||
.idea/ | ||
.git/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# ---> Go | ||
# Compiled Object files, Static and Dynamic libs (Shared Objects) | ||
*.o | ||
*.a | ||
*.so | ||
|
||
# Folders | ||
_obj | ||
_test | ||
|
||
# Architecture specific extensions/prefixes | ||
*.[568vq] | ||
[568vq].out | ||
|
||
*.cgo1.go | ||
*.cgo2.c | ||
_cgo_defun.c | ||
_cgo_gotypes.go | ||
_cgo_export.* | ||
|
||
_testmain.go | ||
|
||
*.exe | ||
*.test | ||
*.prof | ||
|
||
# ---> JetBrains | ||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio | ||
|
||
*.iml | ||
|
||
## Directory-based project format: | ||
.idea/ | ||
# if you remove the above rule, at least ignore the following: | ||
|
||
# User-specific stuff: | ||
# .idea/workspace.xml | ||
# .idea/tasks.xml | ||
# .idea/dictionaries | ||
|
||
# Sensitive or high-churn files: | ||
# .idea/dataSources.ids | ||
# .idea/dataSources.xml | ||
# .idea/sqlDataSources.xml | ||
# .idea/dynamic.xml | ||
# .idea/uiDesigner.xml | ||
|
||
# Gradle: | ||
# .idea/gradle.xml | ||
# .idea/libraries | ||
|
||
# Mongo Explorer plugin: | ||
# .idea/mongoSettings.xml | ||
|
||
## File-based project format: | ||
*.ipr | ||
*.iws | ||
|
||
## Plugin-specific files: | ||
|
||
# IntelliJ | ||
/out/ | ||
|
||
# mpeltonen/sbt-idea plugin | ||
.idea_modules/ | ||
|
||
# JIRA plugin | ||
atlassian-ide-plugin.xml | ||
|
||
# Crashlytics plugin (for Android Studio and IntelliJ) | ||
com_crashlytics_export_strings.xml | ||
crashlytics.properties | ||
crashlytics-build.properties | ||
|
||
go.sum | ||
*.db | ||
bin/* | ||
data/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
run: | ||
concurrency: 5 | ||
tests: false | ||
# skip-dirs-use-default: true | ||
skip-dirs: | ||
- docs | ||
- vendor | ||
- test | ||
fast: true | ||
|
||
linters: | ||
enable: | ||
# - bodyclose # 检查 HTTP 响应体是否关闭成功 1.18 不支持 | ||
- depguard # 检查包导入是否在可接受包列表中 | ||
- errcheck # 用于检查 gotag 程序中未检查错误的程序 | ||
- exportloopref # 检查指向封闭循环变量的指针 | ||
- gofmt # 检查代码是否是 gofmt-ed | ||
- goimports # | ||
- gosec # 检查源代码是否存在安全问题 | ||
- gosimple | ||
- govet # 检查 Go 源代码并报告可疑结构,例如参数与格式字符串不一致的 Printf | ||
- ineffassign # 检测何时不使用对现有变量的赋值 | ||
- misspell # 在评论中查找常见拼写错误的英语单词 | ||
# - structcheck # 查找未使用的结构字段 1.18 | ||
- typecheck # 像 Go 编译器的前端一样,解析和类型检查 Go 代码 | ||
- errname # 检查标记错误是否以“Err”为前缀,错误类型以“Error”为后缀 | ||
- unconvert # 删除不必要的类型转换 | ||
# - gomnd # 检测幻数的分析器 | ||
# - deadcode | ||
# - unused | ||
# - execinquery # Query 函数中查询字符串检查器的 linter | ||
|
||
linters-settings: | ||
govet: | ||
check-shadowing: false | ||
depguard: | ||
list-type: blacklist | ||
include-go-root: true | ||
packages: | ||
# The io/ioutil package has been deprecated. | ||
# https://go.dev/doc/go1.16#ioutil | ||
- io/ioutil | ||
gosec: | ||
excludes: | ||
- G401 | ||
- G402 | ||
- G501 | ||
issues: | ||
exclude-rules: | ||
- linters: | ||
- staticcheck | ||
- typecheck | ||
- structcheck | ||
text: "unused" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
builds: | ||
- env: | ||
- CGO_ENABLED=0 | ||
- GOPROXY="https://goproxy.cn,direct" | ||
goarch: | ||
- amd64 | ||
- arm64 | ||
goos: | ||
- linux | ||
flags: | ||
- -trimpath | ||
ldflags: | ||
- -s -w | ||
main: ./cmd | ||
|
||
checksum: | ||
name_template: 'checksums.txt' | ||
|
||
changelog: | ||
sort: asc | ||
use: gitlab | ||
groups: | ||
- title: Features | ||
regexp: "^.*feat[(\\w)]*:+.*$" | ||
order: 100 | ||
- title: 'Bug fixes' | ||
regexp: "^.*fix[(\\w)]*:+.*$" | ||
order: 200 | ||
- title: 'Documentation updates' | ||
regexp: "^.*docs[(\\w)]*:+.*$" | ||
order: 400 | ||
- title: Others | ||
order: 999 | ||
filters: | ||
exclude: | ||
- '^test:' | ||
- '^chore' | ||
- 'merge conflict' | ||
- '^ci' | ||
- '^style' | ||
- Merge pull request | ||
- Merge remote-tracking branch | ||
- Merge branch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# gitlab bot | ||
|
||
Customize bot logic with go plugin | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"log" | ||
"net/http" | ||
"os" | ||
"sync/atomic" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/xanzy/go-gitlab" | ||
|
||
"github.com/eleztian/gitlab-bot/config" | ||
"github.com/eleztian/gitlab-bot/hooks" | ||
"github.com/eleztian/gitlab-bot/plugin" | ||
) | ||
|
||
var ( | ||
gitlabToken = os.Getenv("GITLAB_TOKEN") | ||
gitlabURL = os.Getenv("GITLAB_URL") | ||
hookServerAddr = os.Getenv("BOOT_HOOK_ADDR") | ||
) | ||
|
||
var client *gitlab.Client | ||
|
||
func init() { | ||
var err error | ||
client, err = gitlab.NewClient(gitlabToken, | ||
gitlab.WithBaseURL(gitlabURL)) | ||
if err != nil { | ||
log.Fatalf("Failed to create client: %v", err) | ||
return | ||
} | ||
} | ||
|
||
var configFilePath = flag.String("c", "conf/config.yml", "config file path") | ||
|
||
func main() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
cfg, err := config.LoadFile(*configFilePath) | ||
if err != nil { | ||
logrus.WithError(err).Fatalln("failed to load config file") | ||
return | ||
} | ||
|
||
err = plugin.LoadAndWatchPlugins(cfg.PluginDir) | ||
if err != nil { | ||
logrus.WithError(err).Fatalln("plugin load failed") | ||
return | ||
} | ||
|
||
var newHandler = func(cfg *config.Config) http.Handler { | ||
hookServer := hooks.NewHookServer(client) | ||
for name, h := range cfg.Routers { | ||
logrus.WithField("Prefix", name). | ||
WithField("Types", h.Types). | ||
WithField("Handler", h.Handler).Info("add route") | ||
hookServer.AddRouter(name, hooks.NewEventTypeRouter(h.Types...), hooks.PluginHandler(h.Handler)) | ||
} | ||
return hookServer.Handler(ctx) | ||
} | ||
|
||
switchHandler := &SwitchHandler{} | ||
switchHandler.SetHandler(newHandler(cfg)) | ||
err = config.WatchFile(*configFilePath, func(cfg *config.Config) { | ||
switchHandler.SetHandler(newHandler(cfg)) | ||
}) | ||
if err != nil { | ||
logrus.WithError(err).Fatalln("failed to watch config file") | ||
return | ||
} | ||
logrus.WithField("HookServerAddr", hookServerAddr).Info("start hook server....") | ||
err = http.ListenAndServe(hookServerAddr, switchHandler) | ||
if err != nil { | ||
logrus.WithError(err).WithField("Addr", hookServerAddr).Fatalln("http listen and server failed") | ||
} | ||
} | ||
|
||
type SwitchHandler struct { | ||
atomic.Value | ||
} | ||
|
||
func (sh *SwitchHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { | ||
h := sh.Load().(http.Handler) | ||
h.ServeHTTP(writer, request) | ||
} | ||
|
||
func (sh *SwitchHandler) SetHandler(h http.Handler) { | ||
sh.Store(h) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/xanzy/go-gitlab" | ||
) | ||
|
||
type Event struct { | ||
EventType gitlab.EventType | ||
Event interface{} | ||
} | ||
|
||
type Handler interface { | ||
Do(ctx context.Context, client *gitlab.Client, event Event) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module github.com/eleztian/gitlab-bot/common | ||
|
||
go 1.18 | ||
|
||
require github.com/xanzy/go-gitlab v0.74.0 | ||
|
||
require ( | ||
github.com/fsnotify/fsnotify v1.6.0 // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/google/go-querystring v1.1.0 // indirect | ||
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect | ||
github.com/hashicorp/go-retryablehttp v0.7.1 // indirect | ||
golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48 // indirect | ||
golang.org/x/oauth2 v0.0.0-20220722155238-128564f6959c // indirect | ||
golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect | ||
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect | ||
google.golang.org/appengine v1.6.7 // indirect | ||
google.golang.org/protobuf v1.28.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
routers: | ||
/bot-test: | ||
types: | ||
- Issue Hook | ||
- Note Hook | ||
handler: "bot-test" |
Oops, something went wrong.