Skip to content

Commit 8a421b1

Browse files
authored
Add units concept for modulable functions of a repository (#742)
* Add units concept for modulable functions of a repository * remove unused comment codes & fix lints and tests * remove unused comment codes * use struct config instead of map * fix lint * rm wrong files * fix tests
1 parent 49fa03b commit 8a421b1

File tree

16 files changed

+669
-90
lines changed

16 files changed

+669
-90
lines changed

cmd/web.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ func runWeb(ctx *cli.Context) error {
441441

442442
}, func(ctx *context.Context) {
443443
ctx.Data["PageIsSettings"] = true
444-
})
444+
}, context.UnitTypes())
445445
}, reqSignIn, context.RepoAssignment(), reqRepoAdmin, context.RepoRef())
446446

447447
m.Get("/:username/:reponame/action/:action", reqSignIn, context.RepoAssignment(), repo.Action)
@@ -535,7 +535,7 @@ func runWeb(ctx *cli.Context) error {
535535
return
536536
}
537537
})
538-
}, reqSignIn, context.RepoAssignment(), repo.MustBeNotBare)
538+
}, reqSignIn, context.RepoAssignment(), repo.MustBeNotBare, context.UnitTypes())
539539

540540
m.Group("/:username/:reponame", func() {
541541
m.Group("", func() {
@@ -581,7 +581,7 @@ func runWeb(ctx *cli.Context) error {
581581
m.Get("/commit/:sha([a-f0-9]{7,40})\\.:ext(patch|diff)", repo.RawDiff)
582582

583583
m.Get("/compare/:before([a-z0-9]{40})\\.\\.\\.:after([a-z0-9]{40})", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.CompareDiff)
584-
}, ignSignIn, context.RepoAssignment(), repo.MustBeNotBare)
584+
}, ignSignIn, context.RepoAssignment(), repo.MustBeNotBare, context.UnitTypes())
585585
m.Group("/:username/:reponame", func() {
586586
m.Get("/stars", repo.Stars)
587587
m.Get("/watchers", repo.Watchers)
@@ -591,7 +591,7 @@ func runWeb(ctx *cli.Context) error {
591591
m.Group("/:reponame", func() {
592592
m.Get("", repo.SetEditorconfigIfExists, repo.Home)
593593
m.Get("\\.git$", repo.SetEditorconfigIfExists, repo.Home)
594-
}, ignSignIn, context.RepoAssignment(true), context.RepoRef())
594+
}, ignSignIn, context.RepoAssignment(true), context.RepoRef(), context.UnitTypes())
595595

596596
m.Group("/:reponame", func() {
597597
m.Group("/info/lfs", func() {

models/migrations/migrations.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ var migrations = []Migration{
7676

7777
// v13 -> v14:v0.9.87
7878
NewMigration("set comment updated with created", setCommentUpdatedWithCreated),
79-
// v14
79+
// v14 -> v15
8080
NewMigration("create user column diff view style", createUserColumnDiffViewStyle),
81-
// v15
81+
// v15 -> v16
8282
NewMigration("create user column allow create organization", createAllowCreateOrganizationColumn),
83+
// V16 -> v17
84+
NewMigration("create repo unit table and add units for all repos", addUnitsToTables),
8385
}
8486

8587
// Migrate database to current version

models/migrations/v16.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package migrations
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"code.gitea.io/gitea/modules/markdown"
8+
9+
"github.com/go-xorm/xorm"
10+
)
11+
12+
// RepoUnit describes all units of a repository
13+
type RepoUnit struct {
14+
ID int64
15+
RepoID int64 `xorm:"INDEX(s)"`
16+
Type int `xorm:"INDEX(s)"`
17+
Index int
18+
Config map[string]string `xorm:"JSON"`
19+
CreatedUnix int64 `xorm:"INDEX CREATED"`
20+
Created time.Time `xorm:"-"`
21+
}
22+
23+
// Enumerate all the unit types
24+
const (
25+
UnitTypeCode = iota + 1 // 1 code
26+
UnitTypeIssues // 2 issues
27+
UnitTypePRs // 3 PRs
28+
UnitTypeCommits // 4 Commits
29+
UnitTypeReleases // 5 Releases
30+
UnitTypeWiki // 6 Wiki
31+
UnitTypeSettings // 7 Settings
32+
UnitTypeExternalWiki // 8 ExternalWiki
33+
UnitTypeExternalTracker // 9 ExternalTracker
34+
)
35+
36+
// Repo describes a repository
37+
type Repo struct {
38+
ID int64
39+
EnableWiki, EnableExternalWiki, EnableIssues, EnableExternalTracker, EnablePulls bool
40+
ExternalWikiURL, ExternalTrackerURL, ExternalTrackerFormat, ExternalTrackerStyle string
41+
}
42+
43+
func addUnitsToTables(x *xorm.Engine) error {
44+
var repos []Repo
45+
err := x.Table("repository").Find(&repos)
46+
if err != nil {
47+
return fmt.Errorf("Query repositories: %v", err)
48+
}
49+
50+
sess := x.NewSession()
51+
defer sess.Close()
52+
53+
if err := sess.Begin(); err != nil {
54+
return err
55+
}
56+
57+
var repoUnit RepoUnit
58+
if err := sess.CreateTable(&repoUnit); err != nil {
59+
return fmt.Errorf("CreateTable RepoUnit: %v", err)
60+
}
61+
62+
if err := sess.CreateUniques(&repoUnit); err != nil {
63+
return fmt.Errorf("CreateUniques RepoUnit: %v", err)
64+
}
65+
66+
if err := sess.CreateIndexes(&repoUnit); err != nil {
67+
return fmt.Errorf("CreateIndexes RepoUnit: %v", err)
68+
}
69+
70+
for _, repo := range repos {
71+
for i := 1; i <= 9; i++ {
72+
if (i == UnitTypeWiki || i == UnitTypeExternalWiki) && !repo.EnableWiki {
73+
continue
74+
}
75+
if i == UnitTypeExternalWiki && !repo.EnableExternalWiki {
76+
continue
77+
}
78+
if i == UnitTypePRs && !repo.EnablePulls {
79+
continue
80+
}
81+
if (i == UnitTypeIssues || i == UnitTypeExternalTracker) && !repo.EnableIssues {
82+
continue
83+
}
84+
if i == UnitTypeExternalTracker && !repo.EnableExternalTracker {
85+
continue
86+
}
87+
88+
var config = make(map[string]string)
89+
switch i {
90+
case UnitTypeExternalTracker:
91+
config["ExternalTrackerURL"] = repo.ExternalTrackerURL
92+
config["ExternalTrackerFormat"] = repo.ExternalTrackerFormat
93+
if len(repo.ExternalTrackerStyle) == 0 {
94+
repo.ExternalTrackerStyle = markdown.IssueNameStyleNumeric
95+
}
96+
config["ExternalTrackerStyle"] = repo.ExternalTrackerStyle
97+
case UnitTypeExternalWiki:
98+
config["ExternalWikiURL"] = repo.ExternalWikiURL
99+
}
100+
101+
if _, err = sess.Insert(&RepoUnit{
102+
RepoID: repo.ID,
103+
Type: i,
104+
Index: i,
105+
Config: config,
106+
}); err != nil {
107+
return fmt.Errorf("Insert repo unit: %v", err)
108+
}
109+
}
110+
}
111+
112+
if err := sess.Commit(); err != nil {
113+
return err
114+
}
115+
116+
return nil
117+
}

models/models.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func init() {
106106
new(IssueUser),
107107
new(LFSMetaObject),
108108
new(TwoFactor),
109+
new(RepoUnit),
109110
)
110111

111112
gonicNames := []string{"SSL", "UID"}

0 commit comments

Comments
 (0)