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 testsrelease
parent
49fa03bf42
commit
8a421b1fd7
@ -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"`
|
||||
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
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
// 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 models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/core"
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
// RepoUnit describes all units of a repository
|
||||
type RepoUnit struct {
|
||||
ID int64
|
||||
RepoID int64 `xorm:"INDEX(s)"`
|
||||
Type UnitType `xorm:"INDEX(s)"`
|
||||
Index int
|
||||
Config core.Conversion `xorm:"TEXT"`
|
||||
CreatedUnix int64 `xorm:"INDEX CREATED"`
|
||||
Created time.Time `xorm:"-"`
|
||||
}
|
||||
|
||||
// UnitConfig describes common unit config
|
||||
type UnitConfig struct {
|
||||
}
|
||||
|
||||
// FromDB fills up a UnitConfig from serialized format.
|
||||
func (cfg *UnitConfig) FromDB(bs []byte) error {
|
||||
return json.Unmarshal(bs, &cfg)
|
||||
}
|
||||
|
||||
// ToDB exports a UnitConfig to a serialized format.
|
||||
func (cfg *UnitConfig) ToDB() ([]byte, error) {
|
||||
return json.Marshal(cfg)
|
||||
}
|
||||
|
||||
// ExternalWikiConfig describes external wiki config
|
||||
type ExternalWikiConfig struct {
|
||||
ExternalWikiURL string
|
||||
}
|
||||
|
||||
// FromDB fills up a ExternalWikiConfig from serialized format.
|
||||
func (cfg *ExternalWikiConfig) FromDB(bs []byte) error {
|
||||
return json.Unmarshal(bs, &cfg)
|
||||
}
|
||||
|
||||
// ToDB exports a ExternalWikiConfig to a serialized format.
|
||||
func (cfg *ExternalWikiConfig) ToDB() ([]byte, error) {
|
||||
return json.Marshal(cfg)
|
||||
}
|
||||
|
||||
// ExternalTrackerConfig describes external tracker config
|
||||
type ExternalTrackerConfig struct {
|
||||
ExternalTrackerURL string
|
||||
ExternalTrackerFormat string
|
||||
ExternalTrackerStyle string
|
||||
}
|
||||
|
||||
// FromDB fills up a ExternalTrackerConfig from serialized format.
|
||||
func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error {
|
||||
return json.Unmarshal(bs, &cfg)
|
||||
}
|
||||
|
||||
// ToDB exports a ExternalTrackerConfig to a serialized format.
|
||||
func (cfg *ExternalTrackerConfig) ToDB() ([]byte, error) {
|
||||
return json.Marshal(cfg)
|
||||
}
|
||||
|
||||
// BeforeSet is invoked from XORM before setting the value of a field of this object.
|
||||
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:
|
||||
r.Config = new(UnitConfig)
|
||||
case UnitTypeExternalWiki:
|
||||
r.Config = new(ExternalWikiConfig)
|
||||
case UnitTypeExternalTracker:
|
||||
r.Config = new(ExternalTrackerConfig)
|
||||
default:
|
||||
panic("unrecognized repo unit type: " + com.ToStr(*val))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AfterSet is invoked from XORM after setting the value of a field of this object.
|
||||
func (r *RepoUnit) AfterSet(colName string, _ xorm.Cell) {
|
||||
switch colName {
|
||||
case "created_unix":
|
||||
r.Created = time.Unix(r.CreatedUnix, 0).Local()
|
||||
}
|
||||
}
|
||||
|
||||
// Unit returns Unit
|
||||
func (r *RepoUnit) Unit() Unit {
|
||||
return Units[r.Type]
|
||||
}
|
||||
|
||||
// CodeConfig returns config for UnitTypeCode
|
||||
func (r *RepoUnit) CodeConfig() *UnitConfig {
|
||||
return r.Config.(*UnitConfig)
|
||||
}
|
||||
|
||||
// IssuesConfig returns config for UnitTypeIssues
|
||||
func (r *RepoUnit) IssuesConfig() *UnitConfig {
|
||||
return r.Config.(*UnitConfig)
|
||||
}
|
||||
|
||||
// PullRequestsConfig returns config for UnitTypePullRequests
|
||||
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)
|
||||
}
|
||||
|
||||
// ExternalWikiConfig returns config for UnitTypeExternalWiki
|
||||
func (r *RepoUnit) ExternalWikiConfig() *ExternalWikiConfig {
|
||||
return r.Config.(*ExternalWikiConfig)
|
||||
}
|
||||
|
||||
// ExternalTrackerConfig returns config for UnitTypeExternalTracker
|
||||
func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig {
|
||||
return r.Config.(*ExternalTrackerConfig)
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
// 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 models
|
||||
|
||||
// UnitType is Unit's Type
|
||||
type UnitType int
|
||||
|
||||
// Enumerate all the unit types
|
||||
const (
|
||||
UnitTypeCode UnitType = iota + 1 // 1 code
|
||||
UnitTypeIssues // 2 issues
|
||||
UnitTypePullRequests // 3 PRs
|
||||
UnitTypeCommits // 4 Commits
|
||||
UnitTypeReleases // 5 Releases
|
||||
UnitTypeWiki // 6 Wiki
|
||||
UnitTypeSettings // 7 Settings
|
||||
UnitTypeExternalWiki // 8 ExternalWiki
|
||||
UnitTypeExternalTracker // 9 ExternalTracker
|
||||
)
|
||||
|
||||
// Unit is a tab page of one repository
|
||||
type Unit struct {
|
||||
Type UnitType
|
||||
NameKey string
|
||||
URI string
|
||||
DescKey string
|
||||
Idx int
|
||||
}
|
||||
|
||||
// Enumerate all the units
|
||||
var (
|
||||
UnitCode = Unit{
|
||||
UnitTypeCode,
|
||||
"repo.code",
|
||||
"/",
|
||||
"repo.code_desc",
|
||||
0,
|
||||
}
|
||||
|
||||
UnitIssues = Unit{
|
||||
UnitTypeIssues,
|
||||
"repo.issues",
|
||||
"/issues",
|
||||
"repo.issues_desc",
|
||||
1,
|
||||
}
|
||||
|
||||
UnitExternalTracker = Unit{
|
||||
UnitTypeExternalTracker,
|
||||
"repo.issues",
|
||||
"/issues",
|
||||
"repo.issues_desc",
|
||||
1,
|
||||
}
|
||||
|
||||
UnitPullRequests = Unit{
|
||||
UnitTypePullRequests,
|
||||
"repo.pulls",
|
||||
"/pulls",
|
||||
"repo.pulls_desc",
|
||||
2,
|
||||
}
|
||||
|
||||
UnitCommits = Unit{
|
||||
UnitTypeCommits,
|
||||
"repo.commits",
|
||||
"/commits/master",
|
||||
"repo.commits_desc",
|
||||
3,
|
||||
}
|
||||
|
||||
UnitReleases = Unit{
|
||||
UnitTypeReleases,
|
||||
"repo.releases",
|
||||
"/releases",
|
||||
"repo.releases_desc",
|
||||
4,
|
||||
}
|
||||
|
||||
UnitWiki = Unit{
|
||||
UnitTypeWiki,
|
||||
"repo.wiki",
|
||||
"/wiki",
|
||||
"repo.wiki_desc",
|
||||
5,
|
||||
}
|
||||
|
||||
UnitExternalWiki = Unit{
|
||||
UnitTypeExternalWiki,
|
||||
"repo.wiki",
|
||||
"/wiki",
|
||||
"repo.wiki_desc",
|
||||
5,
|
||||
}
|
||||
|
||||
UnitSettings = Unit{
|
||||
UnitTypeSettings,
|
||||
"repo.settings",
|
||||
"/settings",
|
||||
"repo.settings_desc",
|
||||
6,
|
||||
}
|
||||
|
||||
// defaultRepoUnits contains all the default unit types
|
||||
defaultRepoUnits = []UnitType{
|
||||
UnitTypeCode,
|
||||
UnitTypeIssues,
|
||||
UnitTypePullRequests,
|
||||
UnitTypeCommits,
|
||||
UnitTypeReleases,
|
||||
UnitTypeWiki,
|
||||
UnitTypeSettings,
|
||||
}
|
||||
|
||||
// MustRepoUnits contains the units could be disabled currently
|
||||
MustRepoUnits = []UnitType{
|
||||
UnitTypeCode,
|
||||
UnitTypeCommits,
|
||||
UnitTypeReleases,
|
||||
UnitTypeSettings,
|
||||
}
|
||||
|
||||
// Units contains all the units
|
||||
Units = map[UnitType]Unit{
|
||||
UnitTypeCode: UnitCode,
|
||||
UnitTypeIssues: UnitIssues,
|
||||
UnitTypeExternalTracker: UnitExternalTracker,
|
||||
UnitTypePullRequests: UnitPullRequests,
|
||||
UnitTypeCommits: UnitCommits,
|
||||
UnitTypeReleases: UnitReleases,
|
||||
UnitTypeWiki: UnitWiki,
|
||||
UnitTypeExternalWiki: UnitExternalWiki,
|
||||
UnitTypeSettings: UnitSettings,
|
||||
}
|
||||
)
|
Loading…
Reference in New Issue