refactor: wire Jira CLI through SyncEngine, delete duplicated sync code
Replace Python-script-based Jira sync with native Go REST API client. Jira CLI now follows same thin pattern as Linear/GitLab: create tracker, create engine, call engine.Sync(). Completes Phase 4 of dolt-y60t. - Implement Jira REST API v3 client (search, get, create, update) - Add ADF (Atlassian Document Format) plain text conversion - Wire Jira tracker IssueTracker methods with real API calls - Complete jiraFieldMapper.IssueToBeads/IssueToTracker - Delete conflict.go (Engine handles conflicts), scripts.go (Python dependency removed), Jira-specific stat types (Engine types used) - Fix engine pull guard: skip locally-modified issues unconditionally, let Phase 2 conflict detection handle them Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Executed-By: beads/crew/emma Rig: beads Role: crew
This commit is contained in:
parent
0987367a8d
commit
8f00fb7e76
11 changed files with 952 additions and 892 deletions
646
cmd/bd/jira.go
646
cmd/bd/jira.go
|
|
@ -1,19 +1,14 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"cmp"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/steveyegge/beads/internal/jira"
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
|
|
@ -28,7 +23,6 @@ Configuration:
|
|||
bd config set jira.project "PROJ"
|
||||
bd config set jira.api_token "YOUR_TOKEN"
|
||||
bd config set jira.username "your_email@company.com" # For Jira Cloud
|
||||
bd config set jira.pull_prefix "hippo" # Imported issues get hippo-1, hippo-2, etc.
|
||||
bd config set jira.push_prefix "hippo" # Only push hippo-* issues to Jira
|
||||
bd config set jira.push_prefix "proj1,proj2" # Multiple prefixes (comma-separated)
|
||||
|
||||
|
|
@ -64,162 +58,7 @@ Examples:
|
|||
bd jira sync --push --create-only # Push new issues only
|
||||
bd jira sync --dry-run # Preview without changes
|
||||
bd jira sync --prefer-local # Bidirectional, local wins`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Flag errors are unlikely but check one to ensure cobra is working
|
||||
pull, _ := cmd.Flags().GetBool("pull")
|
||||
push, _ := cmd.Flags().GetBool("push")
|
||||
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
||||
preferLocal, _ := cmd.Flags().GetBool("prefer-local")
|
||||
preferJira, _ := cmd.Flags().GetBool("prefer-jira")
|
||||
createOnly, _ := cmd.Flags().GetBool("create-only")
|
||||
updateRefs, _ := cmd.Flags().GetBool("update-refs")
|
||||
state, _ := cmd.Flags().GetString("state")
|
||||
|
||||
// Block writes in readonly mode (sync modifies data)
|
||||
if !dryRun {
|
||||
CheckReadonly("jira sync")
|
||||
}
|
||||
|
||||
// Validate conflicting flags
|
||||
if preferLocal && preferJira {
|
||||
fmt.Fprintf(os.Stderr, "Error: cannot use both --prefer-local and --prefer-jira\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Ensure store is available
|
||||
if err := ensureStoreActive(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: database not available: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Ensure we have Jira configuration
|
||||
if err := validateJiraConfig(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Default mode: bidirectional (pull then push)
|
||||
if !pull && !push {
|
||||
pull = true
|
||||
push = true
|
||||
}
|
||||
|
||||
ctx := rootCtx
|
||||
result := &jira.SyncResult{Success: true}
|
||||
|
||||
// Step 1: Pull from Jira
|
||||
if pull {
|
||||
if dryRun {
|
||||
fmt.Println("→ [DRY RUN] Would pull issues from Jira")
|
||||
} else {
|
||||
fmt.Println("→ Pulling issues from Jira...")
|
||||
}
|
||||
|
||||
pullStats, err := doPullFromJira(ctx, dryRun, state)
|
||||
if err != nil {
|
||||
result.Success = false
|
||||
result.Error = err.Error()
|
||||
if jsonOutput {
|
||||
outputJSON(result)
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error pulling from Jira: %v\n", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
result.Stats.Pulled = pullStats.Created + pullStats.Updated
|
||||
result.Stats.Created += pullStats.Created
|
||||
result.Stats.Updated += pullStats.Updated
|
||||
result.Stats.Skipped += pullStats.Skipped
|
||||
|
||||
if !dryRun {
|
||||
fmt.Printf("✓ Pulled %d issues (%d created, %d updated)\n",
|
||||
result.Stats.Pulled, pullStats.Created, pullStats.Updated)
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2: Handle conflicts (if bidirectional)
|
||||
if pull && push && !dryRun {
|
||||
client := newJiraClient(ctx)
|
||||
conflicts, err := jira.DetectConflicts(ctx, client, store, os.Stderr)
|
||||
if err != nil {
|
||||
result.Warnings = append(result.Warnings, fmt.Sprintf("conflict detection failed: %v", err))
|
||||
} else if len(conflicts) > 0 {
|
||||
result.Stats.Conflicts = len(conflicts)
|
||||
if preferLocal {
|
||||
fmt.Printf("→ Resolving %d conflicts (preferring local)\n", len(conflicts))
|
||||
// Local wins - no action needed, push will overwrite
|
||||
} else if preferJira {
|
||||
fmt.Printf("→ Resolving %d conflicts (preferring Jira)\n", len(conflicts))
|
||||
// Jira wins - re-import conflicting issues
|
||||
if err := jira.ReimportConflicts(conflicts, os.Stderr); err != nil {
|
||||
result.Warnings = append(result.Warnings, fmt.Sprintf("conflict resolution failed: %v", err))
|
||||
}
|
||||
} else {
|
||||
// Default: timestamp-based (newer wins)
|
||||
fmt.Printf("→ Resolving %d conflicts (newer wins)\n", len(conflicts))
|
||||
if err := jira.ResolveConflictsByTimestamp(conflicts, os.Stderr); err != nil {
|
||||
result.Warnings = append(result.Warnings, fmt.Sprintf("conflict resolution failed: %v", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Step 3: Push to Jira
|
||||
if push {
|
||||
if dryRun {
|
||||
fmt.Println("→ [DRY RUN] Would push issues to Jira")
|
||||
} else {
|
||||
fmt.Println("→ Pushing issues to Jira...")
|
||||
}
|
||||
|
||||
pushStats, err := doPushToJira(ctx, dryRun, createOnly, updateRefs)
|
||||
if err != nil {
|
||||
result.Success = false
|
||||
result.Error = err.Error()
|
||||
if jsonOutput {
|
||||
outputJSON(result)
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error pushing to Jira: %v\n", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
result.Stats.Pushed = pushStats.Created + pushStats.Updated
|
||||
result.Stats.Created += pushStats.Created
|
||||
result.Stats.Updated += pushStats.Updated
|
||||
result.Stats.Skipped += pushStats.Skipped
|
||||
result.Stats.Errors += pushStats.Errors
|
||||
|
||||
if !dryRun {
|
||||
fmt.Printf("✓ Pushed %d issues (%d created, %d updated)\n",
|
||||
result.Stats.Pushed, pushStats.Created, pushStats.Updated)
|
||||
}
|
||||
}
|
||||
|
||||
// Update last sync timestamp
|
||||
if !dryRun && result.Success {
|
||||
result.LastSync = time.Now().Format(time.RFC3339)
|
||||
if err := store.SetConfig(ctx, "jira.last_sync", result.LastSync); err != nil {
|
||||
result.Warnings = append(result.Warnings, fmt.Sprintf("failed to update last_sync: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
// Output result
|
||||
if jsonOutput {
|
||||
outputJSON(result)
|
||||
} else if dryRun {
|
||||
fmt.Println("\n✓ Dry run complete (no changes made)")
|
||||
} else {
|
||||
fmt.Println("\n✓ Jira sync complete")
|
||||
if len(result.Warnings) > 0 {
|
||||
fmt.Println("\nWarnings:")
|
||||
for _, w := range result.Warnings {
|
||||
fmt.Printf(" - %s\n", w)
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Run: runJiraSync,
|
||||
}
|
||||
|
||||
var jiraStatusCmd = &cobra.Command{
|
||||
|
|
@ -230,98 +69,16 @@ var jiraStatusCmd = &cobra.Command{
|
|||
- Configuration status
|
||||
- Number of issues with Jira links
|
||||
- Issues pending push (no external_ref)`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx := rootCtx
|
||||
|
||||
// Ensure store is available
|
||||
if err := ensureStoreActive(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Get configuration
|
||||
jiraURL, _ := store.GetConfig(ctx, "jira.url")
|
||||
jiraProject, _ := store.GetConfig(ctx, "jira.project")
|
||||
lastSync, _ := store.GetConfig(ctx, "jira.last_sync")
|
||||
|
||||
// Check if configured
|
||||
configured := jiraURL != "" && jiraProject != ""
|
||||
|
||||
// Count issues with Jira links
|
||||
allIssues, err := store.SearchIssues(ctx, "", types.IssueFilter{})
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
withJiraRef := 0
|
||||
pendingPush := 0
|
||||
for _, issue := range allIssues {
|
||||
if issue.ExternalRef != nil && jira.IsJiraExternalRef(*issue.ExternalRef, jiraURL) {
|
||||
withJiraRef++
|
||||
} else if issue.ExternalRef == nil {
|
||||
// Only count issues without any external_ref as pending push
|
||||
pendingPush++
|
||||
}
|
||||
// Issues with non-Jira external_ref are not counted in either category
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
outputJSON(map[string]interface{}{
|
||||
"configured": configured,
|
||||
"jira_url": jiraURL,
|
||||
"jira_project": jiraProject,
|
||||
"last_sync": lastSync,
|
||||
"total_issues": len(allIssues),
|
||||
"with_jira_ref": withJiraRef,
|
||||
"pending_push": pendingPush,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Jira Sync Status")
|
||||
fmt.Println("================")
|
||||
fmt.Println()
|
||||
|
||||
if !configured {
|
||||
fmt.Println("Status: Not configured")
|
||||
fmt.Println()
|
||||
fmt.Println("To configure Jira integration:")
|
||||
fmt.Println(" bd config set jira.url \"https://company.atlassian.net\"")
|
||||
fmt.Println(" bd config set jira.project \"PROJ\"")
|
||||
fmt.Println(" bd config set jira.api_token \"YOUR_TOKEN\"")
|
||||
fmt.Println(" bd config set jira.username \"your@email.com\"")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Jira URL: %s\n", jiraURL)
|
||||
fmt.Printf("Project: %s\n", jiraProject)
|
||||
if lastSync != "" {
|
||||
fmt.Printf("Last Sync: %s\n", lastSync)
|
||||
} else {
|
||||
fmt.Println("Last Sync: Never")
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Printf("Total Issues: %d\n", len(allIssues))
|
||||
fmt.Printf("With Jira: %d\n", withJiraRef)
|
||||
fmt.Printf("Local Only: %d\n", pendingPush)
|
||||
|
||||
if pendingPush > 0 {
|
||||
fmt.Println()
|
||||
fmt.Printf("Run 'bd jira sync --push' to push %d local issue(s) to Jira\n", pendingPush)
|
||||
}
|
||||
},
|
||||
Run: runJiraStatus,
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Sync command flags
|
||||
jiraSyncCmd.Flags().Bool("pull", false, "Pull issues from Jira")
|
||||
jiraSyncCmd.Flags().Bool("push", false, "Push issues to Jira")
|
||||
jiraSyncCmd.Flags().Bool("dry-run", false, "Preview sync without making changes")
|
||||
jiraSyncCmd.Flags().Bool("prefer-local", false, "Prefer local version on conflicts")
|
||||
jiraSyncCmd.Flags().Bool("prefer-jira", false, "Prefer Jira version on conflicts")
|
||||
jiraSyncCmd.Flags().Bool("create-only", false, "Only create new issues, don't update existing")
|
||||
jiraSyncCmd.Flags().Bool("update-refs", true, "Update external_ref after creating Jira issues")
|
||||
jiraSyncCmd.Flags().String("state", "all", "Issue state to sync: open, closed, all")
|
||||
|
||||
jiraCmd.AddCommand(jiraSyncCmd)
|
||||
|
|
@ -329,18 +86,200 @@ func init() {
|
|||
rootCmd.AddCommand(jiraCmd)
|
||||
}
|
||||
|
||||
// newJiraClient creates a Jira client from the current store configuration.
|
||||
func newJiraClient(ctx context.Context) *jira.Client {
|
||||
func runJiraSync(cmd *cobra.Command, args []string) {
|
||||
pull, _ := cmd.Flags().GetBool("pull")
|
||||
push, _ := cmd.Flags().GetBool("push")
|
||||
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
||||
preferLocal, _ := cmd.Flags().GetBool("prefer-local")
|
||||
preferJira, _ := cmd.Flags().GetBool("prefer-jira")
|
||||
createOnly, _ := cmd.Flags().GetBool("create-only")
|
||||
state, _ := cmd.Flags().GetString("state")
|
||||
|
||||
if !dryRun {
|
||||
CheckReadonly("jira sync")
|
||||
}
|
||||
|
||||
if preferLocal && preferJira {
|
||||
fmt.Fprintf(os.Stderr, "Error: cannot use both --prefer-local and --prefer-jira\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := ensureStoreActive(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: database not available: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := validateJiraConfig(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
ctx := rootCtx
|
||||
|
||||
// Create and initialize the Jira tracker
|
||||
jt := &jira.Tracker{}
|
||||
if err := jt.Init(ctx, store); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error initializing Jira tracker: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Create the sync engine
|
||||
engine := tracker.NewEngine(jt, store, actor)
|
||||
engine.OnMessage = func(msg string) { fmt.Println(" " + msg) }
|
||||
engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) }
|
||||
|
||||
// Set up Jira-specific push hooks (prefix filtering)
|
||||
engine.PushHooks = buildJiraPushHooks(ctx)
|
||||
|
||||
// Build sync options from CLI flags
|
||||
opts := tracker.SyncOptions{
|
||||
Pull: pull,
|
||||
Push: push,
|
||||
DryRun: dryRun,
|
||||
CreateOnly: createOnly,
|
||||
State: state,
|
||||
}
|
||||
|
||||
// Map conflict resolution
|
||||
if preferLocal {
|
||||
opts.ConflictResolution = tracker.ConflictLocal
|
||||
} else if preferJira {
|
||||
opts.ConflictResolution = tracker.ConflictExternal
|
||||
} else {
|
||||
opts.ConflictResolution = tracker.ConflictTimestamp
|
||||
}
|
||||
|
||||
// Run sync
|
||||
result, err := engine.Sync(ctx, opts)
|
||||
if err != nil {
|
||||
if jsonOutput {
|
||||
outputJSON(result)
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Output results
|
||||
if jsonOutput {
|
||||
outputJSON(result)
|
||||
} else if dryRun {
|
||||
fmt.Println("\n✓ Dry run complete (no changes made)")
|
||||
} else {
|
||||
if result.Stats.Pulled > 0 {
|
||||
fmt.Printf("✓ Pulled %d issues (%d created, %d updated)\n",
|
||||
result.Stats.Pulled, result.Stats.Created, result.Stats.Updated)
|
||||
}
|
||||
if result.Stats.Pushed > 0 {
|
||||
fmt.Printf("✓ Pushed %d issues\n", result.Stats.Pushed)
|
||||
}
|
||||
if result.Stats.Conflicts > 0 {
|
||||
fmt.Printf("→ Resolved %d conflicts\n", result.Stats.Conflicts)
|
||||
}
|
||||
fmt.Println("\n✓ Jira sync complete")
|
||||
if len(result.Warnings) > 0 {
|
||||
fmt.Println("\nWarnings:")
|
||||
for _, w := range result.Warnings {
|
||||
fmt.Printf(" - %s\n", w)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// buildJiraPushHooks creates PushHooks for Jira-specific push behavior.
|
||||
func buildJiraPushHooks(ctx context.Context) *tracker.PushHooks {
|
||||
return &tracker.PushHooks{
|
||||
ShouldPush: func(issue *types.Issue) bool {
|
||||
pushPrefix, _ := store.GetConfig(ctx, "jira.push_prefix")
|
||||
if pushPrefix == "" {
|
||||
return true
|
||||
}
|
||||
for _, prefix := range strings.Split(pushPrefix, ",") {
|
||||
prefix = strings.TrimSpace(prefix)
|
||||
prefix = strings.TrimSuffix(prefix, "-")
|
||||
if prefix != "" && strings.HasPrefix(issue.ID, prefix+"-") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func runJiraStatus(cmd *cobra.Command, args []string) {
|
||||
ctx := rootCtx
|
||||
|
||||
if err := ensureStoreActive(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
jiraURL, _ := store.GetConfig(ctx, "jira.url")
|
||||
apiToken, _ := store.GetConfig(ctx, "jira.api_token")
|
||||
if apiToken == "" {
|
||||
apiToken = os.Getenv("JIRA_API_TOKEN")
|
||||
jiraProject, _ := store.GetConfig(ctx, "jira.project")
|
||||
lastSync, _ := store.GetConfig(ctx, "jira.last_sync")
|
||||
|
||||
configured := jiraURL != "" && jiraProject != ""
|
||||
|
||||
allIssues, err := store.SearchIssues(ctx, "", types.IssueFilter{})
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
username, _ := store.GetConfig(ctx, "jira.username")
|
||||
if username == "" {
|
||||
username = os.Getenv("JIRA_USERNAME")
|
||||
|
||||
withJiraRef := 0
|
||||
pendingPush := 0
|
||||
for _, issue := range allIssues {
|
||||
if issue.ExternalRef != nil && jira.IsJiraExternalRef(*issue.ExternalRef, jiraURL) {
|
||||
withJiraRef++
|
||||
} else if issue.ExternalRef == nil {
|
||||
pendingPush++
|
||||
}
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
outputJSON(map[string]interface{}{
|
||||
"configured": configured,
|
||||
"jira_url": jiraURL,
|
||||
"jira_project": jiraProject,
|
||||
"last_sync": lastSync,
|
||||
"total_issues": len(allIssues),
|
||||
"with_jira_ref": withJiraRef,
|
||||
"pending_push": pendingPush,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Jira Sync Status")
|
||||
fmt.Println("================")
|
||||
fmt.Println()
|
||||
|
||||
if !configured {
|
||||
fmt.Println("Status: Not configured")
|
||||
fmt.Println()
|
||||
fmt.Println("To configure Jira integration:")
|
||||
fmt.Println(" bd config set jira.url \"https://company.atlassian.net\"")
|
||||
fmt.Println(" bd config set jira.project \"PROJ\"")
|
||||
fmt.Println(" bd config set jira.api_token \"YOUR_TOKEN\"")
|
||||
fmt.Println(" bd config set jira.username \"your@email.com\"")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Jira URL: %s\n", jiraURL)
|
||||
fmt.Printf("Project: %s\n", jiraProject)
|
||||
if lastSync != "" {
|
||||
fmt.Printf("Last Sync: %s\n", lastSync)
|
||||
} else {
|
||||
fmt.Println("Last Sync: Never")
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Printf("Total Issues: %d\n", len(allIssues))
|
||||
fmt.Printf("With Jira: %d\n", withJiraRef)
|
||||
fmt.Printf("Local Only: %d\n", pendingPush)
|
||||
|
||||
if pendingPush > 0 {
|
||||
fmt.Println()
|
||||
fmt.Printf("Run 'bd jira sync --push' to push %d local issue(s) to Jira\n", pendingPush)
|
||||
}
|
||||
return jira.NewClient(jiraURL, username, apiToken)
|
||||
}
|
||||
|
||||
// validateJiraConfig checks that required Jira configuration is present.
|
||||
|
|
@ -360,7 +299,6 @@ func validateJiraConfig() error {
|
|||
return fmt.Errorf("jira.project not configured\nRun: bd config set jira.project \"PROJ\"")
|
||||
}
|
||||
|
||||
// Check for API token (from config or env)
|
||||
apiToken, _ := store.GetConfig(ctx, "jira.api_token")
|
||||
if apiToken == "" && os.Getenv("JIRA_API_TOKEN") == "" {
|
||||
return fmt.Errorf("Jira API token not configured\nRun: bd config set jira.api_token \"YOUR_TOKEN\"\nOr: export JIRA_API_TOKEN=YOUR_TOKEN")
|
||||
|
|
@ -368,199 +306,3 @@ func validateJiraConfig() error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
// doPullFromJira imports issues from Jira using the Python script.
|
||||
func doPullFromJira(ctx context.Context, dryRun bool, state string) (*jira.PullStats, error) {
|
||||
stats := &jira.PullStats{}
|
||||
|
||||
// Find the Python script
|
||||
scriptPath, err := jira.FindScript("jira2jsonl.py")
|
||||
if err != nil {
|
||||
return stats, fmt.Errorf("jira2jsonl.py not found: %w", err)
|
||||
}
|
||||
|
||||
// Build command
|
||||
args := []string{scriptPath, "--from-config"}
|
||||
if state != "" && state != "all" {
|
||||
args = append(args, "--state", state)
|
||||
}
|
||||
|
||||
// Add pull prefix if configured
|
||||
pullPrefix, _ := store.GetConfig(ctx, "jira.pull_prefix")
|
||||
if pullPrefix != "" {
|
||||
args = append(args, "--prefix", pullPrefix)
|
||||
}
|
||||
|
||||
// Run Python script to get JSONL output
|
||||
cmd := exec.CommandContext(ctx, "python3", args...)
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return stats, fmt.Errorf("failed to fetch from Jira: %w", err)
|
||||
}
|
||||
|
||||
if dryRun {
|
||||
// Count issues in output
|
||||
scanner := bufio.NewScanner(strings.NewReader(string(output)))
|
||||
count := 0
|
||||
for scanner.Scan() {
|
||||
if strings.TrimSpace(scanner.Text()) != "" {
|
||||
count++
|
||||
}
|
||||
}
|
||||
fmt.Printf(" Would import %d issues from Jira\n", count)
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// Parse JSONL and import
|
||||
scanner := bufio.NewScanner(strings.NewReader(string(output)))
|
||||
var issues []*types.Issue
|
||||
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
var issue types.Issue
|
||||
if err := json.Unmarshal([]byte(line), &issue); err != nil {
|
||||
return stats, fmt.Errorf("failed to parse issue JSON: %w", err)
|
||||
}
|
||||
issues = append(issues, &issue)
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return stats, fmt.Errorf("failed to read JSONL: %w", err)
|
||||
}
|
||||
|
||||
// Import issues using shared logic
|
||||
opts := ImportOptions{
|
||||
DryRun: false,
|
||||
SkipUpdate: false,
|
||||
}
|
||||
|
||||
result, err := importIssuesCore(ctx, dbPath, store, issues, opts)
|
||||
if err != nil {
|
||||
return stats, fmt.Errorf("import failed: %w", err)
|
||||
}
|
||||
|
||||
stats.Created = result.Created
|
||||
stats.Updated = result.Updated
|
||||
stats.Skipped = result.Skipped
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// doPushToJira exports issues to Jira using the Python script.
|
||||
func doPushToJira(ctx context.Context, dryRun bool, createOnly bool, updateRefs bool) (*jira.PushStats, error) {
|
||||
stats := &jira.PushStats{}
|
||||
|
||||
// Find the Python script
|
||||
scriptPath, err := jira.FindScript("jsonl2jira.py")
|
||||
if err != nil {
|
||||
return stats, fmt.Errorf("jsonl2jira.py not found: %w", err)
|
||||
}
|
||||
|
||||
// Get all issues
|
||||
issues, err := store.SearchIssues(ctx, "", types.IssueFilter{})
|
||||
if err != nil {
|
||||
return stats, fmt.Errorf("failed to get issues: %w", err)
|
||||
}
|
||||
|
||||
// Filter by push prefix if configured
|
||||
pushPrefixConfig, _ := store.GetConfig(ctx, "jira.push_prefix")
|
||||
if pushPrefixConfig != "" {
|
||||
var filteredIssues []*types.Issue
|
||||
|
||||
// Parse comma-separated prefixes, normalize (remove trailing dash)
|
||||
allowedPrefixes := make(map[string]bool)
|
||||
for _, prefix := range strings.Split(pushPrefixConfig, ",") {
|
||||
prefix = strings.TrimSpace(prefix)
|
||||
prefix = strings.TrimSuffix(prefix, "-")
|
||||
if prefix != "" {
|
||||
allowedPrefixes[prefix] = true
|
||||
}
|
||||
}
|
||||
|
||||
for _, issue := range issues {
|
||||
for prefix := range allowedPrefixes {
|
||||
if strings.HasPrefix(issue.ID, prefix+"-") {
|
||||
filteredIssues = append(filteredIssues, issue)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
issues = filteredIssues
|
||||
}
|
||||
|
||||
// Sort by ID for consistent output
|
||||
slices.SortFunc(issues, func(a, b *types.Issue) int {
|
||||
return cmp.Compare(a.ID, b.ID)
|
||||
})
|
||||
|
||||
// Generate JSONL for export
|
||||
var jsonlLines []string
|
||||
for _, issue := range issues {
|
||||
data, err := json.Marshal(issue)
|
||||
if err != nil {
|
||||
return stats, fmt.Errorf("failed to encode issue %s: %w", issue.ID, err)
|
||||
}
|
||||
jsonlLines = append(jsonlLines, string(data))
|
||||
}
|
||||
|
||||
jsonlContent := strings.Join(jsonlLines, "\n")
|
||||
|
||||
// Build command
|
||||
args := []string{scriptPath, "--from-config"}
|
||||
if dryRun {
|
||||
args = append(args, "--dry-run")
|
||||
}
|
||||
if createOnly {
|
||||
args = append(args, "--create-only")
|
||||
}
|
||||
if updateRefs {
|
||||
args = append(args, "--update-refs")
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(ctx, "python3", args...)
|
||||
cmd.Stdin = strings.NewReader(jsonlContent)
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return stats, fmt.Errorf("failed to push to Jira: %w", err)
|
||||
}
|
||||
|
||||
// Parse output for statistics and external_ref updates
|
||||
scanner := bufio.NewScanner(strings.NewReader(string(output)))
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// Parse mapping output: {"bd_id": "...", "jira_key": "...", "external_ref": "..."}
|
||||
var mapping struct {
|
||||
BDID string `json:"bd_id"`
|
||||
JiraKey string `json:"jira_key"`
|
||||
ExternalRef string `json:"external_ref"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(line), &mapping); err == nil && mapping.BDID != "" {
|
||||
stats.Created++
|
||||
|
||||
// Update external_ref if requested
|
||||
if updateRefs && !dryRun && mapping.ExternalRef != "" {
|
||||
updates := map[string]interface{}{
|
||||
"external_ref": mapping.ExternalRef,
|
||||
}
|
||||
if err := store.UpdateIssue(ctx, mapping.BDID, updates, actor); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to update external_ref for %s: %v\n", mapping.BDID, err)
|
||||
stats.Errors++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,12 +3,11 @@ package main
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/jira"
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
)
|
||||
|
||||
func TestJiraSyncStats(t *testing.T) {
|
||||
// Test that stats struct initializes correctly (uses jira.SyncStats now)
|
||||
stats := jira.SyncStats{}
|
||||
stats := tracker.SyncStats{}
|
||||
|
||||
if stats.Pulled != 0 {
|
||||
t.Errorf("expected Pulled to be 0, got %d", stats.Pulled)
|
||||
|
|
@ -34,10 +33,9 @@ func TestJiraSyncStats(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestJiraSyncResult(t *testing.T) {
|
||||
// Test result struct initialization (uses jira.SyncResult now)
|
||||
result := jira.SyncResult{
|
||||
result := tracker.SyncResult{
|
||||
Success: true,
|
||||
Stats: jira.SyncStats{
|
||||
Stats: tracker.SyncStats{
|
||||
Created: 5,
|
||||
Updated: 3,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,16 +1,86 @@
|
|||
package jira
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Issue represents a Jira issue from the REST API.
|
||||
type Issue struct {
|
||||
ID string `json:"id"`
|
||||
Key string `json:"key"`
|
||||
Self string `json:"self"`
|
||||
Fields IssueFields `json:"fields"`
|
||||
}
|
||||
|
||||
// IssueFields contains the fields of a Jira issue.
|
||||
type IssueFields struct {
|
||||
Summary string `json:"summary"`
|
||||
Description json.RawMessage `json:"description"` // ADF (Atlassian Document Format) or plain text
|
||||
Status *StatusField `json:"status"`
|
||||
Priority *PriorityField `json:"priority"`
|
||||
IssueType *IssueTypeField `json:"issuetype"`
|
||||
Project *ProjectField `json:"project"`
|
||||
Assignee *UserField `json:"assignee"`
|
||||
Labels []string `json:"labels"`
|
||||
Created string `json:"created"`
|
||||
Updated string `json:"updated"`
|
||||
Resolution *ResolutionField `json:"resolution"`
|
||||
}
|
||||
|
||||
// StatusField represents a Jira issue status.
|
||||
type StatusField struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// PriorityField represents a Jira issue priority.
|
||||
type PriorityField struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// IssueTypeField represents a Jira issue type.
|
||||
type IssueTypeField struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// ProjectField represents a Jira project.
|
||||
type ProjectField struct {
|
||||
ID string `json:"id"`
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
// UserField represents a Jira user.
|
||||
type UserField struct {
|
||||
AccountID string `json:"accountId"`
|
||||
DisplayName string `json:"displayName"`
|
||||
EmailAddress string `json:"emailAddress"`
|
||||
}
|
||||
|
||||
// ResolutionField represents a Jira resolution.
|
||||
type ResolutionField struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// SearchResult represents a Jira JQL search response.
|
||||
type SearchResult struct {
|
||||
StartAt int `json:"startAt"`
|
||||
MaxResults int `json:"maxResults"`
|
||||
Total int `json:"total"`
|
||||
Issues []Issue `json:"issues"`
|
||||
}
|
||||
|
||||
// Client provides HTTP access to a Jira instance.
|
||||
type Client struct {
|
||||
URL string
|
||||
|
|
@ -35,50 +105,11 @@ func NewClient(url, username, apiToken string) *Client {
|
|||
func (c *Client) FetchIssueTimestamp(ctx context.Context, jiraKey string) (time.Time, error) {
|
||||
var zero time.Time
|
||||
|
||||
if c.URL == "" {
|
||||
return zero, fmt.Errorf("jira URL not configured")
|
||||
}
|
||||
if c.APIToken == "" {
|
||||
return zero, fmt.Errorf("jira API token not configured")
|
||||
}
|
||||
apiURL := fmt.Sprintf("%s/rest/api/3/issue/%s?fields=updated", c.URL, url.PathEscape(jiraKey))
|
||||
|
||||
// Build API URL - use v3 for Jira Cloud (v2 is deprecated)
|
||||
// Only fetch the 'updated' field to minimize response size
|
||||
apiURL := fmt.Sprintf("%s/rest/api/3/issue/%s?fields=updated", c.URL, jiraKey)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", apiURL, nil)
|
||||
body, err := c.doRequest(ctx, "GET", apiURL, nil)
|
||||
if err != nil {
|
||||
return zero, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
// Set authentication header
|
||||
isCloud := strings.Contains(c.URL, "atlassian.net")
|
||||
if isCloud && c.Username != "" {
|
||||
// Jira Cloud: Basic auth with email:api_token
|
||||
auth := base64.StdEncoding.EncodeToString([]byte(c.Username + ":" + c.APIToken))
|
||||
req.Header.Set("Authorization", "Basic "+auth)
|
||||
} else if c.Username != "" {
|
||||
// Jira Server with username: Basic auth
|
||||
auth := base64.StdEncoding.EncodeToString([]byte(c.Username + ":" + c.APIToken))
|
||||
req.Header.Set("Authorization", "Basic "+auth)
|
||||
} else {
|
||||
// Jira Server without username: Bearer token (PAT)
|
||||
req.Header.Set("Authorization", "Bearer "+c.APIToken)
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("User-Agent", "bd-jira-sync/1.0")
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return zero, fmt.Errorf("failed to fetch issue %s: %w", jiraKey, err)
|
||||
}
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return zero, fmt.Errorf("jira API returned %d for issue %s: %s", resp.StatusCode, jiraKey, string(body))
|
||||
return zero, fmt.Errorf("fetch issue %s: %w", jiraKey, err)
|
||||
}
|
||||
|
||||
var result struct {
|
||||
|
|
@ -87,14 +118,269 @@ func (c *Client) FetchIssueTimestamp(ctx context.Context, jiraKey string) (time.
|
|||
} `json:"fields"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return zero, fmt.Errorf("failed to parse Jira response: %w", err)
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return zero, fmt.Errorf("parse Jira response: %w", err)
|
||||
}
|
||||
|
||||
updated, err := ParseTimestamp(result.Fields.Updated)
|
||||
if err != nil {
|
||||
return zero, fmt.Errorf("failed to parse Jira timestamp: %w", err)
|
||||
return zero, fmt.Errorf("parse Jira timestamp: %w", err)
|
||||
}
|
||||
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
// searchFields is the default set of fields to request in search/get queries.
|
||||
const searchFields = "summary,description,status,priority,issuetype,project,assignee,labels,created,updated,resolution"
|
||||
|
||||
// SearchIssues queries Jira using JQL and returns all matching issues, handling pagination.
|
||||
func (c *Client) SearchIssues(ctx context.Context, jql string) ([]Issue, error) {
|
||||
var allIssues []Issue
|
||||
startAt := 0
|
||||
maxResults := 100
|
||||
|
||||
for {
|
||||
params := url.Values{
|
||||
"jql": {jql},
|
||||
"fields": {searchFields},
|
||||
"startAt": {fmt.Sprintf("%d", startAt)},
|
||||
"maxResults": {fmt.Sprintf("%d", maxResults)},
|
||||
}
|
||||
|
||||
apiURL := fmt.Sprintf("%s/rest/api/3/search?%s", c.URL, params.Encode())
|
||||
|
||||
body, err := c.doRequest(ctx, "GET", apiURL, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("search issues: %w", err)
|
||||
}
|
||||
|
||||
var result SearchResult
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse search response: %w", err)
|
||||
}
|
||||
|
||||
allIssues = append(allIssues, result.Issues...)
|
||||
|
||||
if startAt+len(result.Issues) >= result.Total {
|
||||
break
|
||||
}
|
||||
startAt += len(result.Issues)
|
||||
}
|
||||
|
||||
return allIssues, nil
|
||||
}
|
||||
|
||||
// GetIssue fetches a single Jira issue by key (e.g., "PROJ-123").
|
||||
func (c *Client) GetIssue(ctx context.Context, key string) (*Issue, error) {
|
||||
apiURL := fmt.Sprintf("%s/rest/api/3/issue/%s?fields=%s", c.URL, url.PathEscape(key), searchFields)
|
||||
|
||||
body, err := c.doRequest(ctx, "GET", apiURL, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get issue %s: %w", key, err)
|
||||
}
|
||||
|
||||
var issue Issue
|
||||
if err := json.Unmarshal(body, &issue); err != nil {
|
||||
return nil, fmt.Errorf("parse issue response: %w", err)
|
||||
}
|
||||
|
||||
return &issue, nil
|
||||
}
|
||||
|
||||
// CreateIssue creates a new issue in Jira.
|
||||
// fields should include "project", "summary", "issuetype", and optionally other fields.
|
||||
func (c *Client) CreateIssue(ctx context.Context, fields map[string]interface{}) (*Issue, error) {
|
||||
payload := map[string]interface{}{"fields": fields}
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal create request: %w", err)
|
||||
}
|
||||
|
||||
apiURL := fmt.Sprintf("%s/rest/api/3/issue", c.URL)
|
||||
|
||||
body, err := c.doRequest(ctx, "POST", apiURL, data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create issue: %w", err)
|
||||
}
|
||||
|
||||
// Create response only returns id, key, self. Fetch the full issue.
|
||||
var created struct {
|
||||
ID string `json:"id"`
|
||||
Key string `json:"key"`
|
||||
Self string `json:"self"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &created); err != nil {
|
||||
return nil, fmt.Errorf("parse create response: %w", err)
|
||||
}
|
||||
|
||||
return c.GetIssue(ctx, created.Key)
|
||||
}
|
||||
|
||||
// UpdateIssue updates an existing Jira issue by key.
|
||||
func (c *Client) UpdateIssue(ctx context.Context, key string, fields map[string]interface{}) error {
|
||||
payload := map[string]interface{}{"fields": fields}
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal update request: %w", err)
|
||||
}
|
||||
|
||||
apiURL := fmt.Sprintf("%s/rest/api/3/issue/%s", c.URL, url.PathEscape(key))
|
||||
|
||||
_, err = c.doRequest(ctx, "PUT", apiURL, data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update issue %s: %w", key, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// doRequest executes an authenticated HTTP request and returns the response body.
|
||||
func (c *Client) doRequest(ctx context.Context, method, apiURL string, body []byte) ([]byte, error) {
|
||||
if c.URL == "" {
|
||||
return nil, fmt.Errorf("jira URL not configured")
|
||||
}
|
||||
if c.APIToken == "" {
|
||||
return nil, fmt.Errorf("jira API token not configured")
|
||||
}
|
||||
|
||||
var bodyReader io.Reader
|
||||
if body != nil {
|
||||
bodyReader = bytes.NewReader(body)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, method, apiURL, bodyReader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
c.setAuth(req)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("User-Agent", "bd-jira-sync/1.0")
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
// PUT returns 204 No Content on success
|
||||
if resp.StatusCode == http.StatusNoContent {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return nil, fmt.Errorf("jira API returned %d: %s", resp.StatusCode, string(respBody))
|
||||
}
|
||||
|
||||
return respBody, nil
|
||||
}
|
||||
|
||||
// setAuth sets the appropriate authentication header on the request.
|
||||
func (c *Client) setAuth(req *http.Request) {
|
||||
isCloud := strings.Contains(c.URL, "atlassian.net")
|
||||
if (isCloud || c.Username != "") && c.Username != "" {
|
||||
auth := base64.StdEncoding.EncodeToString([]byte(c.Username + ":" + c.APIToken))
|
||||
req.Header.Set("Authorization", "Basic "+auth)
|
||||
} else {
|
||||
req.Header.Set("Authorization", "Bearer "+c.APIToken)
|
||||
}
|
||||
}
|
||||
|
||||
// DescriptionToPlainText extracts plain text from Jira's ADF (Atlassian Document Format).
|
||||
// Jira v3 API returns descriptions as ADF JSON, not plain text.
|
||||
func DescriptionToPlainText(raw json.RawMessage) string {
|
||||
if len(raw) == 0 || string(raw) == "null" {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Try to parse as ADF document
|
||||
var doc struct {
|
||||
Type string `json:"type"`
|
||||
Content []struct {
|
||||
Type string `json:"type"`
|
||||
Content []struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
} `json:"content"`
|
||||
} `json:"content"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(raw, &doc); err != nil {
|
||||
// Not JSON - treat as plain text string
|
||||
var s string
|
||||
if err := json.Unmarshal(raw, &s); err == nil {
|
||||
return s
|
||||
}
|
||||
return string(raw)
|
||||
}
|
||||
|
||||
if doc.Type != "doc" {
|
||||
// Not ADF - try plain string
|
||||
var s string
|
||||
if err := json.Unmarshal(raw, &s); err == nil {
|
||||
return s
|
||||
}
|
||||
return string(raw)
|
||||
}
|
||||
|
||||
// Extract text from ADF nodes
|
||||
var parts []string
|
||||
for _, block := range doc.Content {
|
||||
var line []string
|
||||
for _, inline := range block.Content {
|
||||
if inline.Text != "" {
|
||||
line = append(line, inline.Text)
|
||||
}
|
||||
}
|
||||
if len(line) > 0 {
|
||||
parts = append(parts, strings.Join(line, ""))
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Join(parts, "\n")
|
||||
}
|
||||
|
||||
// PlainTextToADF converts plain text to Jira's ADF (Atlassian Document Format).
|
||||
func PlainTextToADF(text string) json.RawMessage {
|
||||
if text == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
paragraphs := strings.Split(text, "\n")
|
||||
var content []interface{}
|
||||
for _, para := range paragraphs {
|
||||
if para == "" {
|
||||
content = append(content, map[string]interface{}{
|
||||
"type": "paragraph",
|
||||
"content": []interface{}{},
|
||||
})
|
||||
continue
|
||||
}
|
||||
content = append(content, map[string]interface{}{
|
||||
"type": "paragraph",
|
||||
"content": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "text",
|
||||
"text": para,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
doc := map[string]interface{}{
|
||||
"type": "doc",
|
||||
"version": 1,
|
||||
"content": content,
|
||||
}
|
||||
|
||||
data, _ := json.Marshal(doc)
|
||||
return data
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,171 +0,0 @@
|
|||
package jira
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// ConfigGetter provides read access to configuration values.
|
||||
type ConfigGetter interface {
|
||||
GetConfig(ctx context.Context, key string) (string, error)
|
||||
}
|
||||
|
||||
// IssueSearcher provides read access to issues.
|
||||
type IssueSearcher interface {
|
||||
SearchIssues(ctx context.Context, query string, filter types.IssueFilter) ([]*types.Issue, error)
|
||||
}
|
||||
|
||||
// DetectConflicts finds issues that have been modified both locally and in Jira
|
||||
// since the last sync. It fetches each potentially conflicting issue from Jira
|
||||
// to compare timestamps.
|
||||
func DetectConflicts(ctx context.Context, client *Client, store interface {
|
||||
ConfigGetter
|
||||
IssueSearcher
|
||||
}, stderr io.Writer) ([]Conflict, error) {
|
||||
// Get last sync time
|
||||
lastSyncStr, _ := store.GetConfig(ctx, "jira.last_sync")
|
||||
if lastSyncStr == "" {
|
||||
// No previous sync - no conflicts possible
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
lastSync, err := time.Parse(time.RFC3339, lastSyncStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid last_sync timestamp: %w", err)
|
||||
}
|
||||
|
||||
// Get all issues with Jira refs that were updated since last sync
|
||||
allIssues, err := store.SearchIssues(ctx, "", types.IssueFilter{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get jiraURL for validation
|
||||
jiraURL, _ := store.GetConfig(ctx, "jira.url")
|
||||
|
||||
var conflicts []Conflict
|
||||
for _, issue := range allIssues {
|
||||
if issue.ExternalRef == nil || !IsJiraExternalRef(*issue.ExternalRef, jiraURL) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if local issue was updated since last sync
|
||||
if !issue.UpdatedAt.After(lastSync) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Local was updated - now check if Jira was also updated
|
||||
jiraKey := ExtractJiraKey(*issue.ExternalRef)
|
||||
if jiraKey == "" {
|
||||
// Can't extract key - treat as potential conflict for safety
|
||||
conflicts = append(conflicts, Conflict{
|
||||
IssueID: issue.ID,
|
||||
LocalUpdated: issue.UpdatedAt,
|
||||
JiraExternalRef: *issue.ExternalRef,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
// Fetch Jira issue timestamp
|
||||
jiraUpdated, err := client.FetchIssueTimestamp(ctx, jiraKey)
|
||||
if err != nil {
|
||||
// Can't fetch from Jira - log warning and treat as potential conflict
|
||||
fmt.Fprintf(stderr, "Warning: couldn't fetch Jira issue %s: %v\n", jiraKey, err)
|
||||
conflicts = append(conflicts, Conflict{
|
||||
IssueID: issue.ID,
|
||||
LocalUpdated: issue.UpdatedAt,
|
||||
JiraExternalRef: *issue.ExternalRef,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
// Only a conflict if Jira was ALSO updated since last sync
|
||||
if jiraUpdated.After(lastSync) {
|
||||
conflicts = append(conflicts, Conflict{
|
||||
IssueID: issue.ID,
|
||||
LocalUpdated: issue.UpdatedAt,
|
||||
JiraUpdated: jiraUpdated,
|
||||
JiraExternalRef: *issue.ExternalRef,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return conflicts, nil
|
||||
}
|
||||
|
||||
// ReimportConflicts re-imports conflicting issues from Jira (Jira wins).
|
||||
// NOTE: Full implementation would fetch the complete Jira issue and update local copy.
|
||||
// Currently shows detailed conflict info for manual review.
|
||||
func ReimportConflicts(conflicts []Conflict, stderr io.Writer) error {
|
||||
if len(conflicts) == 0 {
|
||||
return nil
|
||||
}
|
||||
fmt.Fprintf(stderr, "Warning: conflict resolution (--prefer-jira) not fully implemented\n")
|
||||
fmt.Fprintf(stderr, " %d issue(s) have conflicts - Jira version would win:\n", len(conflicts))
|
||||
for _, c := range conflicts {
|
||||
if !c.JiraUpdated.IsZero() {
|
||||
fmt.Fprintf(stderr, " - %s (local: %s, jira: %s)\n",
|
||||
c.IssueID,
|
||||
c.LocalUpdated.Format(time.RFC3339),
|
||||
c.JiraUpdated.Format(time.RFC3339))
|
||||
} else {
|
||||
fmt.Fprintf(stderr, " - %s (local: %s, jira: unknown)\n",
|
||||
c.IssueID,
|
||||
c.LocalUpdated.Format(time.RFC3339))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResolveConflictsByTimestamp resolves conflicts by keeping the newer version.
|
||||
func ResolveConflictsByTimestamp(conflicts []Conflict, stderr io.Writer) error {
|
||||
if len(conflicts) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var localWins, jiraWins, unknown int
|
||||
for _, c := range conflicts {
|
||||
if c.JiraUpdated.IsZero() {
|
||||
unknown++
|
||||
} else if c.LocalUpdated.After(c.JiraUpdated) {
|
||||
localWins++
|
||||
} else {
|
||||
jiraWins++
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(stderr, "Conflict resolution by timestamp:\n")
|
||||
fmt.Fprintf(stderr, " Local wins (newer): %d\n", localWins)
|
||||
fmt.Fprintf(stderr, " Jira wins (newer): %d\n", jiraWins)
|
||||
if unknown > 0 {
|
||||
fmt.Fprintf(stderr, " Unknown (couldn't fetch): %d\n", unknown)
|
||||
}
|
||||
|
||||
// Show details
|
||||
for _, c := range conflicts {
|
||||
if c.JiraUpdated.IsZero() {
|
||||
fmt.Fprintf(stderr, " - %s: local version kept (couldn't fetch Jira timestamp)\n", c.IssueID)
|
||||
} else if c.LocalUpdated.After(c.JiraUpdated) {
|
||||
fmt.Fprintf(stderr, " - %s: local wins (local: %s > jira: %s)\n",
|
||||
c.IssueID,
|
||||
c.LocalUpdated.Format(time.RFC3339),
|
||||
c.JiraUpdated.Format(time.RFC3339))
|
||||
} else {
|
||||
fmt.Fprintf(stderr, " - %s: jira wins (jira: %s >= local: %s)\n",
|
||||
c.IssueID,
|
||||
c.JiraUpdated.Format(time.RFC3339),
|
||||
c.LocalUpdated.Format(time.RFC3339))
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: Full implementation would actually re-import the Jira version for jiraWins issues
|
||||
if jiraWins > 0 {
|
||||
fmt.Fprintf(stderr, "Warning: %d issue(s) should be re-imported from Jira (not yet implemented)\n", jiraWins)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -1,16 +1,16 @@
|
|||
package jira
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// jiraFieldMapper implements tracker.FieldMapper for Jira.
|
||||
// This is a minimal implementation since Jira sync currently uses the Python script.
|
||||
type jiraFieldMapper struct{}
|
||||
|
||||
func (m *jiraFieldMapper) PriorityToBeads(trackerPriority interface{}) int {
|
||||
// Jira priority names: Highest(1), High(2), Medium(3), Low(4), Lowest(5)
|
||||
if name, ok := trackerPriority.(string); ok {
|
||||
switch name {
|
||||
case "Highest":
|
||||
|
|
@ -105,10 +105,101 @@ func (m *jiraFieldMapper) TypeToTracker(beadsType types.IssueType) interface{} {
|
|||
}
|
||||
}
|
||||
|
||||
func (m *jiraFieldMapper) IssueToBeads(_ *tracker.TrackerIssue) *tracker.IssueConversion {
|
||||
return nil
|
||||
func (m *jiraFieldMapper) IssueToBeads(ti *tracker.TrackerIssue) *tracker.IssueConversion {
|
||||
ji, ok := ti.Raw.(*Issue)
|
||||
if !ok || ji == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
issue := &types.Issue{
|
||||
Title: ji.Fields.Summary,
|
||||
Description: DescriptionToPlainText(ji.Fields.Description),
|
||||
Priority: m.PriorityToBeads(priorityName(ji)),
|
||||
Status: m.StatusToBeads(statusName(ji)),
|
||||
IssueType: m.TypeToBeads(typeName(ji)),
|
||||
}
|
||||
|
||||
if ji.Fields.Assignee != nil {
|
||||
issue.Owner = ji.Fields.Assignee.DisplayName
|
||||
}
|
||||
|
||||
if ji.Fields.Labels != nil {
|
||||
issue.Labels = ji.Fields.Labels
|
||||
}
|
||||
|
||||
// Set external ref from issue URL
|
||||
if ji.Self != "" {
|
||||
ref := extractBrowseURL(ji)
|
||||
issue.ExternalRef = &ref
|
||||
}
|
||||
|
||||
return &tracker.IssueConversion{
|
||||
Issue: issue,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *jiraFieldMapper) IssueToTracker(_ *types.Issue) map[string]interface{} {
|
||||
return nil
|
||||
func (m *jiraFieldMapper) IssueToTracker(issue *types.Issue) map[string]interface{} {
|
||||
fields := map[string]interface{}{
|
||||
"summary": issue.Title,
|
||||
}
|
||||
|
||||
// Convert description to ADF
|
||||
if issue.Description != "" {
|
||||
fields["description"] = PlainTextToADF(issue.Description)
|
||||
}
|
||||
|
||||
// Set issue type
|
||||
typeName := m.TypeToTracker(issue.IssueType)
|
||||
if name, ok := typeName.(string); ok {
|
||||
fields["issuetype"] = map[string]string{"name": name}
|
||||
}
|
||||
|
||||
// Set priority
|
||||
priorityName := m.PriorityToTracker(issue.Priority)
|
||||
if name, ok := priorityName.(string); ok {
|
||||
fields["priority"] = map[string]string{"name": name}
|
||||
}
|
||||
|
||||
// Set labels
|
||||
if len(issue.Labels) > 0 {
|
||||
fields["labels"] = issue.Labels
|
||||
}
|
||||
|
||||
return fields
|
||||
}
|
||||
|
||||
// Helper functions for safe field extraction from Jira issues.
|
||||
|
||||
func priorityName(ji *Issue) string {
|
||||
if ji.Fields.Priority != nil {
|
||||
return ji.Fields.Priority.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func statusName(ji *Issue) string {
|
||||
if ji.Fields.Status != nil {
|
||||
return ji.Fields.Status.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func typeName(ji *Issue) string {
|
||||
if ji.Fields.IssueType != nil {
|
||||
return ji.Fields.IssueType.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// extractBrowseURL builds the human-readable browse URL from a Jira issue.
|
||||
// Self is "https://company.atlassian.net/rest/api/3/issue/10001";
|
||||
// we need "https://company.atlassian.net/browse/PROJ-123".
|
||||
func extractBrowseURL(ji *Issue) string {
|
||||
if ji.Self == "" || ji.Key == "" {
|
||||
return ""
|
||||
}
|
||||
if idx := strings.Index(ji.Self, "/rest/api/"); idx > 0 {
|
||||
return ji.Self[:idx] + "/browse/" + ji.Key
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,63 +0,0 @@
|
|||
package jira
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/steveyegge/beads/internal/beads"
|
||||
)
|
||||
|
||||
// FindScript locates a Jira Python script by name.
|
||||
func FindScript(name string) (string, error) {
|
||||
// Check environment variable first (allows users to specify script location)
|
||||
if envPath := os.Getenv("BD_JIRA_SCRIPT"); envPath != "" {
|
||||
if _, err := os.Stat(envPath); err == nil {
|
||||
return envPath, nil
|
||||
}
|
||||
return "", fmt.Errorf("BD_JIRA_SCRIPT points to non-existent file: %s", envPath)
|
||||
}
|
||||
|
||||
// Check common locations
|
||||
locations := []string{
|
||||
// Relative to current working directory
|
||||
filepath.Join("examples", "jira-import", name),
|
||||
}
|
||||
|
||||
// Add executable-relative path
|
||||
if exe, err := os.Executable(); err == nil {
|
||||
exeDir := filepath.Dir(exe)
|
||||
locations = append(locations, filepath.Join(exeDir, "examples", "jira-import", name))
|
||||
locations = append(locations, filepath.Join(exeDir, "..", "examples", "jira-import", name))
|
||||
}
|
||||
|
||||
// Check BEADS_DIR or current .beads location
|
||||
if beadsDir := beads.FindBeadsDir(); beadsDir != "" {
|
||||
repoRoot := filepath.Dir(beadsDir)
|
||||
locations = append(locations, filepath.Join(repoRoot, "examples", "jira-import", name))
|
||||
}
|
||||
|
||||
for _, loc := range locations {
|
||||
if _, err := os.Stat(loc); err == nil {
|
||||
absPath, err := filepath.Abs(loc)
|
||||
if err == nil {
|
||||
return absPath, nil
|
||||
}
|
||||
return loc, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf(`script not found: %s
|
||||
|
||||
The Jira sync feature requires the Python script from the beads repository.
|
||||
|
||||
To fix this, either:
|
||||
1. Set BD_JIRA_SCRIPT to point to the script:
|
||||
export BD_JIRA_SCRIPT=/path/to/jira2jsonl.py
|
||||
|
||||
2. Or download it from GitHub:
|
||||
curl -o jira2jsonl.py https://raw.githubusercontent.com/steveyegge/beads/main/examples/jira-import/jira2jsonl.py
|
||||
export BD_JIRA_SCRIPT=$PWD/jira2jsonl.py
|
||||
|
||||
Looked in: %v`, name, locations)
|
||||
}
|
||||
|
|
@ -2,19 +2,15 @@ package jira
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// ErrNotImplemented is returned by Jira tracker methods that are not yet supported.
|
||||
// Jira pull currently uses a Python script (jira2jsonl.py), not the Go API client.
|
||||
var ErrNotImplemented = errors.New("not implemented: Jira sync uses Python script (jira2jsonl.py)")
|
||||
|
||||
func init() {
|
||||
tracker.Register("jira", func() tracker.IssueTracker {
|
||||
return &Tracker{}
|
||||
|
|
@ -23,9 +19,10 @@ func init() {
|
|||
|
||||
// Tracker implements tracker.IssueTracker for Jira.
|
||||
type Tracker struct {
|
||||
client *Client
|
||||
store storage.Storage
|
||||
jiraURL string
|
||||
client *Client
|
||||
store storage.Storage
|
||||
jiraURL string
|
||||
projectKey string
|
||||
}
|
||||
|
||||
func (t *Tracker) Name() string { return "jira" }
|
||||
|
|
@ -41,6 +38,12 @@ func (t *Tracker) Init(ctx context.Context, store storage.Storage) error {
|
|||
}
|
||||
t.jiraURL = jiraURL
|
||||
|
||||
projectKey, err := t.getConfig(ctx, "jira.project", "JIRA_PROJECT")
|
||||
if err != nil || projectKey == "" {
|
||||
return fmt.Errorf("Jira project not configured (set jira.project or JIRA_PROJECT)")
|
||||
}
|
||||
t.projectKey = projectKey
|
||||
|
||||
username, _ := t.getConfig(ctx, "jira.username", "JIRA_USERNAME")
|
||||
apiToken, err := t.getConfig(ctx, "jira.api_token", "JIRA_API_TOKEN")
|
||||
if err != nil || apiToken == "" {
|
||||
|
|
@ -60,20 +63,80 @@ func (t *Tracker) Validate() error {
|
|||
|
||||
func (t *Tracker) Close() error { return nil }
|
||||
|
||||
func (t *Tracker) FetchIssues(_ context.Context, _ tracker.FetchOptions) ([]tracker.TrackerIssue, error) {
|
||||
return nil, ErrNotImplemented
|
||||
func (t *Tracker) FetchIssues(ctx context.Context, opts tracker.FetchOptions) ([]tracker.TrackerIssue, error) {
|
||||
// Build JQL query
|
||||
jql := fmt.Sprintf("project = %q", t.projectKey)
|
||||
|
||||
// State filter
|
||||
switch opts.State {
|
||||
case "open":
|
||||
jql += " AND statusCategory != Done"
|
||||
case "closed":
|
||||
jql += " AND statusCategory = Done"
|
||||
}
|
||||
|
||||
// Incremental sync
|
||||
if opts.Since != nil {
|
||||
jql += fmt.Sprintf(" AND updated >= %q", opts.Since.Format("2006-01-02 15:04"))
|
||||
}
|
||||
|
||||
jql += " ORDER BY updated DESC"
|
||||
|
||||
issues, err := t.client.SearchIssues(ctx, jql)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make([]tracker.TrackerIssue, 0, len(issues))
|
||||
for i := range issues {
|
||||
result = append(result, jiraToTrackerIssue(&issues[i]))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (t *Tracker) FetchIssue(_ context.Context, _ string) (*tracker.TrackerIssue, error) {
|
||||
return nil, ErrNotImplemented
|
||||
func (t *Tracker) FetchIssue(ctx context.Context, identifier string) (*tracker.TrackerIssue, error) {
|
||||
issue, err := t.client.GetIssue(ctx, identifier)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if issue == nil {
|
||||
return nil, nil
|
||||
}
|
||||
ti := jiraToTrackerIssue(issue)
|
||||
return &ti, nil
|
||||
}
|
||||
|
||||
func (t *Tracker) CreateIssue(_ context.Context, _ *types.Issue) (*tracker.TrackerIssue, error) {
|
||||
return nil, ErrNotImplemented
|
||||
func (t *Tracker) CreateIssue(ctx context.Context, issue *types.Issue) (*tracker.TrackerIssue, error) {
|
||||
mapper := t.FieldMapper()
|
||||
fields := mapper.IssueToTracker(issue)
|
||||
|
||||
// Set project
|
||||
fields["project"] = map[string]string{"key": t.projectKey}
|
||||
|
||||
created, err := t.client.CreateIssue(ctx, fields)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ti := jiraToTrackerIssue(created)
|
||||
return &ti, nil
|
||||
}
|
||||
|
||||
func (t *Tracker) UpdateIssue(_ context.Context, _ string, _ *types.Issue) (*tracker.TrackerIssue, error) {
|
||||
return nil, ErrNotImplemented
|
||||
func (t *Tracker) UpdateIssue(ctx context.Context, externalID string, issue *types.Issue) (*tracker.TrackerIssue, error) {
|
||||
mapper := t.FieldMapper()
|
||||
fields := mapper.IssueToTracker(issue)
|
||||
|
||||
if err := t.client.UpdateIssue(ctx, externalID, fields); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Fetch the updated issue to return current state
|
||||
updated, err := t.client.GetIssue(ctx, externalID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ti := jiraToTrackerIssue(updated)
|
||||
return &ti, nil
|
||||
}
|
||||
|
||||
func (t *Tracker) FieldMapper() tracker.FieldMapper {
|
||||
|
|
@ -105,3 +168,89 @@ func (t *Tracker) getConfig(ctx context.Context, key, envVar string) (string, er
|
|||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// jiraToTrackerIssue converts a Jira API Issue to the generic TrackerIssue format.
|
||||
func jiraToTrackerIssue(ji *Issue) tracker.TrackerIssue {
|
||||
ti := tracker.TrackerIssue{
|
||||
ID: ji.ID,
|
||||
Identifier: ji.Key,
|
||||
URL: ji.Self,
|
||||
Title: ji.Fields.Summary,
|
||||
Labels: ji.Fields.Labels,
|
||||
Raw: ji,
|
||||
}
|
||||
|
||||
// Description: convert ADF to plain text
|
||||
ti.Description = DescriptionToPlainText(ji.Fields.Description)
|
||||
|
||||
// Priority
|
||||
if ji.Fields.Priority != nil {
|
||||
ti.Priority = jiraPriorityToNumeric(ji.Fields.Priority.Name)
|
||||
ti.State = ji.Fields.Status // Store for FieldMapper
|
||||
}
|
||||
|
||||
// State
|
||||
if ji.Fields.Status != nil {
|
||||
ti.State = ji.Fields.Status.Name
|
||||
}
|
||||
|
||||
// Type
|
||||
if ji.Fields.IssueType != nil {
|
||||
ti.Type = ji.Fields.IssueType.Name
|
||||
}
|
||||
|
||||
// Assignee
|
||||
if ji.Fields.Assignee != nil {
|
||||
ti.Assignee = ji.Fields.Assignee.DisplayName
|
||||
ti.AssigneeEmail = ji.Fields.Assignee.EmailAddress
|
||||
ti.AssigneeID = ji.Fields.Assignee.AccountID
|
||||
}
|
||||
|
||||
// Timestamps
|
||||
if t, err := ParseTimestamp(ji.Fields.Created); err == nil {
|
||||
ti.CreatedAt = t
|
||||
}
|
||||
if t, err := ParseTimestamp(ji.Fields.Updated); err == nil {
|
||||
ti.UpdatedAt = t
|
||||
}
|
||||
|
||||
// Store Jira-specific metadata
|
||||
ti.Metadata = map[string]interface{}{
|
||||
"source_system": fmt.Sprintf("jira:%s:%s", projectKeyFromIssue(ji), ji.Key),
|
||||
}
|
||||
if ji.Fields.IssueType != nil {
|
||||
ti.Metadata["jira_type"] = ji.Fields.IssueType.Name
|
||||
}
|
||||
|
||||
return ti
|
||||
}
|
||||
|
||||
// jiraPriorityToNumeric converts a Jira priority name to a numeric value (0=highest, 4=lowest).
|
||||
func jiraPriorityToNumeric(name string) int {
|
||||
switch strings.ToLower(name) {
|
||||
case "highest":
|
||||
return 0
|
||||
case "high":
|
||||
return 1
|
||||
case "medium":
|
||||
return 2
|
||||
case "low":
|
||||
return 3
|
||||
case "lowest":
|
||||
return 4
|
||||
default:
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
// projectKeyFromIssue extracts the project key from a Jira issue.
|
||||
func projectKeyFromIssue(ji *Issue) string {
|
||||
if ji.Fields.Project != nil {
|
||||
return ji.Fields.Project.Key
|
||||
}
|
||||
// Fall back to extracting from issue key (e.g., "PROJ-123" → "PROJ")
|
||||
if idx := strings.LastIndex(ji.Key, "-"); idx > 0 {
|
||||
return ji.Key[:idx]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
package jira
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
func TestRegistered(t *testing.T) {
|
||||
|
|
@ -69,18 +72,188 @@ func TestBuildExternalRef(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestStubMethodsReturnErrNotImplemented(t *testing.T) {
|
||||
tr := &Tracker{}
|
||||
if _, err := tr.FetchIssues(nil, tracker.FetchOptions{}); err != ErrNotImplemented {
|
||||
t.Errorf("FetchIssues error = %v, want ErrNotImplemented", err)
|
||||
func TestJiraToTrackerIssue(t *testing.T) {
|
||||
ji := &Issue{
|
||||
ID: "10001",
|
||||
Key: "PROJ-42",
|
||||
Self: "https://company.atlassian.net/rest/api/3/issue/10001",
|
||||
Fields: IssueFields{
|
||||
Summary: "Fix login bug",
|
||||
Description: json.RawMessage(`"A plain text description"`),
|
||||
Status: &StatusField{ID: "1", Name: "In Progress"},
|
||||
Priority: &PriorityField{ID: "2", Name: "High"},
|
||||
IssueType: &IssueTypeField{ID: "10001", Name: "Bug"},
|
||||
Project: &ProjectField{ID: "10000", Key: "PROJ"},
|
||||
Assignee: &UserField{AccountID: "abc123", DisplayName: "Alice", EmailAddress: "alice@example.com"},
|
||||
Labels: []string{"backend", "urgent"},
|
||||
Created: "2025-01-15T10:30:00.000+0000",
|
||||
Updated: "2025-01-16T14:20:00.000+0000",
|
||||
},
|
||||
}
|
||||
if _, err := tr.FetchIssue(nil, "X-1"); err != ErrNotImplemented {
|
||||
t.Errorf("FetchIssue error = %v, want ErrNotImplemented", err)
|
||||
|
||||
ti := jiraToTrackerIssue(ji)
|
||||
|
||||
if ti.ID != "10001" {
|
||||
t.Errorf("ID = %q, want %q", ti.ID, "10001")
|
||||
}
|
||||
if _, err := tr.CreateIssue(nil, nil); err != ErrNotImplemented {
|
||||
t.Errorf("CreateIssue error = %v, want ErrNotImplemented", err)
|
||||
if ti.Identifier != "PROJ-42" {
|
||||
t.Errorf("Identifier = %q, want %q", ti.Identifier, "PROJ-42")
|
||||
}
|
||||
if _, err := tr.UpdateIssue(nil, "", nil); err != ErrNotImplemented {
|
||||
t.Errorf("UpdateIssue error = %v, want ErrNotImplemented", err)
|
||||
if ti.Title != "Fix login bug" {
|
||||
t.Errorf("Title = %q, want %q", ti.Title, "Fix login bug")
|
||||
}
|
||||
if ti.Description != "A plain text description" {
|
||||
t.Errorf("Description = %q, want %q", ti.Description, "A plain text description")
|
||||
}
|
||||
if ti.Assignee != "Alice" {
|
||||
t.Errorf("Assignee = %q, want %q", ti.Assignee, "Alice")
|
||||
}
|
||||
if ti.AssigneeEmail != "alice@example.com" {
|
||||
t.Errorf("AssigneeEmail = %q, want %q", ti.AssigneeEmail, "alice@example.com")
|
||||
}
|
||||
if ti.CreatedAt.IsZero() {
|
||||
t.Error("CreatedAt should not be zero")
|
||||
}
|
||||
if ti.UpdatedAt.IsZero() {
|
||||
t.Error("UpdatedAt should not be zero")
|
||||
}
|
||||
if len(ti.Labels) != 2 {
|
||||
t.Errorf("Labels length = %d, want 2", len(ti.Labels))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDescriptionToPlainText(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
raw json.RawMessage
|
||||
want string
|
||||
}{
|
||||
{"null", json.RawMessage(`null`), ""},
|
||||
{"empty", json.RawMessage(``), ""},
|
||||
{"plain string", json.RawMessage(`"hello world"`), "hello world"},
|
||||
{"ADF document", json.RawMessage(`{
|
||||
"type": "doc",
|
||||
"content": [
|
||||
{
|
||||
"type": "paragraph",
|
||||
"content": [
|
||||
{"type": "text", "text": "First paragraph"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "paragraph",
|
||||
"content": [
|
||||
{"type": "text", "text": "Second paragraph"}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`), "First paragraph\nSecond paragraph"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := DescriptionToPlainText(tt.raw)
|
||||
if got != tt.want {
|
||||
t.Errorf("DescriptionToPlainText() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlainTextToADF(t *testing.T) {
|
||||
adf := PlainTextToADF("Hello\nWorld")
|
||||
if adf == nil {
|
||||
t.Fatal("PlainTextToADF returned nil")
|
||||
}
|
||||
|
||||
var doc struct {
|
||||
Type string `json:"type"`
|
||||
Version int `json:"version"`
|
||||
Content []struct {
|
||||
Type string `json:"type"`
|
||||
Content []struct {
|
||||
Text string `json:"text"`
|
||||
} `json:"content"`
|
||||
} `json:"content"`
|
||||
}
|
||||
if err := json.Unmarshal(adf, &doc); err != nil {
|
||||
t.Fatalf("Failed to parse ADF: %v", err)
|
||||
}
|
||||
if doc.Type != "doc" {
|
||||
t.Errorf("doc type = %q, want %q", doc.Type, "doc")
|
||||
}
|
||||
if len(doc.Content) != 2 {
|
||||
t.Fatalf("content length = %d, want 2", len(doc.Content))
|
||||
}
|
||||
if doc.Content[0].Content[0].Text != "Hello" {
|
||||
t.Errorf("first paragraph text = %q, want %q", doc.Content[0].Content[0].Text, "Hello")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldMapperIssueToBeads(t *testing.T) {
|
||||
ji := &Issue{
|
||||
ID: "10001",
|
||||
Key: "PROJ-42",
|
||||
Self: "https://company.atlassian.net/rest/api/3/issue/10001",
|
||||
Fields: IssueFields{
|
||||
Summary: "Test issue",
|
||||
Description: json.RawMessage(`"Description text"`),
|
||||
Status: &StatusField{Name: "In Progress"},
|
||||
Priority: &PriorityField{Name: "High"},
|
||||
IssueType: &IssueTypeField{Name: "Bug"},
|
||||
Assignee: &UserField{DisplayName: "Bob"},
|
||||
Labels: []string{"frontend"},
|
||||
Created: time.Now().Format(time.RFC3339),
|
||||
Updated: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
|
||||
ti := jiraToTrackerIssue(ji)
|
||||
mapper := &jiraFieldMapper{}
|
||||
conv := mapper.IssueToBeads(&ti)
|
||||
|
||||
if conv == nil {
|
||||
t.Fatal("IssueToBeads returned nil")
|
||||
}
|
||||
if conv.Issue.Title != "Test issue" {
|
||||
t.Errorf("Title = %q, want %q", conv.Issue.Title, "Test issue")
|
||||
}
|
||||
if conv.Issue.Description != "Description text" {
|
||||
t.Errorf("Description = %q, want %q", conv.Issue.Description, "Description text")
|
||||
}
|
||||
if conv.Issue.Priority != 1 {
|
||||
t.Errorf("Priority = %d, want 1 (High)", conv.Issue.Priority)
|
||||
}
|
||||
if conv.Issue.Owner != "Bob" {
|
||||
t.Errorf("Owner = %q, want %q", conv.Issue.Owner, "Bob")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldMapperIssueToTracker(t *testing.T) {
|
||||
mapper := &jiraFieldMapper{}
|
||||
|
||||
issue := &types.Issue{
|
||||
Title: "New feature",
|
||||
Description: "Feature description",
|
||||
Priority: 0,
|
||||
IssueType: types.TypeBug,
|
||||
Labels: []string{"critical"},
|
||||
}
|
||||
|
||||
fields := mapper.IssueToTracker(issue)
|
||||
|
||||
if fields["summary"] != "New feature" {
|
||||
t.Errorf("summary = %v, want %q", fields["summary"], "New feature")
|
||||
}
|
||||
if fields["description"] == nil {
|
||||
t.Error("description should not be nil")
|
||||
}
|
||||
issueType, ok := fields["issuetype"].(map[string]string)
|
||||
if !ok || issueType["name"] != "Bug" {
|
||||
t.Errorf("issuetype = %v, want Bug", fields["issuetype"])
|
||||
}
|
||||
priority, ok := fields["priority"].(map[string]string)
|
||||
if !ok || priority["name"] != "Highest" {
|
||||
t.Errorf("priority = %v, want Highest", fields["priority"])
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,47 +1,2 @@
|
|||
// Package jira provides client, types, and utilities for Jira integration.
|
||||
package jira
|
||||
|
||||
import "time"
|
||||
|
||||
// SyncStats tracks statistics for a Jira sync operation.
|
||||
type SyncStats struct {
|
||||
Pulled int `json:"pulled"`
|
||||
Pushed int `json:"pushed"`
|
||||
Created int `json:"created"`
|
||||
Updated int `json:"updated"`
|
||||
Skipped int `json:"skipped"`
|
||||
Errors int `json:"errors"`
|
||||
Conflicts int `json:"conflicts"`
|
||||
}
|
||||
|
||||
// SyncResult represents the result of a Jira sync operation.
|
||||
type SyncResult struct {
|
||||
Success bool `json:"success"`
|
||||
Stats SyncStats `json:"stats"`
|
||||
LastSync string `json:"last_sync,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Warnings []string `json:"warnings,omitempty"`
|
||||
}
|
||||
|
||||
// PullStats tracks pull operation statistics.
|
||||
type PullStats struct {
|
||||
Created int
|
||||
Updated int
|
||||
Skipped int
|
||||
}
|
||||
|
||||
// PushStats tracks push operation statistics.
|
||||
type PushStats struct {
|
||||
Created int
|
||||
Updated int
|
||||
Skipped int
|
||||
Errors int
|
||||
}
|
||||
|
||||
// Conflict represents a conflict between local and Jira versions.
|
||||
type Conflict struct {
|
||||
IssueID string
|
||||
LocalUpdated time.Time
|
||||
JiraUpdated time.Time
|
||||
JiraExternalRef string
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,101 +0,0 @@
|
|||
package jira
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSyncStats(t *testing.T) {
|
||||
stats := SyncStats{}
|
||||
|
||||
if stats.Pulled != 0 {
|
||||
t.Errorf("expected Pulled to be 0, got %d", stats.Pulled)
|
||||
}
|
||||
if stats.Pushed != 0 {
|
||||
t.Errorf("expected Pushed to be 0, got %d", stats.Pushed)
|
||||
}
|
||||
if stats.Created != 0 {
|
||||
t.Errorf("expected Created to be 0, got %d", stats.Created)
|
||||
}
|
||||
if stats.Updated != 0 {
|
||||
t.Errorf("expected Updated to be 0, got %d", stats.Updated)
|
||||
}
|
||||
if stats.Skipped != 0 {
|
||||
t.Errorf("expected Skipped to be 0, got %d", stats.Skipped)
|
||||
}
|
||||
if stats.Errors != 0 {
|
||||
t.Errorf("expected Errors to be 0, got %d", stats.Errors)
|
||||
}
|
||||
if stats.Conflicts != 0 {
|
||||
t.Errorf("expected Conflicts to be 0, got %d", stats.Conflicts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncResult(t *testing.T) {
|
||||
result := SyncResult{
|
||||
Success: true,
|
||||
Stats: SyncStats{
|
||||
Created: 5,
|
||||
Updated: 3,
|
||||
},
|
||||
LastSync: "2025-01-15T10:30:00Z",
|
||||
}
|
||||
|
||||
if !result.Success {
|
||||
t.Error("expected Success to be true")
|
||||
}
|
||||
if result.Stats.Created != 5 {
|
||||
t.Errorf("expected Created to be 5, got %d", result.Stats.Created)
|
||||
}
|
||||
if result.Stats.Updated != 3 {
|
||||
t.Errorf("expected Updated to be 3, got %d", result.Stats.Updated)
|
||||
}
|
||||
if result.LastSync != "2025-01-15T10:30:00Z" {
|
||||
t.Errorf("unexpected LastSync value: %s", result.LastSync)
|
||||
}
|
||||
if result.Error != "" {
|
||||
t.Errorf("expected Error to be empty, got %s", result.Error)
|
||||
}
|
||||
if len(result.Warnings) != 0 {
|
||||
t.Errorf("expected Warnings to be empty, got %v", result.Warnings)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPullStats(t *testing.T) {
|
||||
stats := PullStats{
|
||||
Created: 10,
|
||||
Updated: 5,
|
||||
Skipped: 2,
|
||||
}
|
||||
|
||||
if stats.Created != 10 {
|
||||
t.Errorf("expected Created to be 10, got %d", stats.Created)
|
||||
}
|
||||
if stats.Updated != 5 {
|
||||
t.Errorf("expected Updated to be 5, got %d", stats.Updated)
|
||||
}
|
||||
if stats.Skipped != 2 {
|
||||
t.Errorf("expected Skipped to be 2, got %d", stats.Skipped)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushStats(t *testing.T) {
|
||||
stats := PushStats{
|
||||
Created: 8,
|
||||
Updated: 4,
|
||||
Skipped: 1,
|
||||
Errors: 2,
|
||||
}
|
||||
|
||||
if stats.Created != 8 {
|
||||
t.Errorf("expected Created to be 8, got %d", stats.Created)
|
||||
}
|
||||
if stats.Updated != 4 {
|
||||
t.Errorf("expected Updated to be 4, got %d", stats.Updated)
|
||||
}
|
||||
if stats.Skipped != 1 {
|
||||
t.Errorf("expected Skipped to be 1, got %d", stats.Skipped)
|
||||
}
|
||||
if stats.Errors != 2 {
|
||||
t.Errorf("expected Errors to be 2, got %d", stats.Errors)
|
||||
}
|
||||
}
|
||||
|
|
@ -280,13 +280,14 @@ func (e *Engine) doPull(ctx context.Context, opts SyncOptions) (*PullStats, erro
|
|||
}
|
||||
|
||||
if existing != nil {
|
||||
// Conflict-aware pull: when prefer-local, skip updating issues
|
||||
// that were locally modified since last sync to preserve local changes.
|
||||
if opts.ConflictResolution == ConflictLocal && lastSync != nil {
|
||||
if existing.UpdatedAt.After(*lastSync) {
|
||||
stats.Skipped++
|
||||
continue
|
||||
}
|
||||
// Conflict-aware pull: skip updating issues that were locally
|
||||
// modified since last sync. Conflict detection (Phase 2) will
|
||||
// handle these per the configured resolution strategy.
|
||||
// Without this guard, pull silently overwrites local changes
|
||||
// before conflict detection can compare timestamps.
|
||||
if lastSync != nil && existing.UpdatedAt.After(*lastSync) {
|
||||
stats.Skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
// Update existing issue
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue