fix(linear): state_map validation for deferred, pinned, hooked, custom (GH#3754) (#3772)

This commit is contained in:
Kevin Glynn 2026-07-07 01:11:02 -04:00 • committed by GitHub
commit 0a056e69d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 111 additions and 6 deletions

View file

@ -153,6 +153,10 @@ type MappingConfig struct {
// RelationMap maps Linear relation types to Beads dependency types.
// Key is Linear relation type, value is Beads dependency type.
RelationMap map[string]string
// CustomStatuses holds typed entries from status.custom (beads config).
// Used for push-time state_map validation and matching non-built-in statuses.
CustomStatuses []types.CustomStatus
}
// DefaultMappingConfig returns sensible default mappings.
@ -231,6 +235,16 @@ func LoadMappingConfig(loader ConfigLoader) *MappingConfig {
}
for key, value := range allConfig {
if key == "status.custom" {
value = strings.TrimSpace(value)
if value != "" {
if custom, err := types.ParseCustomStatusConfig(value); err == nil {
config.CustomStatuses = custom
}
}
continue
}
// Parse priority mappings: linear.priority_map.<linear_priority>
if strings.HasPrefix(key, "linear.priority_map.") {
linearPriority := strings.TrimPrefix(key, "linear.priority_map.")
@ -338,7 +352,14 @@ func stateMapMatchesStatus(mapped string, status types.Status) bool {
if normalizedMapped == normalizedStatus {
return true
}
if status.IsValid() && ParseBeadsStatus(mapped) == status {
parsed := ParseBeadsStatus(mapped)
if parsed == status {
// ParseBeadsStatus returns StatusOpen for unrecognized strings; do not
// treat those as matching built-in open (avoids false "ambiguous mapping"
// when state_map values are custom status names like "review").
if parsed == types.StatusOpen && normalizedMapped != "open" {
return false
}
return true
}
return false

View file

@ -613,6 +613,7 @@ func TestLoadMappingConfig(t *testing.T) {
"linear.state_map.custom": "in_progress",
"linear.label_type_map.story": "feature",
"linear.relation_map.parent": "parent-child",
"status.custom": "review:active",
},
}
@ -645,6 +646,10 @@ func TestLoadMappingConfig(t *testing.T) {
if config.StateMap["started"] != "in_progress" {
t.Errorf("StateMap[started] = %s, want in_progress (default preserved)", config.StateMap["started"])
}
if len(config.CustomStatuses) != 1 || config.CustomStatuses[0].Name != "review" {
t.Fatalf("CustomStatuses = %#v, want one entry named review", config.CustomStatuses)
}
}
func TestLoadMappingConfigDottedStateMapResolvesPushByName(t *testing.T) {
@ -900,6 +905,54 @@ func TestLoadMappingConfigParsesOutboundStateMap(t *testing.T) {
}
}
// Regression GH#3754: explicit state_map values that are custom status names
// must not parse as StatusOpen and create phantom matches for beads "open".
func TestResolveStateIDForBeadsStatusCustomValueDoesNotFalseMatchOpen(t *testing.T) {
cache := &StateCache{
States: []State{
{ID: "state-todo", Name: "Todo", Type: "unstarted"},
{ID: "state-review", Name: "In Review", Type: "unstarted"},
},
}
config := DefaultMappingConfig()
config.ExplicitStateMap["todo"] = "open"
config.ExplicitStateMap["in review"] = "review"
got, err := ResolveStateIDForBeadsStatus(cache, types.StatusOpen, config)
if err != nil {
t.Fatalf("ResolveStateIDForBeadsStatus(open) error = %v", err)
}
if got != "state-todo" {
t.Fatalf("ResolveStateIDForBeadsStatus(open) = %q, want state-todo", got)
}
got2, err := ResolveStateIDForBeadsStatus(cache, types.Status("review"), config)
if err != nil {
t.Fatalf("ResolveStateIDForBeadsStatus(review) error = %v", err)
}
if got2 != "state-review" {
t.Fatalf("ResolveStateIDForBeadsStatus(review) = %q, want state-review", got2)
}
}
func TestResolveStateIDForBeadsStatusDeferredExplicitMapping(t *testing.T) {
cache := &StateCache{
States: []State{
{ID: "state-icebox", Name: "Icebox", Type: "unstarted"},
},
}
config := DefaultMappingConfig()
config.ExplicitStateMap["icebox"] = "deferred"
got, err := ResolveStateIDForBeadsStatus(cache, types.StatusDeferred, config)
if err != nil {
t.Fatalf("ResolveStateIDForBeadsStatus() error = %v", err)
}
if got != "state-icebox" {
t.Fatalf("ResolveStateIDForBeadsStatus() = %q, want state-icebox", got)
}
}
func TestPushFieldsEqualIgnoresLocalOnlyDifferences(t *testing.T) {
config := DefaultMappingConfig()
local := &types.Issue{

View file

@ -527,12 +527,37 @@ func (t *Tracker) BuildExternalRef(issue *tracker.TrackerIssue) string {
return fmt.Sprintf("https://linear.app/issue/%s", issue.Identifier)
}
func skipOptionalPushStateMapping(status types.Status, err error, custom []types.CustomStatus) bool {
if !strings.Contains(err.Error(), "has no configured Linear state") {
return false
}
switch status {
case types.StatusBlocked, types.StatusDeferred, types.StatusPinned, types.StatusHooked:
return true
}
for _, cs := range custom {
if types.Status(cs.Name) == status {
return true
}
}
return false
}
// ValidatePushStateMappings ensures push has explicit, non-ambiguous status
// mappings for every configured team before any mutation occurs.
func (t *Tracker) ValidatePushStateMappings(ctx context.Context) error {
if t.config == nil || len(t.config.ExplicitStateMap) == 0 {
return fmt.Errorf("%s", missingExplicitStateMapMessage)
}
statuses := []types.Status{
types.StatusOpen,
types.StatusInProgress,
types.StatusBlocked,
types.StatusClosed,
types.StatusDeferred,
types.StatusPinned,
types.StatusHooked,
}
for _, teamID := range t.teamIDs {
client := t.clients[teamID]
if client == nil {
@ -542,12 +567,18 @@ func (t *Tracker) ValidatePushStateMappings(ctx context.Context) error {
if err != nil {
return fmt.Errorf("fetching workflow states for team %s: %w", teamID, err)
}
for _, status := range []types.Status{types.StatusOpen, types.StatusInProgress, types.StatusBlocked, types.StatusClosed} {
for _, status := range statuses {
if _, err := ResolveStateIDForBeadsStatus(cache, status, t.config); err != nil {
// Only fail for statuses the config explicitly tries to map or when
// mappings are entirely absent. Missing blocked mappings are allowed
// until a blocked issue is actually pushed.
if status == types.StatusBlocked && strings.Contains(err.Error(), "has no configured Linear state") {
if skipOptionalPushStateMapping(status, err, t.config.CustomStatuses) {
continue
}
return err
}
}
for _, cs := range t.config.CustomStatuses {
st := types.Status(cs.Name)
if _, err := ResolveStateIDForBeadsStatus(cache, st, t.config); err != nil {
if skipOptionalPushStateMapping(st, err, t.config.CustomStatuses) {
continue
}
return err