ResolveTaskProjectOrPlaybook resolves a ref that could be a task, project, or playbook. Supports task/, project/, playbook/ prefixes. On bare refs, tries each kind; errors on ambiguity (slug exists in 2+ tables).
(db *sql.DB, ref string, includeArchived bool)
| 78 | // or playbook. Supports task/, project/, playbook/ prefixes. On bare refs, |
| 79 | // tries each kind; errors on ambiguity (slug exists in 2+ tables). |
| 80 | func ResolveTaskProjectOrPlaybook(db *sql.DB, ref string, includeArchived bool) (kind, slug string, err error) { |
| 81 | if strings.HasPrefix(ref, "task/") { |
| 82 | t, err := ResolveTask(db, strings.TrimPrefix(ref, "task/"), includeArchived) |
| 83 | if err != nil { |
| 84 | return "", "", err |
| 85 | } |
| 86 | return "task", t.Slug, nil |
| 87 | } |
| 88 | if strings.HasPrefix(ref, "project/") { |
| 89 | p, err := ResolveProject(db, strings.TrimPrefix(ref, "project/"), includeArchived) |
| 90 | if err != nil { |
| 91 | return "", "", err |
| 92 | } |
| 93 | return "project", p.Slug, nil |
| 94 | } |
| 95 | if strings.HasPrefix(ref, "playbook/") { |
| 96 | pb, err := ResolvePlaybook(db, strings.TrimPrefix(ref, "playbook/"), includeArchived) |
| 97 | if err != nil { |
| 98 | return "", "", err |
| 99 | } |
| 100 | return "playbook", pb.Slug, nil |
| 101 | } |
| 102 | |
| 103 | t, tErr := ResolveTask(db, ref, includeArchived) |
| 104 | p, pErr := ResolveProject(db, ref, includeArchived) |
| 105 | pb, pbErr := ResolvePlaybook(db, ref, includeArchived) |
| 106 | |
| 107 | matches := []string{} |
| 108 | if tErr == nil { |
| 109 | matches = append(matches, "task "+t.Slug) |
| 110 | } |
| 111 | if pErr == nil { |
| 112 | matches = append(matches, "project "+p.Slug) |
| 113 | } |
| 114 | if pbErr == nil { |
| 115 | matches = append(matches, "playbook "+pb.Slug) |
| 116 | } |
| 117 | |
| 118 | switch len(matches) { |
| 119 | case 0: |
| 120 | return "", "", fmt.Errorf("no task, project, or playbook matching %q", ref) |
| 121 | case 1: |
| 122 | switch { |
| 123 | case tErr == nil: |
| 124 | return "task", t.Slug, nil |
| 125 | case pErr == nil: |
| 126 | return "project", p.Slug, nil |
| 127 | default: |
| 128 | return "playbook", pb.Slug, nil |
| 129 | } |
| 130 | default: |
| 131 | return "", "", fmt.Errorf("ambiguous ref %q matches %s; use a prefix like task/, project/, or playbook/", ref, strings.Join(matches, ", ")) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // ResolveTaskOrProject resolves a ref that could be either a task or project. |
| 136 | // Supports optional task/ or project/ prefix. Without prefix, tries both. |
no test coverage detected