Skip to content

Commit e574f78

Browse files
committed
Improvements to content history
* initialize content history when making an edit to an old item created before the introduction of content history * show edit history for code comments on pull request files tab
1 parent 8511eec commit e574f78

File tree

6 files changed

+55
-6
lines changed

6 files changed

+55
-6
lines changed

models/issue.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,14 +824,25 @@ func (issue *Issue) UpdateAttachments(uuids []string) (err error) {
824824

825825
// ChangeContent changes issue content, as the given user.
826826
func (issue *Issue) ChangeContent(doer *User, content string) (err error) {
827-
issue.Content = content
828-
829827
ctx, committer, err := db.TxContext()
830828
if err != nil {
831829
return err
832830
}
833831
defer committer.Close()
834832

833+
hasContentHistory, err := issues.HasIssueContentHistory(ctx, issue.ID, 0)
834+
if err != nil {
835+
return fmt.Errorf("HasIssueContentHistory: %v", err)
836+
}
837+
if !hasContentHistory {
838+
if err = issues.SaveIssueContentHistory(db.GetEngine(ctx), issue.PosterID, issue.ID, 0,
839+
issue.CreatedUnix, issue.Content, true); err != nil {
840+
return fmt.Errorf("SaveIssueContentHistory: %v", err)
841+
}
842+
}
843+
844+
issue.Content = content
845+
835846
if err = updateIssueCols(db.GetEngine(ctx), issue, "content"); err != nil {
836847
return fmt.Errorf("UpdateIssueCols: %v", err)
837848
}

models/issues/content_history.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,20 @@ func FetchIssueContentHistoryList(dbCtx context.Context, issueID int64, commentI
167167
return res, nil
168168
}
169169

170-
//SoftDeleteIssueContentHistory soft delete
170+
// HasIssueContentHistory check if a ContentHistory entry exists
171+
func HasIssueContentHistory(dbCtx context.Context, issueID int64, commentID int64) (bool, error) {
172+
exists, err := db.GetEngine(dbCtx).Cols("id").Exist(&ContentHistory{
173+
IssueID: issueID,
174+
CommentID: commentID,
175+
})
176+
if err != nil {
177+
log.Error("can not fetch issue content history. err=%v", err)
178+
return false, err
179+
}
180+
return exists, err
181+
}
182+
183+
// SoftDeleteIssueContentHistory soft delete
171184
func SoftDeleteIssueContentHistory(dbCtx context.Context, historyID int64) error {
172185
if _, err := db.GetEngine(dbCtx).ID(historyID).Cols("is_deleted", "content_text").Update(&ContentHistory{
173186
IsDeleted: true,

models/issues/content_history_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ func TestContentHistory(t *testing.T) {
5353
list2, _ := FetchIssueContentHistoryList(dbCtx, 10, 100)
5454
assert.Len(t, list2, 5)
5555

56+
hasHistory1, _ := HasIssueContentHistory(dbCtx, 10, 0)
57+
assert.True(t, hasHistory1)
58+
hasHistory2, _ := HasIssueContentHistory(dbCtx, 10, 1)
59+
assert.False(t, hasHistory2)
60+
5661
h6, h6Prev, _ := GetIssueContentHistoryAndPrev(dbCtx, 6)
5762
assert.EqualValues(t, 6, h6.ID)
5863
assert.EqualValues(t, 5, h6Prev.ID)

services/comments/comments.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,26 @@ func CreateIssueComment(doer *models.User, repo *models.Repository, issue *model
4242

4343
// UpdateComment updates information of comment.
4444
func UpdateComment(c *models.Comment, doer *models.User, oldContent string) error {
45+
var needsContentHistory = c.Content != oldContent &&
46+
(c.Type == models.CommentTypeComment || c.Type == models.CommentTypeReview || c.Type == models.CommentTypeCode)
47+
if needsContentHistory {
48+
hasContentHistory, err := issues.HasIssueContentHistory(db.DefaultContext, c.IssueID, c.ID)
49+
if err != nil {
50+
return err
51+
}
52+
if !hasContentHistory {
53+
if err = issues.SaveIssueContentHistory(db.GetEngine(db.DefaultContext), c.PosterID, c.IssueID, c.ID,
54+
c.CreatedUnix, oldContent, true); err != nil {
55+
return err
56+
}
57+
}
58+
}
59+
4560
if err := models.UpdateComment(c, doer); err != nil {
4661
return err
4762
}
4863

49-
if c.Type == models.CommentTypeComment && c.Content != oldContent {
64+
if needsContentHistory {
5065
err := issues.SaveIssueContentHistory(db.GetEngine(db.DefaultContext), doer.ID, c.IssueID, c.ID, timeutil.TimeStampNow(), c.Content, false)
5166
if err != nil {
5267
return err

templates/repo/pulls/files.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{{template "base/head" .}}
2+
3+
<input type="hidden" id="repolink" value="{{$.RepoRelPath}}">
4+
<input type="hidden" id="issueIndex" value="{{.Issue.Index}}"/>
5+
26
<div class="page-content repository view issue pull files diff">
37
{{template "repo/header" .}}
48
<div class="ui container {{if .IsSplitStyle}}fluid padded{{end}}">

web_src/js/features/repo-issue-content.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ function showContentHistoryMenu(issueBaseUrl, $item, commentId) {
107107
export function initRepoIssueContentHistory() {
108108
const issueIndex = $('#issueIndex').val();
109109
const $itemIssue = $('.timeline-item.comment.first');
110-
if (!issueIndex || !$itemIssue.length) return;
110+
const $comments = $('.comment');
111+
if (!issueIndex || !$comments.length) return;
111112

112113
const repoLink = $('#repolink').val();
113114
const issueBaseUrl = `${appSubUrl}/${repoLink}/issues/${issueIndex}`;
@@ -123,7 +124,7 @@ export function initRepoIssueContentHistory() {
123124
i18nTextDeleteFromHistoryConfirm = resp.i18n.textDeleteFromHistoryConfirm;
124125
i18nTextOptions = resp.i18n.textOptions;
125126

126-
if (resp.editedHistoryCountMap[0]) {
127+
if (resp.editedHistoryCountMap[0] && $itemIssue.length) {
127128
showContentHistoryMenu(issueBaseUrl, $itemIssue, '0');
128129
}
129130
for (const [commentId, _editedCount] of Object.entries(resp.editedHistoryCountMap)) {

0 commit comments

Comments
 (0)