Skip to content

Commit 7c0037d

Browse files
committed
Merge branch 'lunny/commit_status_webhook' into lunny/commit_status_webhook2
2 parents b068dbd + 6ec27c4 commit 7c0037d

File tree

9 files changed

+82
-22
lines changed

9 files changed

+82
-22
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: 24 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,27 @@ 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 per https://docs.github.com/en/webhooks/webhook-events-and-payloads#status
509+
Commit *PayloadCommit `json:"commit"`
510+
Context string `json:"context"`
511+
// swagger:strfmt date-time
512+
CreatedAt time.Time `json:"created_at"`
513+
Description string `json:"description"`
514+
ID int64 `json:"id"`
515+
// Name string `json:"name"`
516+
Repo *Repository `json:"repository"`
517+
Sender *User `json:"sender"`
518+
SHA string `json:"sha"`
519+
State string `json:"state"`
520+
TargetURL string `json:"target_url"`
521+
// swagger:strfmt date-time
522+
UpdatedAt *time.Time `json:"updated_at"`
523+
}
524+
525+
// JSONPayload implements Payload
526+
func (p *CommitStatusPayload) JSONPayload() ([]byte, error) {
527+
return json.MarshalIndent(p, "", " ")
528+
}

modules/webhook/type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
HookEventRelease HookEventType = "release"
3333
HookEventPackage HookEventType = "package"
3434
HookEventSchedule HookEventType = "schedule"
35+
HookEventStatus HookEventType = "status"
3536
)
3637

3738
// Event returns the HookEventType as an event string

services/actions/commit_status.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,16 @@ func createCommitStatus(ctx context.Context, job *actions_model.ActionRunJob) er
128128
if err != nil {
129129
return fmt.Errorf("HashTypeInterfaceFromHashString: %w", err)
130130
}
131-
if err := commitstatus_service.CreateCommitStatus(ctx, repo, creator, commitID.String(), &git_model.CommitStatus{
131+
status := git_model.CommitStatus{
132132
SHA: sha,
133133
TargetURL: fmt.Sprintf("%s/jobs/%d", run.Link(), index),
134134
Description: description,
135135
Context: ctxname,
136136
CreatorID: creator.ID,
137137
State: state,
138-
}); err != nil {
139-
return fmt.Errorf("NewCommitStatus: %w", err)
140138
}
141139

142-
return nil
140+
return commitstatus_service.CreateCommitStatus(ctx, repo, creator, commitID.String(), &status)
143141
}
144142

145143
func toCommitStatus(status actions_model.Status) api.CommitStatusState {

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: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ import (
1414
repo_model "code.gitea.io/gitea/models/repo"
1515
user_model "code.gitea.io/gitea/models/user"
1616
"code.gitea.io/gitea/modules/cache"
17-
"code.gitea.io/gitea/modules/git"
1817
"code.gitea.io/gitea/modules/gitrepo"
1918
"code.gitea.io/gitea/modules/json"
2019
"code.gitea.io/gitea/modules/log"
20+
"code.gitea.io/gitea/modules/repository"
2121
api "code.gitea.io/gitea/modules/structs"
2222
"code.gitea.io/gitea/services/automerge"
23+
"code.gitea.io/gitea/services/notify"
2324
)
2425

2526
func getCacheKey(repoID int64, brancheName string) string {
@@ -77,16 +78,10 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
7778
}
7879
defer closer.Close()
7980

80-
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
81-
8281
commit, err := gitRepo.GetCommit(sha)
8382
if err != nil {
8483
return fmt.Errorf("GetCommit[%s]: %w", sha, err)
8584
}
86-
if len(sha) != objectFormat.FullLength() {
87-
// use complete commit sha
88-
sha = commit.ID.String()
89-
}
9085

9186
if err := db.WithTx(ctx, func(ctx context.Context) error {
9287
if err := git_model.NewCommitStatus(ctx, git_model.NewCommitStatusOptions{
@@ -103,6 +98,10 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
10398
return err
10499
}
105100

101+
pushCommit := repository.CommitToPushCommit(commit)
102+
103+
notify.CreateCommitStatus(ctx, repo, pushCommit, creator, status)
104+
106105
defaultBranchCommit, err := gitRepo.GetBranchCommit(repo.DefaultBranch)
107106
if err != nil {
108107
return fmt.Errorf("GetBranchCommit[%s]: %w", repo.DefaultBranch, err)

services/webhook/notifier.go

Lines changed: 31 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"
@@ -861,6 +862,36 @@ func (m *webhookNotifier) SyncPushCommits(ctx context.Context, pusher *user_mode
861862
}
862863
}
863864

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

0 commit comments

Comments
 (0)