Skip to content

Commit 4df2ed2

Browse files
lunnysapk
andauthored
Refactor: Move PushUpdateOptions (#13363)
Co-authored-by: Antoine GIRARD <[email protected]>
1 parent dd12384 commit 4df2ed2

File tree

15 files changed

+169
-146
lines changed

15 files changed

+169
-146
lines changed

modules/notification/action/action.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ func (*actionNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *mode
275275
}
276276
}
277277

278-
func (a *actionNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) {
278+
func (a *actionNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
279279
data, err := json.Marshal(commits)
280280
if err != nil {
281281
log.Error("json.Marshal: %v", err)
@@ -289,7 +289,7 @@ func (a *actionNotifier) NotifySyncPushCommits(pusher *models.User, repo *models
289289
RepoID: repo.ID,
290290
Repo: repo,
291291
IsPrivate: repo.IsPrivate,
292-
RefName: refName,
292+
RefName: opts.RefFullName,
293293
Content: string(data),
294294
}); err != nil {
295295
log.Error("notifyWatchers: %v", err)

modules/notification/base/notifier.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ type Notifier interface {
4848
NotifyUpdateRelease(doer *models.User, rel *models.Release)
4949
NotifyDeleteRelease(doer *models.User, rel *models.Release)
5050

51-
NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits)
51+
NotifyPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits)
5252
NotifyCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string)
5353
NotifyDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string)
5454

55-
NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits)
55+
NotifySyncPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits)
5656
NotifySyncCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string)
5757
NotifySyncDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string)
5858
}

modules/notification/base/null.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (*NullNotifier) NotifyMigrateRepository(doer *models.User, u *models.User,
128128
}
129129

130130
// NotifyPushCommits notifies commits pushed to notifiers
131-
func (*NullNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) {
131+
func (*NullNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
132132
}
133133

134134
// NotifyCreateRef notifies branch or tag creation to notifiers
@@ -148,7 +148,7 @@ func (*NullNotifier) NotifyTransferRepository(doer *models.User, repo *models.Re
148148
}
149149

150150
// NotifySyncPushCommits places a place holder function
151-
func (*NullNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) {
151+
func (*NullNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
152152
}
153153

154154
// NotifySyncCreateRef places a place holder function

modules/notification/indexer/indexer.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,17 @@ func (r *indexerNotifier) NotifyMigrateRepository(doer *models.User, u *models.U
123123
}
124124
}
125125

126-
func (r *indexerNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) {
127-
if setting.Indexer.RepoIndexerEnabled && refName == git.BranchPrefix+repo.DefaultBranch {
126+
func (r *indexerNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
127+
if setting.Indexer.RepoIndexerEnabled && opts.RefFullName == git.BranchPrefix+repo.DefaultBranch {
128128
code_indexer.UpdateRepoIndexer(repo)
129129
}
130130
if err := stats_indexer.UpdateRepoIndexer(repo); err != nil {
131131
log.Error("stats_indexer.UpdateRepoIndexer(%d) failed: %v", repo.ID, err)
132132
}
133133
}
134134

135-
func (r *indexerNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) {
136-
if setting.Indexer.RepoIndexerEnabled && refName == git.BranchPrefix+repo.DefaultBranch {
135+
func (r *indexerNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
136+
if setting.Indexer.RepoIndexerEnabled && opts.RefFullName == git.BranchPrefix+repo.DefaultBranch {
137137
code_indexer.UpdateRepoIndexer(repo)
138138
}
139139
if err := stats_indexer.UpdateRepoIndexer(repo); err != nil {

modules/notification/notification.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ func NotifyRenameRepository(doer *models.User, repo *models.Repository, oldName
236236
}
237237

238238
// NotifyPushCommits notifies commits pushed to notifiers
239-
func NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) {
239+
func NotifyPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
240240
for _, notifier := range notifiers {
241-
notifier.NotifyPushCommits(pusher, repo, refName, oldCommitID, newCommitID, commits)
241+
notifier.NotifyPushCommits(pusher, repo, opts, commits)
242242
}
243243
}
244244

@@ -257,9 +257,9 @@ func NotifyDeleteRef(pusher *models.User, repo *models.Repository, refType, refF
257257
}
258258

259259
// NotifySyncPushCommits notifies commits pushed to notifiers
260-
func NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) {
260+
func NotifySyncPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
261261
for _, notifier := range notifiers {
262-
notifier.NotifySyncPushCommits(pusher, repo, refName, oldCommitID, newCommitID, commits)
262+
notifier.NotifySyncPushCommits(pusher, repo, opts, commits)
263263
}
264264
}
265265

modules/notification/webhook/webhook.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *models.User, issue *m
548548
}
549549
}
550550

551-
func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) {
551+
func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
552552
apiPusher := convert.ToUser(pusher, false, false)
553553
apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
554554
if err != nil {
@@ -557,9 +557,9 @@ func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Re
557557
}
558558

559559
if err := webhook_module.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
560-
Ref: refName,
561-
Before: oldCommitID,
562-
After: newCommitID,
560+
Ref: opts.RefFullName,
561+
Before: opts.OldCommitID,
562+
After: opts.NewCommitID,
563563
CompareURL: setting.AppURL + commits.CompareURL,
564564
Commits: apiCommits,
565565
Repo: repo.APIFormat(models.AccessModeOwner),
@@ -776,7 +776,7 @@ func (m *webhookNotifier) NotifyDeleteRelease(doer *models.User, rel *models.Rel
776776
sendReleaseHook(doer, rel, api.HookReleaseDeleted)
777777
}
778778

779-
func (m *webhookNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) {
779+
func (m *webhookNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
780780
apiPusher := convert.ToUser(pusher, false, false)
781781
apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
782782
if err != nil {
@@ -785,9 +785,9 @@ func (m *webhookNotifier) NotifySyncPushCommits(pusher *models.User, repo *model
785785
}
786786

787787
if err := webhook_module.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
788-
Ref: refName,
789-
Before: oldCommitID,
790-
After: newCommitID,
788+
Ref: opts.RefFullName,
789+
Before: opts.OldCommitID,
790+
After: opts.NewCommitID,
791791
CompareURL: setting.AppURL + commits.CompareURL,
792792
Commits: apiCommits,
793793
Repo: repo.APIFormat(models.AccessModeOwner),

modules/repository/push.go

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package repository
6+
7+
import (
8+
"path/filepath"
9+
"strings"
10+
11+
"code.gitea.io/gitea/modules/git"
12+
"code.gitea.io/gitea/modules/setting"
13+
)
14+
15+
// PushUpdateOptions defines the push update options
16+
type PushUpdateOptions struct {
17+
PusherID int64
18+
PusherName string
19+
RepoUserName string
20+
RepoName string
21+
RefFullName string // branch, tag or other name to push
22+
OldCommitID string
23+
NewCommitID string
24+
}
25+
26+
// IsNewRef return true if it's a first-time push to a branch, tag or etc.
27+
func (opts PushUpdateOptions) IsNewRef() bool {
28+
return opts.OldCommitID == git.EmptySHA
29+
}
30+
31+
// IsDelRef return true if it's a deletion to a branch or tag
32+
func (opts PushUpdateOptions) IsDelRef() bool {
33+
return opts.NewCommitID == git.EmptySHA
34+
}
35+
36+
// IsUpdateRef return true if it's an update operation
37+
func (opts PushUpdateOptions) IsUpdateRef() bool {
38+
return !opts.IsNewRef() && !opts.IsDelRef()
39+
}
40+
41+
// IsTag return true if it's an operation to a tag
42+
func (opts PushUpdateOptions) IsTag() bool {
43+
return strings.HasPrefix(opts.RefFullName, git.TagPrefix)
44+
}
45+
46+
// IsNewTag return true if it's a creation to a tag
47+
func (opts PushUpdateOptions) IsNewTag() bool {
48+
return opts.IsTag() && opts.IsNewRef()
49+
}
50+
51+
// IsDelTag return true if it's a deletion to a tag
52+
func (opts PushUpdateOptions) IsDelTag() bool {
53+
return opts.IsTag() && opts.IsDelRef()
54+
}
55+
56+
// IsBranch return true if it's a push to branch
57+
func (opts PushUpdateOptions) IsBranch() bool {
58+
return strings.HasPrefix(opts.RefFullName, git.BranchPrefix)
59+
}
60+
61+
// IsNewBranch return true if it's the first-time push to a branch
62+
func (opts PushUpdateOptions) IsNewBranch() bool {
63+
return opts.IsBranch() && opts.IsNewRef()
64+
}
65+
66+
// IsUpdateBranch return true if it's not the first push to a branch
67+
func (opts PushUpdateOptions) IsUpdateBranch() bool {
68+
return opts.IsBranch() && opts.IsUpdateRef()
69+
}
70+
71+
// IsDelBranch return true if it's a deletion to a branch
72+
func (opts PushUpdateOptions) IsDelBranch() bool {
73+
return opts.IsBranch() && opts.IsDelRef()
74+
}
75+
76+
// TagName returns simple tag name if it's an operation to a tag
77+
func (opts PushUpdateOptions) TagName() string {
78+
return opts.RefFullName[len(git.TagPrefix):]
79+
}
80+
81+
// BranchName returns simple branch name if it's an operation to branch
82+
func (opts PushUpdateOptions) BranchName() string {
83+
return opts.RefFullName[len(git.BranchPrefix):]
84+
}
85+
86+
// RefName returns simple name for ref
87+
func (opts PushUpdateOptions) RefName() string {
88+
if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
89+
return opts.RefFullName[len(git.TagPrefix):]
90+
} else if strings.HasPrefix(opts.RefFullName, git.BranchPrefix) {
91+
return opts.RefFullName[len(git.BranchPrefix):]
92+
}
93+
return ""
94+
}
95+
96+
// RepoFullName returns repo full name
97+
func (opts PushUpdateOptions) RepoFullName() string {
98+
return opts.RepoUserName + "/" + opts.RepoName
99+
}
100+
101+
// IsForcePush detect if a push is a force push
102+
func IsForcePush(opts *PushUpdateOptions) (bool, error) {
103+
if !opts.IsUpdateBranch() {
104+
return false, nil
105+
}
106+
107+
output, err := git.NewCommand("rev-list", "--max-count=1", opts.OldCommitID, "^"+opts.NewCommitID).
108+
RunInDir(filepath.Join(setting.RepoRootPath, opts.RepoUserName, opts.RepoName))
109+
if err != nil {
110+
return false, err
111+
} else if len(output) > 0 {
112+
return true, nil
113+
}
114+
return false, nil
115+
}

routers/api/v1/repo/branch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func DeleteBranch(ctx *context.APIContext) {
162162

163163
// Don't return error below this
164164
if err := repo_service.PushUpdate(
165-
&repo_service.PushUpdateOptions{
165+
&repo_module.PushUpdateOptions{
166166
RefFullName: git.BranchPrefix + ctx.Repo.BranchName,
167167
OldCommitID: c.ID.String(),
168168
NewCommitID: git.EmptySHA,

routers/private/hook.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/git"
1919
"code.gitea.io/gitea/modules/log"
2020
"code.gitea.io/gitea/modules/private"
21+
repo_module "code.gitea.io/gitea/modules/repository"
2122
"code.gitea.io/gitea/modules/setting"
2223
"code.gitea.io/gitea/modules/util"
2324
pull_service "code.gitea.io/gitea/services/pull"
@@ -375,7 +376,7 @@ func HookPostReceive(ctx *macaron.Context, opts private.HookOptions) {
375376
repoName := ctx.Params(":repo")
376377

377378
var repo *models.Repository
378-
updates := make([]*repo_service.PushUpdateOptions, 0, len(opts.OldCommitIDs))
379+
updates := make([]*repo_module.PushUpdateOptions, 0, len(opts.OldCommitIDs))
379380
wasEmpty := false
380381

381382
for i := range opts.OldCommitIDs {
@@ -402,7 +403,7 @@ func HookPostReceive(ctx *macaron.Context, opts private.HookOptions) {
402403
wasEmpty = repo.IsEmpty
403404
}
404405

405-
option := repo_service.PushUpdateOptions{
406+
option := repo_module.PushUpdateOptions{
406407
RefFullName: refFullName,
407408
OldCommitID: opts.OldCommitIDs[i],
408409
NewCommitID: opts.NewCommitIDs[i],

routers/repo/branch.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func RestoreBranchPost(ctx *context.Context) {
120120

121121
// Don't return error below this
122122
if err := repo_service.PushUpdate(
123-
&repo_service.PushUpdateOptions{
123+
&repo_module.PushUpdateOptions{
124124
RefFullName: git.BranchPrefix + deletedBranch.Name,
125125
OldCommitID: git.EmptySHA,
126126
NewCommitID: deletedBranch.Commit,
@@ -157,7 +157,7 @@ func deleteBranch(ctx *context.Context, branchName string) error {
157157

158158
// Don't return error below this
159159
if err := repo_service.PushUpdate(
160-
&repo_service.PushUpdateOptions{
160+
&repo_module.PushUpdateOptions{
161161
RefFullName: git.BranchPrefix + branchName,
162162
OldCommitID: commit.ID.String(),
163163
NewCommitID: git.EmptySHA,

routers/repo/pull.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"code.gitea.io/gitea/modules/git"
2323
"code.gitea.io/gitea/modules/log"
2424
"code.gitea.io/gitea/modules/notification"
25+
repo_module "code.gitea.io/gitea/modules/repository"
2526
"code.gitea.io/gitea/modules/setting"
2627
"code.gitea.io/gitea/modules/structs"
2728
"code.gitea.io/gitea/modules/upload"
@@ -1185,7 +1186,7 @@ func CleanUpPullRequest(ctx *context.Context) {
11851186
}
11861187

11871188
if err := repo_service.PushUpdate(
1188-
&repo_service.PushUpdateOptions{
1189+
&repo_module.PushUpdateOptions{
11891190
RefFullName: git.BranchPrefix + pr.HeadBranch,
11901191
OldCommitID: branchCommitID,
11911192
NewCommitID: git.EmptySHA,

services/mirror/mirror.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,11 @@ func syncMirror(repoID string) {
472472

473473
theCommits.CompareURL = m.Repo.ComposeCompareURL(oldCommitID, newCommitID)
474474

475-
notification.NotifySyncPushCommits(m.Repo.MustOwner(), m.Repo, result.refName, oldCommitID, newCommitID, theCommits)
475+
notification.NotifySyncPushCommits(m.Repo.MustOwner(), m.Repo, &repo_module.PushUpdateOptions{
476+
RefFullName: result.refName,
477+
OldCommitID: oldCommitID,
478+
NewCommitID: newCommitID,
479+
}, theCommits)
476480
}
477481
log.Trace("SyncMirrors [repo: %-v]: done notifying updated branches/tags - now updating last commit time", m.Repo)
478482

services/release/release.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ func createTag(gitRepo *git.Repository, rel *models.Release) error {
4343
return err
4444
}
4545
notification.NotifyPushCommits(
46-
rel.Publisher, rel.Repo, git.TagPrefix+rel.TagName,
47-
git.EmptySHA, commit.ID.String(), repository.NewPushCommits())
46+
rel.Publisher, rel.Repo,
47+
&repository.PushUpdateOptions{
48+
RefFullName: git.TagPrefix + rel.TagName,
49+
OldCommitID: git.EmptySHA,
50+
NewCommitID: commit.ID.String(),
51+
}, repository.NewPushCommits())
4852
notification.NotifyCreateRef(rel.Publisher, rel.Repo, "tag", git.TagPrefix+rel.TagName)
4953
rel.CreatedUnix = timeutil.TimeStampNow()
5054
}

0 commit comments

Comments
 (0)