Skip to content

Commit e9c9e87

Browse files
committed
Add new event commit status creation and webhook implementation
1 parent 9336286 commit e9c9e87

File tree

7 files changed

+78
-12
lines changed

7 files changed

+78
-12
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
@@ -261,13 +261,6 @@ func (p *ReleasePayload) JSONPayload() ([]byte, error) {
261261
return json.MarshalIndent(p, "", " ")
262262
}
263263

264-
// __________ .__
265-
// \______ \__ __ _____| |__
266-
// | ___/ | \/ ___/ | \
267-
// | | | | /\___ \| Y \
268-
// |____| |____//____ >___| /
269-
// \/ \/
270-
271264
// PushPayload represents a payload information of push event.
272265
type PushPayload struct {
273266
Ref string `json:"ref"`
@@ -494,3 +487,25 @@ type PackagePayload struct {
494487
func (p *PackagePayload) JSONPayload() ([]byte, error) {
495488
return json.MarshalIndent(p, "", " ")
496489
}
490+
491+
// CommitStatusPayload represents a payload information of commit status event.
492+
type CommitStatusPayload struct {
493+
// TODO: add Branches
494+
Commit *PayloadCommit `json:"commit"`
495+
Context string `json:"context"`
496+
CreatedAt time.Time `json:"created_at"`
497+
Description string `json:"description"`
498+
ID int64 `json:"id"`
499+
// Name string `json:"name"`
500+
Repo *Repository `json:"repository"`
501+
Sender *User `json:"sender"`
502+
SHA string `json:"sha"`
503+
State string `json:"state"`
504+
TargetURL string `json:"target_url"`
505+
UpdatedAt *time.Time `json:"updated_at"`
506+
}
507+
508+
// JSONPayload FIXME
509+
func (p *CommitStatusPayload) JSONPayload() ([]byte, error) {
510+
return json.MarshalIndent(p, "", " ")
511+
}

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/files/commit.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import (
1212
repo_model "code.gitea.io/gitea/models/repo"
1313
user_model "code.gitea.io/gitea/models/user"
1414
"code.gitea.io/gitea/modules/git"
15+
repo_module "code.gitea.io/gitea/modules/repository"
1516
"code.gitea.io/gitea/modules/structs"
1617
"code.gitea.io/gitea/services/automerge"
18+
"code.gitea.io/gitea/services/notify"
1719
)
1820

1921
// CreateCommitStatus creates a new CommitStatus given a bunch of parameters
@@ -29,7 +31,8 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
2931
}
3032
defer closer.Close()
3133

32-
if commit, err := gitRepo.GetCommit(sha); err != nil {
34+
commit, err := gitRepo.GetCommit(sha)
35+
if err != nil {
3336
gitRepo.Close()
3437
return fmt.Errorf("GetCommit[%s]: %w", sha, err)
3538
} else if len(sha) != git.SHAFullLength {
@@ -47,6 +50,8 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
4750
return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %w", repo.ID, creator.ID, sha, err)
4851
}
4952

53+
notify.CreateCommitStatus(ctx, repo, repo_module.CommitToPushCommit(commit), creator, status)
54+
5055
if status.State.IsSuccess() {
5156
if err := automerge.MergeScheduledPullRequest(ctx, sha, repo); err != nil {
5257
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"
@@ -851,6 +853,36 @@ func (m *webhookNotifier) SyncPushCommits(ctx context.Context, pusher *user_mode
851853
}
852854
}
853855

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

0 commit comments

Comments
 (0)