gitea/services/issue/dev_link.go

86 lines
2.1 KiB
Go
Raw Normal View History

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package issue
import (
"context"
2024-08-22 04:08:18 +02:00
"fmt"
"sort"
"strconv"
git_model "code.gitea.io/gitea/models/git"
issues_model "code.gitea.io/gitea/models/issues"
repo_model "code.gitea.io/gitea/models/repo"
2024-08-22 04:08:18 +02:00
"code.gitea.io/gitea/modules/container"
)
func FindIssueDevLinksByIssue(ctx context.Context, issue *issues_model.Issue) (issues_model.IssueDevLinks, error) {
devLinks, err := issues_model.FindIssueDevLinksByIssueID(ctx, issue.ID)
if err != nil {
return nil, err
}
if err := issue.LoadRepo(ctx); err != nil {
return nil, err
}
2024-08-22 04:08:18 +02:00
sort.Slice(devLinks, func(i, j int) bool {
switch {
case devLinks[j].LinkType == issues_model.IssueDevLinkTypePullRequest:
return false
default:
return true
}
})
branchPRExists := make(container.Set[string])
for _, link := range devLinks {
2024-11-29 01:13:40 +01:00
link.Repo = issue.Repo
if link.LinkedRepoID == 0 {
link.LinkedRepoID = issue.RepoID
}
isSameRepo := issue.RepoID == link.LinkedRepoID
if isSameRepo {
link.LinkedRepo = issue.Repo
} else if link.LinkedRepoID > 0 {
repo, err := repo_model.GetRepositoryByID(ctx, link.LinkedRepoID)
if err != nil {
return nil, err
}
link.LinkedRepo = repo
}
switch link.LinkType {
case issues_model.IssueDevLinkTypePullRequest:
pullID, err := strconv.ParseInt(link.LinkIndex, 10, 64)
if err != nil {
return nil, err
}
pull, err := issues_model.GetPullRequestByID(ctx, pullID)
if err != nil {
return nil, err
}
2024-08-22 04:08:18 +02:00
pull.BaseRepo = issue.Repo
pull.HeadRepo = link.LinkedRepo
if err := pull.LoadIssue(ctx); err != nil {
return nil, err
}
pull.Issue.Repo = issue.Repo
link.PullRequest = pull
2024-08-22 04:08:18 +02:00
branchPRExists.Add(fmt.Sprintf("%d-%s", link.LinkedRepoID, pull.HeadBranch))
case issues_model.IssueDevLinkTypeBranch:
branch, err := git_model.GetBranch(ctx, link.LinkedRepoID, link.LinkIndex)
if err != nil {
return nil, err
}
link.Branch = branch
link.Branch.Repo = link.LinkedRepo
2024-08-22 04:08:18 +02:00
link.DisplayBranch = !branchPRExists.Contains(fmt.Sprintf("%d-%s", link.LinkedRepoID, link.LinkIndex))
}
}
return devLinks, nil
}