Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move mirror sync actions to notification #9022

Merged
merged 7 commits into from
Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions modules/git/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ func RefEndName(refStr string) string {
return refStr
}

// SplitRefName splits a full refname to reftype and simple refname
func SplitRefName(refStr string) (string, string) {
if strings.HasPrefix(refStr, BranchPrefix) {
return BranchPrefix, refStr[len(BranchPrefix):]
}

if strings.HasPrefix(refStr, TagPrefix) {
return TagPrefix, refStr[len(TagPrefix):]
}

return "", refStr
}

// ParseBool returns the boolean value represented by the string as per git's git_config_bool
// true will be returned for the result if the string is empty, but valid will be false.
// "true", "yes", "on" are all true, true
Expand Down
50 changes: 50 additions & 0 deletions modules/notification/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package action

import (
"encoding/json"
"fmt"
"path"
"strings"
Expand Down Expand Up @@ -206,3 +207,52 @@ func (*actionNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *mode
log.Error("NotifyWatchers [%d]: %v", pr.ID, err)
}
}

func (a *actionNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) {
data, err := json.Marshal(commits)
if err != nil {
log.Error("json.Marshal: %v", err)
return
}

if err := models.NotifyWatchers(&models.Action{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish this could be deactivated by the user. My dashboard has more updates from mirrors than my company's projects. 🙄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just not watch that repositories?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just not watch that repositories?

🤦‍♂ 👍

ActUserID: repo.OwnerID,
ActUser: repo.MustOwner(),
OpType: models.ActionMirrorSyncPush,
RepoID: repo.ID,
Repo: repo,
IsPrivate: repo.IsPrivate,
RefName: refName,
Content: string(data),
}); err != nil {
log.Error("notifyWatchers: %v", err)
}
}

func (a *actionNotifier) NotifySyncCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string) {
if err := models.NotifyWatchers(&models.Action{
ActUserID: repo.OwnerID,
ActUser: repo.MustOwner(),
OpType: models.ActionMirrorSyncCreate,
RepoID: repo.ID,
Repo: repo,
IsPrivate: repo.IsPrivate,
RefName: refFullName,
}); err != nil {
log.Error("notifyWatchers: %v", err)
}
}

func (a *actionNotifier) NotifySyncDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string) {
if err := models.NotifyWatchers(&models.Action{
ActUserID: repo.OwnerID,
ActUser: repo.MustOwner(),
OpType: models.ActionMirrorSyncCreate,
RepoID: repo.ID,
Repo: repo,
IsPrivate: repo.IsPrivate,
RefName: refFullName,
}); err != nil {
log.Error("notifyWatchers: %v", err)
}
}
4 changes: 4 additions & 0 deletions modules/notification/base/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ type Notifier interface {
NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits)
NotifyCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string)
NotifyDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string)

NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits)
NotifySyncCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string)
NotifySyncDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string)
}
12 changes: 12 additions & 0 deletions modules/notification/base/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,15 @@ func (*NullNotifier) NotifyRenameRepository(doer *models.User, repo *models.Repo
// NotifyTransferRepository places a place holder function
func (*NullNotifier) NotifyTransferRepository(doer *models.User, repo *models.Repository, oldOwnerName string) {
}

// NotifySyncPushCommits places a place holder function
func (*NullNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) {
}

// NotifySyncCreateRef places a place holder function
func (*NullNotifier) NotifySyncCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string) {
}

// NotifySyncDeleteRef places a place holder function
func (*NullNotifier) NotifySyncDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string) {
}
21 changes: 21 additions & 0 deletions modules/notification/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,24 @@ func NotifyDeleteRef(pusher *models.User, repo *models.Repository, refType, refF
notifier.NotifyDeleteRef(pusher, repo, refType, refFullName)
}
}

// NotifySyncPushCommits notifies commits pushed to notifiers
func NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) {
for _, notifier := range notifiers {
notifier.NotifySyncPushCommits(pusher, repo, refName, oldCommitID, newCommitID, commits)
}
}

// NotifySyncCreateRef notifies branch or tag creation to notifiers
func NotifySyncCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
for _, notifier := range notifiers {
notifier.NotifySyncCreateRef(pusher, repo, refType, refFullName)
}
}

// NotifySyncDeleteRef notifies branch or tag deletion to notifiers
func NotifySyncDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
for _, notifier := range notifiers {
notifier.NotifySyncDeleteRef(pusher, repo, refType, refFullName)
}
}
22 changes: 22 additions & 0 deletions modules/notification/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,3 +695,25 @@ func (m *webhookNotifier) NotifyUpdateRelease(doer *models.User, rel *models.Rel
func (m *webhookNotifier) NotifyDeleteRelease(doer *models.User, rel *models.Release) {
sendReleaseHook(doer, rel, api.HookReleaseDeleted)
}

func (m *webhookNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) {
apiPusher := pusher.APIFormat()
apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
if err != nil {
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
return
}

if err := webhook_module.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
Ref: refName,
Before: oldCommitID,
After: newCommitID,
CompareURL: setting.AppURL + commits.CompareURL,
Commits: apiCommits,
Repo: repo.APIFormat(models.AccessModeOwner),
Pusher: apiPusher,
Sender: apiPusher,
}); err != nil {
log.Error("PrepareWebhooks: %v", err)
}
}
27 changes: 13 additions & 14 deletions services/mirror/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/sync"
Expand Down Expand Up @@ -336,19 +337,17 @@ func syncMirror(repoID string) {
continue
}

tp, _ := git.SplitRefName(result.refName)

// Create reference
if result.oldCommitID == gitShortEmptySha {
if err = SyncCreateAction(m.Repo, result.refName); err != nil {
log.Error("SyncCreateAction [repo_id: %d]: %v", m.RepoID, err)
}
notification.NotifySyncCreateRef(m.Repo.MustOwner(), m.Repo, tp, result.refName)
continue
}

// Delete reference
if result.newCommitID == gitShortEmptySha {
if err = SyncDeleteAction(m.Repo, result.refName); err != nil {
log.Error("SyncDeleteAction [repo_id: %d]: %v", m.RepoID, err)
}
notification.NotifySyncDeleteRef(m.Repo.MustOwner(), m.Repo, tp, result.refName)
continue
}

Expand All @@ -368,15 +367,15 @@ func syncMirror(repoID string) {
log.Error("CommitsBetweenIDs [repo_id: %d, new_commit_id: %s, old_commit_id: %s]: %v", m.RepoID, newCommitID, oldCommitID, err)
continue
}
if err = SyncPushAction(m.Repo, SyncPushActionOptions{
RefName: result.refName,
OldCommitID: oldCommitID,
NewCommitID: newCommitID,
Commits: models.ListToPushCommits(commits),
}); err != nil {
log.Error("SyncPushAction [repo_id: %d]: %v", m.RepoID, err)
continue

theCommits := models.ListToPushCommits(commits)
if len(theCommits.Commits) > setting.UI.FeedMaxCommitNum {
theCommits.Commits = theCommits.Commits[:setting.UI.FeedMaxCommitNum]
}

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

notification.NotifySyncPushCommits(m.Repo.MustOwner(), m.Repo, result.refName, oldCommitID, newCommitID, models.ListToPushCommits(commits))
}

// Get latest commit date and update to current repository updated time
Expand Down
67 changes: 0 additions & 67 deletions services/mirror/sync.go

This file was deleted.