From d2f2033fd35fc5a186d56e38f2a091891e9d49d5 Mon Sep 17 00:00:00 2001 From: tbxark Date: Mon, 9 Dec 2024 15:53:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=AE=9A=E4=B9=89=EF=BC=8C=E5=B0=86BackupProvider?= =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=B8=BAProvider=EF=BC=8C=E5=B9=B6=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E7=9B=B8=E5=85=B3=E6=9F=A5=E8=AF=A2=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- provider/gitea/gitea.go | 2 +- provider/github/github.go | 29 ++++++++++++----------------- provider/provider/provider.go | 2 +- task.go | 2 +- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/provider/gitea/gitea.go b/provider/gitea/gitea.go index 1dd81a6..aa02336 100644 --- a/provider/gitea/gitea.go +++ b/provider/gitea/gitea.go @@ -14,7 +14,7 @@ type Config struct { AuthUsername string `json:"auth_username"` } -var _ provider.BackupProvider = &Gitea{} +var _ provider.Provider = &Gitea{} type Gitea struct { conf *Config diff --git a/provider/github/github.go b/provider/github/github.go index 5d39cb9..2910178 100644 --- a/provider/github/github.go +++ b/provider/github/github.go @@ -28,7 +28,7 @@ func NewGithub(token string) *Github { func (g *Github) LoadAllRepos(owner string, isOrg bool) ([]Repo, error) { tmpl := ` query { - %s { + repositories: %s { repositories( first: 100, after: %s @@ -53,13 +53,10 @@ query { ` next := "null" queryType := "" - dataKey := "" var repos []Repo if isOrg { - dataKey = "organization" queryType = fmt.Sprintf("organization(login: \"%s\")", owner) } else { - dataKey = "repositoryOwner" queryType = fmt.Sprintf("repositoryOwner(login: \"%s\")", owner) } token := request.WithAuthorization(g.Token, "bearer") @@ -70,31 +67,29 @@ query { if err != nil { return nil, err } - info, ok := data.Data[dataKey] - if !ok { - return nil, fmt.Errorf("no data key %s", dataKey) - } - for _, repo := range info.Repositories.Nodes { + for _, repo := range data.Data.Repositories.Repositories.Nodes { if strings.ToLower(repo.Owner.Login) == ownerLower { repos = append(repos, repo) } } - if !info.Repositories.PageInfo.HasNextPage { + if !data.Data.Repositories.Repositories.PageInfo.HasNextPage { break } - next = fmt.Sprintf(`"%s"`, info.Repositories.PageInfo.EndCursor) + next = fmt.Sprintf(`"%s"`, data.Data.Repositories.Repositories.PageInfo.EndCursor) } return repos, nil } type reposQuery struct { - Data map[string]struct { + Data struct { Repositories struct { - PageInfo struct { - HasNextPage bool `json:"hasNextPage"` - EndCursor string `json:"endCursor"` - } `json:"pageInfo"` - Nodes []Repo `json:"nodes"` + Repositories struct { + PageInfo struct { + HasNextPage bool `json:"hasNextPage"` + EndCursor string `json:"endCursor"` + } `json:"pageInfo"` + Nodes []Repo `json:"nodes"` + } `json:"repositories"` } `json:"repositories"` } `json:"data"` } diff --git a/provider/provider/provider.go b/provider/provider/provider.go index 42a1472..5ef97c6 100644 --- a/provider/provider/provider.go +++ b/provider/provider/provider.go @@ -11,7 +11,7 @@ type Repo struct { AuthToken string } -type BackupProvider interface { +type Provider interface { LoadRepos(owner *Owner) ([]string, error) MigrateRepo(from *Owner, to *Owner, repo *Repo) (string, error) DeleteRepo(owner, repo string) (string, error) diff --git a/task.go b/task.go index f5ddc4e..c8a5790 100644 --- a/task.go +++ b/task.go @@ -10,7 +10,7 @@ import ( "log" ) -func BuildBackupProvider(conf *config.BackupProviderConfig) (provider.BackupProvider, error) { +func BuildBackupProvider(conf *config.BackupProviderConfig) (provider.Provider, error) { switch conf.Type { case config.BackupProviderConfigTypeGitea: c, err := config.ConvertToBackupProviderConfig[gitea.Config](conf.Config)