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 <lauris@nix.lv>
This commit is contained in:
2020-02-03 04:27:34 +08:00
committed by GitHub
parent 70aa629ae7
commit bf1970d0bd
5 changed files with 193 additions and 153 deletions

View File

@ -95,3 +95,6 @@ issues:
- linters: - linters:
- misspell - misspell
text: '`Unknwon` is a misspelling of `Unknown`' text: '`Unknwon` is a misspelling of `Unknown`'
- path: models/update.go
linters:
- unused

View File

@ -8,7 +8,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"html" "html"
"strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
@ -151,12 +150,9 @@ func UpdateIssuesCommit(doer *models.User, repo *models.Repository, commits []*r
// CommitRepoActionOptions represent options of a new commit action. // CommitRepoActionOptions represent options of a new commit action.
type CommitRepoActionOptions struct { type CommitRepoActionOptions struct {
PusherName string PushUpdateOptions
RepoOwnerID int64 RepoOwnerID int64
RepoName string
RefFullName string
OldCommitID string
NewCommitID string
Commits *repository.PushCommits Commits *repository.PushCommits
} }
@ -192,7 +188,7 @@ func CommitRepoAction(optsList ...*CommitRepoActionOptions) error {
refName := git.RefEndName(opts.RefFullName) refName := git.RefEndName(opts.RefFullName)
// Change default branch and empty status only if pushed ref is non-empty branch. // Change default branch and empty status only if pushed ref is non-empty branch.
if repo.IsEmpty && opts.NewCommitID != git.EmptySHA && strings.HasPrefix(opts.RefFullName, git.BranchPrefix) { if repo.IsEmpty && opts.IsBranch() && !opts.IsDelRef() {
repo.DefaultBranch = refName repo.DefaultBranch = refName
repo.IsEmpty = false repo.IsEmpty = false
if refName != "master" { if refName != "master" {
@ -210,24 +206,21 @@ func CommitRepoAction(optsList ...*CommitRepoActionOptions) error {
} }
} }
isNewBranch := false
opType := models.ActionCommitRepo opType := models.ActionCommitRepo
// Check it's tag push or branch. // Check it's tag push or branch.
if strings.HasPrefix(opts.RefFullName, git.TagPrefix) { if opts.IsTag() {
opType = models.ActionPushTag opType = models.ActionPushTag
if opts.NewCommitID == git.EmptySHA { if opts.IsDelRef() {
opType = models.ActionDeleteTag opType = models.ActionDeleteTag
} }
opts.Commits = &repository.PushCommits{} opts.Commits = &repository.PushCommits{}
} else if opts.NewCommitID == git.EmptySHA { } else if opts.IsDelRef() {
opType = models.ActionDeleteBranch opType = models.ActionDeleteBranch
opts.Commits = &repository.PushCommits{} opts.Commits = &repository.PushCommits{}
} else { } else {
// if not the first commit, set the compare URL. // if not the first commit, set the compare URL.
if opts.OldCommitID == git.EmptySHA { if !opts.IsNewRef() {
isNewBranch = true
} else {
opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID) opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
} }
@ -259,7 +252,7 @@ func CommitRepoAction(optsList ...*CommitRepoActionOptions) error {
var isHookEventPush = true var isHookEventPush = true
switch opType { switch opType {
case models.ActionCommitRepo: // Push case models.ActionCommitRepo: // Push
if isNewBranch { if opts.IsNewBranch() {
notification.NotifyCreateRef(pusher, repo, "branch", opts.RefFullName) notification.NotifyCreateRef(pusher, repo, "branch", opts.RefFullName)
} }
case models.ActionDeleteBranch: // Delete Branch case models.ActionDeleteBranch: // Delete Branch

View File

@ -32,9 +32,11 @@ func TestCommitRepoAction(t *testing.T) {
userID: 2, userID: 2,
repositoryID: 16, repositoryID: 16,
commitRepoActionOptions: CommitRepoActionOptions{ commitRepoActionOptions: CommitRepoActionOptions{
RefFullName: "refName", PushUpdateOptions: PushUpdateOptions{
OldCommitID: "oldCommitID", RefFullName: "refName",
NewCommitID: "newCommitID", OldCommitID: "oldCommitID",
NewCommitID: "newCommitID",
},
Commits: &repository.PushCommits{ Commits: &repository.PushCommits{
Commits: []*repository.PushCommit{ Commits: []*repository.PushCommit{
{ {
@ -66,10 +68,12 @@ func TestCommitRepoAction(t *testing.T) {
userID: 2, userID: 2,
repositoryID: 1, repositoryID: 1,
commitRepoActionOptions: CommitRepoActionOptions{ commitRepoActionOptions: CommitRepoActionOptions{
RefFullName: git.TagPrefix + "v1.1", PushUpdateOptions: PushUpdateOptions{
OldCommitID: git.EmptySHA, RefFullName: git.TagPrefix + "v1.1",
NewCommitID: "newCommitID", OldCommitID: git.EmptySHA,
Commits: &repository.PushCommits{}, NewCommitID: "newCommitID",
},
Commits: &repository.PushCommits{},
}, },
action: models.Action{ action: models.Action{
OpType: models.ActionPushTag, OpType: models.ActionPushTag,
@ -80,10 +84,12 @@ func TestCommitRepoAction(t *testing.T) {
userID: 2, userID: 2,
repositoryID: 1, repositoryID: 1,
commitRepoActionOptions: CommitRepoActionOptions{ commitRepoActionOptions: CommitRepoActionOptions{
RefFullName: git.TagPrefix + "v1.1", PushUpdateOptions: PushUpdateOptions{
OldCommitID: "oldCommitID", RefFullName: git.TagPrefix + "v1.1",
NewCommitID: git.EmptySHA, OldCommitID: "oldCommitID",
Commits: &repository.PushCommits{}, NewCommitID: git.EmptySHA,
},
Commits: &repository.PushCommits{},
}, },
action: models.Action{ action: models.Action{
OpType: models.ActionDeleteTag, OpType: models.ActionDeleteTag,
@ -94,10 +100,12 @@ func TestCommitRepoAction(t *testing.T) {
userID: 2, userID: 2,
repositoryID: 1, repositoryID: 1,
commitRepoActionOptions: CommitRepoActionOptions{ commitRepoActionOptions: CommitRepoActionOptions{
RefFullName: git.BranchPrefix + "feature/1", PushUpdateOptions: PushUpdateOptions{
OldCommitID: "oldCommitID", RefFullName: git.BranchPrefix + "feature/1",
NewCommitID: git.EmptySHA, OldCommitID: "oldCommitID",
Commits: &repository.PushCommits{}, NewCommitID: git.EmptySHA,
},
Commits: &repository.PushCommits{},
}, },
action: models.Action{ action: models.Action{
OpType: models.ActionDeleteBranch, OpType: models.ActionDeleteBranch,

File diff suppressed because it is too large Load Diff

View File

@ -304,12 +304,6 @@ func HookPostReceive(ctx *macaron.Context, opts private.HookOptions) {
for i := range opts.OldCommitIDs { for i := range opts.OldCommitIDs {
refFullName := opts.RefFullNames[i] refFullName := opts.RefFullNames[i]
branch := opts.RefFullNames[i]
if strings.HasPrefix(branch, git.BranchPrefix) {
branch = strings.TrimPrefix(branch, git.BranchPrefix)
} else {
branch = strings.TrimPrefix(branch, git.TagPrefix)
}
// Only trigger activity updates for changes to branches or // Only trigger activity updates for changes to branches or
// tags. Updates to other refs (eg, refs/notes, refs/changes, // tags. Updates to other refs (eg, refs/notes, refs/changes,
@ -336,14 +330,13 @@ func HookPostReceive(ctx *macaron.Context, opts private.HookOptions) {
RefFullName: refFullName, RefFullName: refFullName,
OldCommitID: opts.OldCommitIDs[i], OldCommitID: opts.OldCommitIDs[i],
NewCommitID: opts.NewCommitIDs[i], NewCommitID: opts.NewCommitIDs[i],
Branch: branch,
PusherID: opts.UserID, PusherID: opts.UserID,
PusherName: opts.UserName, PusherName: opts.UserName,
RepoUserName: ownerName, RepoUserName: ownerName,
RepoName: repoName, RepoName: repoName,
} }
updates = append(updates, &option) updates = append(updates, &option)
if repo.IsEmpty && branch == "master" && strings.HasPrefix(refFullName, git.BranchPrefix) { if repo.IsEmpty && option.IsBranch() && option.BranchName() == "master" {
// put the master branch first // put the master branch first
copy(updates[1:], updates) copy(updates[1:], updates)
updates[0] = &option updates[0] = &option
@ -355,7 +348,7 @@ func HookPostReceive(ctx *macaron.Context, opts private.HookOptions) {
if err := repofiles.PushUpdates(repo, updates); err != nil { if err := repofiles.PushUpdates(repo, updates); err != nil {
log.Error("Failed to Update: %s/%s Total Updates: %d", ownerName, repoName, len(updates)) log.Error("Failed to Update: %s/%s Total Updates: %d", ownerName, repoName, len(updates))
for i, update := range updates { for i, update := range updates {
log.Error("Failed to Update: %s/%s Update: %d/%d: Branch: %s", ownerName, repoName, i, len(updates), update.Branch) log.Error("Failed to Update: %s/%s Update: %d/%d: Branch: %s", ownerName, repoName, i, len(updates), update.BranchName())
} }
log.Error("Failed to Update: %s/%s Error: %v", ownerName, repoName, err) log.Error("Failed to Update: %s/%s Error: %v", ownerName, repoName, err)