Skip to content

Commit d5039b6

Browse files
lng2020silverwind
authored andcommitted
Remove deadcode under models/issues (go-gitea#28536)
Using the Go Official tool `golang.org/x/tools/cmd/deadcode@latest` mentioned by [go blog](https://go.dev/blog/deadcode). Just use `deadcode .` in the project root folder and it gives a list of unused functions. Though it has some false alarms. This PR removes dead code detected in `models/issues`.
1 parent 927a9a8 commit d5039b6

11 files changed

+9
-200
lines changed

models/issues/assignees_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ func TestUpdateAssignee(t *testing.T) {
1818
assert.NoError(t, unittest.PrepareTestDatabase())
1919

2020
// Fake issue with assignees
21-
issue, err := issues_model.GetIssueWithAttrsByID(db.DefaultContext, 1)
21+
issue, err := issues_model.GetIssueByID(db.DefaultContext, 1)
22+
assert.NoError(t, err)
23+
24+
err = issue.LoadAttributes(db.DefaultContext)
2225
assert.NoError(t, err)
2326

2427
// Assign multiple users

models/issues/issue.go

-9
Original file line numberDiff line numberDiff line change
@@ -534,15 +534,6 @@ func GetIssueByID(ctx context.Context, id int64) (*Issue, error) {
534534
return issue, nil
535535
}
536536

537-
// GetIssueWithAttrsByID returns an issue with attributes by given ID.
538-
func GetIssueWithAttrsByID(ctx context.Context, id int64) (*Issue, error) {
539-
issue, err := GetIssueByID(ctx, id)
540-
if err != nil {
541-
return nil, err
542-
}
543-
return issue, issue.LoadAttributes(ctx)
544-
}
545-
546537
// GetIssuesByIDs return issues with the given IDs.
547538
// If keepOrder is true, the order of the returned issues will be the same as the given IDs.
548539
func GetIssuesByIDs(ctx context.Context, issueIDs []int64, keepOrder ...bool) (IssueList, error) {

models/issues/issue_search.go

-20
Original file line numberDiff line numberDiff line change
@@ -455,26 +455,6 @@ func applySubscribedCondition(sess *xorm.Session, subscriberID int64) *xorm.Sess
455455
)
456456
}
457457

458-
// GetRepoIDsForIssuesOptions find all repo ids for the given options
459-
func GetRepoIDsForIssuesOptions(ctx context.Context, opts *IssuesOptions, user *user_model.User) ([]int64, error) {
460-
repoIDs := make([]int64, 0, 5)
461-
e := db.GetEngine(ctx)
462-
463-
sess := e.Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
464-
465-
applyConditions(sess, opts)
466-
467-
accessCond := repo_model.AccessibleRepositoryCondition(user, unit.TypeInvalid)
468-
if err := sess.Where(accessCond).
469-
Distinct("issue.repo_id").
470-
Table("issue").
471-
Find(&repoIDs); err != nil {
472-
return nil, fmt.Errorf("unable to GetRepoIDsForIssuesOptions: %w", err)
473-
}
474-
475-
return repoIDs, nil
476-
}
477-
478458
// Issues returns a list of issues by given conditions.
479459
func Issues(ctx context.Context, opts *IssuesOptions) (IssueList, error) {
480460
sess := db.GetEngine(ctx).

models/issues/issue_test.go

-30
Original file line numberDiff line numberDiff line change
@@ -216,36 +216,6 @@ func TestIssue_loadTotalTimes(t *testing.T) {
216216
assert.Equal(t, int64(3682), ms.TotalTrackedTime)
217217
}
218218

219-
func TestGetRepoIDsForIssuesOptions(t *testing.T) {
220-
assert.NoError(t, unittest.PrepareTestDatabase())
221-
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
222-
for _, test := range []struct {
223-
Opts issues_model.IssuesOptions
224-
ExpectedRepoIDs []int64
225-
}{
226-
{
227-
issues_model.IssuesOptions{
228-
AssigneeID: 2,
229-
},
230-
[]int64{3, 32},
231-
},
232-
{
233-
issues_model.IssuesOptions{
234-
RepoCond: builder.In("repo_id", 1, 2),
235-
},
236-
[]int64{1, 2},
237-
},
238-
} {
239-
repoIDs, err := issues_model.GetRepoIDsForIssuesOptions(db.DefaultContext, &test.Opts, user)
240-
assert.NoError(t, err)
241-
if assert.Len(t, repoIDs, len(test.ExpectedRepoIDs)) {
242-
for i, repoID := range repoIDs {
243-
assert.EqualValues(t, test.ExpectedRepoIDs[i], repoID)
244-
}
245-
}
246-
}
247-
}
248-
249219
func testInsertIssue(t *testing.T, title, content string, expectIndex int64) *issues_model.Issue {
250220
var newIssue issues_model.Issue
251221
t.Run(title, func(t *testing.T) {

models/issues/label.go

-16
Original file line numberDiff line numberDiff line change
@@ -424,22 +424,6 @@ func GetLabelInOrgByID(ctx context.Context, orgID, labelID int64) (*Label, error
424424
return l, nil
425425
}
426426

427-
// GetLabelIDsInOrgByNames returns a list of labelIDs by names in a given
428-
// organization.
429-
func GetLabelIDsInOrgByNames(ctx context.Context, orgID int64, labelNames []string) ([]int64, error) {
430-
if orgID <= 0 {
431-
return nil, ErrOrgLabelNotExist{0, orgID}
432-
}
433-
labelIDs := make([]int64, 0, len(labelNames))
434-
435-
return labelIDs, db.GetEngine(ctx).Table("label").
436-
Where("org_id = ?", orgID).
437-
In("name", labelNames).
438-
Asc("name").
439-
Cols("id").
440-
Find(&labelIDs)
441-
}
442-
443427
// GetLabelsInOrgByIDs returns a list of labels by IDs in given organization,
444428
// it silently ignores label IDs that do not belong to the organization.
445429
func GetLabelsInOrgByIDs(ctx context.Context, orgID int64, labelIDs []int64) ([]*Label, error) {

models/issues/label_test.go

-24
Original file line numberDiff line numberDiff line change
@@ -164,30 +164,6 @@ func TestGetLabelInOrgByName(t *testing.T) {
164164
assert.True(t, issues_model.IsErrOrgLabelNotExist(err))
165165
}
166166

167-
func TestGetLabelInOrgByNames(t *testing.T) {
168-
assert.NoError(t, unittest.PrepareTestDatabase())
169-
labelIDs, err := issues_model.GetLabelIDsInOrgByNames(db.DefaultContext, 3, []string{"orglabel3", "orglabel4"})
170-
assert.NoError(t, err)
171-
172-
assert.Len(t, labelIDs, 2)
173-
174-
assert.Equal(t, int64(3), labelIDs[0])
175-
assert.Equal(t, int64(4), labelIDs[1])
176-
}
177-
178-
func TestGetLabelInOrgByNamesDiscardsNonExistentLabels(t *testing.T) {
179-
assert.NoError(t, unittest.PrepareTestDatabase())
180-
// orglabel99 doesn't exists.. See labels.yml
181-
labelIDs, err := issues_model.GetLabelIDsInOrgByNames(db.DefaultContext, 3, []string{"orglabel3", "orglabel4", "orglabel99"})
182-
assert.NoError(t, err)
183-
184-
assert.Len(t, labelIDs, 2)
185-
186-
assert.Equal(t, int64(3), labelIDs[0])
187-
assert.Equal(t, int64(4), labelIDs[1])
188-
assert.NoError(t, err)
189-
}
190-
191167
func TestGetLabelInOrgByID(t *testing.T) {
192168
assert.NoError(t, unittest.PrepareTestDatabase())
193169
label, err := issues_model.GetLabelInOrgByID(db.DefaultContext, 3, 3)

models/issues/milestone_list.go

-26
Original file line numberDiff line numberDiff line change
@@ -160,32 +160,6 @@ func (m MilestonesStats) Total() int64 {
160160
return m.OpenCount + m.ClosedCount
161161
}
162162

163-
// GetMilestonesStatsByRepoCond returns milestone statistic information for dashboard by given conditions.
164-
func GetMilestonesStatsByRepoCond(ctx context.Context, repoCond builder.Cond) (*MilestonesStats, error) {
165-
var err error
166-
stats := &MilestonesStats{}
167-
168-
sess := db.GetEngine(ctx).Where("is_closed = ?", false)
169-
if repoCond.IsValid() {
170-
sess.And(builder.In("repo_id", builder.Select("id").From("repository").Where(repoCond)))
171-
}
172-
stats.OpenCount, err = sess.Count(new(Milestone))
173-
if err != nil {
174-
return nil, err
175-
}
176-
177-
sess = db.GetEngine(ctx).Where("is_closed = ?", true)
178-
if repoCond.IsValid() {
179-
sess.And(builder.In("repo_id", builder.Select("id").From("repository").Where(repoCond)))
180-
}
181-
stats.ClosedCount, err = sess.Count(new(Milestone))
182-
if err != nil {
183-
return nil, err
184-
}
185-
186-
return stats, nil
187-
}
188-
189163
// GetMilestonesStatsByRepoCondAndKw returns milestone statistic information for dashboard by given repo conditions and name keyword.
190164
func GetMilestonesStatsByRepoCondAndKw(ctx context.Context, repoCond builder.Cond, keyword string) (*MilestonesStats, error) {
191165
var err error

models/issues/milestone_test.go

-29
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"code.gitea.io/gitea/modules/util"
1818

1919
"github.com/stretchr/testify/assert"
20-
"xorm.io/builder"
2120
)
2221

2322
func TestMilestone_State(t *testing.T) {
@@ -285,34 +284,6 @@ func TestGetMilestonesByRepoIDs(t *testing.T) {
285284
})
286285
}
287286

288-
func TestGetMilestonesStats(t *testing.T) {
289-
assert.NoError(t, unittest.PrepareTestDatabase())
290-
291-
test := func(repoID int64) {
292-
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID})
293-
stats, err := issues_model.GetMilestonesStatsByRepoCond(db.DefaultContext, builder.And(builder.Eq{"repo_id": repoID}))
294-
assert.NoError(t, err)
295-
assert.EqualValues(t, repo.NumMilestones-repo.NumClosedMilestones, stats.OpenCount)
296-
assert.EqualValues(t, repo.NumClosedMilestones, stats.ClosedCount)
297-
}
298-
test(1)
299-
test(2)
300-
test(3)
301-
302-
stats, err := issues_model.GetMilestonesStatsByRepoCond(db.DefaultContext, builder.And(builder.Eq{"repo_id": unittest.NonexistentID}))
303-
assert.NoError(t, err)
304-
assert.EqualValues(t, 0, stats.OpenCount)
305-
assert.EqualValues(t, 0, stats.ClosedCount)
306-
307-
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
308-
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
309-
310-
milestoneStats, err := issues_model.GetMilestonesStatsByRepoCond(db.DefaultContext, builder.In("repo_id", []int64{repo1.ID, repo2.ID}))
311-
assert.NoError(t, err)
312-
assert.EqualValues(t, repo1.NumOpenMilestones+repo2.NumOpenMilestones, milestoneStats.OpenCount)
313-
assert.EqualValues(t, repo1.NumClosedMilestones+repo2.NumClosedMilestones, milestoneStats.ClosedCount)
314-
}
315-
316287
func TestNewMilestone(t *testing.T) {
317288
assert.NoError(t, unittest.PrepareTestDatabase())
318289
milestone := &issues_model.Milestone{

models/issues/pull.go

-30
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,6 @@ func (err ErrPullRequestAlreadyExists) Unwrap() error {
7878
return util.ErrAlreadyExist
7979
}
8080

81-
// ErrPullRequestHeadRepoMissing represents a "ErrPullRequestHeadRepoMissing" error
82-
type ErrPullRequestHeadRepoMissing struct {
83-
ID int64
84-
HeadRepoID int64
85-
}
86-
87-
// IsErrErrPullRequestHeadRepoMissing checks if an error is a ErrPullRequestHeadRepoMissing.
88-
func IsErrErrPullRequestHeadRepoMissing(err error) bool {
89-
_, ok := err.(ErrPullRequestHeadRepoMissing)
90-
return ok
91-
}
92-
93-
// Error does pretty-printing :D
94-
func (err ErrPullRequestHeadRepoMissing) Error() string {
95-
return fmt.Sprintf("pull request head repo missing [id: %d, head_repo_id: %d]",
96-
err.ID, err.HeadRepoID)
97-
}
98-
9981
// ErrPullWasClosed is used close a closed pull request
10082
type ErrPullWasClosed struct {
10183
ID int64
@@ -758,18 +740,6 @@ func (pr *PullRequest) IsSameRepo() bool {
758740
return pr.BaseRepoID == pr.HeadRepoID
759741
}
760742

761-
// GetPullRequestsByHeadBranch returns all prs by head branch
762-
// Since there could be multiple prs with the same head branch, this function returns a slice of prs
763-
func GetPullRequestsByHeadBranch(ctx context.Context, headBranch string, headRepoID int64) ([]*PullRequest, error) {
764-
log.Trace("GetPullRequestsByHeadBranch: headBranch: '%s', headRepoID: '%d'", headBranch, headRepoID)
765-
prs := make([]*PullRequest, 0, 2)
766-
if err := db.GetEngine(ctx).Where(builder.Eq{"head_branch": headBranch, "head_repo_id": headRepoID}).
767-
Find(&prs); err != nil {
768-
return nil, err
769-
}
770-
return prs, nil
771-
}
772-
773743
// GetBaseBranchLink returns the relative URL of the base branch
774744
func (pr *PullRequest) GetBaseBranchLink(ctx context.Context) string {
775745
if err := pr.LoadBaseRepo(ctx); err != nil {

models/issues/stopwatch.go

-14
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,6 @@ func (err ErrIssueStopwatchNotExist) Unwrap() error {
2929
return util.ErrNotExist
3030
}
3131

32-
// ErrIssueStopwatchAlreadyExist represents an error that stopwatch is already exist
33-
type ErrIssueStopwatchAlreadyExist struct {
34-
UserID int64
35-
IssueID int64
36-
}
37-
38-
func (err ErrIssueStopwatchAlreadyExist) Error() string {
39-
return fmt.Sprintf("issue stopwatch already exists[uid: %d, issue_id: %d", err.UserID, err.IssueID)
40-
}
41-
42-
func (err ErrIssueStopwatchAlreadyExist) Unwrap() error {
43-
return util.ErrAlreadyExist
44-
}
45-
4632
// Stopwatch represents a stopwatch for time tracking.
4733
type Stopwatch struct {
4834
ID int64 `xorm:"pk autoincr"`

services/issue/assignee_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ func TestDeleteNotPassedAssignee(t *testing.T) {
1818
assert.NoError(t, unittest.PrepareTestDatabase())
1919

2020
// Fake issue with assignees
21-
issue, err := issues_model.GetIssueWithAttrsByID(db.DefaultContext, 1)
21+
issue, err := issues_model.GetIssueByID(db.DefaultContext, 1)
2222
assert.NoError(t, err)
23+
24+
err = issue.LoadAttributes(db.DefaultContext)
25+
assert.NoError(t, err)
26+
2327
assert.Len(t, issue.Assignees, 1)
2428

2529
user1, err := user_model.GetUserByID(db.DefaultContext, 1) // This user is already assigned (see the definition in fixtures), so running UpdateAssignee should unassign him

0 commit comments

Comments
 (0)