From 1037f8f637339ac73739df42ebe853ddb39b6d98 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 15 Feb 2024 05:06:12 +0000 Subject: [PATCH 1/5] add IssueChangeAssignee --- services/actions/notifier.go | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/services/actions/notifier.go b/services/actions/notifier.go index 0b4fed5db1282..a0a12da47f3e9 100644 --- a/services/actions/notifier.go +++ b/services/actions/notifier.go @@ -101,6 +101,62 @@ func (n *actionsNotifier) IssueChangeStatus(ctx context.Context, doer *user_mode Notify(ctx) } +// IssueChangeAssignee notifies assignee to notifiers +func (n *actionsNotifier) IssueChangeAssignee(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, assignee *user_model.User, removed bool, comment *issues_model.Comment) { + ctx = withMethod(ctx, "IssueChangeAssignee") + + var err error + if err = issue.LoadRepo(ctx); err != nil { + log.Error("LoadRepo: %v", err) + return + } + + if err = issue.LoadPoster(ctx); err != nil { + log.Error("LoadPoster: %v", err) + return + } + + permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster) + var action api.HookIssueAction + if removed { + action = api.HookIssueUnassigned + } else { + action = api.HookIssueAssigned + } + if issue.IsPull { + if err = issue.LoadPullRequest(ctx); err != nil { + log.Error("loadPullRequest: %v", err) + return + } + if err = issue.PullRequest.LoadIssue(ctx); err != nil { + log.Error("LoadIssue: %v", err) + return + } + newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestAssign). + WithDoer(doer). + WithPayload(&api.PullRequestPayload{ + Action: action, + Index: issue.Index, + PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), + Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}), + Sender: convert.ToUser(ctx, doer, nil), + }). + WithPullRequest(issue.PullRequest). + Notify(ctx) + return + } + newNotifyInputFromIssue(issue, webhook_module.HookEventIssueAssign). + WithDoer(doer). + WithPayload(&api.IssuePayload{ + Action: action, + Index: issue.Index, + Issue: convert.ToAPIIssue(ctx, issue), + Repository: convert.ToRepo(ctx, issue.Repo, permission), + Sender: convert.ToUser(ctx, doer, nil), + }). + Notify(ctx) +} + func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, _, _ []*issues_model.Label, ) { From 79f08593a3bfb85732751a5d16f809ca69a3cb83 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 15 Feb 2024 05:45:40 +0000 Subject: [PATCH 2/5] add PullRequestReviewRequest --- modules/actions/github.go | 3 ++- modules/actions/workflows.go | 7 ++++--- services/actions/notifier.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/modules/actions/github.go b/modules/actions/github.go index fafea4e11a805..b93758a726a24 100644 --- a/modules/actions/github.go +++ b/modules/actions/github.go @@ -52,7 +52,8 @@ func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEvent case webhook_module.HookEventPullRequest, webhook_module.HookEventPullRequestSync, webhook_module.HookEventPullRequestAssign, - webhook_module.HookEventPullRequestLabel: + webhook_module.HookEventPullRequestLabel, + webhook_module.HookEventPullRequestReviewRequest: return true default: diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index a883f4181b2c8..d14a9818b74de 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -221,7 +221,8 @@ func detectMatched(gitRepo *git.Repository, commit *git.Commit, triggedEvent web webhook_module.HookEventPullRequest, webhook_module.HookEventPullRequestSync, webhook_module.HookEventPullRequestAssign, - webhook_module.HookEventPullRequestLabel: + webhook_module.HookEventPullRequestLabel, + webhook_module.HookEventPullRequestReviewRequest: return matchPullRequestEvent(gitRepo, commit, payload.(*api.PullRequestPayload), evt) case // pull_request_review @@ -397,13 +398,13 @@ func matchPullRequestEvent(gitRepo *git.Repository, commit *git.Commit, prPayloa } else { // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request // Actions with the same name: - // opened, edited, closed, reopened, assigned, unassigned + // opened, edited, closed, reopened, assigned, unassigned, review_requested, review_request_removed // Actions need to be converted: // synchronized -> synchronize // label_updated -> labeled // label_cleared -> unlabeled // Unsupported activity types: - // converted_to_draft, ready_for_review, locked, unlocked, review_requested, review_request_removed, auto_merge_enabled, auto_merge_disabled + // converted_to_draft, ready_for_review, locked, unlocked, auto_merge_enabled, auto_merge_disabled action := prPayload.Action switch action { diff --git a/services/actions/notifier.go b/services/actions/notifier.go index a0a12da47f3e9..622c8db647ef0 100644 --- a/services/actions/notifier.go +++ b/services/actions/notifier.go @@ -361,6 +361,39 @@ func (n *actionsNotifier) PullRequestReview(ctx context.Context, pr *issues_mode }).Notify(ctx) } +func (n *actionsNotifier) PullRequestReviewRequest(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, reviewer *user_model.User, isRequest bool, comment *issues_model.Comment) { + if !issue.IsPull { + log.Warn("PullRequestReviewRequest: issue is not a pull request: %v", issue.ID) + return + } + + ctx = withMethod(ctx, "PullRequestReviewRequest") + + permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, doer) + if err := issue.LoadPullRequest(ctx); err != nil { + log.Error("LoadPullRequest failed: %v", err) + return + } + var action api.HookIssueAction + if isRequest { + action = api.HookIssueReviewRequested + } else { + action = api.HookIssueReviewRequestRemoved + } + newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestReviewRequest). + WithDoer(doer). + WithPayload(&api.PullRequestPayload{ + Action: action, + Index: issue.Index, + PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), + RequestedReviewer: convert.ToUser(ctx, reviewer, nil), + Repository: convert.ToRepo(ctx, issue.Repo, permission), + Sender: convert.ToUser(ctx, doer, nil), + }). + WithPullRequest(issue.PullRequest). + Notify(ctx) +} + func (*actionsNotifier) MergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) { ctx = withMethod(ctx, "MergePullRequest") From 6cb28317d1be71cec65395f7838fbb62e3aa5469 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 15 Feb 2024 08:11:12 +0000 Subject: [PATCH 3/5] add IssueChangeMilestone --- modules/actions/github.go | 3 +- modules/actions/workflows.go | 5 ++-- services/actions/notifier.go | 58 +++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/modules/actions/github.go b/modules/actions/github.go index b93758a726a24..18917c5118d66 100644 --- a/modules/actions/github.go +++ b/modules/actions/github.go @@ -53,7 +53,8 @@ func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEvent webhook_module.HookEventPullRequestSync, webhook_module.HookEventPullRequestAssign, webhook_module.HookEventPullRequestLabel, - webhook_module.HookEventPullRequestReviewRequest: + webhook_module.HookEventPullRequestReviewRequest, + webhook_module.HookEventPullRequestMilestone: return true default: diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index d14a9818b74de..6d398eb84aaa3 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -222,7 +222,8 @@ func detectMatched(gitRepo *git.Repository, commit *git.Commit, triggedEvent web webhook_module.HookEventPullRequestSync, webhook_module.HookEventPullRequestAssign, webhook_module.HookEventPullRequestLabel, - webhook_module.HookEventPullRequestReviewRequest: + webhook_module.HookEventPullRequestReviewRequest, + webhook_module.HookEventPullRequestMilestone: return matchPullRequestEvent(gitRepo, commit, payload.(*api.PullRequestPayload), evt) case // pull_request_review @@ -398,7 +399,7 @@ func matchPullRequestEvent(gitRepo *git.Repository, commit *git.Commit, prPayloa } else { // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request // Actions with the same name: - // opened, edited, closed, reopened, assigned, unassigned, review_requested, review_request_removed + // opened, edited, closed, reopened, assigned, unassigned, review_requested, review_request_removed, milestoned, demilestoned // Actions need to be converted: // synchronized -> synchronize // label_updated -> labeled diff --git a/services/actions/notifier.go b/services/actions/notifier.go index 622c8db647ef0..50bba7d066717 100644 --- a/services/actions/notifier.go +++ b/services/actions/notifier.go @@ -101,7 +101,7 @@ func (n *actionsNotifier) IssueChangeStatus(ctx context.Context, doer *user_mode Notify(ctx) } -// IssueChangeAssignee notifies assignee to notifiers +// IssueChangeAssignee notifies assigned or unassigned to notifiers func (n *actionsNotifier) IssueChangeAssignee(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, assignee *user_model.User, removed bool, comment *issues_model.Comment) { ctx = withMethod(ctx, "IssueChangeAssignee") @@ -157,6 +157,62 @@ func (n *actionsNotifier) IssueChangeAssignee(ctx context.Context, doer *user_mo Notify(ctx) } +// IssueChangeMilestone notifies assignee to notifiers +func (n *actionsNotifier) IssueChangeMilestone(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) { + ctx = withMethod(ctx, "IssueChangeMilestone") + + var err error + if err = issue.LoadRepo(ctx); err != nil { + log.Error("LoadRepo: %v", err) + return + } + + if err = issue.LoadPoster(ctx); err != nil { + log.Error("LoadPoster: %v", err) + return + } + + permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster) + var action api.HookIssueAction + if issue.MilestoneID > 0 { + action = api.HookIssueMilestoned + } else { + action = api.HookIssueDemilestoned + } + if issue.IsPull { + if err = issue.LoadPullRequest(ctx); err != nil { + log.Error("loadPullRequest: %v", err) + return + } + if err = issue.PullRequest.LoadIssue(ctx); err != nil { + log.Error("LoadIssue: %v", err) + return + } + newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestMilestone). + WithDoer(doer). + WithPayload(&api.PullRequestPayload{ + Action: action, + Index: issue.Index, + PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), + Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}), + Sender: convert.ToUser(ctx, doer, nil), + }). + WithPullRequest(issue.PullRequest). + Notify(ctx) + return + } + newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestMilestone). + WithDoer(doer). + WithPayload(&api.IssuePayload{ + Action: action, + Index: issue.Index, + Issue: convert.ToAPIIssue(ctx, issue), + Repository: convert.ToRepo(ctx, issue.Repo, permission), + Sender: convert.ToUser(ctx, doer, nil), + }). + Notify(ctx) +} + func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, _, _ []*issues_model.Label, ) { From a69df17b7a6350ad9c60e2c8d8efec157c15c947 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 15 Feb 2024 08:11:37 +0000 Subject: [PATCH 4/5] add unsupported activity types --- modules/actions/workflows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 6d398eb84aaa3..2db4a9296f0e8 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -405,7 +405,7 @@ func matchPullRequestEvent(gitRepo *git.Repository, commit *git.Commit, prPayloa // label_updated -> labeled // label_cleared -> unlabeled // Unsupported activity types: - // converted_to_draft, ready_for_review, locked, unlocked, auto_merge_enabled, auto_merge_disabled + // converted_to_draft, ready_for_review, locked, unlocked, auto_merge_enabled, auto_merge_disabled, enqueued, dequeued action := prPayload.Action switch action { From 85e07990f923d2458dcdfb7c91fa2a21abb2034f Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Fri, 16 Feb 2024 01:27:00 +0000 Subject: [PATCH 5/5] merge similar codes --- services/actions/notifier.go | 107 ++++------------------------------- 1 file changed, 10 insertions(+), 97 deletions(-) diff --git a/services/actions/notifier.go b/services/actions/notifier.go index 50bba7d066717..093607f05cc58 100644 --- a/services/actions/notifier.go +++ b/services/actions/notifier.go @@ -105,119 +105,36 @@ func (n *actionsNotifier) IssueChangeStatus(ctx context.Context, doer *user_mode func (n *actionsNotifier) IssueChangeAssignee(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, assignee *user_model.User, removed bool, comment *issues_model.Comment) { ctx = withMethod(ctx, "IssueChangeAssignee") - var err error - if err = issue.LoadRepo(ctx); err != nil { - log.Error("LoadRepo: %v", err) - return - } - - if err = issue.LoadPoster(ctx); err != nil { - log.Error("LoadPoster: %v", err) - return - } - - permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster) var action api.HookIssueAction if removed { action = api.HookIssueUnassigned } else { action = api.HookIssueAssigned } - if issue.IsPull { - if err = issue.LoadPullRequest(ctx); err != nil { - log.Error("loadPullRequest: %v", err) - return - } - if err = issue.PullRequest.LoadIssue(ctx); err != nil { - log.Error("LoadIssue: %v", err) - return - } - newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestAssign). - WithDoer(doer). - WithPayload(&api.PullRequestPayload{ - Action: action, - Index: issue.Index, - PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), - Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}), - Sender: convert.ToUser(ctx, doer, nil), - }). - WithPullRequest(issue.PullRequest). - Notify(ctx) - return - } - newNotifyInputFromIssue(issue, webhook_module.HookEventIssueAssign). - WithDoer(doer). - WithPayload(&api.IssuePayload{ - Action: action, - Index: issue.Index, - Issue: convert.ToAPIIssue(ctx, issue), - Repository: convert.ToRepo(ctx, issue.Repo, permission), - Sender: convert.ToUser(ctx, doer, nil), - }). - Notify(ctx) + notifyIssueChange(ctx, doer, issue, webhook_module.HookEventPullRequestAssign, action) } // IssueChangeMilestone notifies assignee to notifiers func (n *actionsNotifier) IssueChangeMilestone(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) { ctx = withMethod(ctx, "IssueChangeMilestone") - var err error - if err = issue.LoadRepo(ctx); err != nil { - log.Error("LoadRepo: %v", err) - return - } - - if err = issue.LoadPoster(ctx); err != nil { - log.Error("LoadPoster: %v", err) - return - } - - permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster) var action api.HookIssueAction if issue.MilestoneID > 0 { action = api.HookIssueMilestoned } else { action = api.HookIssueDemilestoned } - if issue.IsPull { - if err = issue.LoadPullRequest(ctx); err != nil { - log.Error("loadPullRequest: %v", err) - return - } - if err = issue.PullRequest.LoadIssue(ctx); err != nil { - log.Error("LoadIssue: %v", err) - return - } - newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestMilestone). - WithDoer(doer). - WithPayload(&api.PullRequestPayload{ - Action: action, - Index: issue.Index, - PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), - Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}), - Sender: convert.ToUser(ctx, doer, nil), - }). - WithPullRequest(issue.PullRequest). - Notify(ctx) - return - } - newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestMilestone). - WithDoer(doer). - WithPayload(&api.IssuePayload{ - Action: action, - Index: issue.Index, - Issue: convert.ToAPIIssue(ctx, issue), - Repository: convert.ToRepo(ctx, issue.Repo, permission), - Sender: convert.ToUser(ctx, doer, nil), - }). - Notify(ctx) + notifyIssueChange(ctx, doer, issue, webhook_module.HookEventPullRequestMilestone, action) } func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, _, _ []*issues_model.Label, ) { ctx = withMethod(ctx, "IssueChangeLabels") + notifyIssueChange(ctx, doer, issue, webhook_module.HookEventPullRequestLabel, api.HookIssueLabelUpdated) +} +func notifyIssueChange(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, event webhook_module.HookEventType, action api.HookIssueAction) { var err error if err = issue.LoadRepo(ctx); err != nil { log.Error("LoadRepo: %v", err) @@ -229,20 +146,15 @@ func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_mode return } - permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster) if issue.IsPull { if err = issue.LoadPullRequest(ctx); err != nil { log.Error("loadPullRequest: %v", err) return } - if err = issue.PullRequest.LoadIssue(ctx); err != nil { - log.Error("LoadIssue: %v", err) - return - } - newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestLabel). + newNotifyInputFromIssue(issue, event). WithDoer(doer). WithPayload(&api.PullRequestPayload{ - Action: api.HookIssueLabelUpdated, + Action: action, Index: issue.Index, PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}), @@ -252,10 +164,11 @@ func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_mode Notify(ctx) return } - newNotifyInputFromIssue(issue, webhook_module.HookEventIssueLabel). + permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster) + newNotifyInputFromIssue(issue, event). WithDoer(doer). WithPayload(&api.IssuePayload{ - Action: api.HookIssueLabelUpdated, + Action: action, Index: issue.Index, Issue: convert.ToAPIIssue(ctx, issue), Repository: convert.ToRepo(ctx, issue.Repo, permission),