diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2024-11-06 22:41:49 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-07 06:41:49 +0000 |
commit | 331e878e81d57235a53199383087c7649797a887 (patch) | |
tree | 736485bdb5b49956022587506315b652076cff2f /modules | |
parent | 145e26698791221b007c7dd460fb506cb0237235 (diff) | |
download | gitea-331e878e81d57235a53199383087c7649797a887.tar.gz gitea-331e878e81d57235a53199383087c7649797a887.zip |
Add new event commit status creation and webhook implementation (#27151)
This PR introduces a new event which is similar as Github's. When a new
commit status submitted, the event will be trigged. That means, now we
can receive all feedback from CI/CD system in webhooks or other notify
systems.
ref:
https://docs.github.com/en/webhooks/webhook-events-and-payloads#status
Fix #20749
Diffstat (limited to 'modules')
-rw-r--r-- | modules/repository/commits.go | 8 | ||||
-rw-r--r-- | modules/structs/hook.go | 30 | ||||
-rw-r--r-- | modules/webhook/type.go | 1 |
3 files changed, 28 insertions, 11 deletions
diff --git a/modules/repository/commits.go b/modules/repository/commits.go index ede60429a1..6e4b75d5ca 100644 --- a/modules/repository/commits.go +++ b/modules/repository/commits.go @@ -42,8 +42,8 @@ func NewPushCommits() *PushCommits { return &PushCommits{} } -// toAPIPayloadCommit converts a single PushCommit to an api.PayloadCommit object. -func (pc *PushCommits) toAPIPayloadCommit(ctx context.Context, emailUsers map[string]*user_model.User, repoPath, repoLink string, commit *PushCommit) (*api.PayloadCommit, error) { +// ToAPIPayloadCommit converts a single PushCommit to an api.PayloadCommit object. +func ToAPIPayloadCommit(ctx context.Context, emailUsers map[string]*user_model.User, repoPath, repoLink string, commit *PushCommit) (*api.PayloadCommit, error) { var err error authorUsername := "" author, ok := emailUsers[commit.AuthorEmail] @@ -105,7 +105,7 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi emailUsers := make(map[string]*user_model.User) for i, commit := range pc.Commits { - apiCommit, err := pc.toAPIPayloadCommit(ctx, emailUsers, repoPath, repoLink, commit) + apiCommit, err := ToAPIPayloadCommit(ctx, emailUsers, repoPath, repoLink, commit) if err != nil { return nil, nil, err } @@ -117,7 +117,7 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi } if pc.HeadCommit != nil && headCommit == nil { var err error - headCommit, err = pc.toAPIPayloadCommit(ctx, emailUsers, repoPath, repoLink, pc.HeadCommit) + headCommit, err = ToAPIPayloadCommit(ctx, emailUsers, repoPath, repoLink, pc.HeadCommit) if err != nil { return nil, nil, err } diff --git a/modules/structs/hook.go b/modules/structs/hook.go index db8b20e7e5..ce5742e5c7 100644 --- a/modules/structs/hook.go +++ b/modules/structs/hook.go @@ -262,13 +262,6 @@ func (p *ReleasePayload) JSONPayload() ([]byte, error) { return json.MarshalIndent(p, "", " ") } -// __________ .__ -// \______ \__ __ _____| |__ -// | ___/ | \/ ___/ | \ -// | | | | /\___ \| Y \ -// |____| |____//____ >___| / -// \/ \/ - // PushPayload represents a payload information of push event. type PushPayload struct { Ref string `json:"ref"` @@ -509,3 +502,26 @@ type WorkflowDispatchPayload struct { func (p *WorkflowDispatchPayload) JSONPayload() ([]byte, error) { return json.MarshalIndent(p, "", " ") } + +// CommitStatusPayload represents a payload information of commit status event. +type CommitStatusPayload struct { + // TODO: add Branches per https://docs.github.com/en/webhooks/webhook-events-and-payloads#status + Commit *PayloadCommit `json:"commit"` + Context string `json:"context"` + // swagger:strfmt date-time + CreatedAt time.Time `json:"created_at"` + Description string `json:"description"` + ID int64 `json:"id"` + Repo *Repository `json:"repository"` + Sender *User `json:"sender"` + SHA string `json:"sha"` + State string `json:"state"` + TargetURL string `json:"target_url"` + // swagger:strfmt date-time + UpdatedAt *time.Time `json:"updated_at"` +} + +// JSONPayload implements Payload +func (p *CommitStatusPayload) JSONPayload() ([]byte, error) { + return json.MarshalIndent(p, "", " ") +} diff --git a/modules/webhook/type.go b/modules/webhook/type.go index 0013691c02..fbec889272 100644 --- a/modules/webhook/type.go +++ b/modules/webhook/type.go @@ -32,6 +32,7 @@ const ( HookEventRelease HookEventType = "release" HookEventPackage HookEventType = "package" HookEventSchedule HookEventType = "schedule" + HookEventStatus HookEventType = "status" ) // Event returns the HookEventType as an event string |