Browse Source

models/mirror: shot push webhook after synced commits (#4528)

pull/5254/merge
Unknwon 7 years ago
parent
commit
c08aab90ec
No known key found for this signature in database
GPG Key ID: 7A02C406FAC875A2
  1. 8
      conf/locale/locale_en-US.ini
  2. 2
      gogs.go
  3. 41
      models/action.go
  4. 27
      models/mirror.go
  5. 28
      models/webhook.go
  6. 4
      pkg/bindata/bindata.go
  7. 2
      templates/.VERSION
  8. 16
      templates/repo/settings/webhook/settings.tmpl

8
conf/locale/locale_en-US.ini

@ -810,9 +810,9 @@ settings.slack_username = Username
settings.slack_icon_url = Icon URL settings.slack_icon_url = Icon URL
settings.slack_color = Color settings.slack_color = Color
settings.event_desc = When should this webhook be triggered? settings.event_desc = When should this webhook be triggered?
settings.event_push_only = Just the <code>push</code> event. settings.event_push_only = Just the <code>push</code> event
settings.event_send_everything = I need <strong>everything</strong>. settings.event_send_everything = I need <strong>everything</strong>
settings.event_choose = Let me choose what I need. settings.event_choose = Let me choose what I need
settings.event_create = Create settings.event_create = Create
settings.event_create_desc = Branch or tag created settings.event_create_desc = Branch or tag created
settings.event_delete = Delete settings.event_delete = Delete
@ -829,8 +829,6 @@ settings.event_issue_comment = Issue Comment
settings.event_issue_comment_desc = Issue comment created, edited, or deleted. settings.event_issue_comment_desc = Issue comment created, edited, or deleted.
settings.event_release = Release settings.event_release = Release
settings.event_release_desc = Release published in a repository. settings.event_release_desc = Release published in a repository.
settings.event_mirror_sync = Mirror Sync
settings.event_mirror_sync_desc = Mirror commits pushed, reference created or deleted from upstream.
settings.active = Active settings.active = Active
settings.active_helper = Details regarding the event which triggered the hook will be delivered as well. settings.active_helper = Details regarding the event which triggered the hook will be delivered as well.
settings.add_hook_success = New webhook has been added. settings.add_hook_success = New webhook has been added.

2
gogs.go

@ -16,7 +16,7 @@ import (
"github.com/gogs/gogs/pkg/setting" "github.com/gogs/gogs/pkg/setting"
) )
const APP_VER = "0.11.52.0603" const APP_VER = "0.11.53.0603"
func init() { func init() {
setting.AppVer = APP_VER setting.AppVer = APP_VER

41
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)) commits := make([]*api.PayloadCommit, len(pc.Commits))
for i, commit := range pc.Commits { for i, commit := range pc.Commits {
authorUsername := "" authorUsername := ""
@ -281,7 +281,7 @@ func (pc *PushCommits) ToApiPayloadCommits(repoPath, repoLink string) ([]*api.Pa
commits[i] = &api.PayloadCommit{ commits[i] = &api.PayloadCommit{
ID: commit.Sha1, ID: commit.Sha1,
Message: commit.Message, Message: commit.Message,
URL: fmt.Sprintf("%s/commit/%s", repoLink, commit.Sha1), URL: fmt.Sprintf("%s/commit/%s", repoURL, commit.Sha1),
Author: &api.PayloadUser{ Author: &api.PayloadUser{
Name: commit.AuthorName, Name: commit.AuthorName,
Email: commit.AuthorEmail, 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. // MirrorSyncPushAction adds new action for mirror synchronization of pushed commits.
func MirrorSyncPushAction(repo *Repository, refName string, commits *PushCommits) error { func MirrorSyncPushAction(repo *Repository, opts MirrorSyncPushActionOptions) error {
data, err := json.Marshal(commits) 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 { if err != nil {
return err 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. // MirrorSyncCreateAction adds new action for mirror synchronization of new reference.

27
models/mirror.go

@ -398,6 +398,11 @@ func SyncMirrors() {
} }
for _, result := range results { for _, result := range results {
// Discard GitHub pull requests, i.e. refs/pull/*
if strings.HasPrefix(result.refName, "refs/pull/") {
continue
}
// Create reference // Create reference
if result.oldCommitID == GIT_SHORT_EMPTY_SHA { if result.oldCommitID == GIT_SHORT_EMPTY_SHA {
if err = MirrorSyncCreateAction(m.Repo, result.refName); err != nil { if err = MirrorSyncCreateAction(m.Repo, result.refName); err != nil {
@ -415,13 +420,27 @@ func SyncMirrors() {
} }
// Push commits // 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 { 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 continue
} }
pushCommits := ListToPushCommits(commits) if err = MirrorSyncPushAction(m.Repo, MirrorSyncPushActionOptions{
if err = MirrorSyncPushAction(m.Repo, result.refName, pushCommits); err != nil { RefName: result.refName,
OldCommitID: oldCommitID,
NewCommitID: newCommitID,
Commits: ListToPushCommits(commits),
}); err != nil {
log.Error(2, "MirrorSyncPushAction [repo_id: %d]: %v", m.RepoID, err) log.Error(2, "MirrorSyncPushAction [repo_id: %d]: %v", m.RepoID, err)
continue continue
} }

28
models/webhook.go

@ -68,8 +68,8 @@ type HookEvents struct {
Fork bool `json:"fork"` Fork bool `json:"fork"`
Push bool `json:"push"` Push bool `json:"push"`
Issues bool `json:"issues"` Issues bool `json:"issues"`
IssueComment bool `json:"issue_comment"`
PullRequest bool `json:"pull_request"` PullRequest bool `json:"pull_request"`
IssueComment bool `json:"issue_comment"`
Release bool `json:"release"` Release bool `json:"release"`
} }
@ -186,18 +186,18 @@ func (w *Webhook) HasIssuesEvent() bool {
(w.ChooseEvents && w.HookEvents.Issues) (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. // HasPullRequestEvent returns true if hook enabled pull request event.
func (w *Webhook) HasPullRequestEvent() bool { func (w *Webhook) HasPullRequestEvent() bool {
return w.SendEverything || return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.PullRequest) (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. // HasReleaseEvent returns true if hook enabled release event.
func (w *Webhook) HasReleaseEvent() bool { func (w *Webhook) HasReleaseEvent() bool {
return w.SendEverything || return w.SendEverything ||
@ -210,15 +210,15 @@ type eventChecker struct {
} }
func (w *Webhook) EventsArray() []string { func (w *Webhook) EventsArray() []string {
events := make([]string, 0, 7) events := make([]string, 0, 8)
eventCheckers := []eventChecker{ eventCheckers := []eventChecker{
{w.HasCreateEvent, HOOK_EVENT_CREATE}, {w.HasCreateEvent, HOOK_EVENT_CREATE},
{w.HasDeleteEvent, HOOK_EVENT_DELETE}, {w.HasDeleteEvent, HOOK_EVENT_DELETE},
{w.HasForkEvent, HOOK_EVENT_FORK}, {w.HasForkEvent, HOOK_EVENT_FORK},
{w.HasPushEvent, HOOK_EVENT_PUSH}, {w.HasPushEvent, HOOK_EVENT_PUSH},
{w.HasIssuesEvent, HOOK_EVENT_ISSUES}, {w.HasIssuesEvent, HOOK_EVENT_ISSUES},
{w.HasIssueCommentEvent, HOOK_EVENT_ISSUE_COMMENT},
{w.HasPullRequestEvent, HOOK_EVENT_PULL_REQUEST}, {w.HasPullRequestEvent, HOOK_EVENT_PULL_REQUEST},
{w.HasIssueCommentEvent, HOOK_EVENT_ISSUE_COMMENT},
{w.HasReleaseEvent, HOOK_EVENT_RELEASE}, {w.HasReleaseEvent, HOOK_EVENT_RELEASE},
} }
for _, c := range eventCheckers { for _, c := range eventCheckers {
@ -392,8 +392,8 @@ const (
HOOK_EVENT_FORK HookEventType = "fork" HOOK_EVENT_FORK HookEventType = "fork"
HOOK_EVENT_PUSH HookEventType = "push" HOOK_EVENT_PUSH HookEventType = "push"
HOOK_EVENT_ISSUES HookEventType = "issues" HOOK_EVENT_ISSUES HookEventType = "issues"
HOOK_EVENT_ISSUE_COMMENT HookEventType = "issue_comment"
HOOK_EVENT_PULL_REQUEST HookEventType = "pull_request" HOOK_EVENT_PULL_REQUEST HookEventType = "pull_request"
HOOK_EVENT_ISSUE_COMMENT HookEventType = "issue_comment"
HOOK_EVENT_RELEASE HookEventType = "release" HOOK_EVENT_RELEASE HookEventType = "release"
) )
@ -549,14 +549,14 @@ func prepareHookTasks(e Engine, repo *Repository, event HookEventType, p api.Pay
if !w.HasIssuesEvent() { if !w.HasIssuesEvent() {
continue continue
} }
case HOOK_EVENT_ISSUE_COMMENT:
if !w.HasIssueCommentEvent() {
continue
}
case HOOK_EVENT_PULL_REQUEST: case HOOK_EVENT_PULL_REQUEST:
if !w.HasPullRequestEvent() { if !w.HasPullRequestEvent() {
continue continue
} }
case HOOK_EVENT_ISSUE_COMMENT:
if !w.HasIssueCommentEvent() {
continue
}
case HOOK_EVENT_RELEASE: case HOOK_EVENT_RELEASE:
if !w.HasReleaseEvent() { if !w.HasReleaseEvent() {
continue continue

4
pkg/bindata/bindata.go

File diff suppressed because one or more lines are too long

2
templates/.VERSION

@ -1 +1 @@
0.11.52.0603 0.11.53.0603

16
templates/repo/settings/webhook/settings.tmpl

@ -72,23 +72,23 @@
</div> </div>
</div> </div>
</div> </div>
<!-- Issue Comment --> <!-- Pull Request -->
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input class="hidden" name="issue_comment" type="checkbox" tabindex="0" {{if .Webhook.IssueComment}}checked{{end}}> <input class="hidden" name="pull_request" type="checkbox" tabindex="0" {{if .Webhook.PullRequest}}checked{{end}}>
<label>{{.i18n.Tr "repo.settings.event_issue_comment"}}</label> <label>{{.i18n.Tr "repo.settings.event_pull_request"}}</label>
<span class="help">{{.i18n.Tr "repo.settings.event_issue_comment_desc"}}</span> <span class="help">{{.i18n.Tr "repo.settings.event_pull_request_desc"}}</span>
</div> </div>
</div> </div>
</div> </div>
<!-- Pull Request --> <!-- Issue Comment -->
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input class="hidden" name="pull_request" type="checkbox" tabindex="0" {{if .Webhook.PullRequest}}checked{{end}}> <input class="hidden" name="issue_comment" type="checkbox" tabindex="0" {{if .Webhook.IssueComment}}checked{{end}}>
<label>{{.i18n.Tr "repo.settings.event_pull_request"}}</label> <label>{{.i18n.Tr "repo.settings.event_issue_comment"}}</label>
<span class="help">{{.i18n.Tr "repo.settings.event_pull_request_desc"}}</span> <span class="help">{{.i18n.Tr "repo.settings.event_issue_comment_desc"}}</span>
</div> </div>
</div> </div>
</div> </div>

Loading…
Cancel
Save