Skip to content

Commit 73e0e44

Browse files
Fix various problems (#37129)
* Fix #37128 * Manually tested with various cases (issue, pr) X (close, reopen) * Fix #36792 * Fix the comment * Fix #36755 * Add a "sleep 3" * Follow up #36697 * Clarify the "attachment uploading" problem and function call --------- Signed-off-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: TheFox0x7 <thefox0x7@gmail.com>
1 parent 1b200dc commit 73e0e44

9 files changed

Lines changed: 56 additions & 42 deletions

File tree

custom/conf/app.example.ini

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,15 @@ RUN_USER = ; git
175175
;; The port number the builtin SSH server should listen on, defaults to SSH_PORT
176176
;SSH_LISTEN_PORT =
177177
;;
178-
;; Root path of SSH directory, default is '~/.ssh', but you have to use '/home/git/.ssh'.
178+
;; Root path of SSH user directory for the system's standalone SSH server if Gitea is not using its builtin SSH server.
179+
;; Default is the '.ssh' directory in the run user's home directory.
179180
;SSH_ROOT_PATH =
180181
;;
181-
;; Gitea will create a authorized_keys file by default when it is not using the internal ssh server
182+
;; Gitea will create an authorized_keys file by default when it is not using the builtin SSH server
182183
;; If you intend to use the AuthorizedKeysCommand functionality then you should turn this off.
183184
;SSH_CREATE_AUTHORIZED_KEYS_FILE = true
184185
;;
185-
;; Gitea will create a authorized_principals file by default when it is not using the internal ssh server
186+
;; Gitea will create an authorized_principals file by default when it is not using the builtin SSH server
186187
;; If you intend to use the AuthorizedPrincipalsCommand functionality then you should turn this off.
187188
;SSH_CREATE_AUTHORIZED_PRINCIPALS_FILE = true
188189
;;
@@ -1178,16 +1179,16 @@ LEVEL = Info
11781179
;[repository.release]
11791180
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11801181
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1181-
;; Comma-separated list of allowed file extensions (`.zip`), mime types (`text/plain`) or wildcard type (`image/*`, `audio/*`, `video/*`). Empty value or `*/*` allows all types.
1182+
;; Comma-separated list of allowed release attachment file extensions (`.zip`), mime types (`text/plain`) or wildcard type (`image/*`, `audio/*`, `video/*`). Empty value or `*/*` allows all types.
11821183
;ALLOWED_TYPES =
11831184
;;
11841185
;; Number of releases that are displayed on release page
11851186
;DEFAULT_PAGING_NUM = 10
11861187
;;
1187-
;; Max size of each file in megabytes. Defaults to 2GB
1188+
;; Max size of each release attachment file in megabytes. Defaults to 2GB
11881189
;FILE_MAX_SIZE = 2048
11891190
;;
1190-
;; Max number of files per upload. Defaults to 5
1191+
;; Max number of release attachment files per upload. Defaults to 5
11911192
;MAX_FILES = 5
11921193

11931194
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -1994,16 +1995,18 @@ LEVEL = Info
19941995
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19951996
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19961997
;;
1997-
;; Whether issue and pull request attachments are enabled. Defaults to `true`
1998+
;; Whether issue, pull-request and release attachments are enabled. Defaults to `true`
1999+
;; ALLOWED_TYPES/MAX_SIZE/MAX_FILES in this section only affect issue and pull-request attachments, not release attachments.
2000+
;; Release attachment has its own config options in [repository.release] section.
19982001
;ENABLED = true
19992002
;;
2000-
;; Comma-separated list of allowed file extensions (`.zip`), mime types (`text/plain`) or wildcard type (`image/*`, `audio/*`, `video/*`). Empty value or `*/*` allows all types.
2003+
;; Comma-separated list of allowed issue/pull-request attachment file extensions (`.zip`), mime types (`text/plain`) or wildcard type (`image/*`, `audio/*`, `video/*`). Empty value or `*/*` allows all types.
20012004
;ALLOWED_TYPES = .avif,.cpuprofile,.csv,.dmp,.docx,.fodg,.fodp,.fods,.fodt,.gif,.gz,.jpeg,.jpg,.json,.jsonc,.log,.md,.mov,.mp4,.odf,.odg,.odp,.ods,.odt,.patch,.pdf,.png,.pptx,.svg,.tgz,.txt,.webm,.webp,.xls,.xlsx,.zip
20022005
;;
2003-
;; Max size of each file. Defaults to 100MB
2006+
;; Max size of each issue/pull-request attachment file. Defaults to 100MB
20042007
;MAX_SIZE = 100
20052008
;;
2006-
;; Max number of files per upload. Defaults to 5
2009+
;; Max number of issue/pull-request attachment files per upload. Defaults to 5
20072010
;MAX_FILES = 5
20082011
;;
20092012
;; Storage type for attachments, `local` for local disk or `minio` for s3 compatible

docker/root/etc/s6/openssh/finish

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
#!/bin/bash
2+
# $1 = exit code of the run script, $2 = signal
3+
if [ "$1" -ne 0 ]; then
4+
# avoid immediately restarting the sshd service, which may cause CPU 100% if the error (permission, configuration) is not fixed
5+
echo "openssh failed with exit code $1 - waiting a short delay before attempting a restart"
6+
sleep 3
7+
fi
28
exit 0

routers/api/v1/repo/issue_attachment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func CreateIssueAttachment(ctx *context.APIContext) {
186186
}
187187

188188
uploaderFile := attachment_service.NewLimitedUploaderKnownSize(file, header.Size)
189-
attachment, err := attachment_service.UploadAttachmentGeneralSizeLimit(ctx, uploaderFile, setting.Attachment.AllowedTypes, &repo_model.Attachment{
189+
attachment, err := attachment_service.UploadAttachmentForIssue(ctx, uploaderFile, &repo_model.Attachment{
190190
Name: filename,
191191
UploaderID: ctx.Doer.ID,
192192
RepoID: ctx.Repo.Repository.ID,

routers/api/v1/repo/issue_comment_attachment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func CreateIssueCommentAttachment(ctx *context.APIContext) {
193193
}
194194

195195
uploaderFile := attachment_service.NewLimitedUploaderKnownSize(file, header.Size)
196-
attachment, err := attachment_service.UploadAttachmentGeneralSizeLimit(ctx, uploaderFile, setting.Attachment.AllowedTypes, &repo_model.Attachment{
196+
attachment, err := attachment_service.UploadAttachmentForIssue(ctx, uploaderFile, &repo_model.Attachment{
197197
Name: filename,
198198
UploaderID: ctx.Doer.ID,
199199
RepoID: ctx.Repo.Repository.ID,

routers/api/v1/repo/release_attachment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
242242
}
243243

244244
// Create a new attachment and save the file
245-
attach, err := attachment_service.UploadAttachmentReleaseSizeLimit(ctx, uploaderFile, setting.Repository.Release.AllowedTypes, &repo_model.Attachment{
245+
attach, err := attachment_service.UploadAttachmentForRelease(ctx, uploaderFile, &repo_model.Attachment{
246246
Name: filename,
247247
UploaderID: ctx.Doer.ID,
248248
RepoID: ctx.Repo.Repository.ID,

routers/web/repo/attachment.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ import (
2323

2424
// UploadIssueAttachment response for Issue/PR attachments
2525
func UploadIssueAttachment(ctx *context.Context) {
26-
uploadAttachment(ctx, ctx.Repo.Repository.ID, setting.Attachment.AllowedTypes)
26+
uploadAttachment(ctx, ctx.Repo.Repository.ID, attachment.UploadAttachmentForIssue)
2727
}
2828

2929
// UploadReleaseAttachment response for uploading release attachments
3030
func UploadReleaseAttachment(ctx *context.Context) {
31-
uploadAttachment(ctx, ctx.Repo.Repository.ID, setting.Repository.Release.AllowedTypes)
31+
uploadAttachment(ctx, ctx.Repo.Repository.ID, attachment.UploadAttachmentForRelease)
3232
}
3333

3434
// UploadAttachment response for uploading attachments
35-
func uploadAttachment(ctx *context.Context, repoID int64, allowedTypes string) {
35+
func uploadAttachment(ctx *context.Context, repoID int64, uploadFunc attachment.UploadAttachmentFunc) {
3636
if !setting.Attachment.Enabled {
3737
ctx.HTTPError(http.StatusNotFound, "attachment is not enabled")
3838
return
@@ -46,7 +46,7 @@ func uploadAttachment(ctx *context.Context, repoID int64, allowedTypes string) {
4646
defer file.Close()
4747

4848
uploaderFile := attachment.NewLimitedUploaderKnownSize(file, header.Size)
49-
attach, err := attachment.UploadAttachmentReleaseSizeLimit(ctx, uploaderFile, allowedTypes, &repo_model.Attachment{
49+
attach, err := uploadFunc(ctx, uploaderFile, &repo_model.Attachment{
5050
Name: header.Filename,
5151
UploaderID: ctx.Doer.ID,
5252
RepoID: repoID,
@@ -56,7 +56,7 @@ func uploadAttachment(ctx *context.Context, repoID int64, allowedTypes string) {
5656
ctx.HTTPError(http.StatusBadRequest, err.Error())
5757
return
5858
}
59-
ctx.ServerError("UploadAttachmentReleaseSizeLimit", err)
59+
ctx.ServerError("uploadAttachment(uploadFunc)", err)
6060
return
6161
}
6262

@@ -119,7 +119,7 @@ func DeleteAttachment(ctx *context.Context) {
119119
})
120120
}
121121

122-
// GetAttachment serve attachments with the given UUID
122+
// ServeAttachment serve attachments with the given UUID
123123
func ServeAttachment(ctx *context.Context, uuid string) {
124124
attach, err := repo_model.GetAttachmentByUUID(ctx, uuid)
125125
if err != nil {

routers/web/repo/issue_comment.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,26 @@ func NewComment(ctx *context.Context) {
5757
return
5858
}
5959

60+
redirect := fmt.Sprintf("%s/%s/%d", ctx.Repo.RepoLink, issueType, issue.Index)
6061
attachments := util.Iif(setting.Attachment.Enabled, form.Files, nil)
6162

62-
// Can allow empty comments if there are attachments or a status change (close, reopen, approve, reject)
63-
// So, only stop if there is no content, no attachments, and no status change.
64-
if form.Content == "" && len(attachments) == 0 && form.Status == "" {
65-
ctx.JSONError(ctx.Tr("repo.issues.comment_no_content"))
66-
return
67-
}
68-
69-
comment, err := issue_service.CreateIssueComment(ctx, ctx.Doer, ctx.Repo.Repository, issue, form.Content, attachments)
70-
if err != nil {
71-
if errors.Is(err, user_model.ErrBlockedUser) {
72-
ctx.JSONError(ctx.Tr("repo.issues.comment.blocked_user"))
73-
} else {
74-
ctx.ServerError("CreateIssueComment", err)
63+
// allow empty content if there are attachments
64+
if form.Content != "" || len(attachments) > 0 {
65+
comment, err := issue_service.CreateIssueComment(ctx, ctx.Doer, ctx.Repo.Repository, issue, form.Content, attachments)
66+
if err != nil {
67+
if errors.Is(err, user_model.ErrBlockedUser) {
68+
ctx.JSONError(ctx.Tr("repo.issues.comment.blocked_user"))
69+
} else {
70+
ctx.ServerError("CreateIssueComment", err)
71+
}
72+
return
7573
}
74+
// redirect to the comment's hashtag
75+
redirect += "#" + comment.HashTag()
76+
} else if form.Status == "" {
77+
// if no status change (close, reopen), it is a plain comment, and content is required
78+
// "approve/reject" are handled differently in SubmitReview
79+
ctx.JSONError(ctx.Tr("repo.issues.comment_no_content"))
7680
return
7781
}
7882

@@ -86,6 +90,7 @@ func NewComment(ctx *context.Context) {
8690
!(issue.IsPull && issue.PullRequest.HasMerged) {
8791
// Duplication and conflict check should apply to reopen pull request.
8892
var branchOtherUnmergedPR *issues_model.PullRequest
93+
var err error
8994
if form.Status == "reopen" && issue.IsPull {
9095
pull := issue.PullRequest
9196
branchOtherUnmergedPR, err = issues_model.GetUnmergedPullRequest(ctx, pull.HeadRepoID, pull.BaseRepoID, pull.HeadBranch, pull.BaseBranch, pull.Flow)
@@ -179,11 +184,6 @@ func NewComment(ctx *context.Context) {
179184
}
180185
} // end if: handle close or reopen
181186

182-
// Redirect to the comment, add hashtag if it exists
183-
redirect := fmt.Sprintf("%s/%s/%d", ctx.Repo.RepoLink, issueType, issue.Index)
184-
if comment != nil {
185-
redirect += "#" + comment.HashTag()
186-
}
187187
ctx.JSONRedirect(redirect)
188188
}
189189

services/attachment/attachment.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,17 @@ func NewLimitedUploaderMaxBytesReader(r io.ReadCloser, w http.ResponseWriter) *U
5454
return &UploaderFile{rd: r, size: -1, respWriter: w}
5555
}
5656

57-
func UploadAttachmentGeneralSizeLimit(ctx context.Context, file *UploaderFile, allowedTypes string, attach *repo_model.Attachment) (*repo_model.Attachment, error) {
58-
return uploadAttachment(ctx, file, allowedTypes, setting.Attachment.MaxSize<<20, attach)
57+
type UploadAttachmentFunc func(ctx context.Context, file *UploaderFile, attach *repo_model.Attachment) (*repo_model.Attachment, error)
58+
59+
func UploadAttachmentForIssue(ctx context.Context, file *UploaderFile, attach *repo_model.Attachment) (*repo_model.Attachment, error) {
60+
return uploadAttachment(ctx, file, setting.Attachment.AllowedTypes, setting.Attachment.MaxSize<<20, attach)
5961
}
6062

61-
func UploadAttachmentReleaseSizeLimit(ctx context.Context, file *UploaderFile, allowedTypes string, attach *repo_model.Attachment) (*repo_model.Attachment, error) {
62-
return uploadAttachment(ctx, file, allowedTypes, setting.Repository.Release.FileMaxSize<<20, attach)
63+
func UploadAttachmentForRelease(ctx context.Context, file *UploaderFile, attach *repo_model.Attachment) (*repo_model.Attachment, error) {
64+
// FIXME: although the release attachment has different settings from the issue attachment,
65+
// it still uses the same attachment table, the same storage and the same upload logic
66+
// So if the "issue attachment [attachment]" is not enabled, it will also affect the release attachment, which is not expected.
67+
return uploadAttachment(ctx, file, setting.Repository.Release.AllowedTypes, setting.Repository.Release.FileMaxSize<<20, attach)
6368
}
6469

6570
func uploadAttachment(ctx context.Context, file *UploaderFile, allowedTypes string, maxFileSize int64, attach *repo_model.Attachment) (*repo_model.Attachment, error) {

services/mailer/incoming/incoming_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (h *ReplyHandler) Handle(ctx context.Context, content *MailContent, doer *u
8888
for _, attachment := range content.Attachments {
8989
attachmentBuf := bytes.NewReader(attachment.Content)
9090
uploaderFile := attachment_service.NewLimitedUploaderKnownSize(attachmentBuf, attachmentBuf.Size())
91-
a, err := attachment_service.UploadAttachmentGeneralSizeLimit(ctx, uploaderFile, setting.Attachment.AllowedTypes, &repo_model.Attachment{
91+
a, err := attachment_service.UploadAttachmentForIssue(ctx, uploaderFile, &repo_model.Attachment{
9292
Name: attachment.Name,
9393
UploaderID: doer.ID,
9494
RepoID: issue.Repo.ID,

0 commit comments

Comments
 (0)