aboutsummaryrefslogtreecommitdiffstats
path: root/routers/web/repo/common_recentbranches.go
blob: c2083dec738797b837faf6c3cf6af6ab4a2f4bc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package repo

import (
	git_model "code.gitea.io/gitea/models/git"
	access_model "code.gitea.io/gitea/models/perm/access"
	unit_model "code.gitea.io/gitea/models/unit"
	"code.gitea.io/gitea/modules/log"
	"code.gitea.io/gitea/services/context"
	repo_service "code.gitea.io/gitea/services/repository"
)

type RecentBranchesPromptDataStruct struct {
	RecentlyPushedNewBranches []*git_model.RecentlyPushedNewBranch
}

func prepareRecentlyPushedNewBranches(ctx *context.Context) {
	if ctx.Doer == nil {
		return
	}
	if err := ctx.Repo.Repository.GetBaseRepo(ctx); err != nil {
		log.Error("GetBaseRepo: %v", err)
		return
	}

	opts := git_model.FindRecentlyPushedNewBranchesOptions{
		Repo:     ctx.Repo.Repository,
		BaseRepo: ctx.Repo.Repository,
	}
	if ctx.Repo.Repository.IsFork {
		opts.BaseRepo = ctx.Repo.Repository.BaseRepo
	}

	baseRepoPerm, err := access_model.GetUserRepoPermission(ctx, opts.BaseRepo, ctx.Doer)
	if err != nil {
		log.Error("GetUserRepoPermission: %v", err)
		return
	}
	if !opts.Repo.CanContentChange() || !opts.BaseRepo.CanContentChange() {
		return
	}
	if !opts.BaseRepo.UnitEnabled(ctx, unit_model.TypePullRequests) || !baseRepoPerm.CanRead(unit_model.TypePullRequests) {
		return
	}

	var finalBranches []*git_model.RecentlyPushedNewBranch
	branches, err := git_model.FindRecentlyPushedNewBranches(ctx, ctx.Doer, opts)
	if err != nil {
		log.Error("FindRecentlyPushedNewBranches failed: %v", err)
		return
	}

	for _, branch := range branches {
		divergingInfo, err := repo_service.GetBranchDivergingInfo(ctx,
			branch.BranchRepo, branch.BranchName, // "base" repo for diverging info
			opts.BaseRepo, opts.BaseRepo.DefaultBranch, // "head" repo for diverging info
		)
		if err != nil {
			log.Error("GetBranchDivergingInfo failed: %v", err)
			continue
		}
		branchRepoHasNewCommits := divergingInfo.BaseHasNewCommits
		baseRepoCommitsBehind := divergingInfo.HeadCommitsBehind
		if branchRepoHasNewCommits || baseRepoCommitsBehind > 0 {
			finalBranches = append(finalBranches, branch)
		}
	}
	if len(finalBranches) > 0 {
		ctx.Data["RecentBranchesPromptData"] = RecentBranchesPromptDataStruct{finalBranches}
	}
}