2026-02-05 10:36:21 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-02-08 23:15:41 +00:00
|
|
|
forgejosdk "codeberg.org/mvdkleijn/forgejo-sdk/forgejo/v2"
|
|
|
|
|
|
|
|
|
|
"github.com/host-uk/core/pkg/forge"
|
2026-02-05 10:36:21 +00:00
|
|
|
"github.com/host-uk/core/pkg/jobrunner"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TickParentHandler ticks a child checkbox in the parent epic issue body
|
|
|
|
|
// after the child's PR has been merged.
|
2026-02-08 23:15:41 +00:00
|
|
|
type TickParentHandler struct {
|
|
|
|
|
forge *forge.Client
|
|
|
|
|
}
|
2026-02-05 10:36:21 +00:00
|
|
|
|
|
|
|
|
// NewTickParentHandler creates a handler that ticks parent epic checkboxes.
|
2026-02-08 23:15:41 +00:00
|
|
|
func NewTickParentHandler(f *forge.Client) *TickParentHandler {
|
|
|
|
|
return &TickParentHandler{forge: f}
|
2026-02-05 10:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Name returns the handler identifier.
|
|
|
|
|
func (h *TickParentHandler) Name() string {
|
|
|
|
|
return "tick_parent"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Match returns true when the child PR has been merged.
|
|
|
|
|
func (h *TickParentHandler) Match(signal *jobrunner.PipelineSignal) bool {
|
|
|
|
|
return signal.PRState == "MERGED"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Execute fetches the epic body, replaces the unchecked checkbox for the
|
2026-02-08 23:15:41 +00:00
|
|
|
// child issue with a checked one, updates the epic, and closes the child issue.
|
2026-02-05 10:36:21 +00:00
|
|
|
func (h *TickParentHandler) Execute(ctx context.Context, signal *jobrunner.PipelineSignal) (*jobrunner.ActionResult, error) {
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
|
|
// Fetch the epic issue body.
|
2026-02-08 23:15:41 +00:00
|
|
|
epic, err := h.forge.GetIssue(signal.RepoOwner, signal.RepoName, int64(signal.EpicNumber))
|
2026-02-05 10:36:21 +00:00
|
|
|
if err != nil {
|
2026-02-08 23:15:41 +00:00
|
|
|
return nil, fmt.Errorf("tick_parent: fetch epic: %w", err)
|
2026-02-05 10:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-08 23:15:41 +00:00
|
|
|
oldBody := epic.Body
|
2026-02-05 10:36:21 +00:00
|
|
|
unchecked := fmt.Sprintf("- [ ] #%d", signal.ChildNumber)
|
|
|
|
|
checked := fmt.Sprintf("- [x] #%d", signal.ChildNumber)
|
|
|
|
|
|
|
|
|
|
if !strings.Contains(oldBody, unchecked) {
|
|
|
|
|
// Already ticked or not found -- nothing to do.
|
|
|
|
|
return &jobrunner.ActionResult{
|
|
|
|
|
Action: "tick_parent",
|
|
|
|
|
RepoOwner: signal.RepoOwner,
|
|
|
|
|
RepoName: signal.RepoName,
|
|
|
|
|
PRNumber: signal.PRNumber,
|
|
|
|
|
Success: true,
|
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
|
Duration: time.Since(start),
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newBody := strings.Replace(oldBody, unchecked, checked, 1)
|
|
|
|
|
|
2026-02-08 23:15:41 +00:00
|
|
|
// Update the epic body.
|
|
|
|
|
_, err = h.forge.EditIssue(signal.RepoOwner, signal.RepoName, int64(signal.EpicNumber), forgejosdk.EditIssueOption{
|
|
|
|
|
Body: &newBody,
|
|
|
|
|
})
|
2026-02-05 10:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return &jobrunner.ActionResult{
|
|
|
|
|
Action: "tick_parent",
|
|
|
|
|
RepoOwner: signal.RepoOwner,
|
|
|
|
|
RepoName: signal.RepoName,
|
|
|
|
|
PRNumber: signal.PRNumber,
|
2026-02-08 23:15:41 +00:00
|
|
|
Error: fmt.Sprintf("edit epic failed: %v", err),
|
2026-02-05 10:36:21 +00:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
|
Duration: time.Since(start),
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 23:15:41 +00:00
|
|
|
// Close the child issue.
|
|
|
|
|
err = h.forge.CloseIssue(signal.RepoOwner, signal.RepoName, int64(signal.ChildNumber))
|
2026-02-05 10:36:21 +00:00
|
|
|
|
|
|
|
|
result := &jobrunner.ActionResult{
|
|
|
|
|
Action: "tick_parent",
|
|
|
|
|
RepoOwner: signal.RepoOwner,
|
|
|
|
|
RepoName: signal.RepoName,
|
|
|
|
|
PRNumber: signal.PRNumber,
|
|
|
|
|
Success: err == nil,
|
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
|
Duration: time.Since(start),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2026-02-08 23:15:41 +00:00
|
|
|
result.Error = fmt.Sprintf("close child issue failed: %v", err)
|
2026-02-05 10:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|