Skip to content

Commit 67fdf7b

Browse files
author
xiaoliangyu
committed
MVP 级功能实现
feat: MVP 级功能实现 Signed-off-by: xiaoliangyu <lyle@hdu.edu.cn>
1 parent 9dca719 commit 67fdf7b

File tree

18 files changed

+8794
-0
lines changed

18 files changed

+8794
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
*.dll
55
*.so
66
*.dylib
7+
.env
8+
.idea
9+
.DS_Store
10+
common.yaml
711

812
# Test binary, built with `go test -c`
913
*.test

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# ospp-cr-bot
2+
23
开源之夏 - 一种通用的 Code Review 机器人

cmd/main.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/lyleshaw/ospp-cr-bot/internal/pkg/config"
6+
"github.com/lyleshaw/ospp-cr-bot/internal/pkg/eventListener"
7+
"github.com/lyleshaw/ospp-cr-bot/internal/pkg/pushChannel/lark"
8+
"github.com/lyleshaw/ospp-cr-bot/pkg/utils/log"
9+
"github.com/sirupsen/logrus"
10+
)
11+
12+
var (
13+
isDebug bool
14+
)
15+
16+
func init() {
17+
initLog()
18+
config.InitConfig()
19+
}
20+
21+
func initLog() {
22+
if isDebug {
23+
logrus.SetLevel(logrus.DebugLevel)
24+
log.Infof("Log level is: %s.", logrus.GetLevel())
25+
} else {
26+
logrus.SetLevel(logrus.InfoLevel)
27+
}
28+
}
29+
30+
func main() {
31+
router := gin.Default()
32+
router.POST("/github/webhook", eventListener.GitHubWebHook)
33+
router.POST("/api/lark/callback", lark.Callback)
34+
err := router.Run(":3000")
35+
if err != nil {
36+
return
37+
}
38+
return
39+
}

go.mod

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module github.com/lyleshaw/ospp-cr-bot
2+
3+
go 1.17
4+
5+
require (
6+
github.com/faabiosr/cachego v0.15.0
7+
github.com/fastwego/feishu v1.0.0-beta.4
8+
github.com/gin-gonic/gin v1.7.7
9+
github.com/sirupsen/logrus v1.8.1
10+
github.com/spf13/viper v1.11.0
11+
github.com/valyala/fasttemplate v1.2.1
12+
gopkg.in/gookit/color.v1 v1.1.6
13+
gopkg.in/yaml.v2 v2.4.0
14+
)
15+
16+
require (
17+
github.com/fsnotify/fsnotify v1.5.1 // indirect
18+
github.com/gin-contrib/sse v0.1.0 // indirect
19+
github.com/go-playground/locales v0.13.0 // indirect
20+
github.com/go-playground/universal-translator v0.17.0 // indirect
21+
github.com/go-playground/validator/v10 v10.4.1 // indirect
22+
github.com/golang/protobuf v1.5.2 // indirect
23+
github.com/hashicorp/hcl v1.0.0 // indirect
24+
github.com/json-iterator/go v1.1.12 // indirect
25+
github.com/leodido/go-urn v1.2.0 // indirect
26+
github.com/magiconair/properties v1.8.6 // indirect
27+
github.com/mattn/go-isatty v0.0.14 // indirect
28+
github.com/mitchellh/mapstructure v1.4.3 // indirect
29+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
30+
github.com/modern-go/reflect2 v1.0.2 // indirect
31+
github.com/pelletier/go-toml v1.9.4 // indirect
32+
github.com/pelletier/go-toml/v2 v2.0.0-beta.8 // indirect
33+
github.com/spf13/afero v1.8.2 // indirect
34+
github.com/spf13/cast v1.4.1 // indirect
35+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
36+
github.com/spf13/pflag v1.0.5 // indirect
37+
github.com/subosito/gotenv v1.2.0 // indirect
38+
github.com/ugorji/go/codec v1.1.7 // indirect
39+
github.com/valyala/bytebufferpool v1.0.0 // indirect
40+
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
41+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
42+
golang.org/x/text v0.3.7 // indirect
43+
google.golang.org/protobuf v1.28.0 // indirect
44+
gopkg.in/ini.v1 v1.66.4 // indirect
45+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
46+
)

go.sum

Lines changed: 874 additions & 0 deletions
Large diffs are not rendered by default.

internal/pkg/config/config.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
6+
"gopkg.in/yaml.v2"
7+
"io/ioutil"
8+
)
9+
10+
var (
11+
Cfg *Config
12+
)
13+
14+
type Config struct {
15+
Tasks []Tasks `yaml:"tasks"`
16+
}
17+
type Tasks struct {
18+
Name string `yaml:"name"`
19+
Repo string `yaml:"repo"`
20+
RepoType string `yaml:"repoType"`
21+
Recevier string `yaml:"recevier"`
22+
RecevierType string `yaml:"recevierType"`
23+
PushChannel string `yaml:"pushChannel"`
24+
}
25+
26+
func InitConfig() {
27+
config, err := ioutil.ReadFile("./common.yaml")
28+
if err != nil {
29+
fmt.Print(err)
30+
}
31+
32+
err = yaml.Unmarshal(config, &Cfg)
33+
if err != nil {
34+
fmt.Println("error")
35+
}
36+
}
37+
38+
func QueryReceiveIDByRepo(repo string) (string, error) {
39+
for _, task := range Cfg.Tasks {
40+
if task.Repo == repo {
41+
return task.Recevier, nil
42+
}
43+
}
44+
return "", fmt.Errorf("repo not found")
45+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package eventListener
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/gin-gonic/gin"
7+
"github.com/lyleshaw/ospp-cr-bot/internal/pkg/config"
8+
"github.com/valyala/fasttemplate"
9+
"io/ioutil"
10+
"net/url"
11+
"strconv"
12+
13+
"github.com/lyleshaw/ospp-cr-bot/internal/pkg/pushChannel/lark"
14+
)
15+
16+
// Event defines a GitHub hook event type
17+
type Event string
18+
19+
// GitHub hook types
20+
const (
21+
IssueCommentEvent Event = "issue_comment"
22+
IssuesEvent Event = "issues"
23+
PullRequestEvent Event = "pull_request"
24+
PullRequestReviewEvent Event = "pull_request_review"
25+
PullRequestReviewCommentEvent Event = "pull_request_review_comment"
26+
)
27+
28+
// GitHubWebHook .
29+
// @router /github/webhook [POST]
30+
func GitHubWebHook(c *gin.Context) {
31+
event := c.GetHeader("X-GitHub-Event")
32+
if event == "" {
33+
return
34+
}
35+
36+
body, _ := ioutil.ReadAll(c.Request.Body)
37+
bodyStr := string(body)
38+
bodyStr = bodyStr[8:]
39+
bodyStr, _ = url.QueryUnescape(bodyStr)
40+
41+
gitHubEvent := Event(event)
42+
43+
switch gitHubEvent {
44+
case IssueCommentEvent:
45+
var req IssueCommentPayload
46+
_ = json.Unmarshal([]byte(bodyStr), &req)
47+
// Do whatever you want from here...
48+
fmt.Printf("%+v\n", req)
49+
fmt.Printf("%s\n", req.Repository.FullName)
50+
case IssuesEvent:
51+
var req IssuesPayload
52+
_ = json.Unmarshal([]byte(bodyStr), &req)
53+
// Do whatever you want from here...
54+
fmt.Printf("%+v\n", req)
55+
fmt.Printf("%s\n", req.Repository.FullName)
56+
case PullRequestEvent:
57+
var req PullRequestPayload
58+
_ = json.Unmarshal([]byte(bodyStr), &req)
59+
t := fasttemplate.New(lark.SEND_PR_MSG, "{{", "}}")
60+
prMsg := t.ExecuteString(map[string]interface{}{
61+
"Time": req.PullRequest.CreatedAt.Format("2006年01月02日 15:04:05"),
62+
"PRTitle": req.PullRequest.Title,
63+
"Login": req.PullRequest.User.Login,
64+
"PRNumber": strconv.FormatInt(req.PullRequest.Number, 10),
65+
"PRURL": req.PullRequest.HTMLURL,
66+
})
67+
receiveID, err := config.QueryReceiveIDByRepo(req.Repository.FullName)
68+
if err != nil {
69+
return
70+
}
71+
lark.SendMessage(receiveID, prMsg)
72+
case PullRequestReviewEvent:
73+
var req PullRequestReviewPayload
74+
_ = json.Unmarshal([]byte(bodyStr), &req)
75+
// Do whatever you want from here...
76+
fmt.Printf("%+v\n", req)
77+
fmt.Printf("%s\n", req.PullRequest.Head.Repo.FullName)
78+
fmt.Printf("%s\n", req.PullRequest.Head.User.Login)
79+
case PullRequestReviewCommentEvent:
80+
var req PullRequestReviewCommentPayload
81+
_ = json.Unmarshal([]byte(bodyStr), &req)
82+
// Do whatever you want from here...
83+
fmt.Printf("%+v\n", req)
84+
fmt.Printf("%s\n", req.PullRequest.Head.Repo.FullName)
85+
fmt.Printf("%s\n", req.PullRequest.Head.User.Login)
86+
}
87+
c.JSON(200, nil)
88+
}

0 commit comments

Comments
 (0)