|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package repo |
| 5 | + |
| 6 | +import ( |
| 7 | + git_model "code.gitea.io/gitea/models/git" |
| 8 | + access_model "code.gitea.io/gitea/models/perm/access" |
| 9 | + unit_model "code.gitea.io/gitea/models/unit" |
| 10 | + "code.gitea.io/gitea/modules/log" |
| 11 | + "code.gitea.io/gitea/services/context" |
| 12 | + repo_service "code.gitea.io/gitea/services/repository" |
| 13 | +) |
| 14 | + |
| 15 | +type RecentBranchesPromptDataStruct struct { |
| 16 | + RecentlyPushedNewBranches []*git_model.RecentlyPushedNewBranch |
| 17 | +} |
| 18 | + |
| 19 | +func prepareRecentlyPushedNewBranches(ctx *context.Context) { |
| 20 | + if ctx.Doer == nil { |
| 21 | + return |
| 22 | + } |
| 23 | + if err := ctx.Repo.Repository.GetBaseRepo(ctx); err != nil { |
| 24 | + log.Error("GetBaseRepo: %v", err) |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + opts := git_model.FindRecentlyPushedNewBranchesOptions{ |
| 29 | + Repo: ctx.Repo.Repository, |
| 30 | + BaseRepo: ctx.Repo.Repository, |
| 31 | + } |
| 32 | + if ctx.Repo.Repository.IsFork { |
| 33 | + opts.BaseRepo = ctx.Repo.Repository.BaseRepo |
| 34 | + } |
| 35 | + |
| 36 | + baseRepoPerm, err := access_model.GetUserRepoPermission(ctx, opts.BaseRepo, ctx.Doer) |
| 37 | + if err != nil { |
| 38 | + log.Error("GetUserRepoPermission: %v", err) |
| 39 | + return |
| 40 | + } |
| 41 | + if !opts.Repo.CanContentChange() || !opts.BaseRepo.CanContentChange() { |
| 42 | + return |
| 43 | + } |
| 44 | + if !opts.BaseRepo.UnitEnabled(ctx, unit_model.TypePullRequests) || !baseRepoPerm.CanRead(unit_model.TypePullRequests) { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + var finalBranches []*git_model.RecentlyPushedNewBranch |
| 49 | + branches, err := git_model.FindRecentlyPushedNewBranches(ctx, ctx.Doer, opts) |
| 50 | + if err != nil { |
| 51 | + log.Error("FindRecentlyPushedNewBranches failed: %v", err) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + for _, branch := range branches { |
| 56 | + divergingInfo, err := repo_service.GetBranchDivergingInfo(ctx, |
| 57 | + branch.BranchRepo, branch.BranchName, // "base" repo for diverging info |
| 58 | + opts.BaseRepo, opts.BaseRepo.DefaultBranch, // "head" repo for diverging info |
| 59 | + ) |
| 60 | + if err != nil { |
| 61 | + log.Error("GetBranchDivergingInfo failed: %v", err) |
| 62 | + continue |
| 63 | + } |
| 64 | + branchRepoHasNewCommits := divergingInfo.BaseHasNewCommits |
| 65 | + baseRepoCommitsBehind := divergingInfo.HeadCommitsBehind |
| 66 | + if branchRepoHasNewCommits || baseRepoCommitsBehind > 0 { |
| 67 | + finalBranches = append(finalBranches, branch) |
| 68 | + } |
| 69 | + } |
| 70 | + if len(finalBranches) > 0 { |
| 71 | + ctx.Data["RecentBranchesPromptData"] = RecentBranchesPromptDataStruct{finalBranches} |
| 72 | + } |
| 73 | +} |
0 commit comments