Properly abstract deployment asset downlod

This commit is contained in:
2026-05-05 17:56:02 +02:00
parent 596e94794e
commit ce4ed9744b
4 changed files with 52 additions and 13 deletions
+18
View File
@@ -0,0 +1,18 @@
package gitprovider
import (
"fmt"
"strings"
)
func NewProvider(serverType, protocol, baseUrl string) (GitProvider, error) {
switch strings.ToLower(serverType) {
case "github":
return &GitHubProvider{
protocol: protocol,
baseUrl: baseUrl,
}, nil
default:
return nil, fmt.Errorf("unsupported git provider: %s", serverType)
}
}
@@ -1,4 +1,4 @@
package github
package gitprovider
import (
"archive/zip"
@@ -12,19 +12,23 @@ import (
"strings"
)
// DeployResult holds metadata about the deployed commit.
type DeployResult struct {
CommitHash string `json:"commit_hash"`
CommitMessage string `json:"commit_message"`
type GitHubProvider struct {
protocol string
baseUrl string
}
func FetchAndDeployBranch(repoOwner, repoName, branch, pat, destDir string) (*DeployResult, error) {
result, err := fetchBranchHead(repoOwner, repoName, branch, pat)
var _ GitProvider = (*GitHubProvider)(nil)
func (p *GitHubProvider) FetchAndDeployBranch(owner, repo, branch, authToken, destPath string) (*DeployResult, error) {
baseUrl := strings.TrimSuffix(p.baseUrl, "/")
apiUrl := fmt.Sprintf("%s://api.%s/", p.protocol, baseUrl)
result, err := fetchBranchHead(apiUrl, owner, repo, branch, authToken)
if err != nil {
return nil, fmt.Errorf("fetching branch head: %w", err)
}
if err = downloadAndExtract(repoOwner, repoName, branch, pat, destDir); err != nil {
if err = downloadAndExtract(owner, repo, branch, authToken, destPath); err != nil {
return nil, err
}
@@ -32,10 +36,10 @@ func FetchAndDeployBranch(repoOwner, repoName, branch, pat, destDir string) (*De
}
// fetchBranchHead returns the SHA and commit message for the tip of the given branch.
func fetchBranchHead(owner, repo, branch, pat string) (*DeployResult, error) {
func fetchBranchHead(apiUrl, owner, repo, branch, pat string) (*DeployResult, error) {
url := fmt.Sprintf(
"https://api.github.com/repos/%s/%s/commits/%s",
owner, repo, branch,
"%srepos/%s/%s/commits/%s",
apiUrl, owner, repo, branch,
)
req, err := http.NewRequest(http.MethodGet, url, nil)
+10
View File
@@ -0,0 +1,10 @@
package gitprovider
type DeployResult struct {
CommitHash string
CommitMessage string
}
type GitProvider interface {
FetchAndDeployBranch(owner, repo, branch, authToken, destPath string) (*DeployResult, error)
}
+9 -2
View File
@@ -5,7 +5,7 @@ import (
"errors"
"log"
"path/filepath"
"quay/app/github"
"quay/app/gitprovider"
"quay/app/models"
"quay/app/repository"
"quay/internal/envconfig"
@@ -129,7 +129,14 @@ func (h *DeploySiteHandler) PostDeploy(c fiber.Ctx) error {
})
}
res, err := github.FetchAndDeployBranch(
provider, err := gitprovider.NewProvider(gitServer.Type, gitServer.Protocol, gitServer.BaseUrl)
if err != nil {
return c.Status(400).JSON(models.APIError{
Message: "Unsupported git provider: " + gitServer.Type,
})
}
res, err := provider.FetchAndDeployBranch(
site.Owner,
site.Repository,
site.Branch,