Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

2.4 KB
Raw Blame
package domain

import (
	"fmt"
	"strings"
	"time"
)

// DefaultWorkBranch names the git work branch for an issue run. Gongfeng issues
// use fix-issue-{iid}; TAPD stories use story-{tapdId}; platform requirements use
// requirement-{number}. The suffix is a local timestamp that keeps branch names
// readable while separating dispatches started in different minutes.
func DefaultWorkBranch(issue Issue, now time.Time) string {
	suffix := now.In(time.Local).Format("20060102-1504")
	source := strings.ToLower(strings.TrimSpace(issue.Source))
	kind := strings.ToLower(strings.TrimSpace(issue.Kind))

	if source == "tapd" || strings.HasPrefix(strings.TrimSpace(issue.ID), "tapd-") {
		if id := tapdStoryID(issue); id != "" {
			return "feature/story-" + SafeGitRefSegment(id) + "-" + suffix
		}
	}
	if source == "gongfeng" || kind == "issue" {
		if n := gongfengIssueNumber(issue); n > 0 {
			return fmt.Sprintf("feature/fix-issue-%d-%s", n, suffix)
		}
	}
	if kind == "requirement" || source == "" || source == "local" {
		if issue.Number > 0 {
			return fmt.Sprintf("feature/requirement-%d-%s", issue.Number, suffix)
		}
	}
	if issue.Number > 0 {
		return fmt.Sprintf("feature/fix-issue-%d-%s", issue.Number, suffix)
	}
	if issue.ID != "" {
		return "feature/" + SafeGitRefSegment(issue.ID) + "-" + suffix
	}
	return "feature/fix-" + suffix
}

func gongfengIssueNumber(issue Issue) int {
	if issue.ExternalRef != nil && issue.ExternalRef.ExternalIID > 0 {
		return issue.ExternalRef.ExternalIID
	}
	return issue.Number
}

func tapdStoryID(issue Issue) string {
	if issue.ExternalRef != nil {
		if id := strings.TrimSpace(issue.ExternalRef.ExternalID); id != "" {
			return id
		}
	}
	return strings.TrimPrefix(strings.TrimSpace(issue.ID), "tapd-")
}

// SafeGitRefSegment turns an arbitrary value into a single git ref segment:
// lowercase letters, digits, dash and underscore, with every other rune
// replaced by a dash and the segment cut at 64 characters. Leading and
// trailing dashes/dots are trimmed; values that sanitize to nothing fall back
// to "run" so callers always get a usable branch name.
func SafeGitRefSegment(value string) string {
	var b strings.Builder
	for _, r := range strings.ToLower(value) {
		if r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '-' || r == '_' {
			b.WriteRune(r)
		} else if b.Len() > 0 {
			b.WriteByte('-')
		}
		if b.Len() >= 64 {
			break
		}
	}
	result := strings.Trim(b.String(), "-.")
	if result == "" {
		return "run"
	}
	return result
}