Skip to content

Commit ffab745

Browse files
committed
Add new event commit status creation and webhook implementation
1 parent b068dbd commit ffab745

File tree

7 files changed

+76
-11
lines changed

7 files changed

+76
-11
lines changed

modules/repository/commits.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func NewPushCommits() *PushCommits {
4242
return &PushCommits{}
4343
}
4444

45-
// toAPIPayloadCommit converts a single PushCommit to an api.PayloadCommit object.
46-
func (pc *PushCommits) toAPIPayloadCommit(ctx context.Context, emailUsers map[string]*user_model.User, repoPath, repoLink string, commit *PushCommit) (*api.PayloadCommit, error) {
45+
// ToAPIPayloadCommit converts a single PushCommit to an api.PayloadCommit object.
46+
func ToAPIPayloadCommit(ctx context.Context, emailUsers map[string]*user_model.User, repoPath, repoLink string, commit *PushCommit) (*api.PayloadCommit, error) {
4747
var err error
4848
authorUsername := ""
4949
author, ok := emailUsers[commit.AuthorEmail]
@@ -105,7 +105,7 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
105105
emailUsers := make(map[string]*user_model.User)
106106

107107
for i, commit := range pc.Commits {
108-
apiCommit, err := pc.toAPIPayloadCommit(ctx, emailUsers, repoPath, repoLink, commit)
108+
apiCommit, err := ToAPIPayloadCommit(ctx, emailUsers, repoPath, repoLink, commit)
109109
if err != nil {
110110
return nil, nil, err
111111
}
@@ -117,7 +117,7 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
117117
}
118118
if pc.HeadCommit != nil && headCommit == nil {
119119
var err error
120-
headCommit, err = pc.toAPIPayloadCommit(ctx, emailUsers, repoPath, repoLink, pc.HeadCommit)
120+
headCommit, err = ToAPIPayloadCommit(ctx, emailUsers, repoPath, repoLink, pc.HeadCommit)
121121
if err != nil {
122122
return nil, nil, err
123123
}

modules/structs/hook.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,6 @@ func (p *ReleasePayload) JSONPayload() ([]byte, error) {
262262
return json.MarshalIndent(p, "", " ")
263263
}
264264

265-
// __________ .__
266-
// \______ \__ __ _____| |__
267-
// | ___/ | \/ ___/ | \
268-
// | | | | /\___ \| Y \
269-
// |____| |____//____ >___| /
270-
// \/ \/
271-
272265
// PushPayload represents a payload information of push event.
273266
type PushPayload struct {
274267
Ref string `json:"ref"`
@@ -509,3 +502,25 @@ type WorkflowDispatchPayload struct {
509502
func (p *WorkflowDispatchPayload) JSONPayload() ([]byte, error) {
510503
return json.MarshalIndent(p, "", " ")
511504
}
505+
506+
// CommitStatusPayload represents a payload information of commit status event.
507+
type CommitStatusPayload struct {
508+
// TODO: add Branches
509+
Commit *PayloadCommit `json:"commit"`
510+
Context string `json:"context"`
511+
CreatedAt time.Time `json:"created_at"`
512+
Description string `json:"description"`
513+
ID int64 `json:"id"`
514+
// Name string `json:"name"`
515+
Repo *Repository `json:"repository"`
516+
Sender *User `json:"sender"`
517+
SHA string `json:"sha"`
518+
State string `json:"state"`
519+
TargetURL string `json:"target_url"`
520+
UpdatedAt *time.Time `json:"updated_at"`
521+
}
522+
523+
// JSONPayload implements Payload
524+
func (p *CommitStatusPayload) JSONPayload() ([]byte, error) {
525+
return json.MarshalIndent(p, "", " ")
526+
}

services/notify/notifier.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package notify
66
import (
77
"context"
88

9+
git_model "code.gitea.io/gitea/models/git"
910
issues_model "code.gitea.io/gitea/models/issues"
1011
packages_model "code.gitea.io/gitea/models/packages"
1112
repo_model "code.gitea.io/gitea/models/repo"
@@ -74,4 +75,6 @@ type Notifier interface {
7475
PackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor)
7576

7677
ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository)
78+
79+
CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, commit *repository.PushCommit, sender *user_model.User, status *git_model.CommitStatus)
7780
}

services/notify/notify.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package notify
66
import (
77
"context"
88

9+
git_model "code.gitea.io/gitea/models/git"
910
issues_model "code.gitea.io/gitea/models/issues"
1011
packages_model "code.gitea.io/gitea/models/packages"
1112
repo_model "code.gitea.io/gitea/models/repo"
@@ -367,3 +368,9 @@ func ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) {
367368
notifier.ChangeDefaultBranch(ctx, repo)
368369
}
369370
}
371+
372+
func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, commit *repository.PushCommit, sender *user_model.User, status *git_model.CommitStatus) {
373+
for _, notifier := range notifiers {
374+
notifier.CreateCommitStatus(ctx, repo, commit, sender, status)
375+
}
376+
}

services/notify/null.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package notify
66
import (
77
"context"
88

9+
git_model "code.gitea.io/gitea/models/git"
910
issues_model "code.gitea.io/gitea/models/issues"
1011
packages_model "code.gitea.io/gitea/models/packages"
1112
repo_model "code.gitea.io/gitea/models/repo"
@@ -208,3 +209,6 @@ func (*NullNotifier) PackageDelete(ctx context.Context, doer *user_model.User, p
208209
// ChangeDefaultBranch places a place holder function
209210
func (*NullNotifier) ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) {
210211
}
212+
213+
func (*NullNotifier) CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, commit *repository.PushCommit, sender *user_model.User, status *git_model.CommitStatus) {
214+
}

services/repository/commitstatus/commitstatus.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import (
1818
"code.gitea.io/gitea/modules/gitrepo"
1919
"code.gitea.io/gitea/modules/json"
2020
"code.gitea.io/gitea/modules/log"
21+
repo_module "code.gitea.io/gitea/modules/repository"
2122
api "code.gitea.io/gitea/modules/structs"
2223
"code.gitea.io/gitea/services/automerge"
24+
"code.gitea.io/gitea/services/notify"
2325
)
2426

2527
func getCacheKey(repoID int64, brancheName string) string {
@@ -114,6 +116,8 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
114116
}
115117
}
116118

119+
notify.CreateCommitStatus(ctx, repo, repo_module.CommitToPushCommit(commit), creator, status)
120+
117121
if status.State.IsSuccess() {
118122
if err := automerge.StartPRCheckAndAutoMergeBySHA(ctx, sha, repo); err != nil {
119123
return fmt.Errorf("MergeScheduledPullRequest[repo_id: %d, user_id: %d, sha: %s]: %w", repo.ID, creator.ID, sha, err)

services/webhook/notifier.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package webhook
66
import (
77
"context"
88

9+
git_model "code.gitea.io/gitea/models/git"
910
issues_model "code.gitea.io/gitea/models/issues"
1011
packages_model "code.gitea.io/gitea/models/packages"
1112
"code.gitea.io/gitea/models/perm"
@@ -15,6 +16,7 @@ import (
1516
"code.gitea.io/gitea/modules/git"
1617
"code.gitea.io/gitea/modules/log"
1718
"code.gitea.io/gitea/modules/repository"
19+
repo_module "code.gitea.io/gitea/modules/repository"
1820
"code.gitea.io/gitea/modules/setting"
1921
api "code.gitea.io/gitea/modules/structs"
2022
webhook_module "code.gitea.io/gitea/modules/webhook"
@@ -861,6 +863,36 @@ func (m *webhookNotifier) SyncPushCommits(ctx context.Context, pusher *user_mode
861863
}
862864
}
863865

866+
func (m *webhookNotifier) CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, commit *repository.PushCommit, sender *user_model.User, status *git_model.CommitStatus) {
867+
apiSender := convert.ToUser(ctx, sender, nil)
868+
apiCommit, err := repo_module.ToAPIPayloadCommit(ctx, map[string]*user_model.User{}, repo.RepoPath(), repo.HTMLURL(), commit)
869+
if err != nil {
870+
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
871+
return
872+
}
873+
874+
payload := api.CommitStatusPayload{
875+
Context: status.Context,
876+
CreatedAt: status.CreatedUnix.AsTime().UTC(),
877+
Description: status.Description,
878+
ID: status.ID,
879+
SHA: commit.Sha1,
880+
State: status.State.String(),
881+
TargetURL: status.TargetURL,
882+
883+
Commit: apiCommit,
884+
Repo: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm.AccessModeOwner}),
885+
Sender: apiSender,
886+
}
887+
if !status.UpdatedUnix.IsZero() {
888+
t := status.UpdatedUnix.AsTime().UTC()
889+
payload.UpdatedAt = &t
890+
}
891+
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventPush, &payload); err != nil {
892+
log.Error("PrepareWebhooks: %v", err)
893+
}
894+
}
895+
864896
func (m *webhookNotifier) SyncCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) {
865897
m.CreateRef(ctx, pusher, repo, refFullName, refID)
866898
}

0 commit comments

Comments
 (0)