[CI-SKIP] Upload current
This commit is contained in:
55
common/provider_gitea.go
Normal file
55
common/provider_gitea.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
// giteaProvider implements RepoProvider against a Gitea instance.
|
||||
type giteaProvider struct {
|
||||
config *AppConfig
|
||||
client *gitea.Client
|
||||
}
|
||||
|
||||
func newGiteaProvider(config *AppConfig) (*giteaProvider, error) {
|
||||
client, err := gitea.NewClient(config.Git.Server, gitea.SetToken(config.Git.APIToken))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("connect gitea %s: %w", config.Git.Server, err)
|
||||
}
|
||||
return &giteaProvider{config: config, client: client}, nil
|
||||
}
|
||||
|
||||
// CreateRepo creates a private org repo named "<domain><repo_suffix>" and
|
||||
// returns its clone URL.
|
||||
func (p *giteaProvider) CreateRepo(domain string, domainConfig *DomainConfig) (string, error) {
|
||||
name := domain + domainConfig.Repo.RepoSuffix
|
||||
opts := gitea.CreateRepoOption{
|
||||
Name: name,
|
||||
Description: "Certificate storage for " + domain,
|
||||
Private: true,
|
||||
AutoInit: false,
|
||||
DefaultBranch: "master",
|
||||
TrustModel: gitea.TrustModelDefault,
|
||||
}
|
||||
repo, _, err := p.client.CreateOrgRepo(p.config.Git.OrgName, opts)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create gitea repo %s/%s: %w", p.config.Git.OrgName, name, err)
|
||||
}
|
||||
return repo.CloneURL, nil
|
||||
}
|
||||
|
||||
// HeadCommit returns the commit ID of branch in the domain's repo. A 404
|
||||
// from Gitea (repo or branch missing) is mapped to ErrRepoNotFound.
|
||||
func (p *giteaProvider) HeadCommit(domain, branch string, domainConfig *DomainConfig) (string, error) {
|
||||
name := domain + domainConfig.Repo.RepoSuffix
|
||||
b, resp, err := p.client.GetRepoBranch(p.config.Git.OrgName, name, branch)
|
||||
if err != nil {
|
||||
if resp != nil && resp.Response != nil && resp.StatusCode == http.StatusNotFound {
|
||||
return "", ErrRepoNotFound
|
||||
}
|
||||
return "", fmt.Errorf("gitea branch %s/%s@%s: %w", p.config.Git.OrgName, name, branch, err)
|
||||
}
|
||||
return b.Commit.ID, nil
|
||||
}
|
||||
Reference in New Issue
Block a user