Skip to content

Commit

Permalink
Merge pull request #137 from CarlJi/local_dev
Browse files Browse the repository at this point in the history
chore: add tool to send github event
  • Loading branch information
CarlJi authored May 29, 2024
2 parents e29ceaa + 775cd4e commit 10236d7
Show file tree
Hide file tree
Showing 4 changed files with 623 additions and 8 deletions.
24 changes: 16 additions & 8 deletions docs/website/docs/getting-started/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ title: 参与开发
sidebar_position: 2
---

如果是通过 `access token` 来测试,可以在个人仓库上,配置好hook后,用以下命令执行:
**Reviewbot** 当前设计上主要作为 [webhook server](https://docs.github.com/en/webhooks/about-webhooks),通过接受 GitHub 事件,针对目标仓库的 PR,执行各种 linter 检查,判断代码是否符合规范。

```bash
go run . -access-token=<your-access-token> -webhook-secret=<webhook-secret> -config ./config/config.yaml -log-level 0
```
所以,如果想在本地开发环境调试**Reviewbot**,需要准备如下:

如果是通过 `Github APP` 方式,执行命令为:
- GitHub 认证 - 有以下两种方式
- [personal access tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)方式
- [GitHub APP](https://docs.github.com/en/apps) 方式
- 启动**Reviewbot**

```bash
go run . -webhook-secret=<webhook-secret> -config ./config/config.yaml -log-level 0 -app-id=<github_app_id> -app-private-key=<github_app_private_key>
```
```bash
# access token 方式
go run . -access-token=<your-access-token> -webhook-secret=<webhook-secret> -config ./config/config.yaml -log-level 0
# Github APP 方式
go run . -webhook-secret=<webhook-secret> -config ./config/config.yaml -log-level 0 -app-id=<github_app_id> -app-private-key=<github_app_private_key>
```

- 测试用的 git 仓库 - 要有 admin 权限,这样可以拿到相应的 GitHub 事件
- 参考 [如何给仓库配置 Webhook](https://docs.github.com/en/webhooks/using-webhooks/creating-webhooks)
- 本地模拟发送 GitHub 事件,可以借助工具 [phony](https://github.com/qiniu/reviewbot/tree/master/tools/phony)
11 changes: 11 additions & 0 deletions tools/phony/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# phony

模拟发送 Github 事件,主要参考 [phony](https://github.com/kubernetes-sigs/prow/tree/main/cmd/phony)

## 使用例子

在当前目录下执行:

```bash
go run . --hmac=<your-webhook-secret> -payload ./pr-open.json --event=pull_request
```
94 changes: 94 additions & 0 deletions tools/phony/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"flag"
"fmt"
"io"
"net/http"
"os"

"github.com/qiniu/x/log"
)

var (
address = flag.String("address", "http://localhost:8888/hook", "Where to send the fake hook.")
hmacS = flag.String("hmac", "abcde12345", "HMAC token to sign payload with.")
event = flag.String("event", "ping", "Type of event to send, such as pull_request.")
payload = flag.String("payload", "", "File to send as payload. If unspecified, sends \"{}\".")
)

func main() {
flag.Parse()

var body []byte
if *payload == "" {
body = []byte("{}")
} else {
d, err := os.ReadFile(*payload)
if err != nil {
log.Fatal("Could not read payload file.", err)
}
body = d
}

if err := SendHook(*address, *event, body, []byte(*hmacS)); err != nil {
log.Errorf("Error sending hook. err: %v", err)
} else {
log.Info("Hook sent.")
}
}

// SendHook sends a GitHub event of type eventType to the provided address.
func SendHook(address, eventType string, payload, hmac []byte) error {
req, err := http.NewRequest(http.MethodPost, address, bytes.NewBuffer(payload))
if err != nil {
return err
}
req.Header.Set("X-GitHub-Event", eventType)
req.Header.Set("X-GitHub-Delivery", "GUID")
req.Header.Set("X-Hub-Signature", PayloadSignature(payload, hmac))
req.Header.Set("content-type", "application/json")

c := &http.Client{}
resp, err := c.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
rb, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("response from hook has status %d and body %s", resp.StatusCode, string(bytes.TrimSpace(rb)))
}
return nil
}

// PayloadSignature returns the signature that matches the payload.
func PayloadSignature(payload []byte, key []byte) string {
mac := hmac.New(sha1.New, key)
mac.Write(payload)
sum := mac.Sum(nil)
return "sha1=" + hex.EncodeToString(sum)
}
Loading

0 comments on commit 10236d7

Please sign in to comment.