feat: 完善后端并新增 Vue3 WebUI
后端: - 修复登录校验、DB 并发(单例+WAL+纯Go sqlite)、会话密钥持久化等核心缺陷 - 新增 Timer CRUD/签到/手动触发、执行日志、仪表盘、驱动元数据等 API - 统一驱动接口: 带 context 的执行器、真实错误返回、敏感字段掩码、连通性测试 - 补全钉钉执行器; Timer 触发/提醒去重; 配置支持默认值与环境变量覆盖 前端: - Vite+Vue3+TS+Pinia+Element Plus, 由驱动元数据动态渲染配置表单 - 登录/仪表盘(倒计时环)/定时器/规则/账号/日志/设置页面, 支持暗色模式 - 主题色 #66ccff, 信号波纹 logo, go:embed 单二进制部署 构建: Dockerfile 增加前端构建阶段; 补充各驱动配置文档 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,22 +1,26 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/go-github/v84/github"
|
||||
"github.com/ssdomei232/goodBaby/internal/meta"
|
||||
"github.com/ssdomei232/goodBaby/internal/retry"
|
||||
)
|
||||
|
||||
// GitHubAccountConfigValidator GitHub账号配置验证器
|
||||
type GitHubAccountConfigValidator struct{}
|
||||
|
||||
func (v *GitHubAccountConfigValidator) GetType() string {
|
||||
return "github-account"
|
||||
return AccountType
|
||||
}
|
||||
|
||||
func (v *GitHubAccountConfigValidator) Validate(config string) error {
|
||||
// GitHub账号配置验证逻辑
|
||||
var cfg GithubAccount
|
||||
if err := json.Unmarshal([]byte(config), &cfg); err != nil {
|
||||
return fmt.Errorf("解析GitHub账号配置失败: %v", err)
|
||||
cfg, err := parseAccount(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cfg.Token == "" {
|
||||
@@ -28,3 +32,44 @@ func (v *GitHubAccountConfigValidator) Validate(config string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Test 用 token 拉一次当前用户信息,验证 token 是否有效
|
||||
func (v *GitHubAccountConfigValidator) Test(config string) error {
|
||||
cfg, err := parseAccount(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), retry.TestTimeout)
|
||||
defer cancel()
|
||||
|
||||
client := github.NewClient(nil).WithAuthToken(cfg.Token)
|
||||
user, _, err := client.Users.Get(ctx, "")
|
||||
if err != nil {
|
||||
return fmt.Errorf("GitHub Token 校验失败: %w", err)
|
||||
}
|
||||
if user == nil || user.GetLogin() == "" {
|
||||
return fmt.Errorf("GitHub Token 无效")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *GitHubAccountConfigValidator) Meta() meta.AccountMeta {
|
||||
return meta.AccountMeta{
|
||||
Type: AccountType,
|
||||
Label: "GitHub",
|
||||
Description: "使用 Personal Access Token 操作仓库,需要 repo 权限。",
|
||||
Fields: []meta.Field{
|
||||
{Key: "token", Label: "Personal Access Token", Type: meta.FieldPassword, Required: true, Secret: true, Placeholder: "ghp_xxx", Help: "需要勾选 repo 权限"},
|
||||
{Key: "owner", Label: "仓库所有者", Type: meta.FieldString, Required: true, Placeholder: "your-github-name", Help: "用户名或组织名"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func parseAccount(config string) (*GithubAccount, error) {
|
||||
var cfg GithubAccount
|
||||
if err := json.Unmarshal([]byte(config), &cfg); err != nil {
|
||||
return nil, fmt.Errorf("解析GitHub账号配置失败: %v", err)
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
+19
-27
@@ -2,41 +2,33 @@ package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/cenkalti/backoff/v5"
|
||||
"github.com/ssdomei232/goodBaby/configs"
|
||||
"github.com/ssdomei232/goodBaby/internal/retry"
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
)
|
||||
|
||||
// 将repos设置为public
|
||||
func MakeRepositoryPublic(rule *model.Rule) {
|
||||
reposConfig, account := GetGithubReposAndAccountFromRule(rule)
|
||||
if reposConfig == nil || account == nil {
|
||||
log.Printf("无法获取 GitHub 配置或账户信息")
|
||||
return
|
||||
// MakeRepositoryPublic 将规则中配置的仓库设置为 public
|
||||
func MakeRepositoryPublic(ctx context.Context, rule *model.Rule) error {
|
||||
reposConfig, account, err := GetGithubReposAndAccountFromRule(rule)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var fails []string
|
||||
for _, repo := range reposConfig.Repos {
|
||||
config, err := configs.GetConfig()
|
||||
err := retry.Do(ctx, func() error {
|
||||
return SetRepositoryPublic(ctx, account.Token, account.Owner, repo)
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("获取配置失败: %v", err)
|
||||
return
|
||||
}
|
||||
var timeout time.Duration = time.Duration(config.TimeoutDurationHours) * time.Hour
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
operation := func() (string, error) {
|
||||
err := SetRepositoryPublic(account.Token, account.Owner, repo)
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = backoff.Retry(ctx, operation, backoff.WithBackOff(backoff.NewExponentialBackOff()))
|
||||
if err != nil {
|
||||
log.Printf("将仓库 %s 设置为 public 失败: %v", repo, err)
|
||||
fails = append(fails, fmt.Sprintf("%s: %v", repo, err))
|
||||
}
|
||||
}
|
||||
|
||||
if len(fails) > 0 {
|
||||
return fmt.Errorf("%d/%d 个仓库设置为 public 失败: %s",
|
||||
len(fails), len(reposConfig.Repos), strings.Join(fails, "; "))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
+10
-3
@@ -1,10 +1,17 @@
|
||||
package github
|
||||
|
||||
const (
|
||||
// AccountType GitHub 账号类型标识
|
||||
AccountType = "github"
|
||||
// RuleTypeMakeRepoPublic 公开仓库的规则类型标识
|
||||
RuleTypeMakeRepoPublic = "github-repo-public"
|
||||
)
|
||||
|
||||
type GithubReposConfig struct {
|
||||
Repos []string `yaml:"repos"`
|
||||
Repos []string `json:"repos"`
|
||||
}
|
||||
|
||||
type GithubAccount struct {
|
||||
Token string
|
||||
Owner string
|
||||
Token string `json:"token"`
|
||||
Owner string `json:"owner"`
|
||||
}
|
||||
|
||||
@@ -3,12 +3,15 @@ package github
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/internal/meta"
|
||||
)
|
||||
|
||||
type GithubMakeRepositoryPublicRuleValidator struct{}
|
||||
|
||||
func (v *GithubMakeRepositoryPublicRuleValidator) GetType() string {
|
||||
return "github_make_repository_public"
|
||||
return RuleTypeMakeRepoPublic
|
||||
}
|
||||
|
||||
func (v *GithubMakeRepositoryPublicRuleValidator) Validate(configJSON string) error {
|
||||
@@ -21,5 +24,33 @@ func (v *GithubMakeRepositoryPublicRuleValidator) Validate(configJSON string) er
|
||||
return fmt.Errorf("GitHub 规则配置错误: 至少需要指定一个仓库")
|
||||
}
|
||||
|
||||
for _, repo := range config.Repos {
|
||||
if strings.TrimSpace(repo) == "" {
|
||||
return fmt.Errorf("GitHub 规则配置错误: 仓库名不能为空")
|
||||
}
|
||||
if strings.Contains(repo, "/") {
|
||||
return fmt.Errorf("GitHub 规则配置错误: 只填写仓库名(不含所有者),收到 %q", repo)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *GithubMakeRepositoryPublicRuleValidator) Meta() meta.RuleMeta {
|
||||
return meta.RuleMeta{
|
||||
Type: RuleTypeMakeRepoPublic,
|
||||
Label: "公开 GitHub 仓库",
|
||||
Description: "触发时把指定的私有仓库改为公开。",
|
||||
AccountType: AccountType,
|
||||
Fields: []meta.Field{
|
||||
{
|
||||
Key: "repos",
|
||||
Label: "仓库列表",
|
||||
Type: meta.FieldStringList,
|
||||
Required: true,
|
||||
Placeholder: "my-repo",
|
||||
Help: "只填仓库名,所有者取自关联的 GitHub 账号",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
@@ -10,13 +11,11 @@ import (
|
||||
type GithubMakeRepoPublicExecutor struct{}
|
||||
|
||||
func (e *GithubMakeRepoPublicExecutor) GetType() string {
|
||||
return "github-make-repo-public"
|
||||
return RuleTypeMakeRepoPublic
|
||||
}
|
||||
|
||||
func (e *GithubMakeRepoPublicExecutor) Execute(rule *model.Rule) error {
|
||||
func (e *GithubMakeRepoPublicExecutor) Execute(ctx context.Context, rule *model.Rule) error {
|
||||
log.Printf("执行GitHub仓库公开规则: %s (ID: %d)", rule.Name, rule.ID)
|
||||
|
||||
MakeRepositoryPublic(rule)
|
||||
|
||||
return nil
|
||||
return MakeRepositoryPublic(ctx, rule)
|
||||
}
|
||||
|
||||
+12
-31
@@ -3,55 +3,36 @@ package github
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/go-github/v84/github"
|
||||
"github.com/ssdomei232/goodBaby/handler/db"
|
||||
"github.com/ssdomei232/goodBaby/model"
|
||||
)
|
||||
|
||||
// 从 rule 中获取 GithubReposConfig 和 GithubAccount
|
||||
func GetGithubReposAndAccountFromRule(rule *model.Rule) (*GithubReposConfig, *GithubAccount) {
|
||||
// GetGithubReposAndAccountFromRule 从 rule 中获取 GithubReposConfig 和 GithubAccount
|
||||
func GetGithubReposAndAccountFromRule(rule *model.Rule) (*GithubReposConfig, *GithubAccount, error) {
|
||||
var githubReposConfig GithubReposConfig
|
||||
if err := json.Unmarshal([]byte(rule.ConfigJson), &githubReposConfig); err != nil {
|
||||
return nil, nil, fmt.Errorf("解析 GitHub 规则配置失败: %w", err)
|
||||
}
|
||||
|
||||
var githubAccount GithubAccount
|
||||
|
||||
// 获取 GithubReposConfig
|
||||
err := json.Unmarshal([]byte(rule.ConfigJson), &githubReposConfig)
|
||||
if err != nil {
|
||||
log.Printf("获取 GitHub 配置失败: %v", err)
|
||||
return nil, nil
|
||||
if err := db.LoadAccountConfig(rule.AccountID, &githubAccount); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// 获取 GithubAccount
|
||||
gormDB, err := db.GetGormDB()
|
||||
if err != nil {
|
||||
log.Printf("获取 Gorm DB 失败: %v", err)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
err = gormDB.Where("id = ?", rule.AccountID).First(&githubAccount).Error
|
||||
if err != nil {
|
||||
log.Printf("获取 GitHub 账户失败: %v", err)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &githubReposConfig, &githubAccount
|
||||
return &githubReposConfig, &githubAccount, nil
|
||||
}
|
||||
|
||||
// 将仓库设置为 public
|
||||
func SetRepositoryPublic(token, owner, repo string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
// 1. 初始化客户端
|
||||
// SetRepositoryPublic 将仓库设置为 public
|
||||
func SetRepositoryPublic(ctx context.Context, token, owner, repo string) error {
|
||||
client := github.NewClient(nil).WithAuthToken(token)
|
||||
|
||||
// 2. 准备修改的参数
|
||||
opts := &github.Repository{
|
||||
Visibility: github.Ptr("public"),
|
||||
}
|
||||
|
||||
// 3. 执行更新操作
|
||||
_, _, err := client.Repositories.Edit(ctx, owner, repo, opts)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user