diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-05-26 09:04:48 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-26 01:04:48 +0000 |
commit | f9cfd6ce5bd7f27e2655fd307022461a802fc49c (patch) | |
tree | 9279cab7249c1c7e66a81e694f1096e709c1a8a1 /services | |
parent | 26fa94bc25ecba731a12af16b6172768389287a7 (diff) | |
download | gitea-f9cfd6ce5bd7f27e2655fd307022461a802fc49c.tar.gz gitea-f9cfd6ce5bd7f27e2655fd307022461a802fc49c.zip |
Use the type RefName for all the needed places and fix pull mirror sync bugs (#24634)
This PR replaces all string refName as a type `git.RefName` to make the
code more maintainable.
Fix #15367
Replaces #23070
It also fixed a bug that tags are not sync because `git remote --prune
origin` will not remove local tags if remote removed.
We in fact should use `git fetch --prune --tags origin` but not `git
remote update origin` to do the sync.
Some answer from ChatGPT as ref.
> If the git fetch --prune --tags command is not working as expected,
there could be a few reasons why. Here are a few things to check:
>
>Make sure that you have the latest version of Git installed on your
system. You can check the version by running git --version in your
terminal. If you have an outdated version, try updating Git and see if
that resolves the issue.
>
>Check that your Git repository is properly configured to track the
remote repository's tags. You can check this by running git config
--get-all remote.origin.fetch and verifying that it includes
+refs/tags/*:refs/tags/*. If it does not, you can add it by running git
config --add remote.origin.fetch "+refs/tags/*:refs/tags/*".
>
>Verify that the tags you are trying to prune actually exist on the
remote repository. You can do this by running git ls-remote --tags
origin to list all the tags on the remote repository.
>
>Check if any local tags have been created that match the names of tags
on the remote repository. If so, these local tags may be preventing the
git fetch --prune --tags command from working properly. You can delete
local tags using the git tag -d command.
---------
Co-authored-by: delvh <dev.lh@web.de>
Diffstat (limited to 'services')
-rw-r--r-- | services/actions/notifier.go | 34 | ||||
-rw-r--r-- | services/actions/notifier_helper.go | 2 | ||||
-rw-r--r-- | services/agit/agit.go | 4 | ||||
-rw-r--r-- | services/issue/issue.go | 2 | ||||
-rw-r--r-- | services/mirror/mirror_pull.go | 74 | ||||
-rw-r--r-- | services/mirror/mirror_test.go | 46 | ||||
-rw-r--r-- | services/release/release.go | 10 | ||||
-rw-r--r-- | services/repository/branch.go | 9 | ||||
-rw-r--r-- | services/repository/cache.go | 16 | ||||
-rw-r--r-- | services/repository/push.go | 20 | ||||
-rw-r--r-- | services/webhook/dingtalk.go | 6 | ||||
-rw-r--r-- | services/webhook/discord.go | 6 | ||||
-rw-r--r-- | services/webhook/feishu.go | 6 | ||||
-rw-r--r-- | services/webhook/matrix.go | 4 | ||||
-rw-r--r-- | services/webhook/msteams.go | 6 | ||||
-rw-r--r-- | services/webhook/notifier.go | 30 | ||||
-rw-r--r-- | services/webhook/slack.go | 4 | ||||
-rw-r--r-- | services/webhook/telegram.go | 6 | ||||
-rw-r--r-- | services/webhook/wechatwork.go | 6 |
19 files changed, 169 insertions, 122 deletions
diff --git a/services/actions/notifier.go b/services/actions/notifier.go index 4ac77276ff..48eec5283b 100644 --- a/services/actions/notifier.go +++ b/services/actions/notifier.go @@ -351,9 +351,9 @@ func (n *actionsNotifier) NotifyPushCommits(ctx context.Context, pusher *user_mo } newNotifyInput(repo, pusher, webhook_module.HookEventPush). - WithRef(opts.RefFullName). + WithRef(opts.RefFullName.String()). WithPayload(&api.PushPayload{ - Ref: opts.RefFullName, + Ref: opts.RefFullName.String(), Before: opts.OldCommitID, After: opts.NewCommitID, CompareURL: setting.AppURL + commits.CompareURL, @@ -366,37 +366,35 @@ func (n *actionsNotifier) NotifyPushCommits(ctx context.Context, pusher *user_mo Notify(ctx) } -func (n *actionsNotifier) NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) { +func (n *actionsNotifier) NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) { ctx = withMethod(ctx, "NotifyCreateRef") apiPusher := convert.ToUser(ctx, pusher, nil) apiRepo := convert.ToRepo(ctx, repo, perm_model.AccessModeNone) - refName := git.RefEndName(refFullName) newNotifyInput(repo, pusher, webhook_module.HookEventCreate). - WithRef(refName). + WithRef(refFullName.ShortName()). // FIXME: should we use a full ref name WithPayload(&api.CreatePayload{ - Ref: refName, + Ref: refFullName.ShortName(), Sha: refID, - RefType: refType, + RefType: refFullName.RefGroup(), Repo: apiRepo, Sender: apiPusher, }). Notify(ctx) } -func (n *actionsNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) { +func (n *actionsNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName) { ctx = withMethod(ctx, "NotifyDeleteRef") apiPusher := convert.ToUser(ctx, pusher, nil) apiRepo := convert.ToRepo(ctx, repo, perm_model.AccessModeNone) - refName := git.RefEndName(refFullName) newNotifyInput(repo, pusher, webhook_module.HookEventDelete). - WithRef(refName). + WithRef(refFullName.ShortName()). // FIXME: should we use a full ref name WithPayload(&api.DeletePayload{ - Ref: refName, - RefType: refType, + Ref: refFullName.ShortName(), + RefType: refFullName.RefGroup(), PusherType: api.PusherTypeUser, Repo: apiRepo, Sender: apiPusher, @@ -415,9 +413,9 @@ func (n *actionsNotifier) NotifySyncPushCommits(ctx context.Context, pusher *use } newNotifyInput(repo, pusher, webhook_module.HookEventPush). - WithRef(opts.RefFullName). + WithRef(opts.RefFullName.String()). WithPayload(&api.PushPayload{ - Ref: opts.RefFullName, + Ref: opts.RefFullName.String(), Before: opts.OldCommitID, After: opts.NewCommitID, CompareURL: setting.AppURL + commits.CompareURL, @@ -431,14 +429,14 @@ func (n *actionsNotifier) NotifySyncPushCommits(ctx context.Context, pusher *use Notify(ctx) } -func (n *actionsNotifier) NotifySyncCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) { +func (n *actionsNotifier) NotifySyncCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) { ctx = withMethod(ctx, "NotifySyncCreateRef") - n.NotifyCreateRef(ctx, pusher, repo, refType, refFullName, refID) + n.NotifyCreateRef(ctx, pusher, repo, refFullName, refID) } -func (n *actionsNotifier) NotifySyncDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) { +func (n *actionsNotifier) NotifySyncDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName) { ctx = withMethod(ctx, "NotifySyncDeleteRef") - n.NotifyDeleteRef(ctx, pusher, repo, refType, refFullName) + n.NotifyDeleteRef(ctx, pusher, repo, refFullName) } func (n *actionsNotifier) NotifyNewRelease(ctx context.Context, rel *repo_model.Release) { diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index 376c9820f1..5e41241d18 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -225,7 +225,7 @@ func notifyRelease(ctx context.Context, doer *user_model.User, rel *repo_model.R mode, _ := access_model.AccessLevel(ctx, doer, rel.Repo) newNotifyInput(rel.Repo, doer, webhook_module.HookEventRelease). - WithRef(git.TagPrefix + rel.TagName). + WithRef(git.RefNameFromTag(rel.TagName).String()). WithPayload(&api.ReleasePayload{ Action: action, Release: convert.ToRelease(ctx, rel), diff --git a/services/agit/agit.go b/services/agit/agit.go index 32fc3cba4a..208ea109f4 100644 --- a/services/agit/agit.go +++ b/services/agit/agit.go @@ -48,7 +48,7 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git. continue } - if !strings.HasPrefix(opts.RefFullNames[i], git.PullRequestPrefix) { + if !opts.RefFullNames[i].IsFor() { results = append(results, private.HookProcReceiveRefResult{ IsNotMatched: true, OriginalRef: opts.RefFullNames[i], @@ -56,7 +56,7 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git. continue } - baseBranchName := opts.RefFullNames[i][len(git.PullRequestPrefix):] + baseBranchName := opts.RefFullNames[i].ForBranchName() curentTopicBranch := "" if !gitRepo.IsBranchExist(baseBranchName) { // try match refs/for/<target-branch>/<topic-branch> diff --git a/services/issue/issue.go b/services/issue/issue.go index 06da47152c..ce2b1c0976 100644 --- a/services/issue/issue.go +++ b/services/issue/issue.go @@ -206,7 +206,7 @@ func GetRefEndNamesAndURLs(issues []*issues_model.Issue, repoLink string) (map[i issueRefURLs := make(map[int64]string, len(issues)) for _, issue := range issues { if issue.Ref != "" { - issueRefEndNames[issue.ID] = git.RefEndName(issue.Ref) + issueRefEndNames[issue.ID] = git.RefName(issue.Ref).ShortName() issueRefURLs[issue.ID] = git.RefURL(repoLink, issue.Ref) } } diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index 60699294c1..53ab632b01 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -78,13 +78,23 @@ func UpdateAddress(ctx context.Context, m *repo_model.Mirror, addr string) error // If the oldCommitID is "0000000", it means a new reference, the value of newCommitID is empty. // If the newCommitID is "0000000", it means the reference is deleted, the value of oldCommitID is empty. type mirrorSyncResult struct { - refName string + refName git.RefName oldCommitID string newCommitID string } // parseRemoteUpdateOutput detects create, update and delete operations of references from upstream. -func parseRemoteUpdateOutput(output string) []*mirrorSyncResult { +// possible output example: +/* +// * [new tag] v0.1.8 -> v0.1.8 +// * [new branch] master -> origin/master +// - [deleted] (none) -> origin/test // delete a branch +// - [deleted] (none) -> 1 // delete a tag +// 957a993..a87ba5f test -> origin/test +// + f895a1e...957a993 test -> origin/test (forced update) +*/ +// TODO: return whether it's a force update +func parseRemoteUpdateOutput(output, remoteName string) []*mirrorSyncResult { results := make([]*mirrorSyncResult, 0, 3) lines := strings.Split(output, "\n") for i := range lines { @@ -94,22 +104,30 @@ func parseRemoteUpdateOutput(output string) []*mirrorSyncResult { continue } - refName := lines[i][idx+3:] + refName := strings.TrimSpace(lines[i][idx+3:]) switch { - case strings.HasPrefix(lines[i], " * "): // New reference - if strings.HasPrefix(lines[i], " * [new tag]") { - refName = git.TagPrefix + refName - } else if strings.HasPrefix(lines[i], " * [new branch]") { - refName = git.BranchPrefix + refName - } + case strings.HasPrefix(lines[i], " * [new tag]"): // new tag results = append(results, &mirrorSyncResult{ - refName: refName, + refName: git.RefNameFromTag(refName), + oldCommitID: gitShortEmptySha, + }) + case strings.HasPrefix(lines[i], " * [new branch]"): // new branch + refName = strings.TrimPrefix(refName, remoteName+"/") + results = append(results, &mirrorSyncResult{ + refName: git.RefNameFromBranch(refName), oldCommitID: gitShortEmptySha, }) case strings.HasPrefix(lines[i], " - "): // Delete reference + isTag := !strings.HasPrefix(refName, remoteName+"/") + var refFullName git.RefName + if isTag { + refFullName = git.RefNameFromTag(refName) + } else { + refFullName = git.RefNameFromBranch(strings.TrimPrefix(refName, remoteName+"/")) + } results = append(results, &mirrorSyncResult{ - refName: refName, + refName: refFullName, newCommitID: gitShortEmptySha, }) case strings.HasPrefix(lines[i], " + "): // Force update @@ -127,7 +145,7 @@ func parseRemoteUpdateOutput(output string) []*mirrorSyncResult { continue } results = append(results, &mirrorSyncResult{ - refName: refName, + refName: git.RefNameFromBranch(strings.TrimPrefix(refName, remoteName+"/")), oldCommitID: shas[0], newCommitID: shas[1], }) @@ -143,7 +161,7 @@ func parseRemoteUpdateOutput(output string) []*mirrorSyncResult { continue } results = append(results, &mirrorSyncResult{ - refName: refName, + refName: git.RefNameFromBranch(strings.TrimPrefix(refName, remoteName+"/")), oldCommitID: shas[0], newCommitID: shas[1], }) @@ -204,11 +222,12 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo log.Trace("SyncMirrors [repo: %-v]: running git remote update...", m.Repo) - cmd := git.NewCommand(ctx, "remote", "update") + // use fetch but not remote update because git fetch support --tags but remote update doesn't + cmd := git.NewCommand(ctx, "fetch") if m.EnablePrune { cmd.AddArguments("--prune") } - cmd.AddDynamicArguments(m.GetRemoteName()) + cmd.AddArguments("--tags").AddDynamicArguments(m.GetRemoteName()) remoteURL, remoteErr := git.GetRemoteURL(ctx, repoPath, m.GetRemoteName()) if remoteErr != nil { @@ -384,7 +403,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo } m.UpdatedUnix = timeutil.TimeStampNow() - return parseRemoteUpdateOutput(output), true + return parseRemoteUpdateOutput(output, m.GetRemoteName()), true } // SyncPullMirror starts the sync of the pull mirror and schedules the next run. @@ -444,20 +463,13 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool { for _, result := range results { // Discard GitHub pull requests, i.e. refs/pull/* - if strings.HasPrefix(result.refName, git.PullPrefix) { + if result.refName.IsPull() { continue } - tp, _ := git.SplitRefName(result.refName) - // Create reference if result.oldCommitID == gitShortEmptySha { - if tp == git.TagPrefix { - tp = "tag" - } else if tp == git.BranchPrefix { - tp = "branch" - } - commitID, err := gitRepo.GetRefCommitID(result.refName) + commitID, err := gitRepo.GetRefCommitID(result.refName.String()) if err != nil { log.Error("SyncMirrors [repo: %-v]: unable to GetRefCommitID [ref_name: %s]: %v", m.Repo, result.refName, err) continue @@ -467,13 +479,13 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool { OldCommitID: git.EmptySHA, NewCommitID: commitID, }, repo_module.NewPushCommits()) - notification.NotifySyncCreateRef(ctx, m.Repo.MustOwner(ctx), m.Repo, tp, result.refName, commitID) + notification.NotifySyncCreateRef(ctx, m.Repo.MustOwner(ctx), m.Repo, result.refName, commitID) continue } // Delete reference if result.newCommitID == gitShortEmptySha { - notification.NotifySyncDeleteRef(ctx, m.Repo.MustOwner(ctx), m.Repo, tp, result.refName) + notification.NotifySyncDeleteRef(ctx, m.Repo.MustOwner(ctx), m.Repo, result.refName) continue } @@ -547,13 +559,11 @@ func checkAndUpdateEmptyRepository(m *repo_model.Mirror, gitRepo *git.Repository } firstName := "" for _, result := range results { - if strings.HasPrefix(result.refName, git.PullPrefix) { - continue - } - tp, name := git.SplitRefName(result.refName) - if len(tp) > 0 && tp != git.BranchPrefix { + if !result.refName.IsBranch() { continue } + + name := result.refName.BranchName() if len(firstName) == 0 { firstName = name } diff --git a/services/mirror/mirror_test.go b/services/mirror/mirror_test.go new file mode 100644 index 0000000000..8ad524b608 --- /dev/null +++ b/services/mirror/mirror_test.go @@ -0,0 +1,46 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package mirror + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_parseRemoteUpdateOutput(t *testing.T) { + output := ` + * [new tag] v0.1.8 -> v0.1.8 + * [new branch] master -> origin/master + - [deleted] (none) -> origin/test1 + - [deleted] (none) -> tag1 + + f895a1e...957a993 test2 -> origin/test2 (forced update) + 957a993..a87ba5f test3 -> origin/test3 +` + results := parseRemoteUpdateOutput(output, "origin") + assert.Len(t, results, 6) + assert.EqualValues(t, "refs/tags/v0.1.8", results[0].refName.String()) + assert.EqualValues(t, gitShortEmptySha, results[0].oldCommitID) + assert.EqualValues(t, "", results[0].newCommitID) + + assert.EqualValues(t, "refs/heads/master", results[1].refName.String()) + assert.EqualValues(t, gitShortEmptySha, results[1].oldCommitID) + assert.EqualValues(t, "", results[1].newCommitID) + + assert.EqualValues(t, "refs/heads/test1", results[2].refName.String()) + assert.EqualValues(t, "", results[2].oldCommitID) + assert.EqualValues(t, gitShortEmptySha, results[2].newCommitID) + + assert.EqualValues(t, "refs/tags/tag1", results[3].refName.String()) + assert.EqualValues(t, "", results[3].oldCommitID) + assert.EqualValues(t, gitShortEmptySha, results[3].newCommitID) + + assert.EqualValues(t, "refs/heads/test2", results[4].refName.String()) + assert.EqualValues(t, "f895a1e", results[4].oldCommitID) + assert.EqualValues(t, "957a993", results[4].newCommitID) + + assert.EqualValues(t, "refs/heads/test3", results[5].refName.String()) + assert.EqualValues(t, "957a993", results[5].oldCommitID) + assert.EqualValues(t, "a87ba5f", results[5].newCommitID) +} diff --git a/services/release/release.go b/services/release/release.go index a9a5231197..c1190305b6 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -80,14 +80,15 @@ func createTag(ctx context.Context, gitRepo *git.Repository, rel *repo_model.Rel commits.HeadCommit = repository.CommitToPushCommit(commit) commits.CompareURL = rel.Repo.ComposeCompareURL(git.EmptySHA, commit.ID.String()) + refFullName := git.RefNameFromTag(rel.TagName) notification.NotifyPushCommits( ctx, rel.Publisher, rel.Repo, &repository.PushUpdateOptions{ - RefFullName: git.TagPrefix + rel.TagName, + RefFullName: refFullName, OldCommitID: git.EmptySHA, NewCommitID: commit.ID.String(), }, commits) - notification.NotifyCreateRef(ctx, rel.Publisher, rel.Repo, "tag", git.TagPrefix+rel.TagName, commit.ID.String()) + notification.NotifyCreateRef(ctx, rel.Publisher, rel.Repo, refFullName, commit.ID.String()) rel.CreatedUnix = timeutil.TimeStampNow() } commit, err := gitRepo.GetTagCommit(rel.TagName) @@ -323,14 +324,15 @@ func DeleteReleaseByID(ctx context.Context, id int64, doer *user_model.User, del return fmt.Errorf("git tag -d: %w", err) } + refName := git.RefNameFromTag(rel.TagName) notification.NotifyPushCommits( ctx, doer, repo, &repository.PushUpdateOptions{ - RefFullName: git.TagPrefix + rel.TagName, + RefFullName: refName, OldCommitID: rel.Sha1, NewCommitID: git.EmptySHA, }, repository.NewPushCommits()) - notification.NotifyDeleteRef(ctx, doer, repo, "tag", git.TagPrefix+rel.TagName) + notification.NotifyDeleteRef(ctx, doer, repo, refName) if err := repo_model.DeleteReleaseByID(ctx, id); err != nil { return fmt.Errorf("DeleteReleaseByID: %w", err) diff --git a/services/repository/branch.go b/services/repository/branch.go index cafad34cef..4027c75dc2 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -139,13 +139,14 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m }); err != nil { return "", err } - refID, err := gitRepo.GetRefCommitID(git.BranchPrefix + to) + refNameTo := git.RefNameFromBranch(to) + refID, err := gitRepo.GetRefCommitID(refNameTo.String()) if err != nil { return "", err } - notification.NotifyDeleteRef(ctx, doer, repo, "branch", git.BranchPrefix+from) - notification.NotifyCreateRef(ctx, doer, repo, "branch", git.BranchPrefix+to, refID) + notification.NotifyDeleteRef(ctx, doer, repo, git.RefNameFromBranch(from)) + notification.NotifyCreateRef(ctx, doer, repo, refNameTo, refID) return "", nil } @@ -187,7 +188,7 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R // Don't return error below this if err := PushUpdate( &repo_module.PushUpdateOptions{ - RefFullName: git.BranchPrefix + branchName, + RefFullName: git.RefNameFromBranch(branchName), OldCommitID: commit.ID.String(), NewCommitID: git.EmptySHA, PusherID: doer.ID, diff --git a/services/repository/cache.go b/services/repository/cache.go index 6fd4fa7250..91351cbf49 100644 --- a/services/repository/cache.go +++ b/services/repository/cache.go @@ -5,7 +5,6 @@ package repository import ( "context" - "strings" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/cache" @@ -13,28 +12,19 @@ import ( "code.gitea.io/gitea/modules/setting" ) -func getRefName(fullRefName string) string { - if strings.HasPrefix(fullRefName, git.TagPrefix) { - return fullRefName[len(git.TagPrefix):] - } else if strings.HasPrefix(fullRefName, git.BranchPrefix) { - return fullRefName[len(git.BranchPrefix):] - } - return "" -} - // CacheRef cachhe last commit information of the branch or the tag -func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, fullRefName string) error { +func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, fullRefName git.RefName) error { if !setting.CacheService.LastCommit.Enabled { return nil } - commit, err := gitRepo.GetCommit(fullRefName) + commit, err := gitRepo.GetCommit(fullRefName.String()) if err != nil { return err } if gitRepo.LastCommitCache == nil { - commitsCount, err := cache.GetInt64(repo.GetCommitsCountCacheKey(getRefName(fullRefName), true), commit.CommitsCount) + commitsCount, err := cache.GetInt64(repo.GetCommitsCountCacheKey(fullRefName.ShortName(), true), commit.CommitsCount) if err != nil { return err } diff --git a/services/repository/push.go b/services/repository/push.go index c7ea8f336e..f213948916 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -107,7 +107,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { if opts.IsNewRef() && opts.IsDelRef() { return fmt.Errorf("old and new revisions are both %s", git.EmptySHA) } - if opts.IsTag() { // If is tag reference + if opts.RefFullName.IsTag() { if pusher == nil || pusher.ID != opts.PusherID { if opts.PusherID == user_model.ActionsUserID { pusher = user_model.NewActionsUser() @@ -118,18 +118,18 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { } } } - tagName := opts.TagName() + tagName := opts.RefFullName.TagName() if opts.IsDelRef() { notification.NotifyPushCommits( ctx, pusher, repo, &repo_module.PushUpdateOptions{ - RefFullName: git.TagPrefix + tagName, + RefFullName: git.RefNameFromTag(tagName), OldCommitID: opts.OldCommitID, NewCommitID: git.EmptySHA, }, repo_module.NewPushCommits()) delTags = append(delTags, tagName) - notification.NotifyDeleteRef(ctx, pusher, repo, "tag", opts.RefFullName) + notification.NotifyDeleteRef(ctx, pusher, repo, opts.RefFullName) } else { // is new tag newCommit, err := gitRepo.GetCommit(opts.NewCommitID) if err != nil { @@ -143,15 +143,15 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { notification.NotifyPushCommits( ctx, pusher, repo, &repo_module.PushUpdateOptions{ - RefFullName: git.TagPrefix + tagName, + RefFullName: opts.RefFullName, OldCommitID: git.EmptySHA, NewCommitID: opts.NewCommitID, }, commits) addTags = append(addTags, tagName) - notification.NotifyCreateRef(ctx, pusher, repo, "tag", opts.RefFullName, opts.NewCommitID) + notification.NotifyCreateRef(ctx, pusher, repo, opts.RefFullName, opts.NewCommitID) } - } else if opts.IsBranch() { // If is branch reference + } else if opts.RefFullName.IsBranch() { if pusher == nil || pusher.ID != opts.PusherID { if opts.PusherID == user_model.ActionsUserID { pusher = user_model.NewActionsUser() @@ -163,7 +163,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { } } - branch := opts.BranchName() + branch := opts.RefFullName.BranchName() if !opts.IsDelRef() { log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name) go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true, opts.OldCommitID, opts.NewCommitID) @@ -198,7 +198,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { if err != nil { return fmt.Errorf("newCommit.CommitsBeforeLimit: %w", err) } - notification.NotifyCreateRef(ctx, pusher, repo, "branch", opts.RefFullName, opts.NewCommitID) + notification.NotifyCreateRef(ctx, pusher, repo, opts.RefFullName, opts.NewCommitID) } else { l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID) if err != nil { @@ -269,7 +269,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { log.Error("repo_module.CacheRef %s/%s failed: %v", repo.ID, branch, err) } } else { - notification.NotifyDeleteRef(ctx, pusher, repo, "branch", opts.RefFullName) + notification.NotifyDeleteRef(ctx, pusher, repo, opts.RefFullName) if err = pull_service.CloseBranchPulls(pusher, repo.ID, branch); err != nil { // close all related pulls log.Error("close related pull request failed: %v", err) diff --git a/services/webhook/dingtalk.go b/services/webhook/dingtalk.go index 99ee6e4d19..69fae03299 100644 --- a/services/webhook/dingtalk.go +++ b/services/webhook/dingtalk.go @@ -36,7 +36,7 @@ func (d *DingtalkPayload) JSONPayload() ([]byte, error) { // Create implements PayloadConvertor Create method func (d *DingtalkPayload) Create(p *api.CreatePayload) (api.Payloader, error) { // created tag/branch - refName := git.RefEndName(p.Ref) + refName := git.RefName(p.Ref).ShortName() title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName) return createDingtalkPayload(title, title, fmt.Sprintf("view ref %s", refName), p.Repo.HTMLURL+"/src/"+util.PathEscapeSegments(refName)), nil @@ -45,7 +45,7 @@ func (d *DingtalkPayload) Create(p *api.CreatePayload) (api.Payloader, error) { // Delete implements PayloadConvertor Delete method func (d *DingtalkPayload) Delete(p *api.DeletePayload) (api.Payloader, error) { // created tag/branch - refName := git.RefEndName(p.Ref) + refName := git.RefName(p.Ref).ShortName() title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName) return createDingtalkPayload(title, title, fmt.Sprintf("view ref %s", refName), p.Repo.HTMLURL+"/src/"+util.PathEscapeSegments(refName)), nil @@ -61,7 +61,7 @@ func (d *DingtalkPayload) Fork(p *api.ForkPayload) (api.Payloader, error) { // Push implements PayloadConvertor Push method func (d *DingtalkPayload) Push(p *api.PushPayload) (api.Payloader, error) { var ( - branchName = git.RefEndName(p.Ref) + branchName = git.RefName(p.Ref).ShortName() commitDesc string ) diff --git a/services/webhook/discord.go b/services/webhook/discord.go index 02f0fb8720..204587eca5 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -112,7 +112,7 @@ var _ PayloadConvertor = &DiscordPayload{} // Create implements PayloadConvertor Create method func (d *DiscordPayload) Create(p *api.CreatePayload) (api.Payloader, error) { // created tag/branch - refName := git.RefEndName(p.Ref) + refName := git.RefName(p.Ref).ShortName() title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName) return d.createPayload(p.Sender, title, "", p.Repo.HTMLURL+"/src/"+util.PathEscapeSegments(refName), greenColor), nil @@ -121,7 +121,7 @@ func (d *DiscordPayload) Create(p *api.CreatePayload) (api.Payloader, error) { // Delete implements PayloadConvertor Delete method func (d *DiscordPayload) Delete(p *api.DeletePayload) (api.Payloader, error) { // deleted tag/branch - refName := git.RefEndName(p.Ref) + refName := git.RefName(p.Ref).ShortName() title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName) return d.createPayload(p.Sender, title, "", p.Repo.HTMLURL+"/src/"+util.PathEscapeSegments(refName), redColor), nil @@ -137,7 +137,7 @@ func (d *DiscordPayload) Fork(p *api.ForkPayload) (api.Payloader, error) { // Push implements PayloadConvertor Push method func (d *DiscordPayload) Push(p *api.PushPayload) (api.Payloader, error) { var ( - branchName = git.RefEndName(p.Ref) + branchName = git.RefName(p.Ref).ShortName() commitDesc string ) diff --git a/services/webhook/feishu.go b/services/webhook/feishu.go index 4fbf8f76a9..885cde3bc5 100644 --- a/services/webhook/feishu.go +++ b/services/webhook/feishu.go @@ -48,7 +48,7 @@ var _ PayloadConvertor = &FeishuPayload{} // Create implements PayloadConvertor Create method func (f *FeishuPayload) Create(p *api.CreatePayload) (api.Payloader, error) { // created tag/branch - refName := git.RefEndName(p.Ref) + refName := git.RefName(p.Ref).ShortName() text := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName) return newFeishuTextPayload(text), nil @@ -57,7 +57,7 @@ func (f *FeishuPayload) Create(p *api.CreatePayload) (api.Payloader, error) { // Delete implements PayloadConvertor Delete method func (f *FeishuPayload) Delete(p *api.DeletePayload) (api.Payloader, error) { // created tag/branch - refName := git.RefEndName(p.Ref) + refName := git.RefName(p.Ref).ShortName() text := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName) return newFeishuTextPayload(text), nil @@ -73,7 +73,7 @@ func (f *FeishuPayload) Fork(p *api.ForkPayload) (api.Payloader, error) { // Push implements PayloadConvertor Push method func (f *FeishuPayload) Push(p *api.PushPayload) (api.Payloader, error) { var ( - branchName = git.RefEndName(p.Ref) + branchName = git.RefName(p.Ref).ShortName() commitDesc string ) diff --git a/services/webhook/matrix.go b/services/webhook/matrix.go index cf2b503cdc..df97b43b64 100644 --- a/services/webhook/matrix.go +++ b/services/webhook/matrix.go @@ -73,7 +73,7 @@ func MatrixLinkFormatter(url, text string) string { // MatrixLinkToRef Matrix-formatter link to a repo ref func MatrixLinkToRef(repoURL, ref string) string { - refName := git.RefEndName(ref) + refName := git.RefName(ref).ShortName() switch { case strings.HasPrefix(ref, git.BranchPrefix): return MatrixLinkFormatter(repoURL+"/src/branch/"+util.PathEscapeSegments(refName), refName) @@ -95,7 +95,7 @@ func (m *MatrixPayload) Create(p *api.CreatePayload) (api.Payloader, error) { // Delete composes Matrix payload for delete a branch or tag. func (m *MatrixPayload) Delete(p *api.DeletePayload) (api.Payloader, error) { - refName := git.RefEndName(p.Ref) + refName := git.RefName(p.Ref).ShortName() repoLink := MatrixLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName) text := fmt.Sprintf("[%s:%s] %s deleted by %s", repoLink, refName, p.RefType, p.Sender.UserName) diff --git a/services/webhook/msteams.go b/services/webhook/msteams.go index 60ef334ded..6ad58a6247 100644 --- a/services/webhook/msteams.go +++ b/services/webhook/msteams.go @@ -70,7 +70,7 @@ var _ PayloadConvertor = &MSTeamsPayload{} // Create implements PayloadConvertor Create method func (m *MSTeamsPayload) Create(p *api.CreatePayload) (api.Payloader, error) { // created tag/branch - refName := git.RefEndName(p.Ref) + refName := git.RefName(p.Ref).ShortName() title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName) return createMSTeamsPayload( @@ -87,7 +87,7 @@ func (m *MSTeamsPayload) Create(p *api.CreatePayload) (api.Payloader, error) { // Delete implements PayloadConvertor Delete method func (m *MSTeamsPayload) Delete(p *api.DeletePayload) (api.Payloader, error) { // deleted tag/branch - refName := git.RefEndName(p.Ref) + refName := git.RefName(p.Ref).ShortName() title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName) return createMSTeamsPayload( @@ -119,7 +119,7 @@ func (m *MSTeamsPayload) Fork(p *api.ForkPayload) (api.Payloader, error) { // Push implements PayloadConvertor Push method func (m *MSTeamsPayload) Push(p *api.PushPayload) (api.Payloader, error) { var ( - branchName = git.RefEndName(p.Ref) + branchName = git.RefName(p.Ref).ShortName() commitDesc string ) diff --git a/services/webhook/notifier.go b/services/webhook/notifier.go index bd25e20805..e10c83e02f 100644 --- a/services/webhook/notifier.go +++ b/services/webhook/notifier.go @@ -596,7 +596,7 @@ func (m *webhookNotifier) NotifyPushCommits(ctx context.Context, pusher *user_mo } if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{ - Ref: opts.RefFullName, + Ref: opts.RefFullName.String(), Before: opts.OldCommitID, After: opts.NewCommitID, CompareURL: setting.AppURL + commits.CompareURL, @@ -747,15 +747,15 @@ func (m *webhookNotifier) NotifyPullReviewRequest(ctx context.Context, doer *use } } -func (m *webhookNotifier) NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) { +func (m *webhookNotifier) NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) { apiPusher := convert.ToUser(ctx, pusher, nil) apiRepo := convert.ToRepo(ctx, repo, perm.AccessModeNone) - refName := git.RefEndName(refFullName) + refName := refFullName.ShortName() if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventCreate, &api.CreatePayload{ - Ref: refName, + Ref: refName, // FIXME: should it be a full ref name? Sha: refID, - RefType: refType, + RefType: refFullName.RefGroup(), Repo: apiRepo, Sender: apiPusher, }); err != nil { @@ -784,19 +784,19 @@ func (m *webhookNotifier) NotifyPullRequestSynchronized(ctx context.Context, doe } } -func (m *webhookNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) { +func (m *webhookNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName) { apiPusher := convert.ToUser(ctx, pusher, nil) apiRepo := convert.ToRepo(ctx, repo, perm.AccessModeNone) - refName := git.RefEndName(refFullName) + refName := refFullName.ShortName() if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventDelete, &api.DeletePayload{ - Ref: refName, - RefType: refType, + Ref: refName, // FIXME: should it be a full ref name? + RefType: refFullName.RefGroup(), PusherType: api.PusherTypeUser, Repo: apiRepo, Sender: apiPusher, }); err != nil { - log.Error("PrepareWebhooks.(delete %s): %v", refType, err) + log.Error("PrepareWebhooks.(delete %s): %v", refFullName.RefGroup(), err) } } @@ -838,7 +838,7 @@ func (m *webhookNotifier) NotifySyncPushCommits(ctx context.Context, pusher *use } if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{ - Ref: opts.RefFullName, + Ref: opts.RefFullName.String(), Before: opts.OldCommitID, After: opts.NewCommitID, CompareURL: setting.AppURL + commits.CompareURL, @@ -853,12 +853,12 @@ func (m *webhookNotifier) NotifySyncPushCommits(ctx context.Context, pusher *use } } -func (m *webhookNotifier) NotifySyncCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) { - m.NotifyCreateRef(ctx, pusher, repo, refType, refFullName, refID) +func (m *webhookNotifier) NotifySyncCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) { + m.NotifyCreateRef(ctx, pusher, repo, refFullName, refID) } -func (m *webhookNotifier) NotifySyncDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) { - m.NotifyDeleteRef(ctx, pusher, repo, refType, refFullName) +func (m *webhookNotifier) NotifySyncDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName) { + m.NotifyDeleteRef(ctx, pusher, repo, refFullName) } func (m *webhookNotifier) NotifyPackageCreate(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) { diff --git a/services/webhook/slack.go b/services/webhook/slack.go index c2d4a7731e..0cb27bb3dd 100644 --- a/services/webhook/slack.go +++ b/services/webhook/slack.go @@ -93,7 +93,7 @@ func SlackLinkFormatter(url, text string) string { // SlackLinkToRef slack-formatter link to a repo ref func SlackLinkToRef(repoURL, ref string) string { url := git.RefURL(repoURL, ref) - refName := git.RefEndName(ref) + refName := git.RefName(ref).ShortName() return SlackLinkFormatter(url, refName) } @@ -110,7 +110,7 @@ func (s *SlackPayload) Create(p *api.CreatePayload) (api.Payloader, error) { // Delete composes Slack payload for delete a branch or tag. func (s *SlackPayload) Delete(p *api.DeletePayload) (api.Payloader, error) { - refName := git.RefEndName(p.Ref) + refName := git.RefName(p.Ref).ShortName() repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName) text := fmt.Sprintf("[%s:%s] %s deleted by %s", repoLink, refName, p.RefType, p.Sender.UserName) diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go index e5c731fc92..2d0484648b 100644 --- a/services/webhook/telegram.go +++ b/services/webhook/telegram.go @@ -57,7 +57,7 @@ func (t *TelegramPayload) JSONPayload() ([]byte, error) { // Create implements PayloadConvertor Create method func (t *TelegramPayload) Create(p *api.CreatePayload) (api.Payloader, error) { // created tag/branch - refName := git.RefEndName(p.Ref) + refName := git.RefName(p.Ref).ShortName() title := fmt.Sprintf(`[<a href="%s">%s</a>] %s <a href="%s">%s</a> created`, p.Repo.HTMLURL, p.Repo.FullName, p.RefType, p.Repo.HTMLURL+"/src/"+refName, refName) @@ -67,7 +67,7 @@ func (t *TelegramPayload) Create(p *api.CreatePayload) (api.Payloader, error) { // Delete implements PayloadConvertor Delete method func (t *TelegramPayload) Delete(p *api.DeletePayload) (api.Payloader, error) { // created tag/branch - refName := git.RefEndName(p.Ref) + refName := git.RefName(p.Ref).ShortName() title := fmt.Sprintf(`[<a href="%s">%s</a>] %s <a href="%s">%s</a> deleted`, p.Repo.HTMLURL, p.Repo.FullName, p.RefType, p.Repo.HTMLURL+"/src/"+refName, refName) @@ -84,7 +84,7 @@ func (t *TelegramPayload) Fork(p *api.ForkPayload) (api.Payloader, error) { // Push implements PayloadConvertor Push method func (t *TelegramPayload) Push(p *api.PushPayload) (api.Payloader, error) { var ( - branchName = git.RefEndName(p.Ref) + branchName = git.RefName(p.Ref).ShortName() commitDesc string ) diff --git a/services/webhook/wechatwork.go b/services/webhook/wechatwork.go index dc8810c4a6..a7680f1c67 100644 --- a/services/webhook/wechatwork.go +++ b/services/webhook/wechatwork.go @@ -56,7 +56,7 @@ var _ PayloadConvertor = &WechatworkPayload{} // Create implements PayloadConvertor Create method func (f *WechatworkPayload) Create(p *api.CreatePayload) (api.Payloader, error) { // created tag/branch - refName := git.RefEndName(p.Ref) + refName := git.RefName(p.Ref).ShortName() title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName) return newWechatworkMarkdownPayload(title), nil @@ -65,7 +65,7 @@ func (f *WechatworkPayload) Create(p *api.CreatePayload) (api.Payloader, error) // Delete implements PayloadConvertor Delete method func (f *WechatworkPayload) Delete(p *api.DeletePayload) (api.Payloader, error) { // created tag/branch - refName := git.RefEndName(p.Ref) + refName := git.RefName(p.Ref).ShortName() title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName) return newWechatworkMarkdownPayload(title), nil @@ -81,7 +81,7 @@ func (f *WechatworkPayload) Fork(p *api.ForkPayload) (api.Payloader, error) { // Push implements PayloadConvertor Push method func (f *WechatworkPayload) Push(p *api.PushPayload) (api.Payloader, error) { var ( - branchName = git.RefEndName(p.Ref) + branchName = git.RefName(p.Ref).ShortName() commitDesc string ) |