Skip to content

Commit

Permalink
Merge pull request #31 from LuBingtan/dev
Browse files Browse the repository at this point in the history
Fix tide
  • Loading branch information
kuilei-bot[bot] authored Feb 23, 2023
2 parents 3858d3e + 644ee24 commit ab6e29e
Show file tree
Hide file tree
Showing 20 changed files with 986 additions and 245 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
build-local:
go build -o bin/kuilei github.com/airconduct/kuilei/cmd/kuilei

.PHONY: test
test:
go test -v --race ./...

Expand Down
130 changes: 12 additions & 118 deletions cmd/kuilei/app/hook.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package app

import (
"github.com/go-logr/logr"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/airconduct/go-probot"
"github.com/airconduct/kuilei/pkg/pluginhelpers"
"github.com/airconduct/kuilei/pkg/plugins"
"github.com/airconduct/kuilei/pkg/app"
"github.com/airconduct/kuilei/pkg/signals"

"github.com/airconduct/kuilei/pkg/app/github"
_ "github.com/airconduct/kuilei/pkg/plugins/factory"
)

func NewHook() *cobra.Command {
opts := &hookOptions{
githubApp: probot.NewGitHubAPP(),

pluginConfigCache: pluginhelpers.NewConfigCache[plugins.Configuration](),
ownersConfigCache: pluginhelpers.NewConfigNearestCache[plugins.OwnersConfiguration](),
githubAppBuilder: github.New(),
}

cmd := &cobra.Command{
Expand All @@ -37,127 +35,23 @@ func NewHook() *cobra.Command {
}

type hookOptions struct {
githubApp probot.App[probot.GitHubClient]

configPath string
ownersFile string
pluginConfigCache pluginhelpers.ConfigCache[plugins.Configuration]
ownersConfigCache pluginhelpers.ConfigCache[plugins.OwnersConfiguration]
githubAppBuilder app.Builder[probot.GitHubClient]
}

func (opts *hookOptions) AddFlags(flags *pflag.FlagSet) {
opts.githubApp.AddFlags(flags)
flags.StringVar(&opts.configPath, "config-path", ".github/kuilei.yml", "config path for kuilei App in git repo")
flags.StringVar(&opts.ownersFile, "owners-file", "OWNERS", "owners file name")
opts.githubAppBuilder.BindFlags(flags)
}

func (opts *hookOptions) Validate(args []string) error {
opts.githubApp.On(probot.GitHub.Issues).
WithHandler(probot.GitHub.Issues.Handler(func(ctx probot.GitHubIssuesContext) {
payload := ctx.Payload()
pluginClient := pluginhelpers.PluginConfigClientFromGithub(
ctx.Client(), opts.configPath, opts.pluginConfigCache,
)
cfg, err := pluginClient.GetConfig(payload.Repo.Owner.GetLogin(), payload.Repo.GetName())
ctx.Must(err)

for _, p := range cfg.Plugins {
plugin := plugins.GetGitCommentPlugin(p.Name, getClientSets(opts, ctx, pluginClient), p.Args...)
if err := plugin.Do(ctx, pluginhelpers.GitCommentEventFromGithubIssuesEvent(payload)); err != nil {
ctx.Logger().Error(err, "Failed to execute plugin", "name", plugin.Name())
}
}
}))
opts.githubApp.On(probot.GitHub.IssueComment).
WithHandler(probot.GitHub.IssueComment.Handler(func(ctx probot.GitHubIssueCommentContext) {
payload := ctx.Payload()
pluginClient := pluginhelpers.PluginConfigClientFromGithub(
ctx.Client(), opts.configPath, opts.pluginConfigCache,
)
cfg, err := pluginClient.GetConfig(payload.Repo.Owner.GetLogin(), payload.Repo.GetName())
ctx.Must(err)

for _, p := range cfg.Plugins {
plugin := plugins.GetGitCommentPlugin(p.Name, getClientSets(opts, ctx, pluginClient), p.Args...)
if err := plugin.Do(ctx, pluginhelpers.GitCommentEventFromGithubIssueCommentEvent(payload)); err != nil {
ctx.Logger().Error(err, "Failed to execute plugin", "name", plugin.Name())
}
}
}))
opts.githubApp.On(probot.GitHub.PullRequest).
WithHandler(probot.GitHub.PullRequest.Handler(func(ctx probot.GitHubPullRequestContext) {
payload := ctx.Payload()
pluginClient := pluginhelpers.PluginConfigClientFromGithub(
ctx.Client(), opts.configPath, opts.pluginConfigCache,
)
cfg, err := pluginClient.GetConfig(payload.Repo.Owner.GetLogin(), payload.Repo.GetName())
ctx.Must(err)

for _, p := range cfg.Plugins {
plugin := plugins.GetGitCommentPlugin(p.Name, getClientSets(opts, ctx, pluginClient), p.Args...)
if err := plugin.Do(ctx, pluginhelpers.GitCommentEventFromGithubPullRequestEvent(payload)); err != nil {
ctx.Logger().Error(err, "Failed to execute plugin", "name", plugin.Name())
}
}
}))
opts.githubApp.On(probot.GitHub.PullRequestReview).
WithHandler(probot.GitHub.PullRequestReview.Handler(func(ctx probot.GitHubPullRequestReviewContext) {
payload := ctx.Payload()
pluginClient := pluginhelpers.PluginConfigClientFromGithub(
ctx.Client(), opts.configPath, opts.pluginConfigCache,
)
cfg, err := pluginClient.GetConfig(payload.Repo.Owner.GetLogin(), payload.Repo.GetName())
ctx.Must(err)

for _, p := range cfg.Plugins {
plugin := plugins.GetGitCommentPlugin(p.Name, getClientSets(opts, ctx, pluginClient), p.Args...)
if err := plugin.Do(ctx, pluginhelpers.GitCommentEventFromGithubPullRequestReviewEvent(payload)); err != nil {
ctx.Logger().Error(err, "Failed to execute plugin", "name", plugin.Name())
}
}
}))
opts.githubApp.On(probot.GitHub.PullRequestReviewComment).
WithHandler(probot.GitHub.PullRequestReviewComment.Handler(func(ctx probot.GitHubPullRequestReviewCommentContext) {
payload := ctx.Payload()
pluginClient := pluginhelpers.PluginConfigClientFromGithub(
ctx.Client(), opts.configPath, opts.pluginConfigCache,
)
cfg, err := pluginClient.GetConfig(payload.Repo.Owner.GetLogin(), payload.Repo.GetName())
ctx.Must(err)

for _, p := range cfg.Plugins {
plugin := plugins.GetGitCommentPlugin(p.Name, getClientSets(opts, ctx, pluginClient), p.Args...)
if err := plugin.Do(ctx, pluginhelpers.GitCommentEventFromGithubPullRequestReviewCommentEvent(payload)); err != nil {
ctx.Logger().Error(err, "Failed to execute plugin", "name", plugin.Name())
}
}
}))
opts.githubApp.On(probot.GitHub.Push).
WithHandler(probot.GitHub.Push.Handler(func(ctx probot.GitHubPushContext) {}))
opts.githubApp.On(probot.GitHub.Status).
WithHandler(probot.GitHub.Status.Handler(func(ctx probot.GitHubStatusContext) {}))
// TODO: validate args
return nil
}

func (opts *hookOptions) Run() error {
ctx := signals.SetupSignalContext()
return opts.githubApp.Run(ctx)
}

func getClientSets[PT any](
opts *hookOptions,
ctx probot.ProbotContext[probot.GitHubClient, PT], pluginClient plugins.PluginConfigClient,
) plugins.ClientSets {
logger := ctx.Logger()
return plugins.ClientSets{
GitIssueClient: pluginhelpers.GitIssueClientFromGithub(ctx.Client()),
GitPRClient: pluginhelpers.GitPRClientFromGithub(ctx.Client()),
PluginConfigClient: pluginClient,
OwnersClient: pluginhelpers.OwnersClientFromGithub(ctx.Client(), opts.ownersFile, opts.ownersConfigCache),
GitRepoClient: pluginhelpers.GitRepoClientFromGithub(ctx.Client()),
GitSearchClient: pluginhelpers.GitSearchClientFromGithub(ctx.GraphQL()),
LoggerClient: pluginhelpers.MakeLoggerClient(func() logr.Logger {
return logger
}),
githubApp, err := opts.githubAppBuilder.Build()
if err != nil {
return fmt.Errorf("faield to build github app: %w", err)
}
return githubApp.Run(ctx)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/go-logr/logr v1.2.3
github.com/go-logr/zapr v1.2.3
github.com/google/go-github/v48 v48.2.0
github.com/h2non/gock v1.2.0
github.com/onsi/ginkgo/v2 v2.8.0
github.com/onsi/gomega v1.26.0
github.com/shurcooL/githubv4 v0.0.0-20221229060216-a8d4a561cc93
Expand All @@ -31,6 +32,7 @@ require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-github/v45 v45.2.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.1 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE=
github.com/h2non/gock v1.2.0/go.mod h1:tNhoxHYW2W42cYkYb1WqzdbYIieALC99kpYr7rH/BQk=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
Expand Down Expand Up @@ -82,6 +84,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/onsi/ginkgo/v2 v2.8.0 h1:pAM+oBNPrpXRs+E/8spkeGx9QgekbRVyr74EUvRVOUI=
Expand Down
11 changes: 11 additions & 0 deletions pkg/app/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package app

import (
"github.com/airconduct/go-probot"
"github.com/spf13/pflag"
)

type Builder[GT probot.GitClientType] interface {
BindFlags(flags *pflag.FlagSet)
Build() (probot.App[GT], error)
}
Loading

0 comments on commit ab6e29e

Please sign in to comment.