Skip to content

Commit bf1970d

Browse files
lunnylafriks
andauthored
Improve push update options (#10105)
* Improve push update options * fix test * More refactor and fix lint * fix lint * Fix lint Co-authored-by: Lauris BH <[email protected]>
1 parent 70aa629 commit bf1970d

File tree

5 files changed

+192
-152
lines changed

5 files changed

+192
-152
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,6 @@ issues:
9595
- linters:
9696
- misspell
9797
text: '`Unknwon` is a misspelling of `Unknown`'
98+
- path: models/update.go
99+
linters:
100+
- unused

modules/repofiles/action.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"encoding/json"
99
"fmt"
1010
"html"
11-
"strings"
1211

1312
"code.gitea.io/gitea/models"
1413
"code.gitea.io/gitea/modules/git"
@@ -151,12 +150,9 @@ func UpdateIssuesCommit(doer *models.User, repo *models.Repository, commits []*r
151150

152151
// CommitRepoActionOptions represent options of a new commit action.
153152
type CommitRepoActionOptions struct {
154-
PusherName string
153+
PushUpdateOptions
154+
155155
RepoOwnerID int64
156-
RepoName string
157-
RefFullName string
158-
OldCommitID string
159-
NewCommitID string
160156
Commits *repository.PushCommits
161157
}
162158

@@ -192,7 +188,7 @@ func CommitRepoAction(optsList ...*CommitRepoActionOptions) error {
192188
refName := git.RefEndName(opts.RefFullName)
193189

194190
// Change default branch and empty status only if pushed ref is non-empty branch.
195-
if repo.IsEmpty && opts.NewCommitID != git.EmptySHA && strings.HasPrefix(opts.RefFullName, git.BranchPrefix) {
191+
if repo.IsEmpty && opts.IsBranch() && !opts.IsDelRef() {
196192
repo.DefaultBranch = refName
197193
repo.IsEmpty = false
198194
if refName != "master" {
@@ -210,24 +206,21 @@ func CommitRepoAction(optsList ...*CommitRepoActionOptions) error {
210206
}
211207
}
212208

213-
isNewBranch := false
214209
opType := models.ActionCommitRepo
215210

216211
// Check it's tag push or branch.
217-
if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
212+
if opts.IsTag() {
218213
opType = models.ActionPushTag
219-
if opts.NewCommitID == git.EmptySHA {
214+
if opts.IsDelRef() {
220215
opType = models.ActionDeleteTag
221216
}
222217
opts.Commits = &repository.PushCommits{}
223-
} else if opts.NewCommitID == git.EmptySHA {
218+
} else if opts.IsDelRef() {
224219
opType = models.ActionDeleteBranch
225220
opts.Commits = &repository.PushCommits{}
226221
} else {
227222
// if not the first commit, set the compare URL.
228-
if opts.OldCommitID == git.EmptySHA {
229-
isNewBranch = true
230-
} else {
223+
if !opts.IsNewRef() {
231224
opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
232225
}
233226

@@ -259,7 +252,7 @@ func CommitRepoAction(optsList ...*CommitRepoActionOptions) error {
259252
var isHookEventPush = true
260253
switch opType {
261254
case models.ActionCommitRepo: // Push
262-
if isNewBranch {
255+
if opts.IsNewBranch() {
263256
notification.NotifyCreateRef(pusher, repo, "branch", opts.RefFullName)
264257
}
265258
case models.ActionDeleteBranch: // Delete Branch

modules/repofiles/action_test.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ func TestCommitRepoAction(t *testing.T) {
3232
userID: 2,
3333
repositoryID: 16,
3434
commitRepoActionOptions: CommitRepoActionOptions{
35-
RefFullName: "refName",
36-
OldCommitID: "oldCommitID",
37-
NewCommitID: "newCommitID",
35+
PushUpdateOptions: PushUpdateOptions{
36+
RefFullName: "refName",
37+
OldCommitID: "oldCommitID",
38+
NewCommitID: "newCommitID",
39+
},
3840
Commits: &repository.PushCommits{
3941
Commits: []*repository.PushCommit{
4042
{
@@ -66,10 +68,12 @@ func TestCommitRepoAction(t *testing.T) {
6668
userID: 2,
6769
repositoryID: 1,
6870
commitRepoActionOptions: CommitRepoActionOptions{
69-
RefFullName: git.TagPrefix + "v1.1",
70-
OldCommitID: git.EmptySHA,
71-
NewCommitID: "newCommitID",
72-
Commits: &repository.PushCommits{},
71+
PushUpdateOptions: PushUpdateOptions{
72+
RefFullName: git.TagPrefix + "v1.1",
73+
OldCommitID: git.EmptySHA,
74+
NewCommitID: "newCommitID",
75+
},
76+
Commits: &repository.PushCommits{},
7377
},
7478
action: models.Action{
7579
OpType: models.ActionPushTag,
@@ -80,10 +84,12 @@ func TestCommitRepoAction(t *testing.T) {
8084
userID: 2,
8185
repositoryID: 1,
8286
commitRepoActionOptions: CommitRepoActionOptions{
83-
RefFullName: git.TagPrefix + "v1.1",
84-
OldCommitID: "oldCommitID",
85-
NewCommitID: git.EmptySHA,
86-
Commits: &repository.PushCommits{},
87+
PushUpdateOptions: PushUpdateOptions{
88+
RefFullName: git.TagPrefix + "v1.1",
89+
OldCommitID: "oldCommitID",
90+
NewCommitID: git.EmptySHA,
91+
},
92+
Commits: &repository.PushCommits{},
8793
},
8894
action: models.Action{
8995
OpType: models.ActionDeleteTag,
@@ -94,10 +100,12 @@ func TestCommitRepoAction(t *testing.T) {
94100
userID: 2,
95101
repositoryID: 1,
96102
commitRepoActionOptions: CommitRepoActionOptions{
97-
RefFullName: git.BranchPrefix + "feature/1",
98-
OldCommitID: "oldCommitID",
99-
NewCommitID: git.EmptySHA,
100-
Commits: &repository.PushCommits{},
103+
PushUpdateOptions: PushUpdateOptions{
104+
RefFullName: git.BranchPrefix + "feature/1",
105+
OldCommitID: "oldCommitID",
106+
NewCommitID: git.EmptySHA,
107+
},
108+
Commits: &repository.PushCommits{},
101109
},
102110
action: models.Action{
103111
OpType: models.ActionDeleteBranch,

0 commit comments

Comments
 (0)