Skip to content

Commit 5efdccd

Browse files
authored
Merge pull request #264 from Bwko/lint/org.go
Lint models/org.go
2 parents 7a92519 + 0b9cf10 commit 5efdccd

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

models/models.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ import (
1313
"path"
1414
"strings"
1515

16+
// Needed for the MySQL driver
1617
_ "github.com/go-sql-driver/mysql"
1718
"github.com/go-xorm/core"
1819
"github.com/go-xorm/xorm"
20+
21+
// Needed for the Postgresql driver
1922
_ "github.com/lib/pq"
2023

2124
"code.gitea.io/gitea/models/migrations"
@@ -45,16 +48,22 @@ func sessionRelease(sess *xorm.Session) {
4548
}
4649

4750
var (
48-
x *xorm.Engine
49-
tables []interface{}
51+
x *xorm.Engine
52+
tables []interface{}
53+
54+
// HasEngine specifies if we have a xorm.Engine
5055
HasEngine bool
5156

57+
// DbCfg holds the database settings
5258
DbCfg struct {
5359
Type, Host, Name, User, Passwd, Path, SSLMode string
5460
}
5561

62+
// EnableSQLite3 use SQLite3
5663
EnableSQLite3 bool
57-
EnableTiDB bool
64+
65+
// EnableTiDB enable TiDB
66+
EnableTiDB bool
5867
)
5968

6069
func init() {
@@ -69,12 +78,13 @@ func init() {
6978
new(Team), new(OrgUser), new(TeamUser), new(TeamRepo),
7079
new(Notice), new(EmailAddress))
7180

72-
gonicNames := []string{"SSL"}
81+
gonicNames := []string{"SSL", "UID"}
7382
for _, name := range gonicNames {
7483
core.LintGonicMapper[name] = true
7584
}
7685
}
7786

87+
// LoadConfigs loads the database settings
7888
func LoadConfigs() {
7989
sec := setting.Cfg.Section("database")
8090
DbCfg.Type = sec.Key("DB_TYPE").String()
@@ -115,7 +125,7 @@ func parsePostgreSQLHostPort(info string) (string, string) {
115125

116126
func getEngine() (*xorm.Engine, error) {
117127
connStr := ""
118-
var Param string = "?"
128+
var Param = "?"
119129
if strings.Contains(DbCfg.Name, Param) {
120130
Param = "&"
121131
}
@@ -159,6 +169,7 @@ func getEngine() (*xorm.Engine, error) {
159169
return xorm.NewEngine(DbCfg.Type, connStr)
160170
}
161171

172+
// NewTestEngine sets a new test xorm.Engine
162173
func NewTestEngine(x *xorm.Engine) (err error) {
163174
x, err = getEngine()
164175
if err != nil {
@@ -169,6 +180,7 @@ func NewTestEngine(x *xorm.Engine) (err error) {
169180
return x.StoreEngine("InnoDB").Sync2(tables...)
170181
}
171182

183+
// SetEngine sets the xorm.Engine
172184
func SetEngine() (err error) {
173185
x, err = getEngine()
174186
if err != nil {
@@ -191,6 +203,7 @@ func SetEngine() (err error) {
191203
return nil
192204
}
193205

206+
// NewEngine initializes a new xorm.Engine
194207
func NewEngine() (err error) {
195208
if err = SetEngine(); err != nil {
196209
return err
@@ -211,6 +224,7 @@ func NewEngine() (err error) {
211224
return nil
212225
}
213226

227+
// Statistic contains the database statistics
214228
type Statistic struct {
215229
Counter struct {
216230
User, Org, PublicKey,
@@ -222,6 +236,7 @@ type Statistic struct {
222236
}
223237
}
224238

239+
// GetStatistic returns the database statistics
225240
func GetStatistic() (stats Statistic) {
226241
stats.Counter.User = CountUsers()
227242
stats.Counter.Org = CountOrganizations()
@@ -248,6 +263,7 @@ func GetStatistic() (stats Statistic) {
248263
return
249264
}
250265

266+
// Ping tests if database is alive
251267
func Ping() error {
252268
return x.Ping()
253269
}

models/org.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import (
1515
)
1616

1717
var (
18-
ErrOrgNotExist = errors.New("Organization does not exist")
18+
// ErrOrgNotExist organization does not exist
19+
ErrOrgNotExist = errors.New("Organization does not exist")
20+
// ErrTeamNotExist team does not exist
1921
ErrTeamNotExist = errors.New("Team does not exist")
2022
)
2123

@@ -68,7 +70,7 @@ func (org *User) GetMembers() error {
6870

6971
var ids = make([]int64, len(ous))
7072
for i, ou := range ous {
71-
ids[i] = ou.Uid
73+
ids[i] = ou.UID
7274
}
7375
org.Members, err = GetUsersByIDs(ids)
7476
return err
@@ -127,7 +129,7 @@ func CreateOrganization(org, owner *User) (err error) {
127129

128130
// Add initial creator to organization and owner team.
129131
if _, err = sess.Insert(&OrgUser{
130-
Uid: owner.ID,
132+
UID: owner.ID,
131133
OrgID: org.ID,
132134
IsOwner: true,
133135
NumTeams: 1,
@@ -235,37 +237,37 @@ func DeleteOrganization(org *User) (err error) {
235237
// OrgUser represents an organization-user relation.
236238
type OrgUser struct {
237239
ID int64 `xorm:"pk autoincr"`
238-
Uid int64 `xorm:"INDEX UNIQUE(s)"`
240+
UID int64 `xorm:"INDEX UNIQUE(s)"`
239241
OrgID int64 `xorm:"INDEX UNIQUE(s)"`
240242
IsPublic bool
241243
IsOwner bool
242244
NumTeams int
243245
}
244246

245247
// IsOrganizationOwner returns true if given user is in the owner team.
246-
func IsOrganizationOwner(orgId, uid int64) bool {
248+
func IsOrganizationOwner(orgID, uid int64) bool {
247249
has, _ := x.
248250
Where("is_owner=?", true).
249251
And("uid=?", uid).
250-
And("org_id=?", orgId).
252+
And("org_id=?", orgID).
251253
Get(new(OrgUser))
252254
return has
253255
}
254256

255257
// IsOrganizationMember returns true if given user is member of organization.
256-
func IsOrganizationMember(orgId, uid int64) bool {
258+
func IsOrganizationMember(orgID, uid int64) bool {
257259
has, _ := x.
258260
Where("uid=?", uid).
259-
And("org_id=?", orgId).
261+
And("org_id=?", orgID).
260262
Get(new(OrgUser))
261263
return has
262264
}
263265

264266
// IsPublicMembership returns true if given user public his/her membership.
265-
func IsPublicMembership(orgId, uid int64) bool {
267+
func IsPublicMembership(orgID, uid int64) bool {
266268
has, _ := x.
267269
Where("uid=?", uid).
268-
And("org_id=?", orgId).
270+
And("org_id=?", orgID).
269271
And("is_public=?", true).
270272
Get(new(OrgUser))
271273
return has
@@ -311,7 +313,7 @@ func GetOwnedOrgsByUserID(userID int64) ([]*User, error) {
311313
return getOwnedOrgsByUserID(sess, userID)
312314
}
313315

314-
// GetOwnedOrganizationsByUserIDDesc returns a list of organizations are owned by
316+
// GetOwnedOrgsByUserIDDesc returns a list of organizations are owned by
315317
// given user ID, ordered descending by the given condition.
316318
func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) {
317319
sess := x.NewSession()
@@ -374,7 +376,7 @@ func AddOrgUser(orgID, uid int64) error {
374376
}
375377

376378
ou := &OrgUser{
377-
Uid: uid,
379+
UID: uid,
378380
OrgID: orgID,
379381
}
380382

@@ -512,7 +514,7 @@ func (org *User) GetUserTeamIDs(userID int64) ([]int64, error) {
512514
return teamIDs, nil
513515
}
514516

515-
// GetTeams returns all teams that belong to organization,
517+
// GetUserTeams returns all teams that belong to user,
516518
// and that the user has joined.
517519
func (org *User) GetUserTeams(userID int64) ([]*Team, error) {
518520
return org.getUserTeams(x, userID)
@@ -560,7 +562,7 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
560562
return repos, repoCount, nil
561563
}
562564

563-
// GetUserRepositories returns mirror repositories of the organization
565+
// GetUserMirrorRepositories returns mirror repositories of the user
564566
// that the user with the given userID has access to.
565567
func (org *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
566568
teamIDs, err := org.GetUserTeamIDs(userID)

0 commit comments

Comments
 (0)