Skip to content

Commit

Permalink
Merge pull request #6 from LuBingtan/dev
Browse files Browse the repository at this point in the history
docs: Add probot docs.
  • Loading branch information
kuilei-bot[bot] committed Dec 26, 2022
2 parents 5b37d6b + e54b761 commit f0627df
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 4 deletions.
20 changes: 20 additions & 0 deletions pkg/probot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<p align="center">
<a href="https://probot.github.io"><img src="docs/robot.svg" width="160" alt="Probot's logo, a cartoon robot" /></a>
</p>

<h1 align="center">Go-Probot</h1>

> A framework for building GitHub Apps to automate and improve your workflow, in **Golang**.
>
> Inspired by [Probot](https://github.com/probot/probot).
## Example

Please check the simplest [example](example/main.go).

## Features

- [x] Support building Github App
- [ ] Simple dashboard
- [ ] Support intergration with [Gitlab community edition](https://docs.gitlab.com/ee/install/)
- [ ] Support intergration with [Gerrit](https://www.gerritcodereview.com/)
5 changes: 5 additions & 0 deletions pkg/probot/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type githubApp struct {
uploadURL string
serverOpts ServerOptions

dataMutex sync.RWMutex
hmacToken []byte
privateKey []byte
loggerOptions
Expand All @@ -53,6 +54,8 @@ func (app *githubApp) GetHTTPHandler() http.Handler {
}

func (app *githubApp) GetSecretToken() []byte {
app.dataMutex.RLock()
defer app.dataMutex.RUnlock()
return app.hmacToken
}

Expand Down Expand Up @@ -103,6 +106,8 @@ func (app *githubApp) Run(ctx context.Context) error {
func (app *githubApp) shutdown() {}

func (app *githubApp) initialize() error {
app.dataMutex.Lock()
defer app.dataMutex.Unlock()
rawToken, err := os.ReadFile(app.hmacTokenFile)
if err != nil {
return fmt.Errorf("failed to read hmac token file, %w", err)
Expand Down
69 changes: 69 additions & 0 deletions pkg/probot/docs/robot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion pkg/probot/example/example_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package github_test
package main

import (
"encoding/json"
Expand Down
5 changes: 2 additions & 3 deletions pkg/probot/example/example_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package github_test
package main

import (
"context"
Expand Down Expand Up @@ -47,7 +47,6 @@ var _ = Describe("Test Probot Example", func() {
Expect(app.Run(ctx)).Should(Succeed())
}()

time.Sleep(3 * time.Second)
Eventually(func(g Gomega) {
g.Expect(mock.Send(
app.(mock.AppMock[probot.GithubClient]),
Expand All @@ -62,6 +61,6 @@ var _ = Describe("Test Probot Example", func() {
Issue: &gh.Issue{Number: probot.ToPointer(1)},
},
)).Should(Succeed())
}, time.Second, 3*time.Second).Should(Succeed())
}, 5*time.Second, time.Second).Should(Succeed())
})
})
46 changes: 46 additions & 0 deletions pkg/probot/example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"context"

"github.com/google/go-github/v48/github"
"github.com/spf13/pflag"

"github.com/airconduct/kuilei/pkg/probot"
probotgh "github.com/airconduct/kuilei/pkg/probot/github"
)

func main() {
app := probot.NewGithubAPP()
app.AddFlags(pflag.CommandLine)
pflag.Parse()

// Add a handler for events "issue_comment.created"
app.On(probotgh.Event.IssueComment_created).WithHandler(probotgh.IssueCommentHandler(func(ctx probotgh.IssueCommentContext) {
payload := ctx.Payload()
ctx.Logger().Info("Get IssueComment event", "payload", payload)
owner := payload.Repo.Owner.GetLogin()
repo := payload.Repo.GetName()
issueNumber := *payload.Issue.Number
// If any error happen, the error message will be logged and sent as response
ctx.Must(ctx.Client().Issues.CreateComment(ctx, owner, repo, issueNumber, &github.IssueComment{
Body: github.String("Reply to this comment."),
}))
}))

// Add a handler for multiple events
app.On(
probotgh.Event.PullRequest_opened, // pull_request.opened
probotgh.Event.PullRequest_edited, // pull_request.edited
probotgh.Event.PullRequest_synchronize, // pull_request.synchronize
probotgh.Event.PullRequest_labeled, // pull_request.labeled
probotgh.Event.PullRequest_assigned, // pull_request.assigned
).WithHandler(probotgh.PullRequestHandler(func(ctx probotgh.PullRequestContext) {
payload := ctx.Payload()
ctx.Logger().Info("Do something", "action", payload.GetAction(), "PullRequest labels", payload.PullRequest.Labels)
}))

if err := app.Run(context.Background()); err != nil {
panic(err)
}
}

0 comments on commit f0627df

Please sign in to comment.