Browse Source

Fix commenting on non-utf8 encoded files (#11916)

* Add comment on non-unicode line to force fail

Signed-off-by: Andrew Thornton <art27@cantab.net>

* Just quote/unquote patch

Signed-off-by: Andrew Thornton <art27@cantab.net>
for-closed-social
zeripath 4 years ago
committed by GitHub
parent
commit
654a970644
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 4 deletions
  1. +7
    -2
      integrations/api_pull_review_test.go
  2. BIN
     
  3. +2
    -0
      integrations/gitea-repositories-meta/user2/repo1.git/objects/90/dcd07da077d1e7cd6032b52d1f79ae2b5f19b2
  4. BIN
     
  5. +1
    -1
      integrations/gitea-repositories-meta/user2/repo1.git/refs/pull/3/head
  6. +31
    -1
      models/issue_comment.go

+ 7
- 2
integrations/api_pull_review_test.go View File

@ -86,6 +86,11 @@ func TestAPIPullReview(t *testing.T) {
Body: "first old line", Body: "first old line",
OldLineNum: 1, OldLineNum: 1,
NewLineNum: 0, NewLineNum: 0,
}, {
Path: "iso-8859-1.txt",
Body: "this line contains a non-utf-8 character",
OldLineNum: 0,
NewLineNum: 1,
}, },
}, },
}) })
@ -93,7 +98,7 @@ func TestAPIPullReview(t *testing.T) {
DecodeJSON(t, resp, &review) DecodeJSON(t, resp, &review)
assert.EqualValues(t, 6, review.ID) assert.EqualValues(t, 6, review.ID)
assert.EqualValues(t, "PENDING", review.State) assert.EqualValues(t, "PENDING", review.State)
assert.EqualValues(t, 2, review.CodeCommentsCount)
assert.EqualValues(t, 3, review.CodeCommentsCount)
// test SubmitPullReview // test SubmitPullReview
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews/%d?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, review.ID, token), &api.SubmitPullReviewOptions{ req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews/%d?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, review.ID, token), &api.SubmitPullReviewOptions{
@ -104,7 +109,7 @@ func TestAPIPullReview(t *testing.T) {
DecodeJSON(t, resp, &review) DecodeJSON(t, resp, &review)
assert.EqualValues(t, 6, review.ID) assert.EqualValues(t, 6, review.ID)
assert.EqualValues(t, "APPROVED", review.State) assert.EqualValues(t, "APPROVED", review.State)
assert.EqualValues(t, 2, review.CodeCommentsCount)
assert.EqualValues(t, 3, review.CodeCommentsCount)
// test DeletePullReview // test DeletePullReview
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, token), &api.CreatePullReviewOptions{ req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, token), &api.CreatePullReviewOptions{

BIN
View File


+ 2
- 0
integrations/gitea-repositories-meta/user2/repo1.git/objects/90/dcd07da077d1e7cd6032b52d1f79ae2b5f19b2 View File

@ -0,0 +1,2 @@
xe�±NÄ0D©#åæŽ4
JÄAÅ5”Ž³—,—xÑzsVþ�‚5„DåÑØ»ž7ý,=®®o.áEå�¢áq5J=éˆý�È rÄ=>4§ú O!óŠý�ã´ðÐ6ms˜8ƒ¾&\Ea¾tÍT„´I¢z”‰Ô…! ¢dso@a›Ú&ÌK5üB)›r4–”Q¦`YèLÚ¯²b ›�¾o`Ûaä3¹@(�ÒeýÔ­5 ô�ÂH—\sÔHÿ9Ÿ9Rª3)Îë@ŽSùã_"§‘4sE0”Rºñ§¤.‘U|/€m¦Û¿]U÷ÌzÀ

BIN
View File


+ 1
- 1
integrations/gitea-repositories-meta/user2/repo1.git/refs/pull/3/head View File

@ -1 +1 @@
4a357436d925b5c974181ff12a994538ddc5a269
5f22f7d0d95d614d25a5b68592adb345a4b5c7fd

+ 31
- 1
models/issue_comment.go View File

@ -11,7 +11,9 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"regexp" "regexp"
"strconv"
"strings" "strings"
"unicode/utf8"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -143,7 +145,8 @@ type Comment struct {
RenderedContent string `xorm:"-"` RenderedContent string `xorm:"-"`
// Path represents the 4 lines of code cemented by this comment // Path represents the 4 lines of code cemented by this comment
Patch string `xorm:"TEXT"`
Patch string `xorm:"-"`
PatchQuoted string `xorm:"TEXT patch"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
@ -199,6 +202,33 @@ func (c *Comment) loadIssue(e Engine) (err error) {
return return
} }
// BeforeInsert will be invoked by XORM before inserting a record
func (c *Comment) BeforeInsert() {
c.PatchQuoted = c.Patch
if !utf8.ValidString(c.Patch) {
c.PatchQuoted = strconv.Quote(c.Patch)
}
}
// BeforeUpdate will be invoked by XORM before updating a record
func (c *Comment) BeforeUpdate() {
c.PatchQuoted = c.Patch
if !utf8.ValidString(c.Patch) {
c.PatchQuoted = strconv.Quote(c.Patch)
}
}
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (c *Comment) AfterLoad(session *xorm.Session) {
c.Patch = c.PatchQuoted
if len(c.PatchQuoted) > 0 && c.PatchQuoted[0] == '"' {
unquoted, err := strconv.Unquote(c.PatchQuoted)
if err == nil {
c.Patch = unquoted
}
}
}
func (c *Comment) loadPoster(e Engine) (err error) { func (c *Comment) loadPoster(e Engine) (err error) {
if c.PosterID <= 0 || c.Poster != nil { if c.PosterID <= 0 || c.Poster != nil {
return nil return nil

Loading…
Cancel
Save