Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add units concept for modulable functions of a repository #742

Merged
merged 7 commits into from
Feb 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func runWeb(ctx *cli.Context) error {

}, func(ctx *context.Context) {
ctx.Data["PageIsSettings"] = true
})
}, context.UnitTypes())
}, reqSignIn, context.RepoAssignment(), reqRepoAdmin, context.RepoRef())

m.Get("/:username/:reponame/action/:action", reqSignIn, context.RepoAssignment(), repo.Action)
Expand Down Expand Up @@ -529,7 +529,7 @@ func runWeb(ctx *cli.Context) error {
return
}
})
}, reqSignIn, context.RepoAssignment(), repo.MustBeNotBare)
}, reqSignIn, context.RepoAssignment(), repo.MustBeNotBare, context.UnitTypes())

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

m.Get("/compare/:before([a-z0-9]{40})\\.\\.\\.:after([a-z0-9]{40})", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.CompareDiff)
}, ignSignIn, context.RepoAssignment(), repo.MustBeNotBare)
}, ignSignIn, context.RepoAssignment(), repo.MustBeNotBare, context.UnitTypes())
m.Group("/:username/:reponame", func() {
m.Get("/stars", repo.Stars)
m.Get("/watchers", repo.Watchers)
Expand All @@ -585,7 +585,7 @@ func runWeb(ctx *cli.Context) error {
m.Group("/:reponame", func() {
m.Get("", repo.SetEditorconfigIfExists, repo.Home)
m.Get("\\.git$", repo.SetEditorconfigIfExists, repo.Home)
}, ignSignIn, context.RepoAssignment(true), context.RepoRef())
}, ignSignIn, context.RepoAssignment(true), context.RepoRef(), context.UnitTypes())

m.Group("/:reponame", func() {
m.Group("/info/lfs", func() {
Expand Down
6 changes: 4 additions & 2 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ var migrations = []Migration{

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

// Migrate database to current version
Expand Down
117 changes: 117 additions & 0 deletions models/migrations/v16.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package migrations

import (
"fmt"
"time"

"code.gitea.io/gitea/modules/markdown"

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

// RepoUnit describes all units of a repository
type RepoUnit struct {
ID int64
RepoID int64 `xorm:"INDEX(s)"`
Type int `xorm:"INDEX(s)"`
Index int
Config map[string]string `xorm:"JSON"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to have the keys of RepoUnit.Config be an enum type instead of string literals? I see two advantages to using an enum:

  1. We don't need to have "magic" string literals in our code. It is easy to accidentally mistype something like "ExternalTrackerFormat", and the compiler won't tell you if you do.
  2. When the RepoUnit.Config field is converted to JSON to store in the database, the generated JSON object will have shorter fields, and thus will use up less disk space.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

CreatedUnix int64 `xorm:"INDEX CREATED"`
Created time.Time `xorm:"-"`
}

// 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
)

// Repo describes a repository
type Repo struct {
ID int64
EnableWiki, EnableExternalWiki, EnableIssues, EnableExternalTracker, EnablePulls bool
ExternalWikiURL, ExternalTrackerURL, ExternalTrackerFormat, ExternalTrackerStyle string
}

func addUnitsToTables(x *xorm.Engine) error {
var repos []Repo
err := x.Table("repository").Find(&repos)
if err != nil {
return fmt.Errorf("Query repositories: %v", err)
}

sess := x.NewSession()
defer sess.Close()

if err := sess.Begin(); err != nil {
return err
}

var repoUnit RepoUnit
if err := sess.CreateTable(&repoUnit); err != nil {
return fmt.Errorf("CreateTable RepoUnit: %v", err)
}

if err := sess.CreateUniques(&repoUnit); err != nil {
return fmt.Errorf("CreateUniques RepoUnit: %v", err)
}

if err := sess.CreateIndexes(&repoUnit); err != nil {
return fmt.Errorf("CreateIndexes RepoUnit: %v", err)
}

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

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

if _, err = sess.Insert(&RepoUnit{
RepoID: repo.ID,
Type: i,
Index: i,
Config: config,
}); err != nil {
return fmt.Errorf("Insert repo unit: %v", err)
}
}
}

if err := sess.Commit(); err != nil {
return err
}

return nil
}
1 change: 1 addition & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func init() {
new(IssueUser),
new(LFSMetaObject),
new(TwoFactor),
new(RepoUnit),
)

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