Refactor GitProvider to take in auth token
This commit is contained in:
@@ -5,12 +5,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewProvider(serverType, protocol, baseUrl string) (GitProvider, error) {
|
func NewProvider(serverType, protocol, baseUrl, authToken string) (GitProvider, error) {
|
||||||
switch strings.ToLower(serverType) {
|
switch strings.ToLower(serverType) {
|
||||||
case "github":
|
case "github":
|
||||||
return &GitHubProvider{
|
return &GitHubProvider{
|
||||||
protocol: protocol,
|
protocol: protocol,
|
||||||
baseUrl: baseUrl,
|
baseUrl: baseUrl,
|
||||||
|
authToken: authToken,
|
||||||
}, nil
|
}, nil
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupported git provider: %s", serverType)
|
return nil, fmt.Errorf("unsupported git provider: %s", serverType)
|
||||||
|
|||||||
@@ -13,22 +13,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type GitHubProvider struct {
|
type GitHubProvider struct {
|
||||||
protocol string
|
protocol string
|
||||||
baseUrl string
|
baseUrl string
|
||||||
|
authToken string
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ GitProvider = (*GitHubProvider)(nil)
|
var _ GitProvider = (*GitHubProvider)(nil)
|
||||||
|
|
||||||
func (p *GitHubProvider) FetchAndDeployBranch(owner, repo, branch, authToken, destPath string) (*DeployResult, error) {
|
func (p *GitHubProvider) FetchAndDeployBranch(owner, repo, branch, destPath string) (*DeployResult, error) {
|
||||||
baseUrl := strings.TrimSuffix(p.baseUrl, "/")
|
baseUrl := strings.TrimSuffix(p.baseUrl, "/")
|
||||||
apiUrl := fmt.Sprintf("%s://api.%s/", p.protocol, baseUrl)
|
apiUrl := fmt.Sprintf("%s://api.%s/", p.protocol, baseUrl)
|
||||||
|
|
||||||
result, err := fetchBranchHead(apiUrl, owner, repo, branch, authToken)
|
result, err := fetchBranchHead(apiUrl, owner, repo, branch, p.authToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("fetching branch head: %w", err)
|
return nil, fmt.Errorf("fetching branch head: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = downloadAndExtract(owner, repo, branch, authToken, destPath); err != nil {
|
if err = downloadAndExtract(owner, repo, branch, p.authToken, destPath); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,5 +6,5 @@ type DeployResult struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type GitProvider interface {
|
type GitProvider interface {
|
||||||
FetchAndDeployBranch(owner, repo, branch, authToken, destPath string) (*DeployResult, error)
|
FetchAndDeployBranch(owner, repo, branch, destPath string) (*DeployResult, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ func (h *DeploySiteHandler) PostDeploy(c fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
provider, err := gitprovider.NewProvider(gitServer.Type, gitServer.Protocol, gitServer.BaseUrl)
|
provider, err := gitprovider.NewProvider(gitServer.Type, gitServer.Protocol, gitServer.BaseUrl, gitServer.AuthToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(400).JSON(models.APIError{
|
return c.Status(400).JSON(models.APIError{
|
||||||
Message: "Unsupported git provider: " + gitServer.Type,
|
Message: "Unsupported git provider: " + gitServer.Type,
|
||||||
@@ -140,7 +140,6 @@ func (h *DeploySiteHandler) PostDeploy(c fiber.Ctx) error {
|
|||||||
site.Owner,
|
site.Owner,
|
||||||
site.Repository,
|
site.Repository,
|
||||||
site.Branch,
|
site.Branch,
|
||||||
gitServer.AuthToken,
|
|
||||||
sitePath,
|
sitePath,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user