Skip to content

Commit 5021977

Browse files
committed
refactor
1 parent 087dbda commit 5021977

File tree

3 files changed

+14
-45
lines changed

3 files changed

+14
-45
lines changed

services/webhook/feishu.go

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,17 @@
44
package webhook
55

66
import (
7-
"bytes"
87
"context"
98
"crypto/hmac"
109
"crypto/sha256"
1110
"encoding/base64"
1211
"fmt"
1312
"net/http"
14-
"strconv"
1513
"strings"
1614
"time"
1715

1816
webhook_model "code.gitea.io/gitea/models/webhook"
1917
"code.gitea.io/gitea/modules/git"
20-
"code.gitea.io/gitea/modules/json"
2118
api "code.gitea.io/gitea/modules/structs"
2219
webhook_module "code.gitea.io/gitea/modules/webhook"
2320
)
@@ -193,27 +190,17 @@ func (feishuConvertor) WorkflowJob(p *api.WorkflowJobPayload) (FeishuPayload, er
193190
return newFeishuTextPayload(text), nil
194191
}
195192

196-
// GenSign generates a signature for Feishu webhook
193+
// feishuGenSign generates a signature for Feishu webhook
197194
// https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot
198-
func GenSign(secret string, timestamp int64) (string, error) {
199-
// timestamp + key do sha256, then base64 encode
195+
func feishuGenSign(secret string, timestamp int64) string {
196+
// key="{timestamp}\n{secret}", then hmac-sha256, then base64 encode
200197
stringToSign := fmt.Sprintf("%d\n%s", timestamp, secret)
201-
202198
h := hmac.New(sha256.New, []byte(stringToSign))
203-
_, err := h.Write([]byte{})
204-
if err != nil {
205-
return "", err
206-
}
207-
208-
signature := base64.StdEncoding.EncodeToString(h.Sum(nil))
209-
return signature, nil
199+
return base64.StdEncoding.EncodeToString(h.Sum(nil))
210200
}
211201

212202
func newFeishuRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
213-
var pc payloadConvertor[FeishuPayload] = feishuConvertor{}
214-
215-
// Get the payload first
216-
payload, err := newPayload(pc, []byte(t.PayloadContent), t.EventType)
203+
payload, err := newPayload(feishuConvertor{}, []byte(t.PayloadContent), t.EventType)
217204
if err != nil {
218205
return nil, nil, err
219206
}
@@ -222,35 +209,10 @@ func newFeishuRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_mo
222209
if w.Secret != "" {
223210
timestamp := time.Now().Unix()
224211
payload.Timestamp = timestamp
225-
226-
// Generate signature
227-
sign, err := GenSign(w.Secret, timestamp)
228-
if err != nil {
229-
return nil, nil, err
230-
}
231-
payload.Sign = sign
232-
}
233-
234-
// Marshal the payload
235-
body, err := json.MarshalIndent(payload, "", " ")
236-
if err != nil {
237-
return nil, nil, err
238-
}
239-
240-
// Create the request
241-
method := w.HTTPMethod
242-
if method == "" {
243-
method = http.MethodPost
244-
}
245-
246-
req, err := http.NewRequest(method, w.URL, bytes.NewReader(body))
247-
if err != nil {
248-
return nil, nil, err
212+
payload.Sign = feishuGenSign(w.Secret, timestamp)
249213
}
250-
req.Header.Set("Content-Type", "application/json")
251214

252-
// Add default headers
253-
return req, body, addDefaultHeaders(req, []byte(w.Secret), w, t, body)
215+
return sendHTTPRequest(payload, w, t, false)
254216
}
255217

256218
func init() {

services/webhook/feishu_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,7 @@ func TestFeishuJSONPayload(t *testing.T) {
190190
assert.NoError(t, err)
191191
assert.Equal(t, "[test/repo:test] \r\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) commit message - user1\r\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) commit message - user1", body.Content.Text)
192192
}
193+
194+
func TestFeishuGenSign(t *testing.T) {
195+
assert.Equal(t, "rWZ84lcag1x9aBFhn1gtV4ZN+4gme3pilfQNMk86vKg=", feishuGenSign("a", 1))
196+
}

services/webhook/payloader.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ func newJSONRequest[T any](pc payloadConvertor[T], w *webhook_model.Webhook, t *
9595
if err != nil {
9696
return nil, nil, err
9797
}
98+
return sendHTTPRequest(payload, w, t, withDefaultHeaders)
99+
}
98100

101+
func sendHTTPRequest[T any](payload T, w *webhook_model.Webhook, t *webhook_model.HookTask, withDefaultHeaders bool) (*http.Request, []byte, error) {
99102
body, err := json.MarshalIndent(payload, "", " ")
100103
if err != nil {
101104
return nil, nil, err

0 commit comments

Comments
 (0)