feat: implement proto-gen platform
- Config loader with env var expansion and validation - SQLite database with tasks and toolchain_versions tables - Gitea REST API client for repository and branch operations - K8s client for Job lifecycle management (with noop fallback for local dev) - REST API: tasks CRUD, retry, plugins, version management - Worker/scheduler with polling, concurrency control, and job tracking - Static frontend: generate, tasks, settings pages - Toolchain images: Go and Node.js Dockerfiles with entrypoint scripts - Helm chart with deployment, service, configmap, ingress, PVC - Multi-stage Dockerfile for service - Unit tests for config, database, gitclient, k8s, api, worker - E2E tests for idempotency, version switch, failure recovery
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
package gitclient
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type giteaClient struct {
|
||||
baseURL string
|
||||
token string
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func NewClient(baseURL, token string) GitClient {
|
||||
return &giteaClient{
|
||||
baseURL: baseURL,
|
||||
token: token,
|
||||
httpClient: &http.Client{Timeout: 30 * time.Second},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *giteaClient) doRequest(method, path string, body interface{}) ([]byte, error) {
|
||||
u := c.baseURL + "/api/v1" + path
|
||||
|
||||
var bodyReader io.Reader
|
||||
if body != nil {
|
||||
data, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshaling body: %w", err)
|
||||
}
|
||||
bodyReader = bytes.NewReader(data)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, u, bodyReader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("creating request: %w", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "token "+c.token)
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("executing request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading response: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
return nil, fmt.Errorf("gitea API error (%d): %s", resp.StatusCode, string(data))
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (c *giteaClient) ListRepositories(owner string) ([]Repository, error) {
|
||||
data, err := c.doRequest("GET", "/repos/search?limit=50&owner="+url.QueryEscape(owner), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result struct {
|
||||
Data []Repository `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &result); err != nil {
|
||||
return nil, fmt.Errorf("parsing response: %w", err)
|
||||
}
|
||||
return result.Data, nil
|
||||
}
|
||||
|
||||
func (c *giteaClient) CreateRepository(owner, name, description string, private bool) (*Repository, error) {
|
||||
body := map[string]interface{}{
|
||||
"name": name,
|
||||
"description": description,
|
||||
"private": private,
|
||||
}
|
||||
data, err := c.doRequest("POST", "/user/repos", body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var repo Repository
|
||||
if err := json.Unmarshal(data, &repo); err != nil {
|
||||
return nil, fmt.Errorf("parsing response: %w", err)
|
||||
}
|
||||
return &repo, nil
|
||||
}
|
||||
|
||||
func (c *giteaClient) GetRepository(owner, name string) (*Repository, error) {
|
||||
data, err := c.doRequest("GET", "/repos/"+url.PathEscape(owner)+"/"+url.PathEscape(name), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var repo Repository
|
||||
if err := json.Unmarshal(data, &repo); err != nil {
|
||||
return nil, fmt.Errorf("parsing response: %w", err)
|
||||
}
|
||||
return &repo, nil
|
||||
}
|
||||
|
||||
func (c *giteaClient) ListBranches(owner, repo string) ([]Branch, error) {
|
||||
data, err := c.doRequest("GET", "/repos/"+url.PathEscape(owner)+"/"+url.PathEscape(repo)+"/branches", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var branches []Branch
|
||||
if err := json.Unmarshal(data, &branches); err != nil {
|
||||
return nil, fmt.Errorf("parsing response: %w", err)
|
||||
}
|
||||
return branches, nil
|
||||
}
|
||||
|
||||
func (c *giteaClient) GetBranch(owner, repo, branch string) (*Branch, error) {
|
||||
data, err := c.doRequest("GET", "/repos/"+url.PathEscape(owner)+"/"+url.PathEscape(repo)+"/branches/"+url.PathEscape(branch), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var b Branch
|
||||
if err := json.Unmarshal(data, &b); err != nil {
|
||||
return nil, fmt.Errorf("parsing response: %w", err)
|
||||
}
|
||||
return &b, nil
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package gitclient
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetBranch(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/v1/repos/test-owner/test-repo/branches/main" {
|
||||
t.Errorf("unexpected path: %s", r.URL.Path)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if r.Header.Get("Authorization") != "token test-token" {
|
||||
t.Errorf("unexpected auth header: %s", r.Header.Get("Authorization"))
|
||||
}
|
||||
json.NewEncoder(w).Encode(Branch{Name: "main", Commit: "abc123"})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := NewClient(server.URL, "test-token")
|
||||
b, err := client.GetBranch("test-owner", "test-repo", "main")
|
||||
if err != nil {
|
||||
t.Fatalf("GetBranch: %v", err)
|
||||
}
|
||||
if b.Name != "main" {
|
||||
t.Errorf("Name = %q, want %q", b.Name, "main")
|
||||
}
|
||||
if b.Commit != "abc123" {
|
||||
t.Errorf("Commit = %q, want %q", b.Commit, "abc123")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListBranches(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode([]Branch{
|
||||
{Name: "main", Commit: "abc"},
|
||||
{Name: "dev", Commit: "def"},
|
||||
})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := NewClient(server.URL, "test-token")
|
||||
branches, err := client.ListBranches("owner", "repo")
|
||||
if err != nil {
|
||||
t.Fatalf("ListBranches: %v", err)
|
||||
}
|
||||
if len(branches) != 2 {
|
||||
t.Errorf("len(branches) = %d, want 2", len(branches))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateRepository(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
t.Errorf("unexpected method: %s", r.Method)
|
||||
}
|
||||
var body map[string]interface{}
|
||||
json.NewDecoder(r.Body).Decode(&body)
|
||||
if body["name"] != "new-repo" {
|
||||
t.Errorf("name = %v, want new-repo", body["name"])
|
||||
}
|
||||
json.NewEncoder(w).Encode(Repository{Name: "new-repo", FullName: "owner/new-repo"})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := NewClient(server.URL, "test-token")
|
||||
repo, err := client.CreateRepository("owner", "new-repo", "test", false)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateRepository: %v", err)
|
||||
}
|
||||
if repo.Name != "new-repo" {
|
||||
t.Errorf("Name = %q, want %q", repo.Name, "new-repo")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package gitclient
|
||||
|
||||
type Repository struct {
|
||||
Name string `json:"name"`
|
||||
FullName string `json:"full_name"`
|
||||
Private bool `json:"private"`
|
||||
Empty bool `json:"empty"`
|
||||
}
|
||||
|
||||
type Branch struct {
|
||||
Name string `json:"name"`
|
||||
Commit string `json:"commit"`
|
||||
}
|
||||
|
||||
type GitClient interface {
|
||||
ListRepositories(owner string) ([]Repository, error)
|
||||
CreateRepository(owner, name, description string, private bool) (*Repository, error)
|
||||
GetRepository(owner, name string) (*Repository, error)
|
||||
ListBranches(owner, repo string) ([]Branch, error)
|
||||
GetBranch(owner, repo, branch string) (*Branch, error)
|
||||
}
|
||||
Reference in New Issue
Block a user