* fix: recognize ado:NNNNN shorthand in IsExternalRef and ExtractIdentifier IsExternalRef and ExtractIdentifier only matched full ADO URLs (/_workitems/edit/\d+), but BuildExternalRef produces ado:NNNNN shorthand when org/project config is unavailable. This caused the sync engine to treat existing beads as new, creating duplicate ADO work items instead of updating existing ones. Fixes harry-miller-trimble/beads#28 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: add shorthand ref support to GitHub/GitLab trackers, tighten ado:0 Address PR #2966 review feedback: - GitHub tracker: add ghShorthandPattern (github:{id}) to IsExternalRef and ExtractIdentifier, fixing the same duplicate-creation bug as ADO - GitLab tracker: add glShorthandPattern (gitlab:{id}) to IsExternalRef and ExtractIdentifier, fixing the same duplicate-creation bug as ADO - ADO tracker: tighten regex from ^ado:(\d+)$ to ^ado:([1-9]\d*)$ to reject ado:0 which is invalid in practice - Add test cases for shorthand format in all three trackers Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
279 lines
7.4 KiB
Go
279 lines
7.4 KiB
Go
package gitlab
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/tracker"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
)
|
|
|
|
func init() {
|
|
tracker.Register("gitlab", func() tracker.IssueTracker {
|
|
return &Tracker{}
|
|
})
|
|
}
|
|
|
|
// issueIIDPattern matches GitLab issue URLs: .../issues/42
|
|
var issueIIDPattern = regexp.MustCompile(`/issues/(\d+)`)
|
|
|
|
// glShorthandPattern matches the "gitlab:{digits}" shorthand produced by BuildExternalRef
|
|
// when a full URL is unavailable.
|
|
var glShorthandPattern = regexp.MustCompile(`^gitlab:([1-9]\d*)$`)
|
|
|
|
// Tracker implements tracker.IssueTracker for GitLab.
|
|
type Tracker struct {
|
|
client *Client
|
|
config *MappingConfig
|
|
store storage.Storage
|
|
filter *IssueFilter // Optional filters for issue fetching
|
|
}
|
|
|
|
func (t *Tracker) Name() string { return "gitlab" }
|
|
func (t *Tracker) DisplayName() string { return "GitLab" }
|
|
func (t *Tracker) ConfigPrefix() string { return "gitlab" }
|
|
|
|
func (t *Tracker) Init(ctx context.Context, store storage.Storage) error {
|
|
t.store = store
|
|
|
|
token, err := t.getConfig(ctx, "gitlab.token", "GITLAB_TOKEN")
|
|
if err != nil || token == "" {
|
|
return fmt.Errorf("GitLab token not configured (set gitlab.token or GITLAB_TOKEN)")
|
|
}
|
|
|
|
baseURL, _ := t.getConfig(ctx, "gitlab.url", "GITLAB_URL")
|
|
if baseURL == "" {
|
|
baseURL = "https://gitlab.com"
|
|
}
|
|
|
|
projectID, _ := t.getConfig(ctx, "gitlab.project_id", "GITLAB_PROJECT_ID")
|
|
groupID, _ := t.getConfig(ctx, "gitlab.group_id", "GITLAB_GROUP_ID")
|
|
defaultProjectID, _ := t.getConfig(ctx, "gitlab.default_project_id", "GITLAB_DEFAULT_PROJECT_ID")
|
|
|
|
// When group_id is set, default_project_id is used for creating issues.
|
|
// When group_id is not set, project_id is required.
|
|
if groupID == "" && projectID == "" {
|
|
return fmt.Errorf("GitLab project ID not configured (set gitlab.project_id or GITLAB_PROJECT_ID)")
|
|
}
|
|
|
|
// For group mode, use default_project_id as the project for creating issues.
|
|
// If default_project_id is not set, fall back to project_id.
|
|
if groupID != "" && projectID == "" {
|
|
if defaultProjectID != "" {
|
|
projectID = defaultProjectID
|
|
}
|
|
}
|
|
|
|
t.client = NewClient(token, baseURL, projectID)
|
|
if groupID != "" {
|
|
t.client = t.client.WithGroupID(groupID)
|
|
}
|
|
t.config = DefaultMappingConfig()
|
|
|
|
// Load optional filter config
|
|
t.filter = t.loadFilterConfig(ctx)
|
|
|
|
return nil
|
|
}
|
|
|
|
// loadFilterConfig reads filter configuration from store/env.
|
|
// Returns nil if no filters are configured.
|
|
func (t *Tracker) loadFilterConfig(ctx context.Context) *IssueFilter {
|
|
labels, _ := t.getConfig(ctx, "gitlab.filter_labels", "GITLAB_FILTER_LABELS")
|
|
projectStr, _ := t.getConfig(ctx, "gitlab.filter_project", "GITLAB_FILTER_PROJECT")
|
|
milestone, _ := t.getConfig(ctx, "gitlab.filter_milestone", "GITLAB_FILTER_MILESTONE")
|
|
assignee, _ := t.getConfig(ctx, "gitlab.filter_assignee", "GITLAB_FILTER_ASSIGNEE")
|
|
|
|
if labels == "" && projectStr == "" && milestone == "" && assignee == "" {
|
|
return nil
|
|
}
|
|
|
|
filter := &IssueFilter{
|
|
Labels: labels,
|
|
Milestone: milestone,
|
|
Assignee: assignee,
|
|
}
|
|
if projectStr != "" {
|
|
if pid, err := strconv.Atoi(projectStr); err == nil {
|
|
filter.ProjectID = pid
|
|
}
|
|
}
|
|
return filter
|
|
}
|
|
|
|
// SetFilter overrides the tracker's issue filter.
|
|
// CLI flags use this to override config-based defaults.
|
|
func (t *Tracker) SetFilter(filter *IssueFilter) {
|
|
t.filter = filter
|
|
}
|
|
|
|
func (t *Tracker) Validate() error {
|
|
if t.client == nil {
|
|
return fmt.Errorf("GitLab tracker not initialized")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (t *Tracker) Close() error { return nil }
|
|
|
|
func (t *Tracker) FetchIssues(ctx context.Context, opts tracker.FetchOptions) ([]tracker.TrackerIssue, error) {
|
|
var issues []Issue
|
|
var err error
|
|
|
|
state := opts.State
|
|
if state == "" {
|
|
state = "all"
|
|
}
|
|
// GitLab uses "opened" not "open"
|
|
if state == "open" {
|
|
state = "opened"
|
|
}
|
|
|
|
if opts.Since != nil {
|
|
issues, err = t.client.FetchIssuesSince(ctx, state, *opts.Since, t.filter)
|
|
} else {
|
|
issues, err = t.client.FetchIssues(ctx, state, t.filter)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := make([]tracker.TrackerIssue, 0, len(issues))
|
|
for _, gl := range issues {
|
|
result = append(result, gitlabToTrackerIssue(&gl))
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (t *Tracker) FetchIssue(ctx context.Context, identifier string) (*tracker.TrackerIssue, error) {
|
|
iid, err := strconv.Atoi(identifier)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid GitLab IID %q: %w", identifier, err)
|
|
}
|
|
|
|
gl, err := t.client.FetchIssueByIID(ctx, iid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if gl == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
ti := gitlabToTrackerIssue(gl)
|
|
return &ti, nil
|
|
}
|
|
|
|
func (t *Tracker) CreateIssue(ctx context.Context, issue *types.Issue) (*tracker.TrackerIssue, error) {
|
|
fields := BeadsIssueToGitLabFields(issue, t.config)
|
|
labels, _ := fields["labels"].([]string)
|
|
|
|
created, err := t.client.CreateIssue(ctx, issue.Title, issue.Description, labels)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ti := gitlabToTrackerIssue(created)
|
|
return &ti, nil
|
|
}
|
|
|
|
func (t *Tracker) UpdateIssue(ctx context.Context, externalID string, issue *types.Issue) (*tracker.TrackerIssue, error) {
|
|
iid, err := strconv.Atoi(externalID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid GitLab IID %q: %w", externalID, err)
|
|
}
|
|
|
|
updates := BeadsIssueToGitLabFields(issue, t.config)
|
|
updated, err := t.client.UpdateIssue(ctx, iid, updates)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ti := gitlabToTrackerIssue(updated)
|
|
return &ti, nil
|
|
}
|
|
|
|
func (t *Tracker) FieldMapper() tracker.FieldMapper {
|
|
return &gitlabFieldMapper{config: t.config}
|
|
}
|
|
|
|
// IsExternalRef checks if a ref belongs to this GitLab tracker.
|
|
// It recognizes both full GitLab URLs and the "gitlab:{id}" shorthand format
|
|
// produced by BuildExternalRef when a URL is unavailable.
|
|
func (t *Tracker) IsExternalRef(ref string) bool {
|
|
if glShorthandPattern.MatchString(ref) {
|
|
return true
|
|
}
|
|
return strings.Contains(ref, "gitlab") && issueIIDPattern.MatchString(ref)
|
|
}
|
|
|
|
// ExtractIdentifier extracts the issue IID from a GitLab URL or shorthand ref.
|
|
func (t *Tracker) ExtractIdentifier(ref string) string {
|
|
if m := glShorthandPattern.FindStringSubmatch(ref); len(m) >= 2 {
|
|
return m[1]
|
|
}
|
|
matches := issueIIDPattern.FindStringSubmatch(ref)
|
|
if len(matches) < 2 {
|
|
return ""
|
|
}
|
|
return matches[1]
|
|
}
|
|
|
|
func (t *Tracker) BuildExternalRef(issue *tracker.TrackerIssue) string {
|
|
if issue.URL != "" {
|
|
return issue.URL
|
|
}
|
|
return fmt.Sprintf("gitlab:%s", issue.Identifier)
|
|
}
|
|
|
|
// getConfig reads a config value from storage, falling back to env var.
|
|
func (t *Tracker) getConfig(ctx context.Context, key, envVar string) (string, error) {
|
|
val, err := t.store.GetConfig(ctx, key)
|
|
if err == nil && val != "" {
|
|
return val, nil
|
|
}
|
|
if envVar != "" {
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
return envVal, nil
|
|
}
|
|
}
|
|
return "", nil
|
|
}
|
|
|
|
// gitlabToTrackerIssue converts a gitlab.Issue to a tracker.TrackerIssue.
|
|
func gitlabToTrackerIssue(gl *Issue) tracker.TrackerIssue {
|
|
ti := tracker.TrackerIssue{
|
|
ID: strconv.Itoa(gl.ID),
|
|
Identifier: strconv.Itoa(gl.IID),
|
|
URL: gl.WebURL,
|
|
Title: gl.Title,
|
|
Description: gl.Description,
|
|
Labels: gl.Labels,
|
|
Raw: gl,
|
|
}
|
|
|
|
if gl.State != "" {
|
|
ti.State = gl.State
|
|
}
|
|
|
|
if gl.Assignee != nil {
|
|
ti.Assignee = gl.Assignee.Username
|
|
ti.AssigneeID = strconv.Itoa(gl.Assignee.ID)
|
|
}
|
|
|
|
if gl.CreatedAt != nil {
|
|
ti.CreatedAt = *gl.CreatedAt
|
|
}
|
|
if gl.UpdatedAt != nil {
|
|
ti.UpdatedAt = *gl.UpdatedAt
|
|
}
|
|
if gl.ClosedAt != nil {
|
|
ti.CompletedAt = gl.ClosedAt
|
|
}
|
|
|
|
return ti
|
|
}
|