Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
f33571fe
Commit
f33571fe
authored
Jun 05, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: update webhook request payload
parent
d1599759
Changes
6
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
296 additions
and
200 deletions
+296
-200
apidocs.swagger.yaml
docs/apidocs.swagger.yaml
+4
-4
webhook.go
plugin/webhook/webhook.go
+16
-39
webhook_service.proto
proto/api/v1/webhook_service.proto
+15
-2
webhook_service.pb.go
proto/gen/api/v1/webhook_service.pb.go
+247
-137
memo_service.go
server/router/api/v1/memo_service.go
+7
-11
webhook_service.go
server/router/api/v1/webhook_service.go
+7
-7
No files found.
docs/apidocs.swagger.yaml
View file @
f33571fe
...
@@ -662,10 +662,10 @@ paths:
...
@@ -662,10 +662,10 @@ paths:
creatorId
:
creatorId
:
type
:
integer
type
:
integer
format
:
int32
format
:
int32
create
d
Time
:
createTime
:
type
:
string
type
:
string
format
:
date-time
format
:
date-time
update
d
Time
:
updateTime
:
type
:
string
type
:
string
format
:
date-time
format
:
date-time
rowStatus
:
rowStatus
:
...
@@ -3117,10 +3117,10 @@ definitions:
...
@@ -3117,10 +3117,10 @@ definitions:
creatorId
:
creatorId
:
type
:
integer
type
:
integer
format
:
int32
format
:
int32
create
d
Time
:
createTime
:
type
:
string
type
:
string
format
:
date-time
format
:
date-time
update
d
Time
:
updateTime
:
type
:
string
type
:
string
format
:
date-time
format
:
date-time
rowStatus
:
rowStatus
:
...
...
plugin/webhook/webhook.go
View file @
f33571fe
...
@@ -8,6 +8,9 @@ import (
...
@@ -8,6 +8,9 @@ import (
"time"
"time"
"github.com/pkg/errors"
"github.com/pkg/errors"
"google.golang.org/protobuf/encoding/protojson"
v1pb
"github.com/usememos/memos/proto/gen/api/v1"
)
)
var
(
var
(
...
@@ -15,45 +18,16 @@ var (
...
@@ -15,45 +18,16 @@ var (
timeout
=
30
*
time
.
Second
timeout
=
30
*
time
.
Second
)
)
type
Memo
struct
{
// The name of the memo.
// Format: memos/{id}
// id is the system generated id.
Name
string
// The name of the creator.
// Format: users/{id}
Creator
string
// The raw content.
Content
string
}
// WebhookPayload is the payload of webhook request.
// nolint
type
WebhookPayload
struct
{
URL
string
`json:"url"`
ActivityType
string
`json:"activityType"`
CreatorID
int32
`json:"creatorId"`
CreatedTs
int64
`json:"createdTs"`
Memo
*
Memo
`json:"memo"`
}
// WebhookResponse is the response of webhook request.
// nolint
type
WebhookResponse
struct
{
Code
int
`json:"code"`
Message
string
`json:"message"`
}
// Post posts the message to webhook endpoint.
// Post posts the message to webhook endpoint.
func
Post
(
payload
Webhook
Payload
)
error
{
func
Post
(
requestPayload
*
v1pb
.
WebhookRequest
Payload
)
error
{
body
,
err
:=
json
.
Marshal
(
&
p
ayload
)
body
,
err
:=
protojson
.
Marshal
(
requestP
ayload
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"failed to marshal webhook request to %s"
,
payload
.
URL
)
return
errors
.
Wrapf
(
err
,
"failed to marshal webhook request to %s"
,
requestPayload
.
Url
)
}
}
req
,
err
:=
http
.
NewRequest
(
"POST"
,
payload
.
URL
,
bytes
.
NewBuffer
(
body
))
req
,
err
:=
http
.
NewRequest
(
"POST"
,
requestPayload
.
Url
,
bytes
.
NewBuffer
(
body
))
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"failed to construct webhook request to %s"
,
payload
.
URL
)
return
errors
.
Wrapf
(
err
,
"failed to construct webhook request to %s"
,
requestPayload
.
Url
)
}
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
...
@@ -62,22 +36,25 @@ func Post(payload WebhookPayload) error {
...
@@ -62,22 +36,25 @@ func Post(payload WebhookPayload) error {
}
}
resp
,
err
:=
client
.
Do
(
req
)
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"failed to post webhook to %s"
,
payload
.
URL
)
return
errors
.
Wrapf
(
err
,
"failed to post webhook to %s"
,
requestPayload
.
Url
)
}
}
b
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
b
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"failed to read webhook response from %s"
,
payload
.
URL
)
return
errors
.
Wrapf
(
err
,
"failed to read webhook response from %s"
,
requestPayload
.
Url
)
}
}
defer
resp
.
Body
.
Close
()
defer
resp
.
Body
.
Close
()
if
resp
.
StatusCode
<
200
||
resp
.
StatusCode
>
299
{
if
resp
.
StatusCode
<
200
||
resp
.
StatusCode
>
299
{
return
errors
.
Errorf
(
"failed to post webhook %s, status code: %d, response body: %s"
,
payload
.
URL
,
resp
.
StatusCode
,
b
)
return
errors
.
Errorf
(
"failed to post webhook %s, status code: %d, response body: %s"
,
requestPayload
.
Url
,
resp
.
StatusCode
,
b
)
}
}
response
:=
&
WebhookResponse
{}
response
:=
&
struct
{
Code
int
`json:"code"`
Message
string
`json:"message"`
}{}
if
err
:=
json
.
Unmarshal
(
b
,
response
);
err
!=
nil
{
if
err
:=
json
.
Unmarshal
(
b
,
response
);
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"failed to unmarshal webhook response from %s"
,
payload
.
URL
)
return
errors
.
Wrapf
(
err
,
"failed to unmarshal webhook response from %s"
,
requestPayload
.
Url
)
}
}
if
response
.
Code
!=
0
{
if
response
.
Code
!=
0
{
...
...
proto/api/v1/webhook_service.proto
View file @
f33571fe
...
@@ -3,6 +3,7 @@ syntax = "proto3";
...
@@ -3,6 +3,7 @@ syntax = "proto3";
package
memos
.
api.v1
;
package
memos
.
api.v1
;
import
"api/v1/common.proto"
;
import
"api/v1/common.proto"
;
import
"api/v1/memo_service.proto"
;
import
"google/api/annotations.proto"
;
import
"google/api/annotations.proto"
;
import
"google/api/client.proto"
;
import
"google/api/client.proto"
;
import
"google/protobuf/empty.proto"
;
import
"google/protobuf/empty.proto"
;
...
@@ -48,9 +49,9 @@ message Webhook {
...
@@ -48,9 +49,9 @@ message Webhook {
int32
creator_id
=
2
;
int32
creator_id
=
2
;
google.protobuf.Timestamp
create
d
_time
=
3
;
google.protobuf.Timestamp
create_time
=
3
;
google.protobuf.Timestamp
update
d
_time
=
4
;
google.protobuf.Timestamp
update_time
=
4
;
RowStatus
row_status
=
5
;
RowStatus
row_status
=
5
;
...
@@ -86,3 +87,15 @@ message UpdateWebhookRequest {
...
@@ -86,3 +87,15 @@ message UpdateWebhookRequest {
message
DeleteWebhookRequest
{
message
DeleteWebhookRequest
{
int32
id
=
1
;
int32
id
=
1
;
}
}
message
WebhookRequestPayload
{
string
url
=
1
;
string
activity_type
=
2
;
int32
creator_id
=
3
;
google.protobuf.Timestamp
create_time
=
4
;
Memo
memo
=
5
;
}
proto/gen/api/v1/webhook_service.pb.go
View file @
f33571fe
This diff is collapsed.
Click to expand it.
server/router/api/v1/memo_service.go
View file @
f33571fe
...
@@ -1243,26 +1243,22 @@ func (s *APIV1Service) dispatchMemoRelatedWebhook(ctx context.Context, memo *v1p
...
@@ -1243,26 +1243,22 @@ func (s *APIV1Service) dispatchMemoRelatedWebhook(ctx context.Context, memo *v1p
return
errors
.
Wrap
(
err
,
"failed to convert memo to webhook payload"
)
return
errors
.
Wrap
(
err
,
"failed to convert memo to webhook payload"
)
}
}
payload
.
ActivityType
=
activityType
payload
.
ActivityType
=
activityType
payload
.
U
RL
=
hook
.
URL
payload
.
U
rl
=
hook
.
URL
if
err
:=
webhook
.
Post
(
*
payload
);
err
!=
nil
{
if
err
:=
webhook
.
Post
(
payload
);
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to post webhook"
)
return
errors
.
Wrap
(
err
,
"failed to post webhook"
)
}
}
}
}
return
nil
return
nil
}
}
func
convertMemoToWebhookPayload
(
memo
*
v1pb
.
Memo
)
(
*
webhook
.
Webhook
Payload
,
error
)
{
func
convertMemoToWebhookPayload
(
memo
*
v1pb
.
Memo
)
(
*
v1pb
.
WebhookRequest
Payload
,
error
)
{
creatorID
,
err
:=
ExtractUserIDFromName
(
memo
.
Creator
)
creatorID
,
err
:=
ExtractUserIDFromName
(
memo
.
Creator
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"invalid memo creator"
)
return
nil
,
errors
.
Wrap
(
err
,
"invalid memo creator"
)
}
}
return
&
webhook
.
WebhookPayload
{
return
&
v1pb
.
WebhookRequestPayload
{
CreatorID
:
creatorID
,
CreatorId
:
creatorID
,
CreatedTs
:
time
.
Now
()
.
Unix
(),
CreateTime
:
timestamppb
.
New
(
time
.
Now
()),
Memo
:
&
webhook
.
Memo
{
Memo
:
memo
,
Name
:
memo
.
Name
,
Creator
:
memo
.
Creator
,
Content
:
memo
.
Content
,
},
},
nil
},
nil
}
}
server/router/api/v1/webhook_service.go
View file @
f33571fe
...
@@ -103,12 +103,12 @@ func (s *APIV1Service) DeleteWebhook(ctx context.Context, request *v1pb.DeleteWe
...
@@ -103,12 +103,12 @@ func (s *APIV1Service) DeleteWebhook(ctx context.Context, request *v1pb.DeleteWe
func
convertWebhookFromStore
(
webhook
*
store
.
Webhook
)
*
v1pb
.
Webhook
{
func
convertWebhookFromStore
(
webhook
*
store
.
Webhook
)
*
v1pb
.
Webhook
{
return
&
v1pb
.
Webhook
{
return
&
v1pb
.
Webhook
{
Id
:
webhook
.
ID
,
Id
:
webhook
.
ID
,
Create
d
Time
:
timestamppb
.
New
(
time
.
Unix
(
webhook
.
CreatedTs
,
0
)),
CreateTime
:
timestamppb
.
New
(
time
.
Unix
(
webhook
.
CreatedTs
,
0
)),
Update
d
Time
:
timestamppb
.
New
(
time
.
Unix
(
webhook
.
UpdatedTs
,
0
)),
UpdateTime
:
timestamppb
.
New
(
time
.
Unix
(
webhook
.
UpdatedTs
,
0
)),
RowStatus
:
convertRowStatusFromStore
(
webhook
.
RowStatus
),
RowStatus
:
convertRowStatusFromStore
(
webhook
.
RowStatus
),
CreatorId
:
webhook
.
CreatorID
,
CreatorId
:
webhook
.
CreatorID
,
Name
:
webhook
.
Name
,
Name
:
webhook
.
Name
,
Url
:
webhook
.
URL
,
Url
:
webhook
.
URL
,
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment