-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from CarlJi/local_dev
chore: add tool to send github event
- Loading branch information
Showing
4 changed files
with
623 additions
and
8 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
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,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 | ||
``` |
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,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) | ||
} |
Oops, something went wrong.