2019-11-03 13:59:09 -07:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-03 13:59:09 -07:00
|
|
|
|
2023-09-05 07:00:52 -06:00
|
|
|
package feed
|
2019-11-03 13:59:09 -07:00
|
|
|
|
|
|
|
import (
|
2022-11-19 01:12:33 -07:00
|
|
|
"context"
|
2019-11-03 13:59:09 -07:00
|
|
|
"fmt"
|
2019-11-15 01:06:11 -07:00
|
|
|
"path"
|
2019-11-13 19:57:36 -07:00
|
|
|
"strings"
|
2019-11-03 13:59:09 -07:00
|
|
|
|
2022-08-24 20:31:57 -06:00
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
2022-06-13 03:37:59 -06:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-12-09 18:27:50 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2023-05-25 19:04:48 -06:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2021-07-24 10:03:58 -06:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2019-11-03 13:59:09 -07:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-01-10 02:34:21 -07:00
|
|
|
"code.gitea.io/gitea/modules/repository"
|
2021-12-08 22:41:17 -07:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2023-09-05 12:37:47 -06:00
|
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
2019-11-03 13:59:09 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type actionNotifier struct {
|
2023-09-05 12:37:47 -06:00
|
|
|
notify_service.NullNotifier
|
2019-11-03 13:59:09 -07:00
|
|
|
}
|
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
var _ notify_service.Notifier = &actionNotifier{}
|
2019-11-03 13:59:09 -07:00
|
|
|
|
2023-09-05 07:00:52 -06:00
|
|
|
func Init() error {
|
2023-09-05 12:37:47 -06:00
|
|
|
notify_service.RegisterNotifier(NewNotifier())
|
2023-09-05 07:00:52 -06:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-08 13:54:50 -07:00
|
|
|
// NewNotifier create a new actionNotifier notifier
|
2023-09-05 12:37:47 -06:00
|
|
|
func NewNotifier() notify_service.Notifier {
|
2019-11-03 13:59:09 -07:00
|
|
|
return &actionNotifier{}
|
|
|
|
}
|
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (a *actionNotifier) NewIssue(ctx context.Context, issue *issues_model.Issue, mentions []*user_model.User) {
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := issue.LoadPoster(ctx); err != nil {
|
2019-11-03 13:59:09 -07:00
|
|
|
log.Error("issue.LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
2019-11-03 13:59:09 -07:00
|
|
|
log.Error("issue.LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
repo := issue.Repo
|
|
|
|
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2019-11-03 13:59:09 -07:00
|
|
|
ActUserID: issue.Poster.ID,
|
|
|
|
ActUser: issue.Poster,
|
2022-08-24 20:31:57 -06:00
|
|
|
OpType: activities_model.ActionCreateIssue,
|
2019-11-03 13:59:09 -07:00
|
|
|
Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
IsPrivate: repo.IsPrivate,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("NotifyWatchers: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
// IssueChangeStatus notifies close or reopen issue to notifiers
|
|
|
|
func (a *actionNotifier) IssueChangeStatus(ctx context.Context, doer *user_model.User, commitID string, issue *issues_model.Issue, actionComment *issues_model.Comment, closeOrReopen bool) {
|
2019-12-15 14:57:34 -07:00
|
|
|
// Compose comment action, could be plain comment, close or reopen issue/pull request.
|
|
|
|
// This object will be used to notify watchers in the end of function.
|
2022-08-24 20:31:57 -06:00
|
|
|
act := &activities_model.Action{
|
2019-12-15 14:57:34 -07:00
|
|
|
ActUserID: doer.ID,
|
|
|
|
ActUser: doer,
|
|
|
|
Content: fmt.Sprintf("%d|%s", issue.Index, ""),
|
|
|
|
RepoID: issue.Repo.ID,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Comment: actionComment,
|
|
|
|
CommentID: actionComment.ID,
|
|
|
|
IsPrivate: issue.Repo.IsPrivate,
|
|
|
|
}
|
|
|
|
// Check comment type.
|
|
|
|
if closeOrReopen {
|
2022-08-24 20:31:57 -06:00
|
|
|
act.OpType = activities_model.ActionCloseIssue
|
2019-12-15 14:57:34 -07:00
|
|
|
if issue.IsPull {
|
2022-08-24 20:31:57 -06:00
|
|
|
act.OpType = activities_model.ActionClosePullRequest
|
2019-12-15 14:57:34 -07:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-24 20:31:57 -06:00
|
|
|
act.OpType = activities_model.ActionReopenIssue
|
2019-12-15 14:57:34 -07:00
|
|
|
if issue.IsPull {
|
2022-08-24 20:31:57 -06:00
|
|
|
act.OpType = activities_model.ActionReopenPullRequest
|
2019-12-15 14:57:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify watchers for whatever action comes in, ignore if no action type.
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, act); err != nil {
|
2019-12-15 14:57:34 -07:00
|
|
|
log.Error("NotifyWatchers: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
// CreateIssueComment notifies comment on an issue to notifiers
|
|
|
|
func (a *actionNotifier) CreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository,
|
2022-06-13 03:37:59 -06:00
|
|
|
issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User,
|
2022-02-23 13:16:07 -07:00
|
|
|
) {
|
2022-08-24 20:31:57 -06:00
|
|
|
act := &activities_model.Action{
|
2019-12-15 14:57:34 -07:00
|
|
|
ActUserID: doer.ID,
|
|
|
|
ActUser: doer,
|
|
|
|
RepoID: issue.Repo.ID,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Comment: comment,
|
|
|
|
CommentID: comment.ID,
|
|
|
|
IsPrivate: issue.Repo.IsPrivate,
|
|
|
|
}
|
2020-09-18 01:38:21 -06:00
|
|
|
|
2021-12-08 22:41:17 -07:00
|
|
|
truncatedContent, truncatedRight := util.SplitStringAtByteN(comment.Content, 200)
|
|
|
|
if truncatedRight != "" {
|
|
|
|
// in case the content is in a Latin family language, we remove the last broken word.
|
|
|
|
lastSpaceIdx := strings.LastIndex(truncatedContent, " ")
|
|
|
|
if lastSpaceIdx != -1 && (len(truncatedContent)-lastSpaceIdx < 15) {
|
|
|
|
truncatedContent = truncatedContent[:lastSpaceIdx] + "…"
|
|
|
|
}
|
2020-09-18 01:38:21 -06:00
|
|
|
}
|
2021-12-08 22:41:17 -07:00
|
|
|
act.Content = fmt.Sprintf("%d|%s", issue.Index, truncatedContent)
|
2020-09-18 01:38:21 -06:00
|
|
|
|
2019-12-22 01:29:26 -07:00
|
|
|
if issue.IsPull {
|
2022-08-24 20:31:57 -06:00
|
|
|
act.OpType = activities_model.ActionCommentPull
|
2019-12-22 01:29:26 -07:00
|
|
|
} else {
|
2022-08-24 20:31:57 -06:00
|
|
|
act.OpType = activities_model.ActionCommentIssue
|
2019-12-22 01:29:26 -07:00
|
|
|
}
|
2019-12-15 14:57:34 -07:00
|
|
|
|
|
|
|
// Notify watchers for whatever action comes in, ignore if no action type.
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, act); err != nil {
|
2019-12-15 14:57:34 -07:00
|
|
|
log.Error("NotifyWatchers: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (a *actionNotifier) NewPullRequest(ctx context.Context, pull *issues_model.PullRequest, mentions []*user_model.User) {
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := pull.LoadIssue(ctx); err != nil {
|
2019-11-03 13:59:09 -07:00
|
|
|
log.Error("pull.LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := pull.Issue.LoadRepo(ctx); err != nil {
|
2019-11-03 13:59:09 -07:00
|
|
|
log.Error("pull.Issue.LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := pull.Issue.LoadPoster(ctx); err != nil {
|
2019-11-03 13:59:09 -07:00
|
|
|
log.Error("pull.Issue.LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2019-11-03 13:59:09 -07:00
|
|
|
ActUserID: pull.Issue.Poster.ID,
|
|
|
|
ActUser: pull.Issue.Poster,
|
2022-08-24 20:31:57 -06:00
|
|
|
OpType: activities_model.ActionCreatePullRequest,
|
2019-11-03 13:59:09 -07:00
|
|
|
Content: fmt.Sprintf("%d|%s", pull.Issue.Index, pull.Issue.Title),
|
|
|
|
RepoID: pull.Issue.Repo.ID,
|
|
|
|
Repo: pull.Issue.Repo,
|
|
|
|
IsPrivate: pull.Issue.Repo.IsPrivate,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("NotifyWatchers: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 13:54:50 -07:00
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (a *actionNotifier) RenameRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldRepoName string) {
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2019-11-08 13:54:50 -07:00
|
|
|
ActUserID: doer.ID,
|
|
|
|
ActUser: doer,
|
2022-08-24 20:31:57 -06:00
|
|
|
OpType: activities_model.ActionRenameRepo,
|
2019-11-08 13:54:50 -07:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
IsPrivate: repo.IsPrivate,
|
2019-11-15 01:06:11 -07:00
|
|
|
Content: oldRepoName,
|
2019-11-08 13:54:50 -07:00
|
|
|
}); err != nil {
|
2019-11-15 01:06:11 -07:00
|
|
|
log.Error("NotifyWatchers: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (a *actionNotifier) TransferRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldOwnerName string) {
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2019-11-15 01:06:11 -07:00
|
|
|
ActUserID: doer.ID,
|
|
|
|
ActUser: doer,
|
2022-08-24 20:31:57 -06:00
|
|
|
OpType: activities_model.ActionTransferRepo,
|
2019-11-15 01:06:11 -07:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
IsPrivate: repo.IsPrivate,
|
|
|
|
Content: path.Join(oldOwnerName, repo.Name),
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("NotifyWatchers: %v", err)
|
2019-11-08 13:54:50 -07:00
|
|
|
}
|
|
|
|
}
|
2019-11-10 20:39:41 -07:00
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (a *actionNotifier) CreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2019-11-10 20:39:41 -07:00
|
|
|
ActUserID: doer.ID,
|
|
|
|
ActUser: doer,
|
2022-08-24 20:31:57 -06:00
|
|
|
OpType: activities_model.ActionCreateRepo,
|
2019-11-10 20:39:41 -07:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
IsPrivate: repo.IsPrivate,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("notify watchers '%d/%d': %v", doer.ID, repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (a *actionNotifier) ForkRepository(ctx context.Context, doer *user_model.User, oldRepo, repo *repo_model.Repository) {
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2019-11-10 20:39:41 -07:00
|
|
|
ActUserID: doer.ID,
|
|
|
|
ActUser: doer,
|
2022-08-24 20:31:57 -06:00
|
|
|
OpType: activities_model.ActionCreateRepo,
|
2019-11-10 20:39:41 -07:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
IsPrivate: repo.IsPrivate,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("notify watchers '%d/%d': %v", doer.ID, repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-13 19:57:36 -07:00
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (a *actionNotifier) PullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) {
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := review.LoadReviewer(ctx); err != nil {
|
2019-11-13 19:57:36 -07:00
|
|
|
log.Error("LoadReviewer '%d/%d': %v", review.ID, review.ReviewerID, err)
|
|
|
|
return
|
|
|
|
}
|
2022-01-19 16:26:57 -07:00
|
|
|
if err := review.LoadCodeComments(ctx); err != nil {
|
2019-11-13 19:57:36 -07:00
|
|
|
log.Error("LoadCodeComments '%d/%d': %v", review.Reviewer.ID, review.ID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-24 20:31:57 -06:00
|
|
|
actions := make([]*activities_model.Action, 0, 10)
|
2019-11-13 19:57:36 -07:00
|
|
|
for _, lines := range review.CodeComments {
|
|
|
|
for _, comments := range lines {
|
|
|
|
for _, comm := range comments {
|
2022-08-24 20:31:57 -06:00
|
|
|
actions = append(actions, &activities_model.Action{
|
2019-11-13 19:57:36 -07:00
|
|
|
ActUserID: review.Reviewer.ID,
|
|
|
|
ActUser: review.Reviewer,
|
|
|
|
Content: fmt.Sprintf("%d|%s", review.Issue.Index, strings.Split(comm.Content, "\n")[0]),
|
2022-08-24 20:31:57 -06:00
|
|
|
OpType: activities_model.ActionCommentPull,
|
2019-11-13 19:57:36 -07:00
|
|
|
RepoID: review.Issue.RepoID,
|
|
|
|
Repo: review.Issue.Repo,
|
|
|
|
IsPrivate: review.Issue.Repo.IsPrivate,
|
|
|
|
Comment: comm,
|
|
|
|
CommentID: comm.ID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 03:37:59 -06:00
|
|
|
if review.Type != issues_model.ReviewTypeComment || strings.TrimSpace(comment.Content) != "" {
|
2022-08-24 20:31:57 -06:00
|
|
|
action := &activities_model.Action{
|
2019-11-13 19:57:36 -07:00
|
|
|
ActUserID: review.Reviewer.ID,
|
|
|
|
ActUser: review.Reviewer,
|
|
|
|
Content: fmt.Sprintf("%d|%s", review.Issue.Index, strings.Split(comment.Content, "\n")[0]),
|
|
|
|
RepoID: review.Issue.RepoID,
|
|
|
|
Repo: review.Issue.Repo,
|
|
|
|
IsPrivate: review.Issue.Repo.IsPrivate,
|
|
|
|
Comment: comment,
|
|
|
|
CommentID: comment.ID,
|
2019-11-14 16:52:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
switch review.Type {
|
2022-06-13 03:37:59 -06:00
|
|
|
case issues_model.ReviewTypeApprove:
|
2022-08-24 20:31:57 -06:00
|
|
|
action.OpType = activities_model.ActionApprovePullRequest
|
2022-06-13 03:37:59 -06:00
|
|
|
case issues_model.ReviewTypeReject:
|
2022-08-24 20:31:57 -06:00
|
|
|
action.OpType = activities_model.ActionRejectPullRequest
|
2019-11-14 16:52:18 -07:00
|
|
|
default:
|
2022-08-24 20:31:57 -06:00
|
|
|
action.OpType = activities_model.ActionCommentPull
|
2019-11-14 16:52:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
actions = append(actions, action)
|
2019-11-13 19:57:36 -07:00
|
|
|
}
|
|
|
|
|
2022-08-24 20:31:57 -06:00
|
|
|
if err := activities_model.NotifyWatchersActions(actions); err != nil {
|
2019-11-13 19:57:36 -07:00
|
|
|
log.Error("notify watchers '%d/%d': %v", review.Reviewer.ID, review.Issue.RepoID, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 10:08:42 -07:00
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (*actionNotifier) MergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2019-11-21 10:08:42 -07:00
|
|
|
ActUserID: doer.ID,
|
|
|
|
ActUser: doer,
|
2022-08-24 20:31:57 -06:00
|
|
|
OpType: activities_model.ActionMergePullRequest,
|
2019-11-21 10:08:42 -07:00
|
|
|
Content: fmt.Sprintf("%d|%s", pr.Issue.Index, pr.Issue.Title),
|
2022-11-03 09:49:00 -06:00
|
|
|
RepoID: pr.Issue.Repo.ID,
|
|
|
|
Repo: pr.Issue.Repo,
|
|
|
|
IsPrivate: pr.Issue.Repo.IsPrivate,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("NotifyWatchers [%d]: %v", pr.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (*actionNotifier) AutoMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2022-11-03 09:49:00 -06:00
|
|
|
ActUserID: doer.ID,
|
|
|
|
ActUser: doer,
|
|
|
|
OpType: activities_model.ActionAutoMergePullRequest,
|
|
|
|
Content: fmt.Sprintf("%d|%s", pr.Issue.Index, pr.Issue.Title),
|
2019-11-21 10:08:42 -07:00
|
|
|
RepoID: pr.Issue.Repo.ID,
|
|
|
|
Repo: pr.Issue.Repo,
|
|
|
|
IsPrivate: pr.Issue.Repo.IsPrivate,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("NotifyWatchers [%d]: %v", pr.ID, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 22:16:59 -07:00
|
|
|
|
2022-11-19 01:12:33 -07:00
|
|
|
func (*actionNotifier) NotifyPullRevieweDismiss(ctx context.Context, doer *user_model.User, review *issues_model.Review, comment *issues_model.Comment) {
|
2021-02-11 10:32:25 -07:00
|
|
|
reviewerName := review.Reviewer.Name
|
|
|
|
if len(review.OriginalAuthor) > 0 {
|
|
|
|
reviewerName = review.OriginalAuthor
|
|
|
|
}
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2021-02-11 10:32:25 -07:00
|
|
|
ActUserID: doer.ID,
|
|
|
|
ActUser: doer,
|
2022-08-24 20:31:57 -06:00
|
|
|
OpType: activities_model.ActionPullReviewDismissed,
|
2021-02-11 10:32:25 -07:00
|
|
|
Content: fmt.Sprintf("%d|%s|%s", review.Issue.Index, reviewerName, comment.Content),
|
|
|
|
RepoID: review.Issue.Repo.ID,
|
|
|
|
Repo: review.Issue.Repo,
|
|
|
|
IsPrivate: review.Issue.Repo.IsPrivate,
|
|
|
|
CommentID: comment.ID,
|
|
|
|
Comment: comment,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("NotifyWatchers [%d]: %v", review.Issue.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (a *actionNotifier) PushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
|
2020-12-07 19:23:18 -07:00
|
|
|
data, err := json.Marshal(commits)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Marshal: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-24 20:31:57 -06:00
|
|
|
opType := activities_model.ActionCommitRepo
|
2020-12-07 19:23:18 -07:00
|
|
|
|
|
|
|
// Check it's tag push or branch.
|
2023-05-25 19:04:48 -06:00
|
|
|
if opts.RefFullName.IsTag() {
|
2022-08-24 20:31:57 -06:00
|
|
|
opType = activities_model.ActionPushTag
|
2020-12-07 19:23:18 -07:00
|
|
|
if opts.IsDelRef() {
|
2022-08-24 20:31:57 -06:00
|
|
|
opType = activities_model.ActionDeleteTag
|
2020-12-07 19:23:18 -07:00
|
|
|
}
|
|
|
|
} else if opts.IsDelRef() {
|
2022-08-24 20:31:57 -06:00
|
|
|
opType = activities_model.ActionDeleteBranch
|
2020-12-07 19:23:18 -07:00
|
|
|
}
|
|
|
|
|
2022-11-19 01:12:33 -07:00
|
|
|
if err = activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2020-12-07 19:23:18 -07:00
|
|
|
ActUserID: pusher.ID,
|
|
|
|
ActUser: pusher,
|
|
|
|
OpType: opType,
|
|
|
|
Content: string(data),
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
2023-05-25 19:04:48 -06:00
|
|
|
RefName: opts.RefFullName.String(),
|
2020-12-07 19:23:18 -07:00
|
|
|
IsPrivate: repo.IsPrivate,
|
|
|
|
}); err != nil {
|
2022-11-19 01:12:33 -07:00
|
|
|
log.Error("NotifyWatchers: %v", err)
|
2020-12-07 19:23:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (a *actionNotifier) CreateRef(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) {
|
2022-08-24 20:31:57 -06:00
|
|
|
opType := activities_model.ActionCommitRepo
|
2023-05-25 19:04:48 -06:00
|
|
|
if refFullName.IsTag() {
|
2023-09-05 12:37:47 -06:00
|
|
|
// has sent same action in `PushCommits`, so skip it.
|
2021-03-21 04:11:22 -06:00
|
|
|
return
|
2020-12-07 19:23:18 -07:00
|
|
|
}
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2020-12-07 19:23:18 -07:00
|
|
|
ActUserID: doer.ID,
|
|
|
|
ActUser: doer,
|
|
|
|
OpType: opType,
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
IsPrivate: repo.IsPrivate,
|
2023-05-25 19:04:48 -06:00
|
|
|
RefName: refFullName.String(),
|
2020-12-07 19:23:18 -07:00
|
|
|
}); err != nil {
|
2022-11-19 01:12:33 -07:00
|
|
|
log.Error("NotifyWatchers: %v", err)
|
2020-12-07 19:23:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (a *actionNotifier) DeleteRef(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, refFullName git.RefName) {
|
2022-08-24 20:31:57 -06:00
|
|
|
opType := activities_model.ActionDeleteBranch
|
2023-05-25 19:04:48 -06:00
|
|
|
if refFullName.IsTag() {
|
2023-09-05 12:37:47 -06:00
|
|
|
// has sent same action in `PushCommits`, so skip it.
|
2021-03-21 04:11:22 -06:00
|
|
|
return
|
2020-12-07 19:23:18 -07:00
|
|
|
}
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2020-12-07 19:23:18 -07:00
|
|
|
ActUserID: doer.ID,
|
|
|
|
ActUser: doer,
|
|
|
|
OpType: opType,
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
IsPrivate: repo.IsPrivate,
|
2023-05-25 19:04:48 -06:00
|
|
|
RefName: refFullName.String(),
|
2020-12-07 19:23:18 -07:00
|
|
|
}); err != nil {
|
2022-11-19 01:12:33 -07:00
|
|
|
log.Error("NotifyWatchers: %v", err)
|
2020-12-07 19:23:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (a *actionNotifier) SyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
|
2019-11-23 22:16:59 -07:00
|
|
|
data, err := json.Marshal(commits)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("json.Marshal: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2019-11-23 22:16:59 -07:00
|
|
|
ActUserID: repo.OwnerID,
|
2022-11-19 01:12:33 -07:00
|
|
|
ActUser: repo.MustOwner(ctx),
|
2022-08-24 20:31:57 -06:00
|
|
|
OpType: activities_model.ActionMirrorSyncPush,
|
2019-11-23 22:16:59 -07:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
IsPrivate: repo.IsPrivate,
|
2023-05-25 19:04:48 -06:00
|
|
|
RefName: opts.RefFullName.String(),
|
2019-11-23 22:16:59 -07:00
|
|
|
Content: string(data),
|
|
|
|
}); err != nil {
|
2022-11-19 01:12:33 -07:00
|
|
|
log.Error("NotifyWatchers: %v", err)
|
2019-11-23 22:16:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (a *actionNotifier) SyncCreateRef(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) {
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2019-11-23 22:16:59 -07:00
|
|
|
ActUserID: repo.OwnerID,
|
2022-11-19 01:12:33 -07:00
|
|
|
ActUser: repo.MustOwner(ctx),
|
2022-08-24 20:31:57 -06:00
|
|
|
OpType: activities_model.ActionMirrorSyncCreate,
|
2019-11-23 22:16:59 -07:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
IsPrivate: repo.IsPrivate,
|
2023-05-25 19:04:48 -06:00
|
|
|
RefName: refFullName.String(),
|
2019-11-23 22:16:59 -07:00
|
|
|
}); err != nil {
|
2022-11-19 01:12:33 -07:00
|
|
|
log.Error("NotifyWatchers: %v", err)
|
2019-11-23 22:16:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (a *actionNotifier) SyncDeleteRef(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, refFullName git.RefName) {
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2019-11-23 22:16:59 -07:00
|
|
|
ActUserID: repo.OwnerID,
|
2022-11-19 01:12:33 -07:00
|
|
|
ActUser: repo.MustOwner(ctx),
|
2022-08-24 20:31:57 -06:00
|
|
|
OpType: activities_model.ActionMirrorSyncDelete,
|
2019-11-23 22:16:59 -07:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
IsPrivate: repo.IsPrivate,
|
2023-05-25 19:04:48 -06:00
|
|
|
RefName: refFullName.String(),
|
2019-11-23 22:16:59 -07:00
|
|
|
}); err != nil {
|
2022-11-19 01:12:33 -07:00
|
|
|
log.Error("NotifyWatchers: %v", err)
|
2019-11-23 22:16:59 -07:00
|
|
|
}
|
|
|
|
}
|
2020-07-29 13:20:54 -06:00
|
|
|
|
2023-09-05 12:37:47 -06:00
|
|
|
func (a *actionNotifier) NewRelease(ctx context.Context, rel *repo_model.Release) {
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := rel.LoadAttributes(ctx); err != nil {
|
|
|
|
log.Error("LoadAttributes: %v", err)
|
2020-07-29 13:20:54 -06:00
|
|
|
return
|
|
|
|
}
|
2022-11-19 01:12:33 -07:00
|
|
|
if err := activities_model.NotifyWatchers(ctx, &activities_model.Action{
|
2020-07-29 13:20:54 -06:00
|
|
|
ActUserID: rel.PublisherID,
|
|
|
|
ActUser: rel.Publisher,
|
2022-08-24 20:31:57 -06:00
|
|
|
OpType: activities_model.ActionPublishRelease,
|
2020-07-29 13:20:54 -06:00
|
|
|
RepoID: rel.RepoID,
|
|
|
|
Repo: rel.Repo,
|
|
|
|
IsPrivate: rel.Repo.IsPrivate,
|
|
|
|
Content: rel.Title,
|
2023-05-25 19:04:48 -06:00
|
|
|
RefName: rel.TagName, // FIXME: use a full ref name?
|
2020-07-29 13:20:54 -06:00
|
|
|
}); err != nil {
|
2022-11-19 01:12:33 -07:00
|
|
|
log.Error("NotifyWatchers: %v", err)
|
2020-07-29 13:20:54 -06:00
|
|
|
}
|
|
|
|
}
|