model

package
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusRunning = "running"
	StatusSuccess = "success"
	StatusFailed  = "failed"
)

Run status constants

View Source
const (
	StatusReceived   = "received"
	StatusProcessing = "processing"
	StatusProcessed  = "processed"
)

Webhook event status constants

View Source
const (
	TriggerManual  = "manual"
	TriggerCron    = "cron"
	TriggerWebhook = "webhook"
)

Trigger source constants

View Source
const (
	DriverMySQL  = "mysql"
	DriverSQLite = "sqlite"
)

Database driver constants

View Source
const (
	DefaultHost          = "0.0.0.0"
	DefaultPort          = 8890
	DefaultMaxIdleConns  = 10
	DefaultMaxOpenConns  = 100
	DefaultMaxConcurrent = 5
	DefaultTimeout       = 300
	DefaultRetryCount    = 3
	DefaultTempDir       = "/tmp/git-sync"
)

Default configuration values

View Source
const (
	StepClone        = "clone"
	StepFetch        = "fetch"
	StepCheckout     = "checkout"
	StepEnsureRemote = "ensure_remote"
	StepPush         = "push"
)

Step name constants

View Source
const (
	ErrorAuth    = "auth"
	ErrorNetwork = "network"
	ErrorConfig  = "config"
	ErrorGit     = "git"
	ErrorUnknown = "unknown"
)

Error type constants

View Source
const (
	TableRepos            = "repos"
	TableSyncTasks        = "sync_tasks"
	TableSyncRuns         = "sync_runs"
	TableSyncRunSteps     = "sync_run_steps"
	TableWebhookRules     = "webhook_rules"
	TableWebhookRuleTasks = "webhook_rule_tasks"
	TableWebhookEvents    = "webhook_events"
	TableOperationLogs    = "operation_logs"
)

Table name constants

View Source
const (
	DefaultBranch    = "main"
	DefaultPlatform  = "unknown"
	DefaultSyncMode  = "single"
	DefaultEventType = "push"
	DefaultAction    = "sync"
)

Model default value constants

View Source
const (
	PlatformTypeGitHub      = "github"
	PlatformTypeGitLab      = "gitlab"
	PlatformTypeGitea       = "gitea"
	PlatformTypeGitee       = "gitee"
	PlatformTypeGitCode     = "gitcode"
	PlatformTypeAtomGit     = "atomgit"
	PlatformTypeTencentCode = "tencent_code"
	PlatformTypeCustom      = "custom"
)

PlatformType 平台类型常量

View Source
const (
	PlatformStatusActive = "active"
	PlatformStatusError  = "error"
)

PlatformStatus 平台状态常量

View Source
const (
	ActionSync = "sync"
)

Webhook rule action constants

View Source
const (
	RepoStatusActive = "active"
)

Repository status constants

Variables

View Source
var PlatformAPIPaths = map[string]string{
	PlatformTypeGitHub:      "/api/v3",
	PlatformTypeGitLab:      "/api/v4",
	PlatformTypeGitea:       "/api/v1",
	PlatformTypeGitee:       "/api/v5",
	PlatformTypeGitCode:     "/api/v5",
	PlatformTypeAtomGit:     "/api/v1",
	PlatformTypeTencentCode: "/api/v3",
}

PlatformAPIPaths 各平台的 API 路径

View Source
var PlatformDefaultInstances = map[string]string{
	PlatformTypeGitHub:      "github.com",
	PlatformTypeGitLab:      "gitlab.com",
	PlatformTypeGitea:       "gitea.com",
	PlatformTypeGitee:       "gitee.com",
	PlatformTypeGitCode:     "gitcode.com",
	PlatformTypeAtomGit:     "atomgit.com",
	PlatformTypeTencentCode: "git.code.tencent.com",
}

PlatformDefaultInstances 各平台的默认实例地址

Functions

func GetAPIURL

func GetAPIURL(platformType, instanceURL string) string

GetAPIURL 根据实例地址生成 API URL

func InitDB

func InitDB(driver, dsn string) (*gorm.DB, error)

Types

type Config

type Config struct {
	Server   ServerConfig   `yaml:"server"`
	Database DatabaseConfig `yaml:"database"`
	Redis    RedisConfig    `yaml:"redis"`
	Git      GitConfig      `yaml:"git"`
	Sync     SyncConfig     `yaml:"sync"`
	Webhook  WebhookConfig  `yaml:"webhook"`
	Log      LogConfig      `yaml:"log"`
}

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig 从指定路径加载配置文件。 path 参数由调用方控制(通常是启动参数或环境变量),不存在用户注入风险。

func (*Config) Validate

func (c *Config) Validate() error

type CreateRepoRequest

type CreateRepoRequest struct {
	Name        string `json:"name"`
	RemoteURL   string `json:"remoteUrl"`
	AccessToken string `json:"accessToken"`
	PlatformID  uint   `json:"platformId"`
}

type CreateRuleRequest

type CreateRuleRequest struct {
	Name          string   `json:"name"`
	RepoKey       string   `json:"repoKey"`
	EventType     string   `json:"eventType"`
	BranchPattern string   `json:"branchPattern"`
	Action        string   `json:"action"`
	TaskKeys      []string `json:"taskKeys"`
	MinInterval   int      `json:"minInterval"`
	Enabled       bool     `json:"enabled"`
	Description   string   `json:"description"`
}

type CreateTaskRequest

type CreateTaskRequest struct {
	Name          string `json:"name"`
	SourceRepoKey string `json:"source_repo_key"`
	SourceBranch  string `json:"source_branch"`
	TargetRepoKey string `json:"target_repo_key"`
	TargetBranch  string `json:"target_branch"`
	SyncMode      string `json:"sync_mode"`
	Cron          string `json:"cron"`
	GitTags       bool   `json:"git_tags"`
	GitForce      bool   `json:"git_force"`
	GitPrune      bool   `json:"git_prune"`
}

type DatabaseConfig

type DatabaseConfig struct {
	Driver       string `yaml:"driver"`
	DSN          string `yaml:"dsn"`
	MaxIdleConns int    `yaml:"max_idle_conns"`
	MaxOpenConns int    `yaml:"max_open_conns"`
}

type GitConfig

type GitConfig struct {
	Backend string `yaml:"backend"`
	TempDir string `yaml:"temp_dir"`
}

type LogConfig

type LogConfig struct {
	Level  string `yaml:"level"`
	Format string `yaml:"format"`
}

type OperationLog

type OperationLog struct {
	ID           uint      `json:"id" gorm:"primaryKey"`
	Action       string    `json:"action" gorm:"size:32;not null;index"`  // create/update/delete/run/retry/sync
	ResourceType string    `json:"resource_type" gorm:"size:32;not null"` // repo/task/rule/platform/event
	ResourceKey  string    `json:"resource_key" gorm:"size:255;index"`    // 资源标识(key/name/id)
	Resource     string    `json:"resource" gorm:"size:500"`              // 中文摘要,如 "创建仓库 repo-main"
	Actor        string    `json:"actor" gorm:"size:128;index"`           // 操作者(X-User,缺省 admin)
	IP           string    `json:"ip" gorm:"size:64"`
	Status       string    `json:"status" gorm:"size:16;default:success"` // success/failed
	Detail       string    `json:"detail" gorm:"type:text"`
	CreatedAt    time.Time `json:"created_at" gorm:"index"`
}

OperationLog 记录用户对系统发起的写操作(审计日志)。

func (OperationLog) TableName

func (OperationLog) TableName() string

type Platform

type Platform struct {
	ID             uint           `json:"id" gorm:"primaryKey"`
	Key            string         `json:"key" gorm:"uniqueIndex;size:255;not null"`
	Name           string         `json:"name" gorm:"size:100;not null"`
	Type           string         `json:"type" gorm:"size:50;not null"`         // github, gitlab, gitea, gitee, gitcode, atomgit, tencent_code, custom
	InstanceURL    string         `json:"instance_url" gorm:"size:255"`         // 实例地址,如 github.com, gitlab.company.com
	APIURL         string         `json:"api_url" gorm:"size:255;not null"`     // API 地址,如 https://api.github.com
	AccessToken    string         `json:"-" gorm:"type:text"`                   // 访问令牌(加密存储)
	SkipTLSVerify  bool           `json:"skip_tls_verify" gorm:"default:false"` // 跳过 TLS 证书验证
	CACertPath     string         `json:"ca_cert_path" gorm:"size:500"`         // 自定义 CA 证书路径
	ProxyURL       string         `json:"proxy_url" gorm:"size:255"`            // HTTP 代理地址
	IsDefault      bool           `json:"is_default" gorm:"default:false"`      // 是否为默认平台
	Status         string         `json:"status" gorm:"size:20;default:active"` // 状态: active, error
	LastTestAt     *time.Time     `json:"last_test_at"`                         // 最后测试时间
	LastTestResult string         `json:"last_test_result" gorm:"size:500"`     // 最后测试结果
	RepoCount      int            `json:"repo_count" gorm:"default:0"`          // 关联的仓库数量
	CreatedAt      time.Time      `json:"created_at"`
	UpdatedAt      time.Time      `json:"updated_at"`
	DeletedAt      gorm.DeletedAt `json:"-" gorm:"index"`
}

Platform 存储 Git 平台配置

func (Platform) TableName

func (Platform) TableName() string

type PreviewSyncRequest

type PreviewSyncRequest struct {
	SourceRepoKey string `json:"sourceRepoKey"`
	SourceBranch  string `json:"sourceBranch"`
	TargetRepoKey string `json:"targetRepoKey"`
	TargetBranch  string `json:"targetBranch"`
}

type PreviewSyncResult

type PreviewSyncResult struct {
	CanSync      bool   `json:"canSync"`
	SourceExists bool   `json:"sourceExists"`
	TargetExists bool   `json:"targetExists"`
	Message      string `json:"message"`
}

type RedisConfig

type RedisConfig struct {
	Addr     string `yaml:"addr"`
	Password string `yaml:"password"`
	DB       int    `yaml:"db"`
}

type Repo

type Repo struct {
	ID            uint           `json:"id" gorm:"primaryKey"`
	Key           string         `json:"key" gorm:"uniqueIndex;size:255;not null"`
	Name          string         `json:"name" gorm:"size:255;not null"`
	PlatformID    uint           `json:"platform_id" gorm:"index"` // 关联平台 ID
	Platform      string         `json:"platform" gorm:"size:50;not null;default:'unknown'"`
	PlatformOwner string         `json:"platform_owner" gorm:"size:200;not null;default:''"`
	PlatformRepo  string         `json:"platform_repo" gorm:"size:200;not null;default:''"`
	CloneURL      string         `json:"clone_url" gorm:"size:500"`
	SSHURL        string         `json:"ssh_url" gorm:"size:500"`
	DefaultBranch string         `json:"default_branch" gorm:"size:100;default:main"`
	AccessToken   string         `json:"-" gorm:"type:text"` // 保留用于兼容,优先使用 Platform 的 Token
	WebhookSecret string         `json:"-" gorm:"size:255"`
	WebhookID     int64          `json:"webhook_id"`
	Status        string         `json:"status" gorm:"size:20;default:active"`
	CreatedAt     time.Time      `json:"created_at"`
	UpdatedAt     time.Time      `json:"updated_at"`
	DeletedAt     gorm.DeletedAt `json:"-" gorm:"index"`

	// 关联
	PlatformRef *Platform `json:"platformRef,omitempty" gorm:"foreignKey:PlatformID"`
}

func (Repo) TableName

func (Repo) TableName() string

type ServerConfig

type ServerConfig struct {
	Host   string `yaml:"host"`
	Port   int    `yaml:"port"`
	Mode   string `yaml:"mode"`
	APIKey string `yaml:"api_key"`
}

type SyncConfig

type SyncConfig struct {
	MaxConcurrent  int `yaml:"max_concurrent"`
	DefaultTimeout int `yaml:"default_timeout"`
	RetryCount     int `yaml:"retry_count"`
}

type SyncRun

type SyncRun struct {
	ID             uint          `json:"id" gorm:"primaryKey"`
	TaskKey        string        `json:"taskKey" gorm:"size:36;not null;index"`
	TriggerSource  string        `json:"triggerSource" gorm:"size:20;not null"`
	Status         string        `json:"status" gorm:"size:20;not null;index"`
	StartTime      time.Time     `json:"startTime"`
	EndTime        *time.Time    `json:"endTime"`
	CommitRange    string        `json:"commitRange" gorm:"size:255"`
	Details        string        `json:"details" gorm:"type:text"`
	ErrorMessage   string        `json:"errorMessage" gorm:"type:text"`
	WebhookEventID *uint         `json:"webhookEventId" gorm:"index"`
	DurationMs     int64         `json:"durationMs"`
	ErrorType      string        `json:"errorType" gorm:"size:30"`
	RetryTotal     int           `json:"retryTotal"`
	Steps          []SyncRunStep `json:"steps" gorm:"foreignKey:RunID"`
	CreatedAt      time.Time     `json:"createdAt"`
}

func (SyncRun) TableName

func (SyncRun) TableName() string

type SyncRunStep

type SyncRunStep struct {
	ID         uint       `json:"id" gorm:"primaryKey"`
	RunID      uint       `json:"runId" gorm:"not null;index"`
	StepName   string     `json:"stepName" gorm:"size:50;not null"`
	Status     string     `json:"status" gorm:"size:20;not null"`
	StartTime  time.Time  `json:"startTime"`
	EndTime    *time.Time `json:"endTime"`
	DurationMs int64      `json:"durationMs"`
	ErrorMsg   string     `json:"errorMsg" gorm:"type:text"`
	ErrorType  string     `json:"errorType" gorm:"size:30"`
	Output     string     `json:"output" gorm:"type:text"`
	RetryCount int        `json:"retryCount"`
	CreatedAt  time.Time  `json:"createdAt"`
}

SyncRunStep records a single step within a sync run.

func (SyncRunStep) TableName

func (SyncRunStep) TableName() string

type SyncTask

type SyncTask struct {
	ID            uint           `json:"id" gorm:"primaryKey"`
	Key           string         `json:"key" gorm:"uniqueIndex;size:36;not null;default:''"`
	Name          string         `json:"name" gorm:"size:100;not null;default:''"`
	SourceRepoKey string         `json:"source_repo_key" gorm:"size:255;not null;index;default:''"`
	SourceBranch  string         `json:"source_branch" gorm:"size:255;not null;default:''"`
	TargetRepoKey string         `json:"target_repo_key" gorm:"size:255;not null;index;default:''"`
	TargetBranch  string         `json:"target_branch" gorm:"size:255;not null;default:''"`
	SyncMode      string         `json:"sync_mode" gorm:"size:20;default:single"`
	Cron          string         `json:"cron" gorm:"size:100"`
	WebhookToken  string         `json:"webhook_token" gorm:"uniqueIndex;size:36"`
	Enabled       bool           `json:"enabled" gorm:"default:true;index"`
	GitTags       bool           `json:"git_tags" gorm:"default:false"`
	GitForce      bool           `json:"git_force" gorm:"default:false"`
	GitPrune      bool           `json:"git_prune" gorm:"default:false"`
	LastRunAt     *time.Time     `json:"last_run_at"`
	LastStatus    string         `json:"last_status" gorm:"size:20"`
	CreatedAt     time.Time      `json:"created_at"`
	UpdatedAt     time.Time      `json:"updated_at"`
	DeletedAt     gorm.DeletedAt `json:"-" gorm:"index"`
}

func (SyncTask) TableName

func (SyncTask) TableName() string

type TestConnectionResult

type TestConnectionResult struct {
	Success bool   `json:"success"`
	Message string `json:"message"`
}

type UpdateRepoRequest

type UpdateRepoRequest struct {
	Key         string `json:"key"`
	Name        string `json:"name"`
	AccessToken string `json:"accessToken"`
}

type UpdateRuleRequest

type UpdateRuleRequest struct {
	ID            uint     `json:"id"`
	Name          string   `json:"name"`
	EventType     string   `json:"eventType"`
	BranchPattern string   `json:"branchPattern"`
	Action        string   `json:"action"`
	TaskKeys      []string `json:"taskKeys"`
	MinInterval   int      `json:"minInterval"`
	Enabled       bool     `json:"enabled"`
	Description   string   `json:"description"`
}

type UpdateTaskRequest

type UpdateTaskRequest struct {
	Key          string `json:"key"`
	Name         string `json:"name"`
	SourceBranch string `json:"source_branch"`
	TargetBranch string `json:"target_branch"`
	SyncMode     string `json:"sync_mode"`
	Cron         string `json:"cron"`
	Enabled      bool   `json:"enabled"`
	GitTags      bool   `json:"git_tags"`
	GitForce     bool   `json:"git_force"`
	GitPrune     bool   `json:"git_prune"`
}

type WebhookConfig

type WebhookConfig struct {
	RateLimit   int `yaml:"rate_limit"`
	MaxBodySize int `yaml:"max_body_size"`
}

type WebhookEvent

type WebhookEvent struct {
	ID           uint       `json:"id" gorm:"primaryKey"`
	EventID      string     `json:"eventId" gorm:"uniqueIndex;size:100;not null"`
	RepoKey      string     `json:"repoKey" gorm:"size:255;index"`
	EventType    string     `json:"eventType" gorm:"size:50;not null"`
	Source       string     `json:"source" gorm:"size:20;not null"`
	ActorName    string     `json:"actorName" gorm:"size:200"`
	Branch       string     `json:"branch" gorm:"size:255"`
	CommitSHA    string     `json:"commitSha" gorm:"size:40"`
	Payload      []byte     `json:"payload" gorm:"type:json"`
	Status       string     `json:"status" gorm:"size:20;default:received;index"`
	ErrorMessage string     `json:"errorMessage" gorm:"type:text"`
	ProcessedAt  *time.Time `json:"processedAt"`
	CreatedAt    time.Time  `json:"createdAt"`
}

func (WebhookEvent) TableName

func (WebhookEvent) TableName() string

type WebhookRule

type WebhookRule struct {
	ID            uint              `json:"id" gorm:"primaryKey"`
	Name          string            `json:"name" gorm:"size:100;not null"`
	RepoKey       string            `json:"repoKey" gorm:"size:255;not null;index"`
	EventType     string            `json:"eventType" gorm:"size:100;default:push"`
	BranchPattern string            `json:"branchPattern" gorm:"size:255"`
	Action        string            `json:"action" gorm:"size:50;default:sync"`
	MinInterval   int               `json:"minInterval" gorm:"default:60"`
	Enabled       bool              `json:"enabled" gorm:"default:true;index"`
	Description   string            `json:"description" gorm:"type:text"`
	Tasks         []WebhookRuleTask `json:"tasks,omitempty" gorm:"foreignKey:RuleID"`
	CreatedAt     time.Time         `json:"createdAt"`
	UpdatedAt     time.Time         `json:"updatedAt"`
	DeletedAt     gorm.DeletedAt    `json:"-" gorm:"index"`
}

func (*WebhookRule) GetTaskKeys

func (r *WebhookRule) GetTaskKeys() []string

GetTaskKeys returns the task keys from the Tasks relationship

func (*WebhookRule) SetTaskKeys

func (r *WebhookRule) SetTaskKeys(keys []string)

SetTaskKeys updates the Tasks relationship

func (WebhookRule) TableName

func (WebhookRule) TableName() string

type WebhookRuleTask

type WebhookRuleTask struct {
	ID        uint      `json:"id" gorm:"primaryKey"`
	RuleID    uint      `json:"ruleId" gorm:"not null;index;uniqueIndex:idx_rule_task"`
	TaskKey   string    `json:"taskKey" gorm:"size:36;not null;uniqueIndex:idx_rule_task"`
	CreatedAt time.Time `json:"createdAt"`
}

func (WebhookRuleTask) TableName

func (WebhookRuleTask) TableName() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL