diff options
author | Lauris BH <lauris@nix.lv> | 2018-09-07 05:06:09 +0300 |
---|---|---|
committer | techknowlogick <techknowlogick@users.noreply.github.com> | 2018-09-06 22:06:09 -0400 |
commit | fa4663e61e39f0cef225ea92235a16e7da977b08 (patch) | |
tree | 7a4a784295c5b0efea8319352ca9987e4abe4148 /models/action.go | |
parent | bf552761894dee08f734d91f5c8175cd0cb2f9f5 (diff) | |
download | gitea-fa4663e61e39f0cef225ea92235a16e7da977b08.tar.gz gitea-fa4663e61e39f0cef225ea92235a16e7da977b08.zip |
Add push webhook support for mirrored repositories (#4127)
Diffstat (limited to 'models/action.go')
-rw-r--r-- | models/action.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/models/action.go b/models/action.go index adf30bb88b..f3f89d143f 100644 --- a/models/action.go +++ b/models/action.go @@ -47,6 +47,9 @@ const ( ActionReopenPullRequest // 15 ActionDeleteTag // 16 ActionDeleteBranch // 17 + ActionMirrorSyncPush // 18 + ActionMirrorSyncCreate // 19 + ActionMirrorSyncDelete // 20 ) var ( @@ -736,6 +739,71 @@ func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error return mergePullRequestAction(x, actUser, repo, pull) } +func mirrorSyncAction(e Engine, opType ActionType, repo *Repository, refName string, data []byte) error { + if err := notifyWatchers(e, &Action{ + ActUserID: repo.OwnerID, + ActUser: repo.MustOwner(), + OpType: opType, + RepoID: repo.ID, + Repo: repo, + IsPrivate: repo.IsPrivate, + RefName: refName, + Content: string(data), + }); err != nil { + return fmt.Errorf("notifyWatchers: %v", err) + } + return nil +} + +// MirrorSyncPushActionOptions mirror synchronization action options. +type MirrorSyncPushActionOptions struct { + RefName string + OldCommitID string + NewCommitID string + Commits *PushCommits +} + +// MirrorSyncPushAction adds new action for mirror synchronization of pushed commits. +func MirrorSyncPushAction(repo *Repository, opts MirrorSyncPushActionOptions) error { + if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum { + opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum] + } + + apiCommits := opts.Commits.ToAPIPayloadCommits(repo.HTMLURL()) + + opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID) + apiPusher := repo.MustOwner().APIFormat() + if err := PrepareWebhooks(repo, HookEventPush, &api.PushPayload{ + Ref: opts.RefName, + Before: opts.OldCommitID, + After: opts.NewCommitID, + CompareURL: setting.AppURL + opts.Commits.CompareURL, + Commits: apiCommits, + Repo: repo.APIFormat(AccessModeOwner), + Pusher: apiPusher, + Sender: apiPusher, + }); err != nil { + return fmt.Errorf("PrepareWebhooks: %v", err) + } + + data, err := json.Marshal(opts.Commits) + if err != nil { + return err + } + + return mirrorSyncAction(x, ActionMirrorSyncPush, repo, opts.RefName, data) +} + +// MirrorSyncCreateAction adds new action for mirror synchronization of new reference. +func MirrorSyncCreateAction(repo *Repository, refName string) error { + return mirrorSyncAction(x, ActionMirrorSyncCreate, repo, refName, nil) +} + +// MirrorSyncDeleteAction adds new action for mirror synchronization of delete reference. +func MirrorSyncDeleteAction(repo *Repository, refName string) error { + return mirrorSyncAction(x, ActionMirrorSyncDelete, repo, refName, nil) +} + // GetFeedsOptions options for retrieving feeds type GetFeedsOptions struct { RequestedUser *User |