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

fix pull 13664 #11

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
24 changes: 1 addition & 23 deletions models/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,28 +185,6 @@ func TestDeleteWebhookByOrgID(t *testing.T) {
assert.True(t, IsErrWebhookNotExist(err))
}

func TestToHookTaskType(t *testing.T) {
assert.Equal(t, GOGS, ToHookTaskType("gogs"))
assert.Equal(t, SLACK, ToHookTaskType("slack"))
assert.Equal(t, GITEA, ToHookTaskType("gitea"))
assert.Equal(t, TELEGRAM, ToHookTaskType("telegram"))
}

func TestHookTaskType_Name(t *testing.T) {
assert.Equal(t, "gogs", GOGS.Name())
assert.Equal(t, "slack", SLACK.Name())
assert.Equal(t, "gitea", GITEA.Name())
assert.Equal(t, "telegram", TELEGRAM.Name())
}

func TestIsValidHookTaskType(t *testing.T) {
assert.True(t, IsValidHookTaskType("gogs"))
assert.True(t, IsValidHookTaskType("slack"))
assert.True(t, IsValidHookTaskType("gitea"))
assert.True(t, IsValidHookTaskType("telegram"))
assert.False(t, IsValidHookTaskType("invalid"))
}

func TestHookTasks(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
hookTasks, err := HookTasks(1, 1)
Expand All @@ -225,7 +203,7 @@ func TestCreateHookTask(t *testing.T) {
hookTask := &HookTask{
RepoID: 3,
HookID: 3,
Type: GITEA,
Typ: GITEA,
URL: "http://www.example.com/unit_test",
Payloader: &api.PushPayload{},
}
Expand Down
72 changes: 36 additions & 36 deletions modules/notification/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
webhook_module "code.gitea.io/gitea/services/webhook"
webhook_services "code.gitea.io/gitea/services/webhook"
)

type webhookNotifier struct {
Expand Down Expand Up @@ -48,15 +48,15 @@ func (m *webhookNotifier) NotifyIssueClearLabels(doer *models.User, issue *model
return
}

err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventPullRequestLabel, &api.PullRequestPayload{
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventPullRequestLabel, &api.PullRequestPayload{
Action: api.HookIssueLabelCleared,
Index: issue.Index,
PullRequest: convert.ToAPIPullRequest(issue.PullRequest),
Repository: issue.Repo.APIFormat(mode),
Sender: convert.ToUser(doer, false, false),
})
} else {
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueLabel, &api.IssuePayload{
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventIssueLabel, &api.IssuePayload{
Action: api.HookIssueLabelCleared,
Index: issue.Index,
Issue: convert.ToAPIIssue(issue),
Expand All @@ -74,7 +74,7 @@ func (m *webhookNotifier) NotifyForkRepository(doer *models.User, oldRepo, repo
mode, _ := models.AccessLevel(doer, repo)

// forked webhook
if err := webhook_module.PrepareWebhooks(oldRepo, models.HookEventFork, &api.ForkPayload{
if err := webhook_services.PrepareWebhooks(oldRepo, models.HookEventFork, &api.ForkPayload{
Forkee: oldRepo.APIFormat(oldMode),
Repo: repo.APIFormat(mode),
Sender: convert.ToUser(doer, false, false),
Expand All @@ -86,7 +86,7 @@ func (m *webhookNotifier) NotifyForkRepository(doer *models.User, oldRepo, repo

// Add to hook queue for created repo after session commit.
if u.IsOrganization() {
if err := webhook_module.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{
if err := webhook_services.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{
Action: api.HookRepoCreated,
Repository: repo.APIFormat(models.AccessModeOwner),
Organization: convert.ToUser(u, false, false),
Expand All @@ -99,7 +99,7 @@ func (m *webhookNotifier) NotifyForkRepository(doer *models.User, oldRepo, repo

func (m *webhookNotifier) NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
// Add to hook queue for created repo after session commit.
if err := webhook_module.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{
if err := webhook_services.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{
Action: api.HookRepoCreated,
Repository: repo.APIFormat(models.AccessModeOwner),
Organization: convert.ToUser(u, false, false),
Expand All @@ -112,7 +112,7 @@ func (m *webhookNotifier) NotifyCreateRepository(doer *models.User, u *models.Us
func (m *webhookNotifier) NotifyDeleteRepository(doer *models.User, repo *models.Repository) {
u := repo.MustOwner()

if err := webhook_module.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{
if err := webhook_services.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{
Action: api.HookRepoDeleted,
Repository: repo.APIFormat(models.AccessModeOwner),
Organization: convert.ToUser(u, false, false),
Expand Down Expand Up @@ -143,7 +143,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(doer *models.User, issue *mo
apiPullRequest.Action = api.HookIssueAssigned
}
// Assignee comment triggers a webhook
if err := webhook_module.PrepareWebhooks(issue.Repo, models.HookEventPullRequestAssign, apiPullRequest); err != nil {
if err := webhook_services.PrepareWebhooks(issue.Repo, models.HookEventPullRequestAssign, apiPullRequest); err != nil {
log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
return
}
Expand All @@ -161,7 +161,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(doer *models.User, issue *mo
apiIssue.Action = api.HookIssueAssigned
}
// Assignee comment triggers a webhook
if err := webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueAssign, apiIssue); err != nil {
if err := webhook_services.PrepareWebhooks(issue.Repo, models.HookEventIssueAssign, apiIssue); err != nil {
log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
return
}
Expand All @@ -177,7 +177,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(doer *models.User, issue *model
return
}
issue.PullRequest.Issue = issue
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
Action: api.HookIssueEdited,
Index: issue.Index,
Changes: &api.ChangesPayload{
Expand All @@ -190,7 +190,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(doer *models.User, issue *model
Sender: convert.ToUser(doer, false, false),
})
} else {
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{
Action: api.HookIssueEdited,
Index: issue.Index,
Changes: &api.ChangesPayload{
Expand Down Expand Up @@ -229,7 +229,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(doer *models.User, issue *mode
} else {
apiPullRequest.Action = api.HookIssueReOpened
}
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, apiPullRequest)
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, apiPullRequest)
} else {
apiIssue := &api.IssuePayload{
Index: issue.Index,
Expand All @@ -242,7 +242,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(doer *models.User, issue *mode
} else {
apiIssue.Action = api.HookIssueReOpened
}
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssues, apiIssue)
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventIssues, apiIssue)
}
if err != nil {
log.Error("PrepareWebhooks [is_pull: %v, is_closed: %v]: %v", issue.IsPull, isClosed, err)
Expand All @@ -260,7 +260,7 @@ func (m *webhookNotifier) NotifyNewIssue(issue *models.Issue) {
}

mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
if err := webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{
if err := webhook_services.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{
Action: api.HookIssueOpened,
Index: issue.Index,
Issue: convert.ToAPIIssue(issue),
Expand All @@ -286,7 +286,7 @@ func (m *webhookNotifier) NotifyNewPullRequest(pull *models.PullRequest) {
}

mode, _ := models.AccessLevel(pull.Issue.Poster, pull.Issue.Repo)
if err := webhook_module.PrepareWebhooks(pull.Issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
if err := webhook_services.PrepareWebhooks(pull.Issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
Action: api.HookIssueOpened,
Index: pull.Issue.Index,
PullRequest: convert.ToAPIPullRequest(pull),
Expand All @@ -302,7 +302,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(doer *models.User, issue *mod
var err error
if issue.IsPull {
issue.PullRequest.Issue = issue
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
Action: api.HookIssueEdited,
Index: issue.Index,
Changes: &api.ChangesPayload{
Expand All @@ -315,7 +315,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(doer *models.User, issue *mod
Sender: convert.ToUser(doer, false, false),
})
} else {
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{
Action: api.HookIssueEdited,
Index: issue.Index,
Changes: &api.ChangesPayload{
Expand Down Expand Up @@ -352,7 +352,7 @@ func (m *webhookNotifier) NotifyUpdateComment(doer *models.User, c *models.Comme

mode, _ := models.AccessLevel(doer, c.Issue.Repo)
if c.Issue.IsPull {
err = webhook_module.PrepareWebhooks(c.Issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{
err = webhook_services.PrepareWebhooks(c.Issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentEdited,
Issue: convert.ToAPIIssue(c.Issue),
Comment: convert.ToComment(c),
Expand All @@ -366,7 +366,7 @@ func (m *webhookNotifier) NotifyUpdateComment(doer *models.User, c *models.Comme
IsPull: true,
})
} else {
err = webhook_module.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
err = webhook_services.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentEdited,
Issue: convert.ToAPIIssue(c.Issue),
Comment: convert.ToComment(c),
Expand All @@ -392,7 +392,7 @@ func (m *webhookNotifier) NotifyCreateIssueComment(doer *models.User, repo *mode

var err error
if issue.IsPull {
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentCreated,
Issue: convert.ToAPIIssue(issue),
Comment: convert.ToComment(comment),
Expand All @@ -401,7 +401,7 @@ func (m *webhookNotifier) NotifyCreateIssueComment(doer *models.User, repo *mode
IsPull: true,
})
} else {
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentCreated,
Issue: convert.ToAPIIssue(issue),
Comment: convert.ToComment(comment),
Expand Down Expand Up @@ -436,7 +436,7 @@ func (m *webhookNotifier) NotifyDeleteComment(doer *models.User, comment *models
mode, _ := models.AccessLevel(doer, comment.Issue.Repo)

if comment.Issue.IsPull {
err = webhook_module.PrepareWebhooks(comment.Issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{
err = webhook_services.PrepareWebhooks(comment.Issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentDeleted,
Issue: convert.ToAPIIssue(comment.Issue),
Comment: convert.ToComment(comment),
Expand All @@ -445,7 +445,7 @@ func (m *webhookNotifier) NotifyDeleteComment(doer *models.User, comment *models
IsPull: true,
})
} else {
err = webhook_module.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
err = webhook_services.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentDeleted,
Issue: convert.ToAPIIssue(comment.Issue),
Comment: convert.ToComment(comment),
Expand Down Expand Up @@ -485,15 +485,15 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(doer *models.User, issue *mode
log.Error("LoadIssue: %v", err)
return
}
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventPullRequestLabel, &api.PullRequestPayload{
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventPullRequestLabel, &api.PullRequestPayload{
Action: api.HookIssueLabelUpdated,
Index: issue.Index,
PullRequest: convert.ToAPIPullRequest(issue.PullRequest),
Repository: issue.Repo.APIFormat(models.AccessModeNone),
Sender: convert.ToUser(doer, false, false),
})
} else {
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueLabel, &api.IssuePayload{
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventIssueLabel, &api.IssuePayload{
Action: api.HookIssueLabelUpdated,
Index: issue.Index,
Issue: convert.ToAPIIssue(issue),
Expand Down Expand Up @@ -527,15 +527,15 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *models.User, issue *m
log.Error("LoadIssue: %v", err)
return
}
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventPullRequestMilestone, &api.PullRequestPayload{
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventPullRequestMilestone, &api.PullRequestPayload{
Action: hookAction,
Index: issue.Index,
PullRequest: convert.ToAPIPullRequest(issue.PullRequest),
Repository: issue.Repo.APIFormat(mode),
Sender: convert.ToUser(doer, false, false),
})
} else {
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueMilestone, &api.IssuePayload{
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventIssueMilestone, &api.IssuePayload{
Action: hookAction,
Index: issue.Index,
Issue: convert.ToAPIIssue(issue),
Expand All @@ -556,7 +556,7 @@ func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Re
return
}

if err := webhook_module.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
if err := webhook_services.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
Ref: opts.RefFullName,
Before: opts.OldCommitID,
After: opts.NewCommitID,
Expand Down Expand Up @@ -602,7 +602,7 @@ func (*webhookNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *mod
Action: api.HookIssueClosed,
}

err = webhook_module.PrepareWebhooks(pr.Issue.Repo, models.HookEventPullRequest, apiPullRequest)
err = webhook_services.PrepareWebhooks(pr.Issue.Repo, models.HookEventPullRequest, apiPullRequest)
if err != nil {
log.Error("PrepareWebhooks: %v", err)
}
Expand All @@ -621,7 +621,7 @@ func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(doer *models.User,
}
issue.PullRequest.Issue = issue
mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
Action: api.HookIssueEdited,
Index: issue.Index,
Changes: &api.ChangesPayload{
Expand Down Expand Up @@ -665,7 +665,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review
log.Error("models.AccessLevel: %v", err)
return
}
if err := webhook_module.PrepareWebhooks(review.Issue.Repo, reviewHookType, &api.PullRequestPayload{
if err := webhook_services.PrepareWebhooks(review.Issue.Repo, reviewHookType, &api.PullRequestPayload{
Action: api.HookIssueReviewed,
Index: review.Issue.Index,
PullRequest: convert.ToAPIPullRequest(pr),
Expand Down Expand Up @@ -699,7 +699,7 @@ func (m *webhookNotifier) NotifyCreateRef(pusher *models.User, repo *models.Repo
}
gitRepo.Close()

if err = webhook_module.PrepareWebhooks(repo, models.HookEventCreate, &api.CreatePayload{
if err = webhook_services.PrepareWebhooks(repo, models.HookEventCreate, &api.CreatePayload{
Ref: refName,
Sha: shaSum,
RefType: refType,
Expand All @@ -720,7 +720,7 @@ func (m *webhookNotifier) NotifyPullRequestSynchronized(doer *models.User, pr *m
return
}

if err := webhook_module.PrepareWebhooks(pr.Issue.Repo, models.HookEventPullRequestSync, &api.PullRequestPayload{
if err := webhook_services.PrepareWebhooks(pr.Issue.Repo, models.HookEventPullRequestSync, &api.PullRequestPayload{
Action: api.HookIssueSynchronized,
Index: pr.Issue.Index,
PullRequest: convert.ToAPIPullRequest(pr),
Expand All @@ -736,7 +736,7 @@ func (m *webhookNotifier) NotifyDeleteRef(pusher *models.User, repo *models.Repo
apiRepo := repo.APIFormat(models.AccessModeNone)
refName := git.RefEndName(refFullName)

if err := webhook_module.PrepareWebhooks(repo, models.HookEventDelete, &api.DeletePayload{
if err := webhook_services.PrepareWebhooks(repo, models.HookEventDelete, &api.DeletePayload{
Ref: refName,
RefType: refType,
PusherType: api.PusherTypeUser,
Expand All @@ -754,7 +754,7 @@ func sendReleaseHook(doer *models.User, rel *models.Release, action api.HookRele
}

mode, _ := models.AccessLevel(rel.Publisher, rel.Repo)
if err := webhook_module.PrepareWebhooks(rel.Repo, models.HookEventRelease, &api.ReleasePayload{
if err := webhook_services.PrepareWebhooks(rel.Repo, models.HookEventRelease, &api.ReleasePayload{
Action: action,
Release: convert.ToRelease(rel),
Repository: rel.Repo.APIFormat(mode),
Expand Down Expand Up @@ -784,7 +784,7 @@ func (m *webhookNotifier) NotifySyncPushCommits(pusher *models.User, repo *model
return
}

if err := webhook_module.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
if err := webhook_services.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
Ref: opts.RefFullName,
Before: opts.OldCommitID,
After: opts.NewCommitID,
Expand Down
Loading