Skip to content

Commit c7bacdc

Browse files
committed
fix
1 parent 2c341b6 commit c7bacdc

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

models/renderhelper/repo_comment.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ type RepoCommentOptions struct {
4848
}
4949

5050
func NewRenderContextRepoComment(ctx context.Context, repo *repo_model.Repository, opts ...RepoCommentOptions) *markup.RenderContext {
51-
helper := &RepoComment{
52-
repoLink: repo.Link(),
53-
opts: util.OptionalArg(opts),
54-
}
51+
helper := &RepoComment{opts: util.OptionalArg(opts)}
5552
rctx := markup.NewRenderContext(ctx)
5653
helper.ctx = rctx
5754
var metas map[string]string
@@ -60,15 +57,16 @@ func NewRenderContextRepoComment(ctx context.Context, repo *repo_model.Repositor
6057
helper.commitChecker = newCommitChecker(ctx, repo)
6158
metas = repo.ComposeCommentMetas(ctx)
6259
} else {
63-
// this is almost dead code, only to pass the incorrect tests
64-
helper.repoLink = fmt.Sprintf("%s/%s", helper.opts.DeprecatedOwnerName, helper.opts.DeprecatedRepoName)
65-
rctx = rctx.WithMetas(map[string]string{
66-
"user": helper.opts.DeprecatedOwnerName,
67-
"repo": helper.opts.DeprecatedRepoName,
68-
69-
"markdownNewLineHardBreak": "true",
70-
"markupAllowShortIssuePattern": "true",
71-
})
60+
// repo can be nil when rendering a commit message in user's dashboard feedback whose repository has been deleted
61+
metas = map[string]string{}
62+
if helper.opts.DeprecatedOwnerName != "" {
63+
// this is almost dead code, only to pass the incorrect tests
64+
helper.repoLink = fmt.Sprintf("%s/%s", helper.opts.DeprecatedOwnerName, helper.opts.DeprecatedRepoName)
65+
metas["user"] = helper.opts.DeprecatedOwnerName
66+
metas["repo"] = helper.opts.DeprecatedRepoName
67+
}
68+
metas["markdownNewLineHardBreak"] = "true"
69+
metas["markupAllowShortIssuePattern"] = "true"
7270
}
7371
metas["footnoteContextId"] = helper.opts.FootnoteContextID
7472
rctx = rctx.WithMetas(metas).WithHelper(helper)

models/renderhelper/repo_comment_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,11 @@ func TestRepoComment(t *testing.T) {
7272
<a href="/user2/repo1/commit/1234/image" target="_blank" rel="nofollow noopener"><img src="/user2/repo1/commit/1234/image" alt="./image"/></a></p>
7373
`, rendered)
7474
})
75+
76+
t.Run("NoRepo", func(t *testing.T) {
77+
rctx := NewRenderContextRepoComment(t.Context(), nil).WithMarkupType(markdown.MarkupName)
78+
rendered, err := markup.RenderString(rctx, "any")
79+
assert.NoError(t, err)
80+
assert.Equal(t, "<p>any</p>\n", rendered)
81+
})
7582
}

modules/templates/util_render.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ func NewRenderUtils(ctx reqctx.RequestContext) *RenderUtils {
3838
// RenderCommitMessage renders commit message with XSS-safe and special links.
3939
func (ut *RenderUtils) RenderCommitMessage(msg string, repo *repo.Repository) template.HTML {
4040
cleanMsg := template.HTMLEscapeString(msg)
41-
// we can safely assume that it will not return any error, since there
42-
// shouldn't be any special HTML.
41+
// we can safely assume that it will not return any error, since there shouldn't be any special HTML.
42+
// "repo" can be nil when rendering commit messages for deleted repositories in a user's dashboard feed.
4343
fullMessage, err := markup.PostProcessCommitMessage(renderhelper.NewRenderContextRepoComment(ut.ctx, repo), cleanMsg)
4444
if err != nil {
4545
log.Error("PostProcessCommitMessage: %v", err)
4646
return ""
4747
}
4848
msgLines := strings.Split(strings.TrimSpace(fullMessage), "\n")
4949
if len(msgLines) == 0 {
50-
return template.HTML("")
50+
return ""
5151
}
5252
return renderCodeBlock(template.HTML(msgLines[0]))
5353
}

0 commit comments

Comments
 (0)