2024-08-22 00:34:03 +02:00
|
|
|
// 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"
|
2024-08-22 00:34:03 +02:00
|
|
|
"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"
|
2024-08-22 00:34:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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])
|
|
|
|
|
2024-08-22 00:34:03 +02:00
|
|
|
for _, link := range devLinks {
|
|
|
|
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
|
2024-08-22 00:34:03 +02:00
|
|
|
link.PullRequest = pull
|
2024-08-22 04:08:18 +02:00
|
|
|
branchPRExists.Add(fmt.Sprintf("%d-%s", link.LinkedRepoID, pull.HeadBranch))
|
2024-08-22 00:34:03 +02:00
|
|
|
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))
|
2024-08-22 00:34:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return devLinks, nil
|
|
|
|
}
|