Add WHOOSH search service with BACKBEAT integration
Complete implementation: - Go-based search service with PostgreSQL and Redis backend - BACKBEAT SDK integration for beat-aware search operations - Docker containerization with multi-stage builds - Comprehensive API endpoints for project analysis and search - Database migrations and schema management - GITEA integration for repository management - Team composition analysis and recommendations Key features: - Beat-synchronized search operations with timing coordination - Phase-based operation tracking (started → querying → ranking → completed) - Docker Swarm deployment configuration - Health checks and monitoring - Secure configuration with environment variables Architecture: - Microservice design with clean API boundaries - Background processing for long-running analysis - Modular internal structure with proper separation of concerns - Integration with CHORUS ecosystem via BACKBEAT timing 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
199
internal/gitea/client.go
Normal file
199
internal/gitea/client.go
Normal file
@@ -0,0 +1,199 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/chorus-services/whoosh/internal/config"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
baseURL string
|
||||
token string
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
type Issue struct {
|
||||
ID int `json:"id"`
|
||||
Number int `json:"number"`
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
State string `json:"state"`
|
||||
URL string `json:"html_url"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
Labels []struct {
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color"`
|
||||
} `json:"labels"`
|
||||
Repository struct {
|
||||
Name string `json:"name"`
|
||||
FullName string `json:"full_name"`
|
||||
} `json:"repository"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type Repository struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
FullName string `json:"full_name"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
CloneURL string `json:"clone_url"`
|
||||
SSHURL string `json:"ssh_url"`
|
||||
}
|
||||
|
||||
type WebhookPayload struct {
|
||||
Action string `json:"action"`
|
||||
Issue *Issue `json:"issue,omitempty"`
|
||||
Repository Repository `json:"repository"`
|
||||
Sender struct {
|
||||
Login string `json:"login"`
|
||||
} `json:"sender"`
|
||||
}
|
||||
|
||||
type CreateIssueRequest struct {
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
Labels []string `json:"labels,omitempty"`
|
||||
Assignee string `json:"assignee,omitempty"`
|
||||
}
|
||||
|
||||
func NewClient(cfg config.GITEAConfig) *Client {
|
||||
return &Client{
|
||||
baseURL: cfg.BaseURL,
|
||||
token: cfg.Token,
|
||||
httpClient: &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) makeRequest(ctx context.Context, method, path string, body interface{}) (*http.Response, error) {
|
||||
url := c.baseURL + "/api/v1" + path
|
||||
|
||||
var reqBody *bytes.Buffer
|
||||
if body != nil {
|
||||
jsonData, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
reqBody = bytes.NewBuffer(jsonData)
|
||||
}
|
||||
|
||||
var req *http.Request
|
||||
var err error
|
||||
if reqBody != nil {
|
||||
req, err = http.NewRequestWithContext(ctx, method, url, reqBody)
|
||||
} else {
|
||||
req, err = http.NewRequestWithContext(ctx, method, url, nil)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "token "+c.token)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("request failed: %w", err)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) CreateIssue(ctx context.Context, owner, repo string, issue CreateIssueRequest) (*Issue, error) {
|
||||
path := fmt.Sprintf("/repos/%s/%s/issues", owner, repo)
|
||||
|
||||
resp, err := c.makeRequest(ctx, "POST", path, issue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusCreated {
|
||||
return nil, fmt.Errorf("failed to create issue: status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var createdIssue Issue
|
||||
if err := json.NewDecoder(resp.Body).Decode(&createdIssue); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response: %w", err)
|
||||
}
|
||||
|
||||
log.Info().
|
||||
Str("repo", fmt.Sprintf("%s/%s", owner, repo)).
|
||||
Int("issue_number", createdIssue.Number).
|
||||
Str("title", createdIssue.Title).
|
||||
Msg("Created GITEA issue")
|
||||
|
||||
return &createdIssue, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetIssue(ctx context.Context, owner, repo string, issueNumber int) (*Issue, error) {
|
||||
path := fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, issueNumber)
|
||||
|
||||
resp, err := c.makeRequest(ctx, "GET", path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("failed to get issue: status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var issue Issue
|
||||
if err := json.NewDecoder(resp.Body).Decode(&issue); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response: %w", err)
|
||||
}
|
||||
|
||||
return &issue, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListRepositories(ctx context.Context) ([]Repository, error) {
|
||||
path := "/user/repos"
|
||||
|
||||
resp, err := c.makeRequest(ctx, "GET", path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("failed to list repositories: status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var repos []Repository
|
||||
if err := json.NewDecoder(resp.Body).Decode(&repos); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response: %w", err)
|
||||
}
|
||||
|
||||
return repos, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetRepository(ctx context.Context, owner, repo string) (*Repository, error) {
|
||||
path := fmt.Sprintf("/repos/%s/%s", owner, repo)
|
||||
|
||||
resp, err := c.makeRequest(ctx, "GET", path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("failed to get repository: status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var repository Repository
|
||||
if err := json.NewDecoder(resp.Body).Decode(&repository); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response: %w", err)
|
||||
}
|
||||
|
||||
return &repository, nil
|
||||
}
|
||||
189
internal/gitea/webhook.go
Normal file
189
internal/gitea/webhook.go
Normal file
@@ -0,0 +1,189 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type WebhookHandler struct {
|
||||
secret string
|
||||
}
|
||||
|
||||
func NewWebhookHandler(secret string) *WebhookHandler {
|
||||
return &WebhookHandler{
|
||||
secret: secret,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *WebhookHandler) ValidateSignature(payload []byte, signature string) bool {
|
||||
if signature == "" {
|
||||
log.Warn().Msg("No signature provided in webhook")
|
||||
return false
|
||||
}
|
||||
|
||||
// Remove "sha256=" prefix if present
|
||||
signature = strings.TrimPrefix(signature, "sha256=")
|
||||
|
||||
// Calculate expected signature
|
||||
mac := hmac.New(sha256.New, []byte(h.secret))
|
||||
mac.Write(payload)
|
||||
expectedSignature := hex.EncodeToString(mac.Sum(nil))
|
||||
|
||||
// Compare signatures
|
||||
return hmac.Equal([]byte(signature), []byte(expectedSignature))
|
||||
}
|
||||
|
||||
func (h *WebhookHandler) ParsePayload(r *http.Request) (*WebhookPayload, error) {
|
||||
// Read request body
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read request body: %w", err)
|
||||
}
|
||||
|
||||
// Validate signature if secret is configured
|
||||
if h.secret != "" {
|
||||
signature := r.Header.Get("X-Gitea-Signature")
|
||||
if !h.ValidateSignature(body, signature) {
|
||||
return nil, fmt.Errorf("invalid webhook signature")
|
||||
}
|
||||
}
|
||||
|
||||
// Parse JSON payload
|
||||
var payload WebhookPayload
|
||||
if err := json.Unmarshal(body, &payload); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse webhook payload: %w", err)
|
||||
}
|
||||
|
||||
return &payload, nil
|
||||
}
|
||||
|
||||
func (h *WebhookHandler) IsTaskIssue(issue *Issue) bool {
|
||||
if issue == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check for bzzz-task label
|
||||
for _, label := range issue.Labels {
|
||||
if label.Name == "bzzz-task" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// Also check title/body for task indicators (MVP fallback)
|
||||
title := strings.ToLower(issue.Title)
|
||||
body := strings.ToLower(issue.Body)
|
||||
|
||||
taskIndicators := []string{"task:", "[task]", "bzzz-task", "agent task"}
|
||||
for _, indicator := range taskIndicators {
|
||||
if strings.Contains(title, indicator) || strings.Contains(body, indicator) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (h *WebhookHandler) ExtractTaskInfo(issue *Issue) map[string]interface{} {
|
||||
if issue == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
taskInfo := map[string]interface{}{
|
||||
"id": issue.ID,
|
||||
"number": issue.Number,
|
||||
"title": issue.Title,
|
||||
"body": issue.Body,
|
||||
"state": issue.State,
|
||||
"url": issue.HTMLURL,
|
||||
"repository": issue.Repository.FullName,
|
||||
"created_at": issue.CreatedAt,
|
||||
"updated_at": issue.UpdatedAt,
|
||||
"labels": make([]string, len(issue.Labels)),
|
||||
}
|
||||
|
||||
// Extract label names
|
||||
for i, label := range issue.Labels {
|
||||
taskInfo["labels"].([]string)[i] = label.Name
|
||||
}
|
||||
|
||||
// Extract task priority from labels
|
||||
priority := "normal"
|
||||
for _, label := range issue.Labels {
|
||||
switch strings.ToLower(label.Name) {
|
||||
case "priority:high", "high-priority", "urgent":
|
||||
priority = "high"
|
||||
case "priority:low", "low-priority":
|
||||
priority = "low"
|
||||
case "priority:critical", "critical":
|
||||
priority = "critical"
|
||||
}
|
||||
}
|
||||
taskInfo["priority"] = priority
|
||||
|
||||
// Extract task type from labels
|
||||
taskType := "general"
|
||||
for _, label := range issue.Labels {
|
||||
switch strings.ToLower(label.Name) {
|
||||
case "type:bug", "bug":
|
||||
taskType = "bug"
|
||||
case "type:feature", "feature", "enhancement":
|
||||
taskType = "feature"
|
||||
case "type:docs", "documentation":
|
||||
taskType = "documentation"
|
||||
case "type:refactor", "refactoring":
|
||||
taskType = "refactor"
|
||||
case "type:test", "testing":
|
||||
taskType = "test"
|
||||
}
|
||||
}
|
||||
taskInfo["task_type"] = taskType
|
||||
|
||||
return taskInfo
|
||||
}
|
||||
|
||||
type WebhookEvent struct {
|
||||
Type string `json:"type"`
|
||||
Action string `json:"action"`
|
||||
Repository string `json:"repository"`
|
||||
Issue *Issue `json:"issue,omitempty"`
|
||||
TaskInfo map[string]interface{} `json:"task_info,omitempty"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
}
|
||||
|
||||
func (h *WebhookHandler) ProcessWebhook(payload *WebhookPayload) *WebhookEvent {
|
||||
event := &WebhookEvent{
|
||||
Type: "gitea_webhook",
|
||||
Action: payload.Action,
|
||||
Repository: payload.Repository.FullName,
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
|
||||
if payload.Issue != nil {
|
||||
event.Issue = payload.Issue
|
||||
|
||||
// Check if this is a task issue
|
||||
if h.IsTaskIssue(payload.Issue) {
|
||||
event.TaskInfo = h.ExtractTaskInfo(payload.Issue)
|
||||
|
||||
log.Info().
|
||||
Str("action", payload.Action).
|
||||
Str("repository", payload.Repository.FullName).
|
||||
Int("issue_number", payload.Issue.Number).
|
||||
Str("title", payload.Issue.Title).
|
||||
Msg("Processing task issue webhook")
|
||||
}
|
||||
}
|
||||
|
||||
return event
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user