Skip to content

Commit 99fc8d2

Browse files
committed
record team name in the content when creating request team review comment
1 parent 1c8bc40 commit 99fc8d2

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

models/issues/comment.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ type PushActionContent struct {
313313
CommitIDs []string `json:"commit_ids"`
314314
}
315315

316+
// RequestTeamReviewActionContent is content of requesting or cancelling request a team to review a PR
317+
type RequestTeamReviewActionContent struct {
318+
TeamName string `json:"team_name"`
319+
}
320+
316321
// LoadIssue loads the issue reference for the comment
317322
func (c *Comment) LoadIssue(ctx context.Context) (err error) {
318323
if c.Issue != nil {

models/issues/review.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package issues
55

66
import (
77
"context"
8+
"encoding/json"
89
"fmt"
910
"strings"
1011

@@ -815,6 +816,14 @@ func AddTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_
815816
}
816817
}
817818

819+
// record the name of team in case that we cannot get the name of team after it is deleted.
820+
var content RequestTeamReviewActionContent
821+
content.TeamName = reviewer.Name
822+
contentJSON, err := json.Marshal(content)
823+
if err != nil {
824+
return nil, err
825+
}
826+
818827
comment, err := CreateComment(ctx, &CreateCommentOptions{
819828
Type: CommentTypeReviewRequest,
820829
Doer: doer,
@@ -823,6 +832,7 @@ func AddTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_
823832
RemovedAssignee: false, // Use RemovedAssignee as !isRequest
824833
AssigneeTeamID: reviewer.ID, // Use AssigneeTeamID as reviewer team ID
825834
ReviewID: review.ID,
835+
Content: string(contentJSON),
826836
})
827837
if err != nil {
828838
return nil, fmt.Errorf("CreateComment(): %w", err)
@@ -875,13 +885,22 @@ func RemoveTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *us
875885
return nil, committer.Commit()
876886
}
877887

888+
// record the name of team in case that we cannot get the name of team after it is deleted.
889+
var content RequestTeamReviewActionContent
890+
content.TeamName = reviewer.Name
891+
contentJSON, err := json.Marshal(content)
892+
if err != nil {
893+
return nil, err
894+
}
895+
878896
comment, err := CreateComment(ctx, &CreateCommentOptions{
879897
Type: CommentTypeReviewRequest,
880898
Doer: doer,
881899
Repo: issue.Repo,
882900
Issue: issue,
883901
RemovedAssignee: true, // Use RemovedAssignee as !isRequest
884902
AssigneeTeamID: reviewer.ID, // Use AssigneeTeamID as reviewer team ID
903+
Content: string(contentJSON),
885904
})
886905
if err != nil {
887906
return nil, fmt.Errorf("CreateComment(): %w", err)

modules/templates/helper.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ func NewFuncMap() []template.FuncMap {
363363
curBranch,
364364
)
365365
},
366+
"GetRequestReviewTeamName": GetRequestReviewTeamName,
366367
}}
367368
}
368369

@@ -799,3 +800,19 @@ func Eval(tokens ...any) (any, error) {
799800
n, err := eval.Expr(tokens...)
800801
return n.Value, err
801802
}
803+
804+
// If the value of commnet type is 27 and assigne_team_id not equal 0, this is a request review for a team.
805+
// So, we can use this func to parse the name of the team from `content` of the comment when the team is deleted.
806+
func GetRequestReviewTeamName(content string) string {
807+
if content == "" {
808+
return content
809+
}
810+
811+
var data issues_model.RequestTeamReviewActionContent
812+
if err := json.Unmarshal([]byte(content), &data); err != nil {
813+
log.Error("json.Unmarshal: %s, error: %v", content, err)
814+
return ""
815+
}
816+
817+
return data.TeamName
818+
}

templates/repo/issue/view_content/comments.tmpl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,29 @@
688688
{{$.locale.Tr "repo.issues.review.add_review_request" (.Assignee.GetDisplayName|Escape) $createdStr | Safe}}
689689
{{end}}
690690
{{else}}
691+
<!--
692+
- For new comments, the name of the team which is requested review is stored in the `content` column of the comment
693+
If the team was deleted, we can parse the name from the `content` column.
694+
Of course, if the team has not been deleted, we can get the name from `.AssigneeTeam` directly.
695+
696+
- For comments that already exist and the row of the team has been deleted,
697+
because we didn't save the name of the team in the `cotent` column of the comment before,
698+
So we can only display "Unknown Team" here.
699+
-->
700+
{{$teamName := "Unknown Team"}}
701+
{{if .AssigneeTeam}}
702+
{{$teamName = .AssigneeTeam.Name}}
703+
{{else if .Content}}
704+
{{$teamNameFromContent := GetRequestReviewTeamName .Content}}
705+
{{if not (eq $teamNameFromContent "")}}
706+
{{$teamName = $teamNameFromContent}}
707+
{{end}}
708+
{{end}}
709+
691710
{{if .RemovedAssignee}}
692-
{{$.locale.Tr "repo.issues.review.remove_review_request" (.AssigneeTeam.Name|Escape) $createdStr | Safe}}
711+
{{$.locale.Tr "repo.issues.review.remove_review_request" ($teamName|Escape) $createdStr | Safe}}
693712
{{else}}
694-
{{$.locale.Tr "repo.issues.review.add_review_request" (.AssigneeTeam.Name|Escape) $createdStr | Safe}}
713+
{{$.locale.Tr "repo.issues.review.add_review_request" ($teamName|Escape) $createdStr | Safe}}
695714
{{end}}
696715
{{end}}
697716
</span>

0 commit comments

Comments
 (0)