-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.go
More file actions
133 lines (116 loc) · 3.89 KB
/
webhooks.go
File metadata and controls
133 lines (116 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package gitcode
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type Webhook struct {
ID int64 `json:"id"`
URL string `json:"url"`
Events []string `json:"events"`
Active bool `json:"active"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type CreateWebhookOptions struct {
URL string `json:"url"`
Secret string `json:"secret,omitempty"`
Events []string `json:"events,omitempty"`
Active *bool `json:"active,omitempty"`
}
type UpdateWebhookOptions struct {
URL string `json:"url,omitempty"`
Secret string `json:"secret,omitempty"`
Events []string `json:"events,omitempty"`
Active *bool `json:"active,omitempty"`
}
func (c *Client) ListWebhooks(ctx context.Context, owner, repo string) ([]*Webhook, error) {
var hooks []*Webhook
err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/repos/%s/%s/hooks", owner, repo), nil, &hooks)
if err != nil {
return nil, err
}
return hooks, nil
}
func (c *Client) GetWebhook(ctx context.Context, owner, repo string, hookID int64) (*Webhook, error) {
var hook Webhook
err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/repos/%s/%s/hooks/%d", owner, repo, hookID), nil, &hook)
if err != nil {
return nil, err
}
return &hook, nil
}
func (c *Client) CreateWebhook(ctx context.Context, owner, repo string, opts CreateWebhookOptions) (*Webhook, error) {
var hook Webhook
err := c.doRequest(ctx, http.MethodPost, fmt.Sprintf("/repos/%s/%s/hooks", owner, repo), opts, &hook)
if err != nil {
return nil, err
}
return &hook, nil
}
func (c *Client) UpdateWebhook(ctx context.Context, owner, repo string, hookID int64, opts UpdateWebhookOptions) (*Webhook, error) {
var hook Webhook
err := c.doRequest(ctx, http.MethodPatch, fmt.Sprintf("/repos/%s/%s/hooks/%d", owner, repo, hookID), opts, &hook)
if err != nil {
return nil, err
}
return &hook, nil
}
func (c *Client) DeleteWebhook(ctx context.Context, owner, repo string, hookID int64) error {
return c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/repos/%s/%s/hooks/%d", owner, repo, hookID), nil, nil)
}
func (c *Client) TestWebhook(ctx context.Context, owner, repo string, hookID int64) error {
return c.doRequest(ctx, http.MethodPost, fmt.Sprintf("/repos/%s/%s/hooks/%d/tests", owner, repo, hookID), nil, nil)
}
type WebhookEvent struct {
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
Repository *Repository `json:"repository"`
Commits []*Commit `json:"commits"`
Sender *User `json:"sender"`
}
type PullRequestWebhookEvent struct {
Action string `json:"action"`
Number int `json:"number"`
PullRequest *PullRequest `json:"pull_request"`
Repository *Repository `json:"repository"`
Sender *User `json:"sender"`
}
type IssueWebhookEvent struct {
Action string `json:"action"`
Issue *Issue `json:"issue"`
Repository *Repository `json:"repository"`
Sender *User `json:"sender"`
}
type PushEvent struct {
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
Repository *Repository `json:"repository"`
Commits []*Commit `json:"commits"`
Sender *User `json:"sender"`
}
func (c *Client) ParsePushEvent(payload []byte) (*PushEvent, error) {
var event PushEvent
if err := json.Unmarshal(payload, &event); err != nil {
return nil, err
}
return &event, nil
}
func (c *Client) ParsePullRequestEvent(payload []byte) (*PullRequestWebhookEvent, error) {
var event PullRequestWebhookEvent
if err := json.Unmarshal(payload, &event); err != nil {
return nil, err
}
return &event, nil
}
func (c *Client) ParseIssueEvent(payload []byte) (*IssueWebhookEvent, error) {
var event IssueWebhookEvent
if err := json.Unmarshal(payload, &event); err != nil {
return nil, err
}
return &event, nil
}