Skip to content

Commit 5d73889

Browse files
committed
Use branch id or pull request id as dev link id
1 parent 1787e02 commit 5d73889

File tree

6 files changed

+27
-14
lines changed

6 files changed

+27
-14
lines changed

models/git/branch.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,19 @@ func init() {
156156
db.RegisterModel(new(RenamedBranch))
157157
}
158158

159+
func GetBranchByID(ctx context.Context, branchID int64) (*Branch, error) {
160+
var branch Branch
161+
has, err := db.GetEngine(ctx).ID(branchID).Get(&branch)
162+
if err != nil {
163+
return nil, err
164+
} else if !has {
165+
return nil, ErrBranchNotExist{
166+
RepoID: branch.RepoID,
167+
}
168+
}
169+
return &branch, nil
170+
}
171+
159172
func GetBranch(ctx context.Context, repoID int64, branchName string) (*Branch, error) {
160173
var branch Branch
161174
has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).And("name=?", branchName).Get(&branch)

models/issues/issue_dev_link.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type IssueDevLink struct {
2525
IssueID int64 `xorm:"INDEX"`
2626
LinkType IssueDevLinkType
2727
LinkedRepoID int64 `xorm:"INDEX"` // it can link to self repo or other repo
28-
LinkIndex string // branch name, pull request number or commit sha
28+
LinkID int64 // branch id in branch table or pull request id
2929
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
3030
Repo *repo_model.Repository `xorm:"-"` // current repo of issue
3131
LinkedRepo *repo_model.Repository `xorm:"-"`

models/migrations/v1_25/v322.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func CreateTableIssueDevLink(x *xorm.Engine) error {
1515
IssueID int64 `xorm:"INDEX"`
1616
LinkType int
1717
LinkedRepoID int64 `xorm:"INDEX"` // it can link to self repo or other repo
18-
LinkIndex string // branch name, pull request number or commit sha
18+
LinkID int64 // branch id in branch table or pull request id
1919
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
2020
}
2121
return x.Sync(new(IssueDevLink))

routers/web/repo/issue_dev.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,17 @@ func CreateBranchFromIssue(ctx *context.Context) {
9999
return
100100
}
101101

102+
branch, err := git_model.GetBranch(ctx, repo.ID, form.NewBranchName)
103+
if err != nil {
104+
ctx.ServerError("GetBranch", err)
105+
return
106+
}
107+
102108
if err := issues_model.CreateIssueDevLink(ctx, &issues_model.IssueDevLink{
103109
IssueID: issue.ID,
104110
LinkType: issues_model.IssueDevLinkTypeBranch,
105111
LinkedRepoID: repo.ID,
106-
LinkIndex: form.NewBranchName,
112+
LinkID: branch.ID,
107113
}); err != nil {
108114
ctx.ServerError("CreateIssueDevLink", err)
109115
return

services/issue/dev_link.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"fmt"
99
"sort"
10-
"strconv"
1110

1211
git_model "code.gitea.io/gitea/models/git"
1312
issues_model "code.gitea.io/gitea/models/issues"
@@ -49,11 +48,7 @@ func FindIssueDevLinksByIssue(ctx context.Context, issue *issues_model.Issue) (i
4948

5049
switch link.LinkType {
5150
case issues_model.IssueDevLinkTypePullRequest:
52-
pullID, err := strconv.ParseInt(link.LinkIndex, 10, 64)
53-
if err != nil {
54-
return nil, err
55-
}
56-
pull, err := issues_model.GetPullRequestByID(ctx, pullID)
51+
pull, err := issues_model.GetPullRequestByID(ctx, link.LinkID)
5752
if err != nil {
5853
return nil, err
5954
}
@@ -64,15 +59,15 @@ func FindIssueDevLinksByIssue(ctx context.Context, issue *issues_model.Issue) (i
6459
}
6560
pull.Issue.Repo = issue.Repo
6661
link.PullRequest = pull
67-
branchPRExists.Add(fmt.Sprintf("%d-%s", link.LinkedRepoID, pull.HeadBranch))
62+
branchPRExists.Add(fmt.Sprintf("%d-%d-%s", link.LinkedRepoID, link.LinkType, pull.HeadBranch))
6863
case issues_model.IssueDevLinkTypeBranch:
69-
branch, err := git_model.GetBranch(ctx, link.LinkedRepoID, link.LinkIndex)
64+
branch, err := git_model.GetBranchByID(ctx, link.LinkID)
7065
if err != nil {
7166
return nil, err
7267
}
7368
link.Branch = branch
7469
link.Branch.Repo = link.LinkedRepo
75-
link.DisplayBranch = !branchPRExists.Contains(fmt.Sprintf("%d-%s", link.LinkedRepoID, link.LinkIndex))
70+
link.DisplayBranch = !branchPRExists.Contains(fmt.Sprintf("%d-%d-%d", link.LinkedRepoID, link.LinkType, link.LinkID))
7671
}
7772
}
7873

services/pull/pull.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"io"
1212
"os"
1313
"regexp"
14-
"strconv"
1514
"strings"
1615
"time"
1716

@@ -152,7 +151,7 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
152151
IssueID: link.IssueID,
153152
LinkType: issues_model.IssueDevLinkTypePullRequest,
154153
LinkedRepoID: pr.HeadRepoID,
155-
LinkIndex: strconv.FormatInt(pr.ID, 10),
154+
LinkID: pr.ID,
156155
}); err != nil {
157156
return err
158157
}

0 commit comments

Comments
 (0)