1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-07-19 01:29:40 +02:00

blob: use NewTruncatedReader for CodeOwners parsing

tested in tests/integration/pull_review_test.go:TestPullView_CodeOwner
This commit is contained in:
oliverpool 2025-06-17 14:18:11 +02:00
parent dd79f0ce2b
commit f708bacfff
3 changed files with 25 additions and 22 deletions

View file

@ -5,6 +5,7 @@
package issues package issues
import ( import (
"bufio"
"context" "context"
"errors" "errors"
"fmt" "fmt"
@ -923,31 +924,30 @@ func MergeBlockedByOutdatedBranch(protectBranch *git_model.ProtectedBranch, pr *
return protectBranch.BlockOnOutdatedBranch && pr.CommitsBehind > 0 return protectBranch.BlockOnOutdatedBranch && pr.CommitsBehind > 0
} }
// GetCodeOwnersFromContent returns the code owners configuration // GetCodeOwnersFromReader returns the code owners configuration
// Return empty slice if files missing
// Return warning messages on parsing errors // Return warning messages on parsing errors
// We're trying to do the best we can when parsing a file. // We're trying to do the best we can when parsing a file.
// Invalid lines are skipped. Non-existent users and teams too. // Invalid lines are skipped. Non-existent users and teams too.
func GetCodeOwnersFromContent(ctx context.Context, data string) ([]*CodeOwnerRule, []string) { func GetCodeOwnersFromReader(ctx context.Context, rc io.ReadCloser, truncated bool) ([]*CodeOwnerRule, []string) {
if len(data) == 0 { defer rc.Close()
return nil, nil scanner := bufio.NewScanner(rc)
}
rules := make([]*CodeOwnerRule, 0) var rules []*CodeOwnerRule
lines := strings.Split(data, "\n") var warnings []string
warnings := make([]string, 0) line := 0
for scanner.Scan() {
line++
for i, line := range lines { tokens := TokenizeCodeOwnersLine(scanner.Text())
tokens := TokenizeCodeOwnersLine(line)
if len(tokens) == 0 { if len(tokens) == 0 {
continue continue
} else if len(tokens) < 2 { } else if len(tokens) < 2 {
warnings = append(warnings, fmt.Sprintf("Line: %d: incorrect format", i+1)) warnings = append(warnings, fmt.Sprintf("Line: %d: incorrect format", line))
continue continue
} }
rule, wr := ParseCodeOwnersLine(ctx, tokens) rule, wr := ParseCodeOwnersLine(ctx, tokens)
for _, w := range wr { for _, w := range wr {
warnings = append(warnings, fmt.Sprintf("Line: %d: %s", i+1, w)) warnings = append(warnings, fmt.Sprintf("Line: %d: %s", line, w))
} }
if rule == nil { if rule == nil {
continue continue
@ -955,6 +955,12 @@ func GetCodeOwnersFromContent(ctx context.Context, data string) ([]*CodeOwnerRul
rules = append(rules, rule) rules = append(rules, rule)
} }
if err := scanner.Err(); err != nil {
warnings = append(warnings, err.Error())
}
if truncated {
warnings = append(warnings, fmt.Sprintf("File too big: truncated while on line %d", line))
}
return rules, warnings return rules, warnings
} }

View file

@ -439,8 +439,8 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry) {
ctx.Data["FileError"] = ctx.Locale.Tr("actions.runs.invalid_workflow_helper", workFlowErr.Error()) ctx.Data["FileError"] = ctx.Locale.Tr("actions.runs.invalid_workflow_helper", workFlowErr.Error())
} }
} else if slices.Contains([]string{"CODEOWNERS", "docs/CODEOWNERS", ".gitea/CODEOWNERS"}, ctx.Repo.TreePath) { } else if slices.Contains([]string{"CODEOWNERS", "docs/CODEOWNERS", ".gitea/CODEOWNERS"}, ctx.Repo.TreePath) {
if data, err := blob.GetBlobContent(setting.UI.MaxDisplayFileSize); err == nil { if rc, size, err := blob.NewTruncatedReader(setting.UI.MaxDisplayFileSize); err == nil {
_, warnings := issue_model.GetCodeOwnersFromContent(ctx, data) _, warnings := issue_model.GetCodeOwnersFromReader(ctx, rc, size > setting.UI.MaxDisplayFileSize)
if len(warnings) > 0 { if len(warnings) > 0 {
ctx.Data["FileWarning"] = strings.Join(warnings, "\n") ctx.Data["FileWarning"] = strings.Join(warnings, "\n")
} }

View file

@ -43,8 +43,6 @@ type ReviewRequestNotifier struct {
} }
func PullRequestCodeOwnersReview(ctx context.Context, issue *issues_model.Issue, pr *issues_model.PullRequest) ([]*ReviewRequestNotifier, error) { func PullRequestCodeOwnersReview(ctx context.Context, issue *issues_model.Issue, pr *issues_model.PullRequest) ([]*ReviewRequestNotifier, error) {
files := []string{"CODEOWNERS", "docs/CODEOWNERS", ".gitea/CODEOWNERS"}
if pr.IsWorkInProgress(ctx) { if pr.IsWorkInProgress(ctx) {
return nil, nil return nil, nil
} }
@ -72,18 +70,17 @@ func PullRequestCodeOwnersReview(ctx context.Context, issue *issues_model.Issue,
return nil, err return nil, err
} }
var data string var rules []*issues_model.CodeOwnerRule
for _, file := range files { for _, file := range []string{"CODEOWNERS", "docs/CODEOWNERS", ".gitea/CODEOWNERS"} {
if blob, err := commit.GetBlobByPath(file); err == nil { if blob, err := commit.GetBlobByPath(file); err == nil {
data, err = blob.GetBlobContent(setting.UI.MaxDisplayFileSize) rc, size, err := blob.NewTruncatedReader(setting.UI.MaxDisplayFileSize)
if err == nil { if err == nil {
rules, _ = issues_model.GetCodeOwnersFromReader(ctx, rc, size > setting.UI.MaxDisplayFileSize)
break break
} }
} }
} }
rules, _ := issues_model.GetCodeOwnersFromContent(ctx, data)
// get the mergebase // get the mergebase
mergeBase, err := getMergeBase(repo, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName()) mergeBase, err := getMergeBase(repo, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitRefName())
if err != nil { if err != nil {