Skip to content

Commit

Permalink
Remove unit types commits and settings (#2161)
Browse files Browse the repository at this point in the history
* Remove unit types commits and settings

* Can not limit units in administrator teams

* Limit changing units only to teams with read and write access mode

* Small code optimization
  • Loading branch information
lafriks authored and lunny committed Jul 17, 2017
1 parent 047a67a commit f33e6ae
Show file tree
Hide file tree
Showing 20 changed files with 174 additions and 104 deletions.
2 changes: 1 addition & 1 deletion integrations/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestViewRepo3(t *testing.T) {
prepareTestEnv(t)

req := NewRequest(t, "GET", "/user3/repo3")
session := loginUser(t, "user3")
session := loginUser(t, "user4")
session.MakeRequest(t, req, http.StatusOK)
}

Expand Down
4 changes: 2 additions & 2 deletions models/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAccessLevel(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

user1 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
user2 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
user2 := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
repo1 := AssertExistsAndLoadBean(t, &Repository{OwnerID: 2, IsPrivate: false}).(*Repository)
repo2 := AssertExistsAndLoadBean(t, &Repository{OwnerID: 3, IsPrivate: true}).(*Repository)

Expand All @@ -46,7 +46,7 @@ func TestHasAccess(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

user1 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
user2 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
user2 := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
repo1 := AssertExistsAndLoadBean(t, &Repository{OwnerID: 2, IsPrivate: false}).(*Repository)
repo2 := AssertExistsAndLoadBean(t, &Repository{OwnerID: 3, IsPrivate: true}).(*Repository)

Expand Down
6 changes: 6 additions & 0 deletions models/fixtures/access.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@
user_id: 4
repo_id: 4
mode: 2 # write

-
id: 3
user_id: 4
repo_id: 3
mode: 2 # write
24 changes: 12 additions & 12 deletions models/fixtures/repo_unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@
id: 2
repo_id: 1
type: 2
index: 0
index: 1
config: "{}"
created_unix: 946684810

-
id: 3
repo_id: 1
type: 3
index: 0
index: 2
config: "{}"
created_unix: 946684810

-
id: 4
repo_id: 1
type: 5
index: 0
type: 4
index: 3
config: "{}"
created_unix: 946684810

-
id: 5
repo_id: 1
type: 7
index: 0
type: 5
index: 4
config: "{}"
created_unix: 946684810

Expand All @@ -50,30 +50,30 @@
id: 7
repo_id: 3
type: 2
index: 0
index: 1
config: "{}"
created_unix: 946684810

-
id: 8
repo_id: 3
type: 3
index: 0
index: 2
config: "{}"
created_unix: 946684810

-
id: 9
repo_id: 3
type: 5
index: 0
type: 4
index: 3
config: "{}"
created_unix: 946684810

-
id: 10
repo_id: 3
type: 7
index: 0
type: 5
index: 4
config: "{}"
created_unix: 946684810
8 changes: 4 additions & 4 deletions models/fixtures/team.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
authorize: 4 # owner
num_repos: 2
num_members: 1
unit_types: '[1,2,3,4,5,6,7,8,9]'
unit_types: '[1,2,3,4,5,6,7]'

-
id: 2
Expand All @@ -16,7 +16,7 @@
authorize: 2 # write
num_repos: 1
num_members: 2
unit_types: '[1,2,3,4,5,6,7,8,9]'
unit_types: '[1,2,3,4,5,6,7]'

-
id: 3
Expand All @@ -26,7 +26,7 @@
authorize: 4 # owner
num_repos: 0
num_members: 1
unit_types: '[1,2,3,4,5,6,7,8,9]'
unit_types: '[1,2,3,4,5,6,7]'

-
id: 4
Expand All @@ -36,4 +36,4 @@
authorize: 4 # owner
num_repos: 0
num_members: 1
unit_types: '[1,2,3,4,5,6,7,8,9]'
unit_types: '[1,2,3,4,5,6,7]'
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ var migrations = []Migration{
NewMigration("regenerate git hooks", regenerateGitHooks36),
// v37 -> v38
NewMigration("unescape user full names", unescapeUserFullNames),
// v38 -> v39
NewMigration("remove commits and settings unit types", removeCommitsUnitType),
}

// Migrate database to current version
Expand Down
32 changes: 16 additions & 16 deletions models/migrations/v16.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ type RepoUnit struct {

// Enumerate all the unit types
const (
UnitTypeCode = iota + 1 // 1 code
UnitTypeIssues // 2 issues
UnitTypePRs // 3 PRs
UnitTypeCommits // 4 Commits
UnitTypeReleases // 5 Releases
UnitTypeWiki // 6 Wiki
UnitTypeSettings // 7 Settings
UnitTypeExternalWiki // 8 ExternalWiki
UnitTypeExternalTracker // 9 ExternalTracker
V16UnitTypeCode = iota + 1 // 1 code
V16UnitTypeIssues // 2 issues
V16UnitTypePRs // 3 PRs
V16UnitTypeCommits // 4 Commits
V16UnitTypeReleases // 5 Releases
V16UnitTypeWiki // 6 Wiki
V16UnitTypeSettings // 7 Settings
V16UnitTypeExternalWiki // 8 ExternalWiki
V16UnitTypeExternalTracker // 9 ExternalTracker
)

// Repo describes a repository
Expand Down Expand Up @@ -79,32 +79,32 @@ func addUnitsToTables(x *xorm.Engine) error {

for _, repo := range repos {
for i := 1; i <= 9; i++ {
if (i == UnitTypeWiki || i == UnitTypeExternalWiki) && !repo.EnableWiki {
if (i == V16UnitTypeWiki || i == V16UnitTypeExternalWiki) && !repo.EnableWiki {
continue
}
if i == UnitTypeExternalWiki && !repo.EnableExternalWiki {
if i == V16UnitTypeExternalWiki && !repo.EnableExternalWiki {
continue
}
if i == UnitTypePRs && !repo.EnablePulls {
if i == V16UnitTypePRs && !repo.EnablePulls {
continue
}
if (i == UnitTypeIssues || i == UnitTypeExternalTracker) && !repo.EnableIssues {
if (i == V16UnitTypeIssues || i == V16UnitTypeExternalTracker) && !repo.EnableIssues {
continue
}
if i == UnitTypeExternalTracker && !repo.EnableExternalTracker {
if i == V16UnitTypeExternalTracker && !repo.EnableExternalTracker {
continue
}

var config = make(map[string]string)
switch i {
case UnitTypeExternalTracker:
case V16UnitTypeExternalTracker:
config["ExternalTrackerURL"] = repo.ExternalTrackerURL
config["ExternalTrackerFormat"] = repo.ExternalTrackerFormat
if len(repo.ExternalTrackerStyle) == 0 {
repo.ExternalTrackerStyle = markdown.IssueNameStyleNumeric
}
config["ExternalTrackerStyle"] = repo.ExternalTrackerStyle
case UnitTypeExternalWiki:
case V16UnitTypeExternalWiki:
config["ExternalWikiURL"] = repo.ExternalWikiURL
}

Expand Down
56 changes: 56 additions & 0 deletions models/migrations/v38.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"code.gitea.io/gitea/models"

"github.com/go-xorm/xorm"
)

func removeCommitsUnitType(x *xorm.Engine) (err error) {
// Update team unit types
const batchSize = 100
for start := 0; ; start += batchSize {
teams := make([]*models.Team, 0, batchSize)
if err := x.Limit(batchSize, start).Find(&teams); err != nil {
return err
}
if len(teams) == 0 {
break
}
for _, team := range teams {
ut := make([]models.UnitType, 0, len(team.UnitTypes))
for _, u := range team.UnitTypes {
if u < V16UnitTypeCommits {
ut = append(ut, u)
} else if u > V16UnitTypeSettings {
ut = append(ut, u-2)
} else if u > V16UnitTypeCommits && u != V16UnitTypeSettings {
ut = append(ut, u-1)
}
}
team.UnitTypes = ut
if _, err := x.Id(team.ID).Cols("unit_types").Update(team); err != nil {
return err
}
}
}

// Delete commits and settings unit types
if _, err = x.In("`type`", []models.UnitType{V16UnitTypeCommits, V16UnitTypeSettings}).Delete(new(RepoUnit)); err != nil {
return err
}
// Fix renumber unit types that where in enumeration after settings unit type
if _, err = x.Where("`type` > ?", V16UnitTypeSettings).Decr("type").Decr("index").Update(new(RepoUnit)); err != nil {
return err
}
// Fix renumber unit types that where in enumeration after commits unit type
if _, err = x.Where("`type` > ?", V16UnitTypeCommits).Decr("type").Decr("index").Update(new(RepoUnit)); err != nil {
return err
}

return nil
}
4 changes: 4 additions & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ func (repo *Repository) getUnitsByUserID(e Engine, userID int64, isAdmin bool) (

var allTypes = make(map[UnitType]struct{}, len(allRepUnitTypes))
for _, team := range teams {
// Administrators can not be limited
if team.Authorize >= AccessModeAdmin {
return nil
}
for _, unitType := range team.UnitTypes {
allTypes[unitType] = struct{}{}
}
Expand Down
9 changes: 2 additions & 7 deletions models/repo_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
switch colName {
case "type":
switch UnitType(Cell2Int64(val)) {
case UnitTypeCode, UnitTypeIssues, UnitTypePullRequests, UnitTypeCommits, UnitTypeReleases,
UnitTypeWiki, UnitTypeSettings:
case UnitTypeCode, UnitTypeIssues, UnitTypePullRequests, UnitTypeReleases,
UnitTypeWiki:
r.Config = new(UnitConfig)
case UnitTypeExternalWiki:
r.Config = new(ExternalWikiConfig)
Expand Down Expand Up @@ -116,11 +116,6 @@ func (r *RepoUnit) PullRequestsConfig() *UnitConfig {
return r.Config.(*UnitConfig)
}

// CommitsConfig returns config for UnitTypeCommits
func (r *RepoUnit) CommitsConfig() *UnitConfig {
return r.Config.(*UnitConfig)
}

// ReleasesConfig returns config for UnitTypeReleases
func (r *RepoUnit) ReleasesConfig() *UnitConfig {
return r.Config.(*UnitConfig)
Expand Down
Loading

0 comments on commit f33e6ae

Please sign in to comment.