diff options
author | Unknwon <u@gogs.io> | 2018-06-03 20:32:44 +0800 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2018-06-03 20:32:44 +0800 |
commit | c08aab90ec696b7fcc56b8da0a468e74d266b89e (patch) | |
tree | 7add72f54be31c2bd05ff1fa6676a99bffe4cc26 /models | |
parent | 8d091ec062815dfd885c540d107ac2e07ed6b880 (diff) |
models/mirror: shot push webhook after synced commits (#4528)
Diffstat (limited to 'models')
-rw-r--r-- | models/action.go | 41 | ||||
-rw-r--r-- | models/mirror.go | 27 | ||||
-rw-r--r-- | models/webhook.go | 28 |
3 files changed, 73 insertions, 23 deletions
diff --git a/models/action.go b/models/action.go index 93ca5b2a..377f6074 100644 --- a/models/action.go +++ b/models/action.go @@ -254,7 +254,7 @@ func NewPushCommits() *PushCommits { } } -func (pc *PushCommits) ToApiPayloadCommits(repoPath, repoLink string) ([]*api.PayloadCommit, error) { +func (pc *PushCommits) ToApiPayloadCommits(repoPath, repoURL string) ([]*api.PayloadCommit, error) { commits := make([]*api.PayloadCommit, len(pc.Commits)) for i, commit := range pc.Commits { authorUsername := "" @@ -281,7 +281,7 @@ func (pc *PushCommits) ToApiPayloadCommits(repoPath, repoLink string) ([]*api.Pa commits[i] = &api.PayloadCommit{ ID: commit.Sha1, Message: commit.Message, - URL: fmt.Sprintf("%s/commit/%s", repoLink, commit.Sha1), + URL: fmt.Sprintf("%s/commit/%s", repoURL, commit.Sha1), Author: &api.PayloadUser{ Name: commit.AuthorName, Email: commit.AuthorEmail, @@ -684,14 +684,45 @@ func mirrorSyncAction(opType ActionType, repo *Repository, refName string, data }) } +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, refName string, commits *PushCommits) error { - data, err := json.Marshal(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, err := opts.Commits.ToApiPayloadCommits(repo.RepoPath(), repo.HTMLURL()) + if err != nil { + return fmt.Errorf("ToApiPayloadCommits: %v", err) + } + + opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID) + apiPusher := repo.MustOwner().APIFormat() + if err := PrepareWebhooks(repo, HOOK_EVENT_PUSH, &api.PushPayload{ + Ref: opts.RefName, + Before: opts.OldCommitID, + After: opts.NewCommitID, + CompareURL: setting.AppURL + opts.Commits.CompareURL, + Commits: apiCommits, + Repo: repo.APIFormat(nil), + 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(ACTION_MIRROR_SYNC_PUSH, repo, refName, data) + return mirrorSyncAction(ACTION_MIRROR_SYNC_PUSH, repo, opts.RefName, data) } // MirrorSyncCreateAction adds new action for mirror synchronization of new reference. diff --git a/models/mirror.go b/models/mirror.go index 81afe68b..76bcd9ec 100644 --- a/models/mirror.go +++ b/models/mirror.go @@ -398,6 +398,11 @@ func SyncMirrors() { } for _, result := range results { + // Discard GitHub pull requests, i.e. refs/pull/* + if strings.HasPrefix(result.refName, "refs/pull/") { + continue + } + // Create reference if result.oldCommitID == GIT_SHORT_EMPTY_SHA { if err = MirrorSyncCreateAction(m.Repo, result.refName); err != nil { @@ -415,13 +420,27 @@ func SyncMirrors() { } // Push commits - commits, err := gitRepo.CommitsBetweenIDs(result.newCommitID, result.oldCommitID) + oldCommitID, err := git.GetFullCommitID(gitRepo.Path, result.oldCommitID) + if err != nil { + log.Error(2, "GetFullCommitID [%d]: %v", m.RepoID, err) + continue + } + newCommitID, err := git.GetFullCommitID(gitRepo.Path, result.newCommitID) + if err != nil { + log.Error(2, "GetFullCommitID [%d]: %v", m.RepoID, err) + continue + } + commits, err := gitRepo.CommitsBetweenIDs(newCommitID, oldCommitID) if err != nil { - log.Error(2, "CommitsBetweenIDs [repo_id: %d, new_commit_id: %s, old_commit_id: %s]: %v", m.RepoID, result.newCommitID, result.oldCommitID, err) + log.Error(2, "CommitsBetweenIDs [repo_id: %d, new_commit_id: %s, old_commit_id: %s]: %v", m.RepoID, newCommitID, oldCommitID, err) continue } - pushCommits := ListToPushCommits(commits) - if err = MirrorSyncPushAction(m.Repo, result.refName, pushCommits); err != nil { + if err = MirrorSyncPushAction(m.Repo, MirrorSyncPushActionOptions{ + RefName: result.refName, + OldCommitID: oldCommitID, + NewCommitID: newCommitID, + Commits: ListToPushCommits(commits), + }); err != nil { log.Error(2, "MirrorSyncPushAction [repo_id: %d]: %v", m.RepoID, err) continue } diff --git a/models/webhook.go b/models/webhook.go index 4008d6b9..f9e5d0c5 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -68,8 +68,8 @@ type HookEvents struct { Fork bool `json:"fork"` Push bool `json:"push"` Issues bool `json:"issues"` - IssueComment bool `json:"issue_comment"` PullRequest bool `json:"pull_request"` + IssueComment bool `json:"issue_comment"` Release bool `json:"release"` } @@ -186,18 +186,18 @@ func (w *Webhook) HasIssuesEvent() bool { (w.ChooseEvents && w.HookEvents.Issues) } -// HasIssueCommentEvent returns true if hook enabled issue comment event. -func (w *Webhook) HasIssueCommentEvent() bool { - return w.SendEverything || - (w.ChooseEvents && w.HookEvents.IssueComment) -} - // HasPullRequestEvent returns true if hook enabled pull request event. func (w *Webhook) HasPullRequestEvent() bool { return w.SendEverything || (w.ChooseEvents && w.HookEvents.PullRequest) } +// HasIssueCommentEvent returns true if hook enabled issue comment event. +func (w *Webhook) HasIssueCommentEvent() bool { + return w.SendEverything || + (w.ChooseEvents && w.HookEvents.IssueComment) +} + // HasReleaseEvent returns true if hook enabled release event. func (w *Webhook) HasReleaseEvent() bool { return w.SendEverything || @@ -210,15 +210,15 @@ type eventChecker struct { } func (w *Webhook) EventsArray() []string { - events := make([]string, 0, 7) + events := make([]string, 0, 8) eventCheckers := []eventChecker{ {w.HasCreateEvent, HOOK_EVENT_CREATE}, {w.HasDeleteEvent, HOOK_EVENT_DELETE}, {w.HasForkEvent, HOOK_EVENT_FORK}, {w.HasPushEvent, HOOK_EVENT_PUSH}, {w.HasIssuesEvent, HOOK_EVENT_ISSUES}, - {w.HasIssueCommentEvent, HOOK_EVENT_ISSUE_COMMENT}, {w.HasPullRequestEvent, HOOK_EVENT_PULL_REQUEST}, + {w.HasIssueCommentEvent, HOOK_EVENT_ISSUE_COMMENT}, {w.HasReleaseEvent, HOOK_EVENT_RELEASE}, } for _, c := range eventCheckers { @@ -392,8 +392,8 @@ const ( HOOK_EVENT_FORK HookEventType = "fork" HOOK_EVENT_PUSH HookEventType = "push" HOOK_EVENT_ISSUES HookEventType = "issues" - HOOK_EVENT_ISSUE_COMMENT HookEventType = "issue_comment" HOOK_EVENT_PULL_REQUEST HookEventType = "pull_request" + HOOK_EVENT_ISSUE_COMMENT HookEventType = "issue_comment" HOOK_EVENT_RELEASE HookEventType = "release" ) @@ -549,14 +549,14 @@ func prepareHookTasks(e Engine, repo *Repository, event HookEventType, p api.Pay if !w.HasIssuesEvent() { continue } - case HOOK_EVENT_ISSUE_COMMENT: - if !w.HasIssueCommentEvent() { - continue - } case HOOK_EVENT_PULL_REQUEST: if !w.HasPullRequestEvent() { continue } + case HOOK_EVENT_ISSUE_COMMENT: + if !w.HasIssueCommentEvent() { + continue + } case HOOK_EVENT_RELEASE: if !w.HasReleaseEvent() { continue |