From 99fc8d2a29434c5fdd13d1a108e48aad99815682 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Fri, 14 Apr 2023 14:41:18 +0800 Subject: [PATCH 1/3] record team name in the content when creating request team review comment --- models/issues/comment.go | 5 ++++ models/issues/review.go | 19 +++++++++++++++ modules/templates/helper.go | 17 ++++++++++++++ .../repo/issue/view_content/comments.tmpl | 23 +++++++++++++++++-- 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index a47dc7c151340..dcd7b67884e12 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -313,6 +313,11 @@ type PushActionContent struct { CommitIDs []string `json:"commit_ids"` } +// RequestTeamReviewActionContent is content of requesting or cancelling request a team to review a PR +type RequestTeamReviewActionContent struct { + TeamName string `json:"team_name"` +} + // LoadIssue loads the issue reference for the comment func (c *Comment) LoadIssue(ctx context.Context) (err error) { if c.Issue != nil { diff --git a/models/issues/review.go b/models/issues/review.go index fe123d73986bd..269ba6b82e6c1 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -5,6 +5,7 @@ package issues import ( "context" + "encoding/json" "fmt" "strings" @@ -815,6 +816,14 @@ func AddTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_ } } + // record the name of team in case that we cannot get the name of team after it is deleted. + var content RequestTeamReviewActionContent + content.TeamName = reviewer.Name + contentJSON, err := json.Marshal(content) + if err != nil { + return nil, err + } + comment, err := CreateComment(ctx, &CreateCommentOptions{ Type: CommentTypeReviewRequest, Doer: doer, @@ -823,6 +832,7 @@ func AddTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_ RemovedAssignee: false, // Use RemovedAssignee as !isRequest AssigneeTeamID: reviewer.ID, // Use AssigneeTeamID as reviewer team ID ReviewID: review.ID, + Content: string(contentJSON), }) if err != nil { return nil, fmt.Errorf("CreateComment(): %w", err) @@ -875,6 +885,14 @@ func RemoveTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *us return nil, committer.Commit() } + // record the name of team in case that we cannot get the name of team after it is deleted. + var content RequestTeamReviewActionContent + content.TeamName = reviewer.Name + contentJSON, err := json.Marshal(content) + if err != nil { + return nil, err + } + comment, err := CreateComment(ctx, &CreateCommentOptions{ Type: CommentTypeReviewRequest, Doer: doer, @@ -882,6 +900,7 @@ func RemoveTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *us Issue: issue, RemovedAssignee: true, // Use RemovedAssignee as !isRequest AssigneeTeamID: reviewer.ID, // Use AssigneeTeamID as reviewer team ID + Content: string(contentJSON), }) if err != nil { return nil, fmt.Errorf("CreateComment(): %w", err) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index f93419fe873eb..ed010a28a9d5c 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -363,6 +363,7 @@ func NewFuncMap() []template.FuncMap { curBranch, ) }, + "GetRequestReviewTeamName": GetRequestReviewTeamName, }} } @@ -799,3 +800,19 @@ func Eval(tokens ...any) (any, error) { n, err := eval.Expr(tokens...) return n.Value, err } + +// If the value of commnet type is 27 and assigne_team_id not equal 0, this is a request review for a team. +// So, we can use this func to parse the name of the team from `content` of the comment when the team is deleted. +func GetRequestReviewTeamName(content string) string { + if content == "" { + return content + } + + var data issues_model.RequestTeamReviewActionContent + if err := json.Unmarshal([]byte(content), &data); err != nil { + log.Error("json.Unmarshal: %s, error: %v", content, err) + return "" + } + + return data.TeamName +} diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index 251f205a0312c..15583b7bac61f 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -688,10 +688,29 @@ {{$.locale.Tr "repo.issues.review.add_review_request" (.Assignee.GetDisplayName|Escape) $createdStr | Safe}} {{end}} {{else}} + + {{$teamName := "Unknown Team"}} + {{if .AssigneeTeam}} + {{$teamName = .AssigneeTeam.Name}} + {{else if .Content}} + {{$teamNameFromContent := GetRequestReviewTeamName .Content}} + {{if not (eq $teamNameFromContent "")}} + {{$teamName = $teamNameFromContent}} + {{end}} + {{end}} + {{if .RemovedAssignee}} - {{$.locale.Tr "repo.issues.review.remove_review_request" (.AssigneeTeam.Name|Escape) $createdStr | Safe}} + {{$.locale.Tr "repo.issues.review.remove_review_request" ($teamName|Escape) $createdStr | Safe}} {{else}} - {{$.locale.Tr "repo.issues.review.add_review_request" (.AssigneeTeam.Name|Escape) $createdStr | Safe}} + {{$.locale.Tr "repo.issues.review.add_review_request" ($teamName|Escape) $createdStr | Safe}} {{end}} {{end}} From fdb15164d1d58767ef9a2eb89d69f2018a49e248 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Fri, 14 Apr 2023 17:06:29 +0800 Subject: [PATCH 2/3] just display ghost team --- models/issues/comment.go | 5 ----- models/issues/review.go | 19 ------------------- modules/templates/helper.go | 17 ----------------- .../repo/issue/view_content/comments.tmpl | 18 ++---------------- 4 files changed, 2 insertions(+), 57 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index dcd7b67884e12..a47dc7c151340 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -313,11 +313,6 @@ type PushActionContent struct { CommitIDs []string `json:"commit_ids"` } -// RequestTeamReviewActionContent is content of requesting or cancelling request a team to review a PR -type RequestTeamReviewActionContent struct { - TeamName string `json:"team_name"` -} - // LoadIssue loads the issue reference for the comment func (c *Comment) LoadIssue(ctx context.Context) (err error) { if c.Issue != nil { diff --git a/models/issues/review.go b/models/issues/review.go index 269ba6b82e6c1..fe123d73986bd 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -5,7 +5,6 @@ package issues import ( "context" - "encoding/json" "fmt" "strings" @@ -816,14 +815,6 @@ func AddTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_ } } - // record the name of team in case that we cannot get the name of team after it is deleted. - var content RequestTeamReviewActionContent - content.TeamName = reviewer.Name - contentJSON, err := json.Marshal(content) - if err != nil { - return nil, err - } - comment, err := CreateComment(ctx, &CreateCommentOptions{ Type: CommentTypeReviewRequest, Doer: doer, @@ -832,7 +823,6 @@ func AddTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_ RemovedAssignee: false, // Use RemovedAssignee as !isRequest AssigneeTeamID: reviewer.ID, // Use AssigneeTeamID as reviewer team ID ReviewID: review.ID, - Content: string(contentJSON), }) if err != nil { return nil, fmt.Errorf("CreateComment(): %w", err) @@ -885,14 +875,6 @@ func RemoveTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *us return nil, committer.Commit() } - // record the name of team in case that we cannot get the name of team after it is deleted. - var content RequestTeamReviewActionContent - content.TeamName = reviewer.Name - contentJSON, err := json.Marshal(content) - if err != nil { - return nil, err - } - comment, err := CreateComment(ctx, &CreateCommentOptions{ Type: CommentTypeReviewRequest, Doer: doer, @@ -900,7 +882,6 @@ func RemoveTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *us Issue: issue, RemovedAssignee: true, // Use RemovedAssignee as !isRequest AssigneeTeamID: reviewer.ID, // Use AssigneeTeamID as reviewer team ID - Content: string(contentJSON), }) if err != nil { return nil, fmt.Errorf("CreateComment(): %w", err) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index ed010a28a9d5c..f93419fe873eb 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -363,7 +363,6 @@ func NewFuncMap() []template.FuncMap { curBranch, ) }, - "GetRequestReviewTeamName": GetRequestReviewTeamName, }} } @@ -800,19 +799,3 @@ func Eval(tokens ...any) (any, error) { n, err := eval.Expr(tokens...) return n.Value, err } - -// If the value of commnet type is 27 and assigne_team_id not equal 0, this is a request review for a team. -// So, we can use this func to parse the name of the team from `content` of the comment when the team is deleted. -func GetRequestReviewTeamName(content string) string { - if content == "" { - return content - } - - var data issues_model.RequestTeamReviewActionContent - if err := json.Unmarshal([]byte(content), &data); err != nil { - log.Error("json.Unmarshal: %s, error: %v", content, err) - return "" - } - - return data.TeamName -} diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl index 15583b7bac61f..39a6722e84ebb 100644 --- a/templates/repo/issue/view_content/comments.tmpl +++ b/templates/repo/issue/view_content/comments.tmpl @@ -688,25 +688,11 @@ {{$.locale.Tr "repo.issues.review.add_review_request" (.Assignee.GetDisplayName|Escape) $createdStr | Safe}} {{end}} {{else}} - - {{$teamName := "Unknown Team"}} + + {{$teamName := "Ghost Team"}} {{if .AssigneeTeam}} {{$teamName = .AssigneeTeam.Name}} - {{else if .Content}} - {{$teamNameFromContent := GetRequestReviewTeamName .Content}} - {{if not (eq $teamNameFromContent "")}} - {{$teamName = $teamNameFromContent}} - {{end}} {{end}} - {{if .RemovedAssignee}} {{$.locale.Tr "repo.issues.review.remove_review_request" ($teamName|Escape) $createdStr | Safe}} {{else}} From a4de58ccbe41b548e4015108b7a7afbe12ede920 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Fri, 14 Apr 2023 18:28:00 +0800 Subject: [PATCH 3/3] batch delete the binding relationship between team and PR --- models/org_team.go | 1 + 1 file changed, 1 insertion(+) diff --git a/models/org_team.go b/models/org_team.go index be3b63b52ec49..4c973b4d1b971 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -414,6 +414,7 @@ func DeleteTeam(t *organization.Team) error { &organization.TeamUser{OrgID: t.OrgID, TeamID: t.ID}, &organization.TeamUnit{TeamID: t.ID}, &organization.TeamInvite{TeamID: t.ID}, + &issues_model.Review{Type: issues_model.ReviewTypeRequest, ReviewerTeamID: t.ID}, // batch delete the binding relationship between team and PR (request review from team) ); err != nil { return err }