mirror of
https://github.com/go-gitea/gitea.git
synced 2025-11-06 23:10:57 +01:00
Use branch id or pull request id as dev link id
This commit is contained in:
parent
1787e028db
commit
5d7388924f
@ -156,6 +156,19 @@ func init() {
|
||||
db.RegisterModel(new(RenamedBranch))
|
||||
}
|
||||
|
||||
func GetBranchByID(ctx context.Context, branchID int64) (*Branch, error) {
|
||||
var branch Branch
|
||||
has, err := db.GetEngine(ctx).ID(branchID).Get(&branch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, ErrBranchNotExist{
|
||||
RepoID: branch.RepoID,
|
||||
}
|
||||
}
|
||||
return &branch, nil
|
||||
}
|
||||
|
||||
func GetBranch(ctx context.Context, repoID int64, branchName string) (*Branch, error) {
|
||||
var branch Branch
|
||||
has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).And("name=?", branchName).Get(&branch)
|
||||
|
||||
@ -25,7 +25,7 @@ type IssueDevLink struct {
|
||||
IssueID int64 `xorm:"INDEX"`
|
||||
LinkType IssueDevLinkType
|
||||
LinkedRepoID int64 `xorm:"INDEX"` // it can link to self repo or other repo
|
||||
LinkIndex string // branch name, pull request number or commit sha
|
||||
LinkID int64 // branch id in branch table or pull request id
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
Repo *repo_model.Repository `xorm:"-"` // current repo of issue
|
||||
LinkedRepo *repo_model.Repository `xorm:"-"`
|
||||
|
||||
@ -15,7 +15,7 @@ func CreateTableIssueDevLink(x *xorm.Engine) error {
|
||||
IssueID int64 `xorm:"INDEX"`
|
||||
LinkType int
|
||||
LinkedRepoID int64 `xorm:"INDEX"` // it can link to self repo or other repo
|
||||
LinkIndex string // branch name, pull request number or commit sha
|
||||
LinkID int64 // branch id in branch table or pull request id
|
||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||||
}
|
||||
return x.Sync(new(IssueDevLink))
|
||||
|
||||
@ -99,11 +99,17 @@ func CreateBranchFromIssue(ctx *context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
branch, err := git_model.GetBranch(ctx, repo.ID, form.NewBranchName)
|
||||
if err != nil {
|
||||
ctx.ServerError("GetBranch", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := issues_model.CreateIssueDevLink(ctx, &issues_model.IssueDevLink{
|
||||
IssueID: issue.ID,
|
||||
LinkType: issues_model.IssueDevLinkTypeBranch,
|
||||
LinkedRepoID: repo.ID,
|
||||
LinkIndex: form.NewBranchName,
|
||||
LinkID: branch.ID,
|
||||
}); err != nil {
|
||||
ctx.ServerError("CreateIssueDevLink", err)
|
||||
return
|
||||
|
||||
@ -7,7 +7,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
git_model "code.gitea.io/gitea/models/git"
|
||||
issues_model "code.gitea.io/gitea/models/issues"
|
||||
@ -49,11 +48,7 @@ func FindIssueDevLinksByIssue(ctx context.Context, issue *issues_model.Issue) (i
|
||||
|
||||
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)
|
||||
pull, err := issues_model.GetPullRequestByID(ctx, link.LinkID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -64,15 +59,15 @@ func FindIssueDevLinksByIssue(ctx context.Context, issue *issues_model.Issue) (i
|
||||
}
|
||||
pull.Issue.Repo = issue.Repo
|
||||
link.PullRequest = pull
|
||||
branchPRExists.Add(fmt.Sprintf("%d-%s", link.LinkedRepoID, pull.HeadBranch))
|
||||
branchPRExists.Add(fmt.Sprintf("%d-%d-%s", link.LinkedRepoID, link.LinkType, pull.HeadBranch))
|
||||
case issues_model.IssueDevLinkTypeBranch:
|
||||
branch, err := git_model.GetBranch(ctx, link.LinkedRepoID, link.LinkIndex)
|
||||
branch, err := git_model.GetBranchByID(ctx, link.LinkID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
link.Branch = branch
|
||||
link.Branch.Repo = link.LinkedRepo
|
||||
link.DisplayBranch = !branchPRExists.Contains(fmt.Sprintf("%d-%s", link.LinkedRepoID, link.LinkIndex))
|
||||
link.DisplayBranch = !branchPRExists.Contains(fmt.Sprintf("%d-%d-%d", link.LinkedRepoID, link.LinkType, link.LinkID))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -11,7 +11,6 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -152,7 +151,7 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
|
||||
IssueID: link.IssueID,
|
||||
LinkType: issues_model.IssueDevLinkTypePullRequest,
|
||||
LinkedRepoID: pr.HeadRepoID,
|
||||
LinkIndex: strconv.FormatInt(pr.ID, 10),
|
||||
LinkID: pr.ID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user