beads/internal/gitlab/tracker_test.go
Harry 99653e0597
fix(trackers): route secret keys through config.yaml in all tracker getConfig methods
Follow-up to #3929 (GitHub tracker fix by @sealocal). The same bug
existed in jira, gitlab, ado, and notion trackers: their getConfig()
methods read every key from the Dolt store, but secret keys (API
tokens, PATs) should never be in the database because it gets pushed
to remotes, triggering secret-scanning blocks.

Changes:
- Add jira.api_token, gitlab.token, ado.pat to the yaml-only key
  list so they are stored in config.yaml (not the Dolt database),
  consistent with github.token and linear.api_key.
- Add IsYamlOnlyKey guard to getConfig() in all four trackers,
  mirroring the pattern in linear/tracker.go and the merged GitHub
  tracker fix.
- Add regression tests (nil-store bypass + yaml-read) for jira,
  gitlab, and ado; defensive yaml-only path test for notion.
- Update existing tests that put secret keys in mock stores to use
  t.Setenv instead, matching the new yaml-only behavior.

Co-Authored-By: sealocal <4951910+sealocal@users.noreply.github.com>
2026-05-15 09:07:12 -07:00

229 lines
6.6 KiB
Go

package gitlab
import (
"context"
"os"
"path/filepath"
"strconv"
"testing"
"time"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/tracker"
"github.com/steveyegge/beads/internal/types"
)
func TestRegistered(t *testing.T) {
factory := tracker.Get("gitlab")
if factory == nil {
t.Fatal("gitlab tracker not registered")
}
tr := factory()
if tr.Name() != "gitlab" {
t.Errorf("Name() = %q, want %q", tr.Name(), "gitlab")
}
if tr.DisplayName() != "GitLab" {
t.Errorf("DisplayName() = %q, want %q", tr.DisplayName(), "GitLab")
}
if tr.ConfigPrefix() != "gitlab" {
t.Errorf("ConfigPrefix() = %q, want %q", tr.ConfigPrefix(), "gitlab")
}
}
func TestIsExternalRef(t *testing.T) {
tr := &Tracker{}
tests := []struct {
ref string
want bool
}{
{"https://gitlab.com/group/project/-/issues/42", true},
{"https://my-gitlab.example.com/team/repo/-/issues/123", true},
{"https://gitlab.com/group/project/-/work_items/42", true},
{"https://linear.app/team/issue/PROJ-123", false},
{"https://github.com/org/repo/issues/1", false},
{"", false},
// Shorthand format produced by BuildExternalRef
{"gitlab:681509", true},
{"gitlab:1", true},
{"gitlab:abc", false},
{"gitlab:123/extra", false},
{"gitlab:0", false},
}
for _, tt := range tests {
if got := tr.IsExternalRef(tt.ref); got != tt.want {
t.Errorf("IsExternalRef(%q) = %v, want %v", tt.ref, got, tt.want)
}
}
}
func TestExtractIdentifier(t *testing.T) {
tr := &Tracker{}
tests := []struct {
ref string
want string
}{
{"https://gitlab.com/group/project/-/issues/42", "42"},
{"https://gitlab.example.com/team/repo/-/issues/123", "123"},
{"https://gitlab.com/group/project/-/work_items/42", "42"},
{"not-a-url", ""},
// Shorthand format
{"gitlab:681509", "681509"},
{"gitlab:1", "1"},
{"gitlab:abc", ""},
}
for _, tt := range tests {
if got := tr.ExtractIdentifier(tt.ref); got != tt.want {
t.Errorf("ExtractIdentifier(%q) = %q, want %q", tt.ref, got, tt.want)
}
}
}
func TestBuildExternalRef(t *testing.T) {
tr := &Tracker{}
ti := &tracker.TrackerIssue{
URL: "https://gitlab.com/group/project/-/issues/42",
Identifier: "42",
}
ref := tr.BuildExternalRef(ti)
if ref != ti.URL {
t.Errorf("BuildExternalRef() = %q, want %q", ref, ti.URL)
}
}
func TestFieldMapperStatus(t *testing.T) {
m := &gitlabFieldMapper{config: DefaultMappingConfig()}
if got := m.StatusToBeads("opened"); got != types.StatusOpen {
t.Errorf("StatusToBeads(opened) = %q, want %q", got, types.StatusOpen)
}
if got := m.StatusToBeads("closed"); got != types.StatusClosed {
t.Errorf("StatusToBeads(closed) = %q, want %q", got, types.StatusClosed)
}
if got := m.StatusToBeads("reopened"); got != types.StatusOpen {
t.Errorf("StatusToBeads(reopened) = %q, want %q", got, types.StatusOpen)
}
}
func TestFieldMapperPriority(t *testing.T) {
m := &gitlabFieldMapper{config: DefaultMappingConfig()}
if got := m.PriorityToBeads("critical"); got != 0 {
t.Errorf("PriorityToBeads(critical) = %d, want 0", got)
}
if got := m.PriorityToBeads("high"); got != 1 {
t.Errorf("PriorityToBeads(high) = %d, want 1", got)
}
if got := m.PriorityToBeads("low"); got != 3 {
t.Errorf("PriorityToBeads(low) = %d, want 3", got)
}
}
func TestGitLabToTrackerIssue(t *testing.T) {
now := time.Now()
gl := &Issue{
ID: 100,
IID: 42,
Title: "Fix pipeline",
Description: "CI is broken",
State: "opened",
WebURL: "https://gitlab.com/group/project/-/issues/42",
Labels: []string{"bug", "priority::high"},
CreatedAt: &now,
UpdatedAt: &now,
Assignee: &User{ID: 5, Username: "bob"},
}
ti := gitlabToTrackerIssue(gl)
if ti.ID != "100" {
t.Errorf("ID = %q, want %q", ti.ID, "100")
}
if ti.Identifier != "42" {
t.Errorf("Identifier = %q, want %q", ti.Identifier, "42")
}
if ti.Assignee != "bob" {
t.Errorf("Assignee = %q, want %q", ti.Assignee, "bob")
}
if ti.AssigneeID != strconv.Itoa(5) {
t.Errorf("AssigneeID = %q, want %q", ti.AssigneeID, "5")
}
if ti.Raw != gl {
t.Error("Raw should reference original gitlab.Issue")
}
if len(ti.Labels) != 2 {
t.Errorf("Labels count = %d, want 2", len(ti.Labels))
}
}
// TestGetConfig_YamlOnlyKeyBypassesStore verifies that yaml-only keys
// (e.g. gitlab.token) bypass the Dolt store entirely. A nil store proves
// the store is never dereferenced; before the fix this would panic.
func TestGetConfig_YamlOnlyKeyBypassesStore(t *testing.T) {
ctx := context.Background()
tr := &Tracker{store: nil}
t.Run("falls back to env var", func(t *testing.T) {
t.Setenv("GITLAB_TOKEN", "env-token-value")
got, err := tr.getConfig(ctx, "gitlab.token", "GITLAB_TOKEN")
if err != nil {
t.Fatalf("getConfig returned error: %v", err)
}
if got != "env-token-value" {
t.Errorf("getConfig(gitlab.token) = %q, want %q", got, "env-token-value")
}
})
t.Run("returns empty when no value is set", func(t *testing.T) {
t.Setenv("GITLAB_TOKEN", "")
got, err := tr.getConfig(ctx, "gitlab.token", "GITLAB_TOKEN")
if err != nil {
t.Fatalf("getConfig returned error: %v", err)
}
if got != "" {
t.Errorf("getConfig(gitlab.token) = %q, want empty", got)
}
})
}
// TestGetConfig_YamlOnlyKeyReadsFromYaml verifies that gitlab.token is
// read from .beads/config.yaml when set there, without depending on the
// GITLAB_TOKEN env var.
func TestGetConfig_YamlOnlyKeyReadsFromYaml(t *testing.T) {
const wantToken = "yaml-config-token-value"
tmpDir := t.TempDir()
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
t.Fatalf("mkdir .beads: %v", err)
}
yamlBody := "gitlab.token: \"" + wantToken + "\"\n"
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(yamlBody), 0o600); err != nil {
t.Fatalf("write config.yaml: %v", err)
}
t.Setenv("GITLAB_TOKEN", "")
t.Setenv("BEADS_DIR", "")
t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1")
t.Setenv("HOME", filepath.Join(tmpDir, "home"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(tmpDir, "xdg"))
t.Chdir(tmpDir)
config.ResetForTesting()
t.Cleanup(config.ResetForTesting)
if err := config.Initialize(); err != nil {
t.Fatalf("config.Initialize: %v", err)
}
if got := config.GetString("gitlab.token"); got != wantToken {
t.Fatalf("config.GetString(gitlab.token) = %q, want %q (yaml not loaded?)", got, wantToken)
}
tr := &Tracker{store: nil}
got, err := tr.getConfig(context.Background(), "gitlab.token", "GITLAB_TOKEN")
if err != nil {
t.Fatalf("getConfig returned error: %v", err)
}
if got != wantToken {
t.Errorf("getConfig(gitlab.token) = %q, want %q (yaml value)", got, wantToken)
}
}