mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Storage buckets, indexer names, the bleve analyzer, the lock prefix, SSH host keys, the markdown front-matter key, the container data paths, the RPM repo id and the API security scheme all read as this product now, and app.example.ini matches the code again. The upstream maintainer list and the changelog generator config went with the release history they belonged to, and the citation comments state the fact rather than link the issue. Three inert rules turn out to have been inert because they named a module path this tree does not use: gci grouped no first-party imports under prefix(), and depguard let models/migrations import models and modules/structs. They now name github.com/hanzoai/git. The config example also documented .gitea/workflows while the code has read .hanzo/workflows for some time. The authorized_keys marker follows the delegate-hook precedent: the new name is written and the old one is still recognised, because a marker we do not know is copied through as a hand-added key and leaves a revoked key authorized. The stored user_setting key keeps its value; only a migration may change that. Co-authored-by: Hanzo Dev <[email protected]>
182 lines
4.9 KiB
Go
182 lines
4.9 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
package issue
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"slices"
|
||
"time"
|
||
|
||
issues_model "github.com/hanzoai/git/models/issues"
|
||
org_model "github.com/hanzoai/git/models/organization"
|
||
user_model "github.com/hanzoai/git/models/user"
|
||
"github.com/hanzoai/git/modules/git"
|
||
"github.com/hanzoai/git/modules/gitrepo"
|
||
"github.com/hanzoai/git/modules/log"
|
||
"github.com/hanzoai/git/modules/setting"
|
||
)
|
||
|
||
type ReviewRequestNotifier struct {
|
||
Comment *issues_model.Comment
|
||
IsAdd bool
|
||
Reviewer *user_model.User
|
||
ReviewTeam *org_model.Team
|
||
}
|
||
|
||
var codeOwnerFiles = []string{"CODEOWNERS", "docs/CODEOWNERS", ".hanzo/CODEOWNERS"}
|
||
|
||
// codeOwnerMatchBudget caps the total wall-clock time spent evaluating all
|
||
// CODEOWNERS rules against all changed files for a single PR.
|
||
const codeOwnerMatchBudget = 2 * time.Second
|
||
|
||
func IsCodeOwnerFile(f string) bool {
|
||
return slices.Contains(codeOwnerFiles, f)
|
||
}
|
||
|
||
func PullRequestCodeOwnersReview(ctx context.Context, pr *issues_model.PullRequest) ([]*ReviewRequestNotifier, error) {
|
||
if err := pr.LoadIssue(ctx); err != nil {
|
||
return nil, err
|
||
}
|
||
issue := pr.Issue
|
||
if pr.IsWorkInProgress(ctx) {
|
||
return nil, nil
|
||
}
|
||
if err := pr.LoadHeadRepo(ctx); err != nil {
|
||
return nil, err
|
||
}
|
||
if err := pr.LoadBaseRepo(ctx); err != nil {
|
||
return nil, err
|
||
}
|
||
pr.Issue.Repo = pr.BaseRepo
|
||
|
||
if pr.BaseRepo.IsFork {
|
||
return nil, nil
|
||
}
|
||
|
||
repo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer repo.Close()
|
||
|
||
commit, err := repo.GetBranchCommit(pr.BaseRepo.DefaultBranch)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
var data string
|
||
for _, file := range codeOwnerFiles {
|
||
if blob, err := commit.GetBlobByPath(ctx, repo, file); err == nil {
|
||
data, err = blob.GetBlobContent(setting.UI.MaxDisplayFileSize)
|
||
if err == nil {
|
||
break
|
||
}
|
||
}
|
||
}
|
||
if data == "" {
|
||
return nil, nil
|
||
}
|
||
|
||
rules, _ := issues_model.GetCodeOwnersFromContent(ctx, data)
|
||
if len(rules) == 0 {
|
||
return nil, nil
|
||
}
|
||
|
||
// get the mergebase
|
||
mergeBase, err := gitrepo.MergeBase(ctx, pr.BaseRepo, git.BranchPrefix+pr.BaseBranch, pr.GetGitHeadRefName())
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
// we need to get the files changed
|
||
// between the merge base and the head commit but not the base branch and the head commit
|
||
changedFiles, err := repo.GetFilesChangedBetween(mergeBase, pr.GetGitHeadRefName())
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
uniqUsers := make(map[int64]*user_model.User)
|
||
uniqTeams := make(map[string]*org_model.Team)
|
||
// Bound the total time spent matching rules×files. The per-rule MatchTimeout
|
||
// only caps a single match; without an aggregate budget a crafted CODEOWNERS
|
||
// plus a PR touching many files could still exhaust CPU inside this loop.
|
||
matchDeadline := time.Now().Add(codeOwnerMatchBudget)
|
||
ruleLoop:
|
||
for _, rule := range rules {
|
||
for _, f := range changedFiles {
|
||
if time.Now().After(matchDeadline) {
|
||
log.Warn("CODEOWNERS matching for PR %s#%d exceeded its time budget; some rules were not evaluated", pr.BaseRepo.FullName(), pr.ID)
|
||
break ruleLoop
|
||
}
|
||
shouldMatch := !rule.Negative
|
||
matched, _ := rule.Rule.MatchString(f) // err only happens when timeouts, any error can be considered as not matched
|
||
if matched == shouldMatch {
|
||
for _, u := range rule.Users {
|
||
uniqUsers[u.ID] = u
|
||
}
|
||
for _, t := range rule.Teams {
|
||
uniqTeams[fmt.Sprintf("%d/%d", t.OrgID, t.ID)] = t
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
notifiers := make([]*ReviewRequestNotifier, 0, len(uniqUsers)+len(uniqTeams))
|
||
|
||
if err := issue.LoadPoster(ctx); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// load all reviews from database
|
||
latestReviews, _, err := issues_model.GetReviewsByIssueID(ctx, pr.IssueID)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
contain := func(list issues_model.ReviewList, u *user_model.User) bool {
|
||
for _, review := range list {
|
||
if review.ReviewerTeamID == 0 && review.ReviewerID == u.ID {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
for _, u := range uniqUsers {
|
||
if u.ID != issue.Poster.ID && !contain(latestReviews, u) {
|
||
comment, err := issues_model.AddReviewRequest(ctx, issue, u, issue.Poster, true)
|
||
if err != nil {
|
||
log.Warn("Failed add review user: %s to PR review: %s#%d, error: %s", u.Name, pr.BaseRepo.Name, pr.ID, err)
|
||
return nil, err
|
||
}
|
||
if comment == nil { // comment maybe nil if review type is ReviewTypeRequest
|
||
continue
|
||
}
|
||
notifiers = append(notifiers, &ReviewRequestNotifier{
|
||
Comment: comment,
|
||
IsAdd: true,
|
||
Reviewer: u,
|
||
})
|
||
}
|
||
}
|
||
|
||
for _, t := range uniqTeams {
|
||
comment, err := issues_model.AddTeamReviewRequest(ctx, issue, t, issue.Poster, true)
|
||
if err != nil {
|
||
log.Warn("Failed add reviewer team: %s to PR review: %s#%d, error: %s", t.Name, pr.BaseRepo.Name, pr.ID, err)
|
||
return nil, err
|
||
}
|
||
if comment == nil { // comment maybe nil if review type is ReviewTypeRequest
|
||
continue
|
||
}
|
||
notifiers = append(notifiers, &ReviewRequestNotifier{
|
||
Comment: comment,
|
||
IsAdd: true,
|
||
ReviewTeam: t,
|
||
})
|
||
}
|
||
|
||
return notifiers, nil
|
||
}
|