2016-11-07 16:53:13 +03:00
// Copyright 2014 The Gogs Authors. All rights reserved.
2017-02-23 02:53:33 +03:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2016-11-07 16:53:13 +03:00
2019-05-11 13:21:34 +03:00
package structs
2016-11-07 16:53:13 +03:00
import (
"errors"
"strings"
"time"
2021-03-02 00:08:10 +03:00
2021-07-24 19:03:58 +03:00
"code.gitea.io/gitea/modules/json"
2016-11-07 16:53:13 +03:00
)
2022-01-20 20:46:10 +03:00
// ErrInvalidReceiveHook FIXME
var ErrInvalidReceiveHook = errors . New ( "Invalid JSON payload received over webhook" )
2016-11-07 16:53:13 +03:00
2016-11-29 11:09:17 +03:00
// Hook a hook is a web hook when one repository changed
2016-11-07 16:53:13 +03:00
type Hook struct {
2022-11-03 21:23:20 +03:00
ID int64 ` json:"id" `
Type string ` json:"type" `
2023-08-21 14:43:52 +03:00
BranchFilter string ` json:"branch_filter" `
2022-11-03 21:23:20 +03:00
URL string ` json:"-" `
Config map [ string ] string ` json:"config" `
Events [ ] string ` json:"events" `
AuthorizationHeader string ` json:"authorization_header" `
Active bool ` json:"active" `
2017-11-13 10:02:25 +03:00
// swagger:strfmt date-time
2018-03-06 04:22:16 +03:00
Updated time . Time ` json:"updated_at" `
2017-11-13 10:02:25 +03:00
// swagger:strfmt date-time
2018-03-06 04:22:16 +03:00
Created time . Time ` json:"created_at" `
2016-11-07 16:53:13 +03:00
}
2017-08-21 14:13:47 +03:00
// HookList represents a list of API hook.
type HookList [ ] * Hook
2019-12-31 07:11:15 +03:00
// CreateHookOptionConfig has all config options in it
// required are "content_type" and "url" Required
type CreateHookOptionConfig map [ string ] string
2016-11-29 11:09:17 +03:00
// CreateHookOption options when create a hook
2016-11-07 16:53:13 +03:00
type CreateHookOption struct {
2017-11-13 10:02:25 +03:00
// required: true
2022-01-23 16:46:30 +03:00
// enum: dingtalk,discord,gitea,gogs,msteams,slack,telegram,feishu,wechatwork,packagist
2017-08-21 14:13:47 +03:00
Type string ` json:"type" binding:"Required" `
2017-11-13 10:02:25 +03:00
// required: true
2022-11-03 21:23:20 +03:00
Config CreateHookOptionConfig ` json:"config" binding:"Required" `
Events [ ] string ` json:"events" `
BranchFilter string ` json:"branch_filter" binding:"GlobPattern" `
AuthorizationHeader string ` json:"authorization_header" `
2017-11-13 10:02:25 +03:00
// default: false
2017-08-21 14:13:47 +03:00
Active bool ` json:"active" `
2016-11-07 16:53:13 +03:00
}
2016-11-29 11:09:17 +03:00
// EditHookOption options when modify one hook
2016-11-07 16:53:13 +03:00
type EditHookOption struct {
2022-11-03 21:23:20 +03:00
Config map [ string ] string ` json:"config" `
Events [ ] string ` json:"events" `
BranchFilter string ` json:"branch_filter" binding:"GlobPattern" `
AuthorizationHeader string ` json:"authorization_header" `
Active * bool ` json:"active" `
2016-11-07 16:53:13 +03:00
}
2016-11-29 11:09:17 +03:00
// Payloader payload is some part of one hook
2016-11-07 16:53:13 +03:00
type Payloader interface {
JSONPayload ( ) ( [ ] byte , error )
}
2017-11-13 10:02:25 +03:00
// PayloadUser represents the author or committer of a commit
2016-11-07 16:53:13 +03:00
type PayloadUser struct {
2017-11-13 10:02:25 +03:00
// Full name of the commit author
2018-03-06 04:22:16 +03:00
Name string ` json:"name" `
2017-11-13 10:02:25 +03:00
// swagger:strfmt email
2016-11-07 16:53:13 +03:00
Email string ` json:"email" `
UserName string ` json:"username" `
}
2017-11-13 10:02:25 +03:00
// FIXME: consider using same format as API when commits API are added.
// applies to PayloadCommit and PayloadCommitVerification
// PayloadCommit represents a commit
2016-11-07 16:53:13 +03:00
type PayloadCommit struct {
2017-11-13 10:02:25 +03:00
// sha1 hash of the commit
2017-03-22 13:43:54 +03:00
ID string ` json:"id" `
Message string ` json:"message" `
URL string ` json:"url" `
Author * PayloadUser ` json:"author" `
Committer * PayloadUser ` json:"committer" `
Verification * PayloadCommitVerification ` json:"verification" `
2017-11-13 10:02:25 +03:00
// swagger:strfmt date-time
2018-03-06 04:22:16 +03:00
Timestamp time . Time ` json:"timestamp" `
2019-01-30 19:33:00 +03:00
Added [ ] string ` json:"added" `
Removed [ ] string ` json:"removed" `
Modified [ ] string ` json:"modified" `
2017-03-22 13:43:54 +03:00
}
2017-11-13 10:02:25 +03:00
// PayloadCommitVerification represents the GPG verification of a commit
2017-03-22 13:43:54 +03:00
type PayloadCommitVerification struct {
2019-10-16 16:42:42 +03:00
Verified bool ` json:"verified" `
Reason string ` json:"reason" `
Signature string ` json:"signature" `
Signer * PayloadUser ` json:"signer" `
Payload string ` json:"payload" `
2016-11-07 16:53:13 +03:00
}
var (
_ Payloader = & CreatePayload { }
2018-05-16 17:01:55 +03:00
_ Payloader = & DeletePayload { }
_ Payloader = & ForkPayload { }
2016-11-07 16:53:13 +03:00
_ Payloader = & PushPayload { }
2017-02-23 02:53:33 +03:00
_ Payloader = & IssuePayload { }
2018-05-16 17:01:55 +03:00
_ Payloader = & IssueCommentPayload { }
2016-11-07 16:53:13 +03:00
_ Payloader = & PullRequestPayload { }
2018-05-16 17:01:55 +03:00
_ Payloader = & RepositoryPayload { }
_ Payloader = & ReleasePayload { }
2022-03-30 11:42:47 +03:00
_ Payloader = & PackagePayload { }
2016-11-07 16:53:13 +03:00
)
// _________ __
// \_ ___ \_______ ____ _____ _/ |_ ____
// / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \
// \ \____| | \/\ ___/ / __ \| | \ ___/
// \______ /|__| \___ >____ /__| \___ >
// \/ \/ \/ \/
2016-11-29 11:09:17 +03:00
// CreatePayload FIXME
2016-11-07 16:53:13 +03:00
type CreatePayload struct {
2016-11-29 11:09:17 +03:00
Sha string ` json:"sha" `
2016-11-07 16:53:13 +03:00
Ref string ` json:"ref" `
RefType string ` json:"ref_type" `
Repo * Repository ` json:"repository" `
Sender * User ` json:"sender" `
}
2016-11-29 11:09:17 +03:00
// JSONPayload return payload information
2016-11-07 16:53:13 +03:00
func ( p * CreatePayload ) JSONPayload ( ) ( [ ] byte , error ) {
return json . MarshalIndent ( p , "" , " " )
}
// 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
}
2018-05-16 17:01:55 +03:00
// ________ .__ __
// \______ \ ____ | | _____/ |_ ____
// | | \_/ __ \| | _/ __ \ __\/ __ \
// | ` \ ___/| |_\ ___/| | \ ___/
// /_______ /\___ >____/\___ >__| \___ >
// \/ \/ \/ \/
// PusherType define the type to push
type PusherType string
// describe all the PusherTypes
const (
PusherTypeUser PusherType = "user"
)
// DeletePayload represents delete payload
type DeletePayload struct {
Ref string ` json:"ref" `
RefType string ` json:"ref_type" `
PusherType PusherType ` json:"pusher_type" `
Repo * Repository ` json:"repository" `
Sender * User ` json:"sender" `
}
// JSONPayload implements Payload
func ( p * DeletePayload ) JSONPayload ( ) ( [ ] byte , error ) {
return json . MarshalIndent ( p , "" , " " )
}
// ___________ __
// \_ _____/__________| | __
// | __)/ _ \_ __ \ |/ /
// | \( <_> ) | \/ <
// \___ / \____/|__| |__|_ \
// \/ \/
// ForkPayload represents fork payload
type ForkPayload struct {
Forkee * Repository ` json:"forkee" `
Repo * Repository ` json:"repository" `
Sender * User ` json:"sender" `
}
// JSONPayload implements Payload
func ( p * ForkPayload ) JSONPayload ( ) ( [ ] byte , error ) {
return json . MarshalIndent ( p , "" , " " )
}
// HookIssueCommentAction defines hook issue comment action
type HookIssueCommentAction string
// all issue comment actions
const (
HookIssueCommentCreated HookIssueCommentAction = "created"
HookIssueCommentEdited HookIssueCommentAction = "edited"
HookIssueCommentDeleted HookIssueCommentAction = "deleted"
)
// IssueCommentPayload represents a payload information of issue comment event.
type IssueCommentPayload struct {
Action HookIssueCommentAction ` json:"action" `
Issue * Issue ` json:"issue" `
Comment * Comment ` json:"comment" `
Changes * ChangesPayload ` json:"changes,omitempty" `
Repository * Repository ` json:"repository" `
Sender * User ` json:"sender" `
2019-10-19 01:42:04 +03:00
IsPull bool ` json:"is_pull" `
2018-05-16 17:01:55 +03:00
}
// JSONPayload implements Payload
func ( p * IssueCommentPayload ) JSONPayload ( ) ( [ ] byte , error ) {
return json . MarshalIndent ( p , "" , " " )
}
// __________ .__
// \______ \ ____ | | ____ _____ ______ ____
// | _// __ \| | _/ __ \\__ \ / ___// __ \
// | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
// |____|_ /\___ >____/\___ >____ /____ >\___ >
// \/ \/ \/ \/ \/ \/
// HookReleaseAction defines hook release action type
type HookReleaseAction string
// all release actions
const (
HookReleasePublished HookReleaseAction = "published"
2018-05-21 05:28:29 +03:00
HookReleaseUpdated HookReleaseAction = "updated"
HookReleaseDeleted HookReleaseAction = "deleted"
2018-05-16 17:01:55 +03:00
)
// ReleasePayload represents a payload information of release event.
type ReleasePayload struct {
Action HookReleaseAction ` json:"action" `
Release * Release ` json:"release" `
Repository * Repository ` json:"repository" `
Sender * User ` json:"sender" `
}
// JSONPayload implements Payload
func ( p * ReleasePayload ) JSONPayload ( ) ( [ ] byte , error ) {
return json . MarshalIndent ( p , "" , " " )
}
2016-11-07 16:53:13 +03:00
// __________ .__
// \______ \__ __ _____| |__
// | ___/ | \/ ___/ | \
// | | | | /\___ \| Y \
// |____| |____//____ >___| /
// \/ \/
// PushPayload represents a payload information of push event.
type PushPayload struct {
2022-10-16 19:22:34 +03:00
Ref string ` json:"ref" `
Before string ` json:"before" `
After string ` json:"after" `
CompareURL string ` json:"compare_url" `
Commits [ ] * PayloadCommit ` json:"commits" `
TotalCommits int ` json:"total_commits" `
HeadCommit * PayloadCommit ` json:"head_commit" `
Repo * Repository ` json:"repository" `
Pusher * User ` json:"pusher" `
Sender * User ` json:"sender" `
2016-11-07 16:53:13 +03:00
}
2016-11-29 11:09:17 +03:00
// JSONPayload FIXME
2016-11-07 16:53:13 +03:00
func ( p * PushPayload ) JSONPayload ( ) ( [ ] byte , error ) {
return json . MarshalIndent ( p , "" , " " )
}
// ParsePushHook parses push event hook content.
func ParsePushHook ( raw [ ] byte ) ( * PushPayload , error ) {
hook := new ( PushPayload )
if err := json . Unmarshal ( raw , hook ) ; err != nil {
return nil , err
}
switch {
case hook . Repo == nil :
return nil , ErrInvalidReceiveHook
case len ( hook . Ref ) == 0 :
return nil , ErrInvalidReceiveHook
}
return hook , nil
}
// Branch returns branch name from a payload
func ( p * PushPayload ) Branch ( ) string {
2020-10-11 23:27:20 +03:00
return strings . ReplaceAll ( p . Ref , "refs/heads/" , "" )
2016-11-07 16:53:13 +03:00
}
// .___
// | | ______ ________ __ ____
// | |/ ___// ___/ | \_/ __ \
// | |\___ \ \___ \| | /\ ___/
// |___/____ >____ >____/ \___ >
// \/ \/ \/
2016-11-29 11:09:17 +03:00
// HookIssueAction FIXME
2016-11-07 16:53:13 +03:00
type HookIssueAction string
const (
2016-11-29 11:09:17 +03:00
// HookIssueOpened opened
HookIssueOpened HookIssueAction = "opened"
// HookIssueClosed closed
HookIssueClosed HookIssueAction = "closed"
// HookIssueReOpened reopened
HookIssueReOpened HookIssueAction = "reopened"
// HookIssueEdited edited
HookIssueEdited HookIssueAction = "edited"
// HookIssueAssigned assigned
HookIssueAssigned HookIssueAction = "assigned"
// HookIssueUnassigned unassigned
HookIssueUnassigned HookIssueAction = "unassigned"
// HookIssueLabelUpdated label_updated
2016-11-07 18:37:32 +03:00
HookIssueLabelUpdated HookIssueAction = "label_updated"
2016-11-29 11:09:17 +03:00
// HookIssueLabelCleared label_cleared
2016-11-07 18:37:32 +03:00
HookIssueLabelCleared HookIssueAction = "label_cleared"
2016-11-29 11:09:17 +03:00
// HookIssueSynchronized synchronized
HookIssueSynchronized HookIssueAction = "synchronized"
2017-02-23 02:53:33 +03:00
// HookIssueMilestoned is an issue action for when a milestone is set on an issue.
HookIssueMilestoned HookIssueAction = "milestoned"
// HookIssueDemilestoned is an issue action for when a milestone is cleared on an issue.
HookIssueDemilestoned HookIssueAction = "demilestoned"
2020-03-06 08:10:48 +03:00
// HookIssueReviewed is an issue action for when a pull request is reviewed
HookIssueReviewed HookIssueAction = "reviewed"
2023-05-25 05:06:27 +03:00
// HookIssueReviewRequested is an issue action for when a reviewer is requested for a pull request.
HookIssueReviewRequested HookIssueAction = "review_requested"
// HookIssueReviewRequestRemoved is an issue action for removing a review request to someone on a pull request.
HookIssueReviewRequestRemoved HookIssueAction = "review_request_removed"
2016-11-07 16:53:13 +03:00
)
2017-02-23 02:53:33 +03:00
// IssuePayload represents the payload information that is sent along with an issue event.
type IssuePayload struct {
Action HookIssueAction ` json:"action" `
Index int64 ` json:"number" `
Changes * ChangesPayload ` json:"changes,omitempty" `
Issue * Issue ` json:"issue" `
Repository * Repository ` json:"repository" `
Sender * User ` json:"sender" `
2023-01-25 07:47:53 +03:00
CommitID string ` json:"commit_id" `
2017-02-23 02:53:33 +03:00
}
// JSONPayload encodes the IssuePayload to JSON, with an indentation of two spaces.
func ( p * IssuePayload ) JSONPayload ( ) ( [ ] byte , error ) {
return json . MarshalIndent ( p , "" , " " )
}
2016-11-29 11:09:17 +03:00
// ChangesFromPayload FIXME
2016-11-07 16:53:13 +03:00
type ChangesFromPayload struct {
From string ` json:"from" `
}
2019-12-16 09:20:25 +03:00
// ChangesPayload represents the payload information of issue change
2016-11-07 16:53:13 +03:00
type ChangesPayload struct {
Title * ChangesFromPayload ` json:"title,omitempty" `
Body * ChangesFromPayload ` json:"body,omitempty" `
2019-12-16 09:20:25 +03:00
Ref * ChangesFromPayload ` json:"ref,omitempty" `
2016-11-07 16:53:13 +03:00
}
// __________ .__ .__ __________ __
// \______ \__ __| | | | \______ \ ____ ________ __ ____ _______/ |_
// | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
// | | | | / |_| |__ | | \ ___< <_| | | /\ ___/ \___ \ | |
// |____| |____/|____/____/ |____|_ /\___ >__ |____/ \___ >____ > |__|
// \/ \/ |__| \/ \/
// PullRequestPayload represents a payload information of pull request event.
type PullRequestPayload struct {
2023-05-25 05:06:27 +03:00
Action HookIssueAction ` json:"action" `
Index int64 ` json:"number" `
Changes * ChangesPayload ` json:"changes,omitempty" `
PullRequest * PullRequest ` json:"pull_request" `
RequestedReviewer * User ` json:"requested_reviewer" `
Repository * Repository ` json:"repository" `
Sender * User ` json:"sender" `
CommitID string ` json:"commit_id" `
Review * ReviewPayload ` json:"review" `
2016-11-07 16:53:13 +03:00
}
2016-11-29 11:09:17 +03:00
// JSONPayload FIXME
2016-11-07 16:53:13 +03:00
func ( p * PullRequestPayload ) JSONPayload ( ) ( [ ] byte , error ) {
return json . MarshalIndent ( p , "" , " " )
}
2017-05-05 05:55:54 +03:00
2019-10-19 01:42:04 +03:00
// ReviewPayload FIXME
type ReviewPayload struct {
Type string ` json:"type" `
Content string ` json:"content" `
}
2022-09-04 22:54:23 +03:00
// __ __.__ __ .__
// / \ / \__| | _|__|
// \ \/\/ / | |/ / |
// \ /| | <| |
// \__/\ / |__|__|_ \__|
// \/ \/
// HookWikiAction an action that happens to a wiki page
type HookWikiAction string
const (
// HookWikiCreated created
HookWikiCreated HookWikiAction = "created"
// HookWikiEdited edited
HookWikiEdited HookWikiAction = "edited"
// HookWikiDeleted deleted
HookWikiDeleted HookWikiAction = "deleted"
)
// WikiPayload payload for repository webhooks
type WikiPayload struct {
Action HookWikiAction ` json:"action" `
Repository * Repository ` json:"repository" `
Sender * User ` json:"sender" `
Page string ` json:"page" `
Comment string ` json:"comment" `
}
// JSONPayload JSON representation of the payload
func ( p * WikiPayload ) JSONPayload ( ) ( [ ] byte , error ) {
return json . MarshalIndent ( p , "" , " " )
}
2017-05-05 05:55:54 +03:00
//__________ .__ __
//\______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__.
// | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | |
// | | \ ___/| |_> > <_> )___ \| || | ( <_> ) | \/\___ |
// |____|_ /\___ > __/ \____/____ >__||__| \____/|__| / ____|
// \/ \/|__| \/ \/
// HookRepoAction an action that happens to a repo
type HookRepoAction string
const (
// HookRepoCreated created
HookRepoCreated HookRepoAction = "created"
// HookRepoDeleted deleted
HookRepoDeleted HookRepoAction = "deleted"
)
// RepositoryPayload payload for repository webhooks
type RepositoryPayload struct {
Action HookRepoAction ` json:"action" `
Repository * Repository ` json:"repository" `
Organization * User ` json:"organization" `
Sender * User ` json:"sender" `
}
// JSONPayload JSON representation of the payload
func ( p * RepositoryPayload ) JSONPayload ( ) ( [ ] byte , error ) {
return json . MarshalIndent ( p , "" , " " )
}
2022-03-30 11:42:47 +03:00
// HookPackageAction an action that happens to a package
type HookPackageAction string
const (
// HookPackageCreated created
HookPackageCreated HookPackageAction = "created"
// HookPackageDeleted deleted
HookPackageDeleted HookPackageAction = "deleted"
)
// PackagePayload represents a package payload
type PackagePayload struct {
Action HookPackageAction ` json:"action" `
Repository * Repository ` json:"repository" `
Package * Package ` json:"package" `
Organization * User ` json:"organization" `
Sender * User ` json:"sender" `
}
// JSONPayload implements Payload
func ( p * PackagePayload ) JSONPayload ( ) ( [ ] byte , error ) {
return json . MarshalIndent ( p , "" , " " )
}