feat: initial gitea-k3s-deployer service, web UI, tests, and K3s/Depot manifests
Build and Push Image / build (push) Failing after 5s
Build and Push Image / build (push) Failing after 5s
This commit is contained in:
commit
bf229aeed2
23 files changed
+2702
No files matched your search
@@ -0,0 +1,209 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrNotFound = errors.New("gitea resource not found")
|
||||
|
||||
type DirEntry struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Type string `json:"type"` // "file" or "dir"
|
||||
DownloadURL string `json:"download_url"`
|
||||
}
|
||||
|
||||
type Client interface {
|
||||
GetFile(ctx context.Context, owner, repo, path, ref string) ([]byte, error)
|
||||
ListDir(ctx context.Context, owner, repo, path, ref string) ([]DirEntry, error)
|
||||
}
|
||||
|
||||
type HTTPClient struct {
|
||||
BaseURL string
|
||||
Token string
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
func NewClient(baseURL, token string) *HTTPClient {
|
||||
return &HTTPClient{
|
||||
BaseURL: strings.TrimRight(baseURL, "/"),
|
||||
Token: token,
|
||||
HTTPClient: &http.Client{
|
||||
Timeout: 15 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *HTTPClient) doRequest(ctx context.Context, reqURL string) ([]byte, int, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if c.Token != "" {
|
||||
req.Header.Set("Authorization", "token "+c.Token)
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20)) // 4 MiB max
|
||||
if err != nil {
|
||||
return nil, resp.StatusCode, err
|
||||
}
|
||||
return body, resp.StatusCode, nil
|
||||
}
|
||||
|
||||
func (c *HTTPClient) GetFile(ctx context.Context, owner, repo, filePath, ref string) ([]byte, error) {
|
||||
escapedPath := escapePathSegments(filePath)
|
||||
u := fmt.Sprintf("%s/api/v1/repos/%s/%s/raw/%s", c.BaseURL, url.PathEscape(owner), url.PathEscape(repo), escapedPath)
|
||||
if ref != "" {
|
||||
u += "?ref=" + url.QueryEscape(ref)
|
||||
}
|
||||
|
||||
body, status, err := c.doRequest(ctx, u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if status == http.StatusNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if status < 200 || status >= 300 {
|
||||
return nil, fmt.Errorf("gitea GET %s returned HTTP %d: %s", u, status, string(body))
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func (c *HTTPClient) ListDir(ctx context.Context, owner, repo, dirPath, ref string) ([]DirEntry, error) {
|
||||
escapedPath := escapePathSegments(dirPath)
|
||||
u := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s", c.BaseURL, url.PathEscape(owner), url.PathEscape(repo), escapedPath)
|
||||
if ref != "" {
|
||||
u += "?ref=" + url.QueryEscape(ref)
|
||||
}
|
||||
|
||||
body, status, err := c.doRequest(ctx, u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if status == http.StatusNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if status < 200 || status >= 300 {
|
||||
return nil, fmt.Errorf("gitea GET %s returned HTTP %d: %s", u, status, string(body))
|
||||
}
|
||||
|
||||
var entries []DirEntry
|
||||
if err := json.Unmarshal(body, &entries); err != nil {
|
||||
return nil, fmt.Errorf("decode gitea contents: %w", err)
|
||||
}
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
func escapePathSegments(p string) string {
|
||||
p = strings.TrimPrefix(p, "/")
|
||||
parts := strings.Split(p, "/")
|
||||
for i := range parts {
|
||||
parts[i] = url.PathEscape(parts[i])
|
||||
}
|
||||
return strings.Join(parts, "/")
|
||||
}
|
||||
|
||||
// VerifySignature validates Gitea's X-Gitea-Signature HMAC-SHA256 hex digest.
|
||||
func VerifySignature(secret string, payload []byte, signature string) bool {
|
||||
if secret == "" {
|
||||
return true
|
||||
}
|
||||
signature = strings.TrimSpace(signature)
|
||||
signature = strings.TrimPrefix(signature, "sha256=")
|
||||
if signature == "" {
|
||||
return false
|
||||
}
|
||||
gotMAC, err := hex.DecodeString(signature)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
mac := hmac.New(sha256.New, []byte(secret))
|
||||
mac.Write(payload)
|
||||
expectedMAC := mac.Sum(nil)
|
||||
return hmac.Equal(gotMAC, expectedMAC)
|
||||
}
|
||||
|
||||
// Webhook Payload Models
|
||||
|
||||
type Repository struct {
|
||||
FullName string `json:"full_name"`
|
||||
Name string `json:"name"`
|
||||
DefaultBranch string `json:"default_branch"`
|
||||
Owner struct {
|
||||
Username string `json:"username"`
|
||||
Login string `json:"login"`
|
||||
} `json:"owner"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Username string `json:"username"`
|
||||
Login string `json:"login"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
type PushEvent struct {
|
||||
Ref string `json:"ref"`
|
||||
After string `json:"after"`
|
||||
Before string `json:"before"`
|
||||
Repository Repository `json:"repository"`
|
||||
Pusher User `json:"pusher"`
|
||||
Sender User `json:"sender"`
|
||||
HeadCommit struct {
|
||||
ID string `json:"id"`
|
||||
Message string `json:"message"`
|
||||
} `json:"head_commit"`
|
||||
}
|
||||
|
||||
type ReleaseEvent struct {
|
||||
Action string `json:"action"`
|
||||
Release struct {
|
||||
TagName string `json:"tag_name"`
|
||||
TargetCommitish string `json:"target_commitish"`
|
||||
Name string `json:"name"`
|
||||
Draft bool `json:"draft"`
|
||||
Prerelease bool `json:"prerelease"`
|
||||
} `json:"release"`
|
||||
Repository Repository `json:"repository"`
|
||||
Sender User `json:"sender"`
|
||||
}
|
||||
|
||||
type WorkflowRunEvent struct {
|
||||
Action string `json:"action"`
|
||||
WorkflowRun struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
HeadBranch string `json:"head_branch"`
|
||||
HeadSHA string `json:"head_sha"`
|
||||
Status string `json:"status"`
|
||||
Conclusion string `json:"conclusion"`
|
||||
} `json:"workflow_run"`
|
||||
Repository Repository `json:"repository"`
|
||||
Sender User `json:"sender"`
|
||||
}
|
||||
|
||||
func BranchFromRef(ref string) string {
|
||||
const prefix = "refs/heads/"
|
||||
if strings.HasPrefix(ref, prefix) {
|
||||
return strings.TrimPrefix(ref, prefix)
|
||||
}
|
||||
return ref
|
||||
}
|
||||
Reference in new issue
Block a user