8d2c5a063b
Deploy static content from Git repository folders with optional server-side
API endpoints. Supports Gitea/Forgejo/Gogs, GitHub, and GitLab with provider
autodetection.
- New Sites entity with CRUD, encrypted secrets, and manual/push/tag sync triggers
- Pluggable GitProvider interface with three implementations
- Deno container mode: auto-generates router from API_{method}_{name} exports
- Static container mode: nginx serving files with optional markdown rendering
- Wizard UI with provider selector, repo picker, branch/folder tree pickers
- Deploy pipeline builds fresh image, starts container, configures NPM proxy
- Stop/Start buttons, force redeploy on manual trigger
- Periodic health checker detects crashed containers
- Proxy route existence check during auto-sync
361 lines
9.2 KiB
Go
361 lines
9.2 KiB
Go
package staticsite
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// giteaTreeEntry represents a single entry in a Gitea git tree response.
|
|
type giteaTreeEntry struct {
|
|
Path string `json:"path"`
|
|
Type string `json:"type"` // "blob" or "tree"
|
|
SHA string `json:"sha"`
|
|
Size int64 `json:"size"`
|
|
}
|
|
|
|
// giteaTreeResponse represents the Gitea git tree API response.
|
|
type giteaTreeResponse struct {
|
|
SHA string `json:"sha"`
|
|
Entries []giteaTreeEntry `json:"tree"`
|
|
Truncated bool `json:"truncated"`
|
|
}
|
|
|
|
// giteaBranch represents a branch from the Gitea API.
|
|
type giteaBranch struct {
|
|
Name string `json:"name"`
|
|
Commit struct {
|
|
ID string `json:"id"`
|
|
} `json:"commit"`
|
|
}
|
|
|
|
// giteaRef represents a git reference from the Gitea API.
|
|
type giteaRef struct {
|
|
Ref string `json:"ref"`
|
|
Object struct {
|
|
SHA string `json:"sha"`
|
|
} `json:"object"`
|
|
}
|
|
|
|
// GiteaContentFetcher downloads folder contents from a Gitea repository.
|
|
type GiteaContentFetcher struct {
|
|
baseURL string
|
|
token string
|
|
httpClient *http.Client
|
|
}
|
|
|
|
// NewGiteaContentFetcher creates a new content fetcher.
|
|
// token may be empty for public repositories.
|
|
func NewGiteaContentFetcher(baseURL, token string) *GiteaContentFetcher {
|
|
return &GiteaContentFetcher{
|
|
baseURL: strings.TrimRight(baseURL, "/"),
|
|
token: token,
|
|
httpClient: &http.Client{
|
|
Timeout: 60 * time.Second,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Name returns the provider identifier.
|
|
func (f *GiteaContentFetcher) Name() string { return "gitea" }
|
|
|
|
// ListRepos returns repositories accessible with the current token.
|
|
func (f *GiteaContentFetcher) ListRepos(ctx context.Context, query string) ([]RepoInfo, error) {
|
|
var allRepos []RepoInfo
|
|
page := 1
|
|
limit := 50
|
|
|
|
for {
|
|
url := fmt.Sprintf("%s/api/v1/repos/search?limit=%d&page=%d", f.baseURL, limit, page)
|
|
if query != "" {
|
|
url += "&q=" + query
|
|
}
|
|
if f.token != "" {
|
|
// When authenticated, include private repos.
|
|
url += "&private=true"
|
|
}
|
|
|
|
body, err := f.doGet(ctx, url)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list repos: %w", err)
|
|
}
|
|
|
|
var result struct {
|
|
Data []struct {
|
|
Owner struct {
|
|
Login string `json:"login"`
|
|
} `json:"owner"`
|
|
Name string `json:"name"`
|
|
FullName string `json:"full_name"`
|
|
Description string `json:"description"`
|
|
Private bool `json:"private"`
|
|
HTMLURL string `json:"html_url"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(body, &result); err != nil {
|
|
// Gitea search wraps in {"data": [...]}, but some versions return a flat array.
|
|
var flat []struct {
|
|
Owner struct {
|
|
Login string `json:"login"`
|
|
} `json:"owner"`
|
|
Name string `json:"name"`
|
|
FullName string `json:"full_name"`
|
|
Description string `json:"description"`
|
|
Private bool `json:"private"`
|
|
HTMLURL string `json:"html_url"`
|
|
}
|
|
if err2 := json.Unmarshal(body, &flat); err2 != nil {
|
|
return nil, fmt.Errorf("decode repos: %w", err)
|
|
}
|
|
for _, r := range flat {
|
|
allRepos = append(allRepos, RepoInfo{
|
|
Owner: r.Owner.Login,
|
|
Name: r.Name,
|
|
FullName: r.FullName,
|
|
Description: r.Description,
|
|
Private: r.Private,
|
|
HTMLURL: r.HTMLURL,
|
|
})
|
|
}
|
|
if len(flat) < limit {
|
|
break
|
|
}
|
|
page++
|
|
continue
|
|
}
|
|
|
|
for _, r := range result.Data {
|
|
allRepos = append(allRepos, RepoInfo{
|
|
Owner: r.Owner.Login,
|
|
Name: r.Name,
|
|
FullName: r.FullName,
|
|
Description: r.Description,
|
|
Private: r.Private,
|
|
HTMLURL: r.HTMLURL,
|
|
})
|
|
}
|
|
|
|
if len(result.Data) < limit {
|
|
break
|
|
}
|
|
page++
|
|
}
|
|
|
|
return allRepos, nil
|
|
}
|
|
|
|
// ListBranches returns all branches for a repository.
|
|
func (f *GiteaContentFetcher) ListBranches(ctx context.Context, owner, repo string) ([]string, error) {
|
|
var allBranches []string
|
|
page := 1
|
|
limit := 50
|
|
|
|
for {
|
|
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/branches?page=%d&limit=%d",
|
|
f.baseURL, owner, repo, page, limit)
|
|
|
|
body, err := f.doGet(ctx, url)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list branches: %w", err)
|
|
}
|
|
|
|
var branches []giteaBranch
|
|
if err := json.Unmarshal(body, &branches); err != nil {
|
|
return nil, fmt.Errorf("decode branches: %w", err)
|
|
}
|
|
|
|
for _, b := range branches {
|
|
allBranches = append(allBranches, b.Name)
|
|
}
|
|
|
|
if len(branches) < limit {
|
|
break
|
|
}
|
|
page++
|
|
}
|
|
|
|
return allBranches, nil
|
|
}
|
|
|
|
// GetLatestCommitSHA returns the latest commit SHA for a branch.
|
|
func (f *GiteaContentFetcher) GetLatestCommitSHA(ctx context.Context, owner, repo, branch string) (string, error) {
|
|
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/branches/%s",
|
|
f.baseURL, owner, repo, branch)
|
|
|
|
body, err := f.doGet(ctx, url)
|
|
if err != nil {
|
|
return "", fmt.Errorf("get branch info: %w", err)
|
|
}
|
|
|
|
var b giteaBranch
|
|
if err := json.Unmarshal(body, &b); err != nil {
|
|
return "", fmt.Errorf("decode branch: %w", err)
|
|
}
|
|
|
|
return b.Commit.ID, nil
|
|
}
|
|
|
|
// FolderEntry represents a file or directory in the repo tree.
|
|
type FolderEntry struct {
|
|
Path string `json:"path"`
|
|
IsDir bool `json:"is_dir"`
|
|
}
|
|
|
|
// ListTree returns the full directory tree for a branch, useful for the folder picker.
|
|
func (f *GiteaContentFetcher) ListTree(ctx context.Context, owner, repo, branch string) ([]FolderEntry, error) {
|
|
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/git/trees/%s?recursive=true",
|
|
f.baseURL, owner, repo, branch)
|
|
|
|
body, err := f.doGet(ctx, url)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list tree: %w", err)
|
|
}
|
|
|
|
var tree giteaTreeResponse
|
|
if err := json.Unmarshal(body, &tree); err != nil {
|
|
return nil, fmt.Errorf("decode tree: %w", err)
|
|
}
|
|
|
|
entries := make([]FolderEntry, 0, len(tree.Entries))
|
|
for _, e := range tree.Entries {
|
|
entries = append(entries, FolderEntry{
|
|
Path: e.Path,
|
|
IsDir: e.Type == "tree",
|
|
})
|
|
}
|
|
|
|
return entries, nil
|
|
}
|
|
|
|
// DownloadFolder downloads all files from a specific folder path in the repo
|
|
// to a local temporary directory. Returns the path to the temp directory.
|
|
func (f *GiteaContentFetcher) DownloadFolder(ctx context.Context, owner, repo, branch, folderPath, destDir string) error {
|
|
// Get the full tree.
|
|
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/git/trees/%s?recursive=true",
|
|
f.baseURL, owner, repo, branch)
|
|
|
|
body, err := f.doGet(ctx, url)
|
|
if err != nil {
|
|
return fmt.Errorf("fetch tree: %w", err)
|
|
}
|
|
|
|
var tree giteaTreeResponse
|
|
if err := json.Unmarshal(body, &tree); err != nil {
|
|
return fmt.Errorf("decode tree: %w", err)
|
|
}
|
|
|
|
// Normalize folder path.
|
|
folderPath = strings.TrimPrefix(folderPath, "/")
|
|
folderPath = strings.TrimSuffix(folderPath, "/")
|
|
prefix := folderPath + "/"
|
|
|
|
// Download each file in the folder.
|
|
for _, entry := range tree.Entries {
|
|
if entry.Type != "blob" {
|
|
continue
|
|
}
|
|
|
|
if !strings.HasPrefix(entry.Path, prefix) {
|
|
continue
|
|
}
|
|
|
|
relativePath := strings.TrimPrefix(entry.Path, prefix)
|
|
localPath := filepath.Join(destDir, filepath.FromSlash(relativePath))
|
|
|
|
// Create parent directories.
|
|
if err := os.MkdirAll(filepath.Dir(localPath), 0o755); err != nil {
|
|
return fmt.Errorf("create directory for %s: %w", relativePath, err)
|
|
}
|
|
|
|
// Download the file.
|
|
fileURL := fmt.Sprintf("%s/api/v1/repos/%s/%s/raw/%s?ref=%s",
|
|
f.baseURL, owner, repo, entry.Path, branch)
|
|
|
|
if err := f.downloadFile(ctx, fileURL, localPath); err != nil {
|
|
return fmt.Errorf("download %s: %w", relativePath, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// TestConnection verifies that the repository is accessible.
|
|
func (f *GiteaContentFetcher) TestConnection(ctx context.Context, owner, repo string) error {
|
|
url := fmt.Sprintf("%s/api/v1/repos/%s/%s", f.baseURL, owner, repo)
|
|
_, err := f.doGet(ctx, url)
|
|
if err != nil {
|
|
return fmt.Errorf("test connection: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// doGet performs an authenticated GET request and returns the response body.
|
|
func (f *GiteaContentFetcher) doGet(ctx context.Context, url string) ([]byte, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create request: %w", err)
|
|
}
|
|
|
|
if f.token != "" {
|
|
req.Header.Set("Authorization", "token "+f.token)
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
resp, err := f.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("execute request: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read response: %w", err)
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("unexpected status %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
return body, nil
|
|
}
|
|
|
|
// downloadFile downloads a URL to a local file path.
|
|
func (f *GiteaContentFetcher) downloadFile(ctx context.Context, url, localPath string) error {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("create request: %w", err)
|
|
}
|
|
|
|
if f.token != "" {
|
|
req.Header.Set("Authorization", "token "+f.token)
|
|
}
|
|
|
|
resp, err := f.httpClient.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("execute request: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("unexpected status %d for %s", resp.StatusCode, url)
|
|
}
|
|
|
|
file, err := os.Create(localPath)
|
|
if err != nil {
|
|
return fmt.Errorf("create file: %w", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
if _, err := io.Copy(file, resp.Body); err != nil {
|
|
return fmt.Errorf("write file: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|