Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
* upstream/main:
  Add Mermaid copy button, avoid unnecessary tooltip hide (go-gitea#22225)
  [skip ci] Updated licenses and gitignores
  Improve testing for pgsql empty repository (go-gitea#22223)
  JS refactors (go-gitea#22227)
  Check primary keys for all tables and drop ForeignReference (go-gitea#21721)
  • Loading branch information
zjjhot committed Dec 26, 2022
2 parents 7029e14 + f5cd0d9 commit 7a1855d
Show file tree
Hide file tree
Showing 58 changed files with 220 additions and 302 deletions.
33 changes: 33 additions & 0 deletions models/db/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/setting"

_ "code.gitea.io/gitea/cmd" // for TestPrimaryKeys

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -51,3 +53,34 @@ func TestDeleteOrphanedObjects(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, countBefore, countAfter)
}

func TestPrimaryKeys(t *testing.T) {
// Some dbs require that all tables have primary keys, see
// https://github.com/go-gitea/gitea/issues/21086
// https://github.com/go-gitea/gitea/issues/16802
// To avoid creating tables without primary key again, this test will check them.
// Import "code.gitea.io/gitea/cmd" to make sure each db.RegisterModel in init functions has been called.

beans, err := db.NamesToBean()
if err != nil {
t.Fatal(err)
}

whitelist := map[string]string{
"the_table_name_to_skip_checking": "Write a note here to explain why",
}

for _, bean := range beans {
table, err := db.TableInfo(bean)
if err != nil {
t.Fatal(err)
}
if why, ok := whitelist[table.Name]; ok {
t.Logf("ignore %q because %q", table.Name, why)
continue
}
if len(table.PrimaryKeys) == 0 {
t.Errorf("table %q has no primary key", table.Name)
}
}
}
1 change: 0 additions & 1 deletion models/fixtures/foreign_reference.yml

This file was deleted.

52 changes: 0 additions & 52 deletions models/foreignreference/error.go

This file was deleted.

31 changes: 0 additions & 31 deletions models/foreignreference/foreignreference.go

This file was deleted.

60 changes: 5 additions & 55 deletions models/issues/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/foreignreference"
"code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm"
access_model "code.gitea.io/gitea/models/perm/access"
Expand Down Expand Up @@ -136,12 +134,11 @@ type Issue struct {
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
ClosedUnix timeutil.TimeStamp `xorm:"INDEX"`

Attachments []*repo_model.Attachment `xorm:"-"`
Comments []*Comment `xorm:"-"`
Reactions ReactionList `xorm:"-"`
TotalTrackedTime int64 `xorm:"-"`
Assignees []*user_model.User `xorm:"-"`
ForeignReference *foreignreference.ForeignReference `xorm:"-"`
Attachments []*repo_model.Attachment `xorm:"-"`
Comments []*Comment `xorm:"-"`
Reactions ReactionList `xorm:"-"`
TotalTrackedTime int64 `xorm:"-"`
Assignees []*user_model.User `xorm:"-"`

// IsLocked limits commenting abilities to users on an issue
// with write access
Expand Down Expand Up @@ -321,29 +318,6 @@ func (issue *Issue) loadReactions(ctx context.Context) (err error) {
return nil
}

func (issue *Issue) loadForeignReference(ctx context.Context) (err error) {
if issue.ForeignReference != nil {
return nil
}
reference := &foreignreference.ForeignReference{
RepoID: issue.RepoID,
LocalIndex: issue.Index,
Type: foreignreference.TypeIssue,
}
has, err := db.GetEngine(ctx).Get(reference)
if err != nil {
return err
} else if !has {
return foreignreference.ErrForeignIndexNotExist{
RepoID: issue.RepoID,
LocalIndex: issue.Index,
Type: foreignreference.TypeIssue,
}
}
issue.ForeignReference = reference
return nil
}

// LoadMilestone load milestone of this issue.
func (issue *Issue) LoadMilestone(ctx context.Context) (err error) {
if (issue.Milestone == nil || issue.Milestone.ID != issue.MilestoneID) && issue.MilestoneID > 0 {
Expand Down Expand Up @@ -406,10 +380,6 @@ func (issue *Issue) LoadAttributes(ctx context.Context) (err error) {
}
}

if err = issue.loadForeignReference(ctx); err != nil && !foreignreference.IsErrForeignIndexNotExist(err) {
return err
}

return issue.loadReactions(ctx)
}

Expand Down Expand Up @@ -1097,26 +1067,6 @@ func GetIssueByIndex(repoID, index int64) (*Issue, error) {
return issue, nil
}

// GetIssueByForeignIndex returns raw issue by foreign ID
func GetIssueByForeignIndex(ctx context.Context, repoID, foreignIndex int64) (*Issue, error) {
reference := &foreignreference.ForeignReference{
RepoID: repoID,
ForeignIndex: strconv.FormatInt(foreignIndex, 10),
Type: foreignreference.TypeIssue,
}
has, err := db.GetEngine(ctx).Get(reference)
if err != nil {
return nil, err
} else if !has {
return nil, foreignreference.ErrLocalIndexNotExist{
RepoID: repoID,
ForeignIndex: foreignIndex,
Type: foreignreference.TypeIssue,
}
}
return GetIssueByIndex(repoID, reference.LocalIndex)
}

// GetIssueWithAttrsByIndex returns issue by index in a repository.
func GetIssueWithAttrsByIndex(repoID, index int64) (*Issue, error) {
issue, err := GetIssueByIndex(repoID, index)
Expand Down
34 changes: 0 additions & 34 deletions models/issues/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import (
"context"
"fmt"
"sort"
"strconv"
"sync"
"testing"
"time"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/foreignreference"
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/organization"
repo_model "code.gitea.io/gitea/models/repo"
Expand Down Expand Up @@ -501,38 +499,6 @@ func TestCorrectIssueStats(t *testing.T) {
assert.EqualValues(t, issueStats.OpenCount, issueAmount)
}

func TestIssueForeignReference(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 4})
assert.NotEqualValues(t, issue.Index, issue.ID) // make sure they are different to avoid false positive

// it is fine for an issue to not have a foreign reference
err := issue.LoadAttributes(db.DefaultContext)
assert.NoError(t, err)
assert.Nil(t, issue.ForeignReference)

var foreignIndex int64 = 12345
_, err = issues_model.GetIssueByForeignIndex(context.Background(), issue.RepoID, foreignIndex)
assert.True(t, foreignreference.IsErrLocalIndexNotExist(err))

err = db.Insert(db.DefaultContext, &foreignreference.ForeignReference{
LocalIndex: issue.Index,
ForeignIndex: strconv.FormatInt(foreignIndex, 10),
RepoID: issue.RepoID,
Type: foreignreference.TypeIssue,
})
assert.NoError(t, err)

err = issue.LoadAttributes(db.DefaultContext)
assert.NoError(t, err)

assert.EqualValues(t, issue.ForeignReference.ForeignIndex, strconv.FormatInt(foreignIndex, 10))

found, err := issues_model.GetIssueByForeignIndex(context.Background(), issue.RepoID, foreignIndex)
assert.NoError(t, err)
assert.EqualValues(t, found.Index, issue.Index)
}

func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
miles := issues_model.MilestoneList{
Expand Down
7 changes: 0 additions & 7 deletions models/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,6 @@ func insertIssue(ctx context.Context, issue *issues_model.Issue) error {
}
}

if issue.ForeignReference != nil {
issue.ForeignReference.LocalIndex = issue.Index
if _, err := sess.Insert(issue.ForeignReference); err != nil {
return err
}
}

return nil
}

Expand Down
12 changes: 0 additions & 12 deletions models/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
package models

import (
"strconv"
"testing"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/foreignreference"
issues_model "code.gitea.io/gitea/models/issues"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
Expand Down Expand Up @@ -48,7 +46,6 @@ func assertCreateIssues(t *testing.T, isPull bool) {
UserID: owner.ID,
}

foreignIndex := int64(12345)
title := "issuetitle1"
is := &issues_model.Issue{
RepoID: repo.ID,
Expand All @@ -62,20 +59,11 @@ func assertCreateIssues(t *testing.T, isPull bool) {
IsClosed: true,
Labels: []*issues_model.Label{label},
Reactions: []*issues_model.Reaction{reaction},
ForeignReference: &foreignreference.ForeignReference{
ForeignIndex: strconv.FormatInt(foreignIndex, 10),
RepoID: repo.ID,
Type: foreignreference.TypeIssue,
},
}
err := InsertIssues(is)
assert.NoError(t, err)

i := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{Title: title})
assert.Nil(t, i.ForeignReference)
err = i.LoadAttributes(db.DefaultContext)
assert.NoError(t, err)
assert.EqualValues(t, strconv.FormatInt(foreignIndex, 10), i.ForeignReference.ForeignIndex)
unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: owner.ID, IssueID: i.ID})
}

Expand Down
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ var migrations = []Migration{
NewMigration("Add index for access_token", v1_19.AddIndexForAccessToken),
// v236 -> v237
NewMigration("Create secrets table", v1_19.CreateSecretsTable),
// v237 -> v238
NewMigration("Drop ForeignReference table", v1_19.DropForeignReferenceTable),
}

// GetCurrentDBVersion returns the current db version
Expand Down
15 changes: 15 additions & 0 deletions models/migrations/v1_19/v237.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_19 //nolint

import (
"xorm.io/xorm"
)

func DropForeignReferenceTable(x *xorm.Engine) error {
// Drop the table introduced in `v211`, it's considered badly designed and doesn't look like to be used.
// See: https://github.com/go-gitea/gitea/issues/21086#issuecomment-1318217453
type ForeignReference struct{}
return x.DropTables(new(ForeignReference))
}
4 changes: 4 additions & 0 deletions options/gitignore/Godot
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Godot 4+ specific ignores
.godot/

# Godot-specific ignores
.import/
export.cfg
Expand All @@ -9,3 +12,4 @@ export_presets.cfg
# Mono-specific ignores
.mono/
data_*/
mono_crash.*.json
9 changes: 9 additions & 0 deletions options/license/Bitstream-Charter
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(c) Copyright 1989-1992, Bitstream Inc., Cambridge, MA.

You are hereby granted permission under all Bitstream propriety rights
to use, copy, modify, sublicense, sell, and redistribute the 4 Bitstream
Charter (r) Type 1 outline fonts and the 4 Courier Type 1 outline fonts for
any purpose and without restriction; provided, that this notice is left
intact on all copies of such fonts and that Bitstream's trademark is acknowledged
as shown below on all unmodified copies of the 4 Charter Type 1 fonts.
BITSTREAM CHARTER is a registered trademark of Bitstream Inc.
5 changes: 5 additions & 0 deletions options/license/Graphics-Gems
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
LICENSE

This code repository predates the concept of Open Source, and predates most licenses along such lines. As such, the official license truly is:

EULA: The Graphics Gems code is copyright-protected. In other words, you cannot claim the text of the code as your own and resell it. Using the code is permitted in any program, product, or library, non-commercial or commercial. Giving credit is not required, though is a nice gesture. The code comes as-is, and if there are any flaws or problems with any Gems code, nobody involved with Gems - authors, editors, publishers, or webmasters - are to be held responsible. Basically, don't be a jerk, and remember that anything free comes with no guarantee.
Loading

0 comments on commit 7a1855d

Please sign in to comment.