mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
No compat with old Gitea deployments, so the wire values that were held back
for it go too.
.gitea/* WorkflowDirs is now {.hanzo, .github}/workflows. Same for the
PR-template, ISSUE_TEMPLATE and repo-template lookups, which
each carried a .gitea variant. .github stays — GitHub compat
is worth having; Gitea compat is what we just decided not to
keep.
webhook type "gitea" -> "native", constant GITEA -> NATIVE, routes
/hooks/gitea/{new,id} -> /hooks/native/{new,id}, and the enum
in both swagger documents.
actions context gitea_runtime_token -> git_runtime_token,
gitea_default_actions_url -> git_default_actions_url.
models/migrations/* is untouched on purpose. A schema migration describes what
the database held AT THAT VERSION; v1_14/v162.go rewriting its own history to
say "native" would make it wrong about the thing it exists to migrate.
Three test fixtures needed aligning rather than renaming, all the same shape as
the bugs this cleanup keeps finding: WorkflowDirs expectations ended up with a
duplicated .hanzo entry, the repo-template test built its fixture under .gitea,
and five API tests still posted Type: "gitea" (422 now, correctly).
models, modules, services, routers green; integration compile gate green.
Co-authored-by: Hanzo Dev <[email protected]>
183 lines
5.1 KiB
Go
183 lines
5.1 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
||
// Copyright 2024 The Gitea Authors. 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
|
||
}
|
||
// https://github.com/go-gitea/gitea/issues/29763, 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
|
||
}
|