Skip to content

Commit a86d933

Browse files
GiteaBotwxiaoguang
andauthored
Fix issue & comment history bugs (#29525) (#29527)
Backport #29525 by @wxiaoguang * Follow #17746: `HasIssueContentHistory` should use expr builder to make sure zero value (0) be respected. * Add "doer" check to make sure `canSoftDeleteContentHistory` only be called by sign-in users. Co-authored-by: wxiaoguang <[email protected]>
1 parent 8d08558 commit a86d933

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

models/issues/content_history.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,9 @@ func FetchIssueContentHistoryList(dbCtx context.Context, issueID, commentID int6
172172

173173
// HasIssueContentHistory check if a ContentHistory entry exists
174174
func HasIssueContentHistory(dbCtx context.Context, issueID, commentID int64) (bool, error) {
175-
exists, err := db.GetEngine(dbCtx).Cols("id").Exist(&ContentHistory{
176-
IssueID: issueID,
177-
CommentID: commentID,
178-
})
175+
exists, err := db.GetEngine(dbCtx).Where(builder.Eq{"issue_id": issueID, "comment_id": commentID}).Exist(&ContentHistory{})
179176
if err != nil {
180-
log.Error("can not fetch issue content history. err=%v", err)
181-
return false, err
177+
return false, fmt.Errorf("can not check issue content history. err: %w", err)
182178
}
183179
return exists, err
184180
}

models/issues/content_history_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,22 @@ func TestContentHistory(t *testing.T) {
7878
assert.EqualValues(t, 7, list2[1].HistoryID)
7979
assert.EqualValues(t, 4, list2[2].HistoryID)
8080
}
81+
82+
func TestHasIssueContentHistoryForCommentOnly(t *testing.T) {
83+
assert.NoError(t, unittest.PrepareTestDatabase())
84+
85+
_ = db.TruncateBeans(db.DefaultContext, &issues_model.ContentHistory{})
86+
87+
hasHistory1, _ := issues_model.HasIssueContentHistory(db.DefaultContext, 10, 0)
88+
assert.False(t, hasHistory1)
89+
hasHistory2, _ := issues_model.HasIssueContentHistory(db.DefaultContext, 10, 100)
90+
assert.False(t, hasHistory2)
91+
92+
_ = issues_model.SaveIssueContentHistory(db.DefaultContext, 1, 10, 100, timeutil.TimeStampNow(), "c-a", true)
93+
_ = issues_model.SaveIssueContentHistory(db.DefaultContext, 1, 10, 100, timeutil.TimeStampNow().Add(5), "c-b", false)
94+
95+
hasHistory1, _ = issues_model.HasIssueContentHistory(db.DefaultContext, 10, 0)
96+
assert.False(t, hasHistory1)
97+
hasHistory2, _ = issues_model.HasIssueContentHistory(db.DefaultContext, 10, 100)
98+
assert.True(t, hasHistory2)
99+
}

routers/web/repo/issue_content_history.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func canSoftDeleteContentHistory(ctx *context.Context, issue *issues_model.Issue
9494
// CanWrite means the doer can manage the issue/PR list
9595
if ctx.Repo.IsOwner() || ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
9696
canSoftDelete = true
97-
} else {
97+
} else if ctx.Doer != nil {
9898
// for read-only users, they could still post issues or comments,
9999
// they should be able to delete the history related to their own issue/comment, a case is:
100100
// 1. the user posts some sensitive data
@@ -186,6 +186,10 @@ func SoftDeleteContentHistory(ctx *context.Context) {
186186
if ctx.Written() {
187187
return
188188
}
189+
if ctx.Doer == nil {
190+
ctx.NotFound("Require SignIn", nil)
191+
return
192+
}
189193

190194
commentID := ctx.FormInt64("comment_id")
191195
historyID := ctx.FormInt64("history_id")

0 commit comments

Comments
 (0)