Skip to content

Commit 53bd977

Browse files
Loïc Dacharyrealaravinth
Loïc Dachary
authored andcommitted
move foreignreference.go & error to models/foreignreference
1 parent 00c2098 commit 53bd977

File tree

7 files changed

+84
-72
lines changed

7 files changed

+84
-72
lines changed

models/error.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,46 +1385,3 @@ func (err ErrNotValidReviewRequest) Error() string {
13851385
err.UserID,
13861386
err.RepoID)
13871387
}
1388-
1389-
//
1390-
// ___________ .__ __________ _____
1391-
// \_ _____/__________ ____ |__| ____ ____\______ \ _____/ ____\___________ ____ ____ ____ ____
1392-
// | __)/ _ \_ __ \_/ __ \| |/ ___\ / \| _// __ \ __\/ __ \_ __ \_/ __ \ / \_/ ___\/ __ \
1393-
// | \( <_> ) | \/\ ___/| / /_/ > | \ | \ ___/| | \ ___/| | \/\ ___/| | \ \__\ ___/
1394-
// \___ / \____/|__| \___ >__\___ /|___| /____|_ /\___ >__| \___ >__| \___ >___| /\___ >___ >
1395-
// \/ \/ /_____/ \/ \/ \/ \/ \/ \/ \/ \/
1396-
//
1397-
1398-
// ErrLocalIndexNotExist represents a "LocalIndexNotExist" kind of error.
1399-
type ErrLocalIndexNotExist struct {
1400-
RepoID int64
1401-
ForeignIndex int64
1402-
Type string
1403-
}
1404-
1405-
// ErrLocalIndexNotExist checks if an error is a ErrLocalIndexNotExist.
1406-
func IsErrLocalIndexNotExist(err error) bool {
1407-
_, ok := err.(ErrLocalIndexNotExist)
1408-
return ok
1409-
}
1410-
1411-
func (err ErrLocalIndexNotExist) Error() string {
1412-
return fmt.Sprintf("repository %d has no LocalIndex for ForeignIndex %d of type %s", err.RepoID, err.ForeignIndex, err.Type)
1413-
}
1414-
1415-
// ErrForeignIndexNotExist represents a "ForeignIndexNotExist" kind of error.
1416-
type ErrForeignIndexNotExist struct {
1417-
RepoID int64
1418-
LocalIndex int64
1419-
Type string
1420-
}
1421-
1422-
// ErrForeignIndexNotExist checks if an error is a ErrForeignIndexNotExist.
1423-
func IsErrForeignIndexNotExist(err error) bool {
1424-
_, ok := err.(ErrForeignIndexNotExist)
1425-
return ok
1426-
}
1427-
1428-
func (err ErrForeignIndexNotExist) Error() string {
1429-
return fmt.Sprintf("repository %d has no ForeignIndex for LocalIndex %d of type %s", err.RepoID, err.LocalIndex, err.Type)
1430-
}

models/foreignreference/error.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2022 Gitea. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package foreignreference
6+
7+
import (
8+
"fmt"
9+
)
10+
11+
// ErrLocalIndexNotExist represents a "LocalIndexNotExist" kind of error.
12+
type ErrLocalIndexNotExist struct {
13+
RepoID int64
14+
ForeignIndex int64
15+
Type string
16+
}
17+
18+
// ErrLocalIndexNotExist checks if an error is a ErrLocalIndexNotExist.
19+
func IsErrLocalIndexNotExist(err error) bool {
20+
_, ok := err.(ErrLocalIndexNotExist)
21+
return ok
22+
}
23+
24+
func (err ErrLocalIndexNotExist) Error() string {
25+
return fmt.Sprintf("repository %d has no LocalIndex for ForeignIndex %d of type %s", err.RepoID, err.ForeignIndex, err.Type)
26+
}
27+
28+
// ErrForeignIndexNotExist represents a "ForeignIndexNotExist" kind of error.
29+
type ErrForeignIndexNotExist struct {
30+
RepoID int64
31+
LocalIndex int64
32+
Type string
33+
}
34+
35+
// ErrForeignIndexNotExist checks if an error is a ErrForeignIndexNotExist.
36+
func IsErrForeignIndexNotExist(err error) bool {
37+
_, ok := err.(ErrForeignIndexNotExist)
38+
return ok
39+
}
40+
41+
func (err ErrForeignIndexNotExist) Error() string {
42+
return fmt.Sprintf("repository %d has no ForeignIndex for LocalIndex %d of type %s", err.RepoID, err.LocalIndex, err.Type)
43+
}

models/foreignreference.go renamed to models/foreignreference/foreignreference.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
// Use of this source code is governed by a MIT-style
33
// license that can be found in the LICENSE file.
44

5-
package models
5+
package foreignreference
66

77
import (
88
"code.gitea.io/gitea/models/db"
99
)
1010

11-
// ForeignType* are valid values for the Type field of ForeignReference
11+
// Type* are valid values for the Type field of ForeignReference
1212
const (
13-
ForeignTypeIssue = "issue"
14-
ForeignTypePullRequest = "pull_request"
15-
ForeignTypeComment = "comment"
16-
ForeignTypeReview = "review"
17-
ForeignTypeReviewComment = "review_comment"
18-
ForeignTypeRelease = "release"
13+
TypeIssue = "issue"
14+
TypePullRequest = "pull_request"
15+
TypeComment = "comment"
16+
TypeReview = "review"
17+
TypeReviewComment = "review_comment"
18+
TypeRelease = "release"
1919
)
2020

2121
// ForeignReference represents external references

models/issue.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
admin_model "code.gitea.io/gitea/models/admin"
1717
"code.gitea.io/gitea/models/db"
18+
"code.gitea.io/gitea/models/foreignreference"
1819
"code.gitea.io/gitea/models/issues"
1920
"code.gitea.io/gitea/models/perm"
2021
repo_model "code.gitea.io/gitea/models/repo"
@@ -67,12 +68,12 @@ type Issue struct {
6768
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
6869
ClosedUnix timeutil.TimeStamp `xorm:"INDEX"`
6970

70-
Attachments []*repo_model.Attachment `xorm:"-"`
71-
Comments []*Comment `xorm:"-"`
72-
Reactions ReactionList `xorm:"-"`
73-
TotalTrackedTime int64 `xorm:"-"`
74-
Assignees []*user_model.User `xorm:"-"`
75-
ForeignReference *ForeignReference `xorm:"-"`
71+
Attachments []*repo_model.Attachment `xorm:"-"`
72+
Comments []*Comment `xorm:"-"`
73+
Reactions ReactionList `xorm:"-"`
74+
TotalTrackedTime int64 `xorm:"-"`
75+
Assignees []*user_model.User `xorm:"-"`
76+
ForeignReference *foreignreference.ForeignReference `xorm:"-"`
7677

7778
// IsLocked limits commenting abilities to users on an issue
7879
// with write access
@@ -276,16 +277,20 @@ func (issue *Issue) loadForeignReference(ctx context.Context) (err error) {
276277
if issue.ForeignReference != nil {
277278
return nil
278279
}
279-
reference := &ForeignReference{
280-
LocalIndex: issue.Index,
280+
reference := &foreignreference.ForeignReference{
281281
RepoID: issue.RepoID,
282-
Type: ForeignTypeIssue,
282+
LocalIndex: issue.Index,
283+
Type: foreignreference.TypeIssue,
283284
}
284285
has, err := db.GetEngine(ctx).Get(reference)
285286
if err != nil {
286287
return err
287288
} else if !has {
288-
return ErrForeignIndexNotExist{issue.RepoID, issue.Index, ForeignTypeIssue}
289+
return foreignreference.ErrForeignIndexNotExist{
290+
RepoID: issue.RepoID,
291+
LocalIndex: issue.Index,
292+
Type: foreignreference.TypeIssue,
293+
}
289294
}
290295
issue.ForeignReference = reference
291296
return nil
@@ -352,7 +357,7 @@ func (issue *Issue) loadAttributes(ctx context.Context) (err error) {
352357
}
353358
}
354359

355-
if err = issue.loadForeignReference(ctx); err != nil && !IsErrForeignIndexNotExist(err) {
360+
if err = issue.loadForeignReference(ctx); err != nil && !foreignreference.IsErrForeignIndexNotExist(err) {
356361
return err
357362
}
358363

@@ -1136,16 +1141,20 @@ func GetIssueByIndex(repoID, index int64) (*Issue, error) {
11361141

11371142
// GetIssueByForeignIndex returns raw issue by foreign ID
11381143
func GetIssueByForeignIndex(ctx context.Context, repoID, foreignIndex int64) (*Issue, error) {
1139-
reference := &ForeignReference{
1144+
reference := &foreignreference.ForeignReference{
11401145
RepoID: repoID,
11411146
ForeignIndex: strconv.FormatInt(foreignIndex, 10),
1142-
Type: ForeignTypeIssue,
1147+
Type: foreignreference.TypeIssue,
11431148
}
11441149
has, err := db.GetEngine(ctx).Get(reference)
11451150
if err != nil {
11461151
return nil, err
11471152
} else if !has {
1148-
return nil, ErrLocalIndexNotExist{repoID, foreignIndex, ForeignTypeIssue}
1153+
return nil, foreignreference.ErrLocalIndexNotExist{
1154+
RepoID: repoID,
1155+
ForeignIndex: foreignIndex,
1156+
Type: foreignreference.TypeIssue,
1157+
}
11491158
}
11501159
return GetIssueByIndex(repoID, reference.LocalIndex)
11511160
}

models/issue_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"code.gitea.io/gitea/models/db"
17+
"code.gitea.io/gitea/models/foreignreference"
1718
repo_model "code.gitea.io/gitea/models/repo"
1819
"code.gitea.io/gitea/models/unittest"
1920
user_model "code.gitea.io/gitea/models/user"
@@ -548,13 +549,13 @@ func TestIssueForeignReference(t *testing.T) {
548549

549550
var foreignIndex int64 = 12345
550551
_, err = GetIssueByForeignIndex(context.Background(), issue.RepoID, foreignIndex)
551-
assert.True(t, IsErrLocalIndexNotExist(err))
552+
assert.True(t, foreignreference.IsErrLocalIndexNotExist(err))
552553

553-
_, err = db.GetEngine(db.DefaultContext).Insert(&ForeignReference{
554+
_, err = db.GetEngine(db.DefaultContext).Insert(&foreignreference.ForeignReference{
554555
LocalIndex: issue.Index,
555556
ForeignIndex: strconv.FormatInt(foreignIndex, 10),
556557
RepoID: issue.RepoID,
557-
Type: ForeignTypeIssue,
558+
Type: foreignreference.TypeIssue,
558559
})
559560
assert.NoError(t, err)
560561

models/migrate_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strconv"
99
"testing"
1010

11+
"code.gitea.io/gitea/models/foreignreference"
1112
repo_model "code.gitea.io/gitea/models/repo"
1213
"code.gitea.io/gitea/models/unittest"
1314
user_model "code.gitea.io/gitea/models/user"
@@ -60,10 +61,10 @@ func assertCreateIssues(t *testing.T, isPull bool) {
6061
IsClosed: true,
6162
Labels: []*Label{label},
6263
Reactions: []*Reaction{reaction},
63-
ForeignReference: &ForeignReference{
64+
ForeignReference: &foreignreference.ForeignReference{
6465
ForeignIndex: strconv.FormatInt(foreignIndex, 10),
6566
RepoID: repo.ID,
66-
Type: ForeignTypeIssue,
67+
Type: foreignreference.TypeIssue,
6768
},
6869
}
6970
err := InsertIssues(is)

services/migrations/gitea_uploader.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"code.gitea.io/gitea/models"
1919
"code.gitea.io/gitea/models/db"
20+
"code.gitea.io/gitea/models/foreignreference"
2021
repo_model "code.gitea.io/gitea/models/repo"
2122
user_model "code.gitea.io/gitea/models/user"
2223
"code.gitea.io/gitea/modules/git"
@@ -374,11 +375,11 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
374375
Labels: labels,
375376
CreatedUnix: timeutil.TimeStamp(issue.Created.Unix()),
376377
UpdatedUnix: timeutil.TimeStamp(issue.Updated.Unix()),
377-
ForeignReference: &models.ForeignReference{
378+
ForeignReference: &foreignreference.ForeignReference{
378379
LocalIndex: issue.GetLocalIndex(),
379380
ForeignIndex: strconv.FormatInt(issue.GetForeignIndex(), 10),
380381
RepoID: g.repo.ID,
381-
Type: models.ForeignTypeIssue,
382+
Type: foreignreference.TypeIssue,
382383
},
383384
}
384385

0 commit comments

Comments
 (0)