From ce4ed9744b3cddc6e46aa0bac63036d355037687 Mon Sep 17 00:00:00 2001 From: KartoffelChipss Date: Tue, 5 May 2026 17:56:02 +0200 Subject: [PATCH] Properly abstract deployment asset downlod --- backend/app/gitprovider/factory.go | 18 +++++++++++++ backend/app/{github => gitprovider}/github.go | 26 +++++++++++-------- backend/app/gitprovider/provider.go | 10 +++++++ backend/app/handlers/deploy.go | 11 ++++++-- 4 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 backend/app/gitprovider/factory.go rename backend/app/{github => gitprovider}/github.go (89%) create mode 100644 backend/app/gitprovider/provider.go diff --git a/backend/app/gitprovider/factory.go b/backend/app/gitprovider/factory.go new file mode 100644 index 0000000..e145eba --- /dev/null +++ b/backend/app/gitprovider/factory.go @@ -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) + } +} diff --git a/backend/app/github/github.go b/backend/app/gitprovider/github.go similarity index 89% rename from backend/app/github/github.go rename to backend/app/gitprovider/github.go index 7896e87..94faefd 100644 --- a/backend/app/github/github.go +++ b/backend/app/gitprovider/github.go @@ -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) diff --git a/backend/app/gitprovider/provider.go b/backend/app/gitprovider/provider.go new file mode 100644 index 0000000..188fdc4 --- /dev/null +++ b/backend/app/gitprovider/provider.go @@ -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) +} diff --git a/backend/app/handlers/deploy.go b/backend/app/handlers/deploy.go index 9b8a89d..36d2715 100644 --- a/backend/app/handlers/deploy.go +++ b/backend/app/handlers/deploy.go @@ -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,