feat: v2(Incomplete,very early version)
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/cenkalti/backoff/v5"
|
||||
"github.com/ssdomei232/goodBaby/configs"
|
||||
"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
|
||||
}
|
||||
|
||||
for _, repo := range reposConfig.Repos {
|
||||
config, err := configs.GetConfig()
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package github
|
||||
|
||||
type GithubReposConfig struct {
|
||||
Repos []string `yaml:"repos"`
|
||||
}
|
||||
|
||||
type GithubAccount struct {
|
||||
Token string
|
||||
Owner string
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"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) {
|
||||
var githubReposConfig GithubReposConfig
|
||||
var githubAccount GithubAccount
|
||||
|
||||
// 获取 GithubReposConfig
|
||||
err := json.Unmarshal([]byte(rule.ConfigJson), &githubReposConfig)
|
||||
if err != nil {
|
||||
log.Printf("获取 GitHub 配置失败: %v", err)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// 获取 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
|
||||
}
|
||||
|
||||
// 将仓库设置为 public
|
||||
func SetRepositoryPublic(token, owner, repo string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
// 1. 初始化客户端
|
||||
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