forgejo-sdk/repo_hook.go

246 lines
7.2 KiB
Go
Raw Normal View History

2014-11-14 22:07:41 +00:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
2014-11-14 22:07:41 +00:00
import (
"bytes"
"encoding/json"
2014-11-15 04:35:52 +00:00
"errors"
2014-11-14 22:07:41 +00:00
"fmt"
2014-11-16 15:34:34 +00:00
"strings"
2015-08-29 04:49:06 +01:00
"time"
2014-11-14 22:07:41 +00:00
)
2014-11-15 04:35:52 +00:00
var (
ErrInvalidReceiveHook = errors.New("Invalid JSON payload received over webhook")
)
2014-11-14 22:07:41 +00:00
type Hook struct {
2015-08-29 04:49:06 +01:00
ID int64 `json:"id"`
Type string `json:"type"`
URL string `json:"-"`
Config map[string]string `json:"config"`
Events []string `json:"events"`
Active bool `json:"active"`
Updated time.Time `json:"updated_at"`
Created time.Time `json:"created_at"`
2014-11-14 22:07:41 +00:00
}
func (c *Client) ListRepoHooks(user, repo string) ([]*Hook, error) {
hooks := make([]*Hook, 0, 10)
2014-12-13 01:29:51 +00:00
return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks)
2014-11-14 22:07:41 +00:00
}
type CreateHookOption struct {
2014-12-13 01:29:51 +00:00
Type string `json:"type" binding:"Required"`
Config map[string]string `json:"config" binding:"Required"`
2015-08-29 04:49:06 +01:00
Events []string `json:"events"`
2014-11-14 22:07:41 +00:00
Active bool `json:"active"`
}
func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook, error) {
2014-11-14 22:07:41 +00:00
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
2014-11-14 22:07:41 +00:00
}
h := new(Hook)
2016-07-17 01:46:54 +01:00
return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), jsonHeader, bytes.NewReader(body), h)
2014-11-14 22:07:41 +00:00
}
type EditHookOption struct {
Config map[string]string `json:"config"`
2015-08-29 04:49:06 +01:00
Events []string `json:"events"`
2014-12-13 01:29:51 +00:00
Active *bool `json:"active"`
2014-11-14 22:07:41 +00:00
}
func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) error {
body, err := json.Marshal(&opt)
if err != nil {
return err
}
2016-07-17 01:46:54 +01:00
_, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), jsonHeader, bytes.NewReader(body))
return err
}
func (c *Client) DeleteRepoHook(user, repo string, id int64) error {
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil)
2014-11-14 22:07:41 +00:00
return err
}
2014-11-15 04:35:52 +00:00
2015-08-28 16:39:30 +01:00
type Payloader interface {
SetSecret(string)
JSONPayload() ([]byte, error)
}
2016-08-14 12:16:36 +01:00
type PayloadUser struct {
Name string `json:"name"`
Email string `json:"email"`
UserName string `json:"username"`
}
2016-08-14 12:16:36 +01:00
// FIXME: consider use same format as API when commits API are added.
2014-11-15 04:35:52 +00:00
type PayloadCommit struct {
2016-08-14 12:16:36 +01:00
ID string `json:"id"`
Message string `json:"message"`
URL string `json:"url"`
Author *PayloadUser `json:"author"`
Committer *PayloadUser `json:"committer"`
Timestamp time.Time `json:"timestamp"`
2014-11-15 04:35:52 +00:00
}
2016-03-14 03:18:27 +00:00
var (
_ Payloader = &CreatePayload{}
_ Payloader = &PushPayload{}
2016-08-14 11:32:53 +01:00
_ Payloader = &PullRequestPayload{}
2016-03-14 03:18:27 +00:00
)
2015-08-28 16:39:30 +01:00
// _________ __
// \_ ___ \_______ ____ _____ _/ |_ ____
// / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \
// \ \____| | \/\ ___/ / __ \| | \ ___/
// \______ /|__| \___ >____ /__| \___ >
// \/ \/ \/ \/
type CreatePayload struct {
2016-08-14 12:16:36 +01:00
Secret string `json:"secret"`
Ref string `json:"ref"`
RefType string `json:"ref_type"`
Repo *Repository `json:"repository"`
Sender *User `json:"sender"`
2015-08-28 16:39:30 +01:00
}
func (p *CreatePayload) SetSecret(secret string) {
p.Secret = secret
}
func (p *CreatePayload) JSONPayload() ([]byte, error) {
2016-08-14 11:32:53 +01:00
return json.MarshalIndent(p, "", " ")
2015-08-28 16:39:30 +01:00
}
// ParseCreateHook parses create event hook content.
func ParseCreateHook(raw []byte) (*CreatePayload, error) {
hook := new(CreatePayload)
if err := json.Unmarshal(raw, hook); err != nil {
return nil, err
}
// it is possible the JSON was parsed, however,
// was not from Gogs (maybe was from Bitbucket)
// So we'll check to be sure certain key fields
// were populated
switch {
case hook.Repo == nil:
return nil, ErrInvalidReceiveHook
case len(hook.Ref) == 0:
return nil, ErrInvalidReceiveHook
}
return hook, nil
}
// __________ .__
// \______ \__ __ _____| |__
// | ___/ | \/ ___/ | \
// | | | | /\___ \| Y \
// |____| |____//____ >___| /
// \/ \/
// PushPayload represents a payload information of push event.
type PushPayload struct {
2014-11-15 04:35:52 +00:00
Secret string `json:"secret"`
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
2016-08-14 12:16:36 +01:00
CompareURL string `json:"compare_url"`
2015-08-28 16:39:30 +01:00
Commits []*PayloadCommit `json:"commits"`
2016-08-14 12:16:36 +01:00
Repo *Repository `json:"repository"`
Pusher *User `json:"pusher"`
Sender *User `json:"sender"`
2015-08-28 16:39:30 +01:00
}
func (p *PushPayload) SetSecret(secret string) {
p.Secret = secret
2014-11-15 04:35:52 +00:00
}
2015-08-28 16:39:30 +01:00
func (p *PushPayload) JSONPayload() ([]byte, error) {
2016-08-14 11:32:53 +01:00
return json.MarshalIndent(p, "", " ")
2015-08-28 16:39:30 +01:00
}
// ParsePushHook parses push event hook content.
func ParsePushHook(raw []byte) (*PushPayload, error) {
hook := new(PushPayload)
2014-11-15 04:35:52 +00:00
if err := json.Unmarshal(raw, hook); err != nil {
return nil, err
}
2015-08-28 16:39:30 +01:00
2014-11-15 04:35:52 +00:00
switch {
case hook.Repo == nil:
return nil, ErrInvalidReceiveHook
case len(hook.Ref) == 0:
return nil, ErrInvalidReceiveHook
}
return hook, nil
}
2014-11-16 15:34:34 +00:00
// Branch returns branch name from a payload
2015-08-28 16:39:30 +01:00
func (p *PushPayload) Branch() string {
return strings.Replace(p.Ref, "refs/heads/", "", -1)
2014-11-16 15:34:34 +00:00
}
2016-08-14 11:32:53 +01:00
// .___
// | | ______ ________ __ ____
// | |/ ___// ___/ | \_/ __ \
// | |\___ \ \___ \| | /\ ___/
// |___/____ >____ >____/ \___ >
// \/ \/ \/
type HookIssueAction string
const (
HOOK_ISSUE_OPENED HookIssueAction = "opened"
HOOK_ISSUE_CLOSED HookIssueAction = "closed"
HOOK_ISSUE_REOPENED HookIssueAction = "reopened"
HOOK_ISSUE_EDITED HookIssueAction = "edited"
HOOK_ISSUE_ASSIGNED HookIssueAction = "assigned"
HOOK_ISSUE_UNASSIGNED HookIssueAction = "unassigned"
HOOK_ISSUE_LABEL_UPDATED HookIssueAction = "label_updated"
HOOK_ISSUE_LABEL_CLEARED HookIssueAction = "label_cleared"
HOOK_ISSUE_SYNCHRONIZED HookIssueAction = "synchronized"
)
type ChangesFromPayload struct {
From string `json:"from"`
}
type ChangesPayload struct {
Title *ChangesFromPayload `json:"title,omitempty"`
Body *ChangesFromPayload `json:"body,omitempty"`
}
// __________ .__ .__ __________ __
// \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
// | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
// | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
// |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
// \/ \/ |__| \/ \/
// PullRequestPayload represents a payload information of pull request event.
type PullRequestPayload struct {
Secret string `json:"secret"`
Action HookIssueAction `json:"action"`
Index int64 `json:"number"`
Changes *ChangesPayload `json:"changes,omitempty"`
PullRequest *PullRequest `json:"pull_request"`
Repository *Repository `json:"repository"`
Sender *User `json:"sender"`
}
func (p *PullRequestPayload) SetSecret(secret string) {
p.Secret = secret
}
func (p *PullRequestPayload) JSONPayload() ([]byte, error) {
return json.MarshalIndent(p, "", " ")
}