|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "unicode/utf8" |
| 9 | + |
| 10 | + "github.com/morluto/gitcontribute/internal/cli" |
| 11 | + "github.com/morluto/gitcontribute/internal/corpus" |
| 12 | + "github.com/morluto/gitcontribute/internal/domain" |
| 13 | + "github.com/morluto/gitcontribute/internal/investigation" |
| 14 | + "github.com/morluto/gitcontribute/internal/research" |
| 15 | +) |
| 16 | + |
| 17 | +const maxThreadSeedDescription = 5000 |
| 18 | + |
| 19 | +// StartInvestigationFromThread atomically creates an investigation and seed |
| 20 | +// hypothesis from one exact local observation. It performs no network access |
| 21 | +// or process execution. |
| 22 | +func (s *Service) StartInvestigationFromThread(ctx context.Context, requested research.ThreadRef) (*cli.ThreadInvestigationResult, error) { |
| 23 | + if err := requested.Validate(); err != nil { |
| 24 | + return nil, err |
| 25 | + } |
| 26 | + if err := ctx.Err(); err != nil { |
| 27 | + return nil, err |
| 28 | + } |
| 29 | + c, err := s.openCorpus(ctx) |
| 30 | + if err != nil { |
| 31 | + return nil, err |
| 32 | + } |
| 33 | + repo, err := c.GetRepository(ctx, requested.Repo.Owner, requested.Repo.Repo) |
| 34 | + if err != nil { |
| 35 | + return nil, fmt.Errorf("get thread investigation repository: %w", err) |
| 36 | + } |
| 37 | + if repo == nil { |
| 38 | + return nil, cli.NewCLIError(cli.ExitNotFound, fmt.Errorf("%w: %s", errRepositoryNotFound, requested.Repo)) |
| 39 | + } |
| 40 | + thread, err := c.GetThreadByNumber(ctx, repo.ID, requested.Number) |
| 41 | + if err != nil { |
| 42 | + return nil, fmt.Errorf("get thread investigation source: %w", err) |
| 43 | + } |
| 44 | + if thread == nil { |
| 45 | + return nil, cli.NewCLIError(cli.ExitNotFound, fmt.Errorf("%w: %s#%d", research.ErrThreadNotFound, requested.Repo, requested.Number)) |
| 46 | + } |
| 47 | + storedKind := domain.ThreadKind(thread.Kind) |
| 48 | + if requested.Kind != "" && requested.Kind != storedKind { |
| 49 | + return nil, cli.NewCLIError(cli.ExitNotFound, research.KindMismatchError(requested.Kind, storedKind)) |
| 50 | + } |
| 51 | + resolved := research.ThreadRef{Repo: requested.Repo, Kind: storedKind, Number: requested.Number} |
| 52 | + observation, err := c.GetThreadObservationRevision(ctx, thread.ID, thread.SourceUpdatedAt, thread.ObservationSequence) |
| 53 | + if err != nil { |
| 54 | + if errors.Is(err, corpus.ErrThreadObservationRevisionNotFound) { |
| 55 | + return nil, fmt.Errorf("%w: projection %s has no matching observation revision", investigation.ErrInvalidThreadBaseline, resolved) |
| 56 | + } |
| 57 | + return nil, fmt.Errorf("read thread investigation baseline: %w", err) |
| 58 | + } |
| 59 | + description, truncated := boundedThreadSeedDescription(thread.Body) |
| 60 | + source := domain.SourceRef{ |
| 61 | + Source: "github:rest", URL: fmt.Sprintf("https://api.github.com/repos/%s/issues/%d", resolved.Repo, resolved.Number), |
| 62 | + ObservedAt: observation.ObservedAt, AsOf: observation.SourceUpdatedAt, |
| 63 | + } |
| 64 | + result, err := investigation.NewService(c, c).StartFromThread(ctx, investigation.StartFromThreadInput{ |
| 65 | + Baseline: investigation.ThreadBaseline{ |
| 66 | + Repo: resolved.Repo, Kind: resolved.Kind, Number: resolved.Number, |
| 67 | + ObservationID: observation.ID, SourceUpdatedAt: observation.SourceUpdatedAt, |
| 68 | + ObservationSequence: observation.ObservationSequence, ObservedAt: observation.ObservedAt, |
| 69 | + Source: source, DescriptionTruncated: truncated, |
| 70 | + }, |
| 71 | + Title: thread.Title, Description: description, |
| 72 | + }) |
| 73 | + if err != nil { |
| 74 | + if errors.Is(err, investigation.ErrInvalidThreadBaseline) { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + return nil, fmt.Errorf("start investigation from thread: %w", err) |
| 78 | + } |
| 79 | + return &cli.ThreadInvestigationResult{ |
| 80 | + Created: result.Created, Investigation: investigationResult(result.Investigation), |
| 81 | + Hypothesis: hypothesisResult(result.Hypothesis), |
| 82 | + }, nil |
| 83 | +} |
| 84 | + |
| 85 | +func boundedThreadSeedDescription(value string) (string, bool) { |
| 86 | + value = strings.TrimSpace(strings.ReplaceAll(value, "\x00", "")) |
| 87 | + if utf8.RuneCountInString(value) <= maxThreadSeedDescription { |
| 88 | + return value, false |
| 89 | + } |
| 90 | + runes := []rune(value) |
| 91 | + return strings.TrimSpace(string(runes[:maxThreadSeedDescription])) + "…", true |
| 92 | +} |
| 93 | + |
| 94 | +func threadBaselineResult(value *investigation.ThreadBaseline) *cli.ThreadBaselineResult { |
| 95 | + if value == nil { |
| 96 | + return nil |
| 97 | + } |
| 98 | + return &cli.ThreadBaselineResult{ |
| 99 | + Ref: value.Ref(), Repository: value.Repo.String(), Kind: string(value.Kind), Number: value.Number, |
| 100 | + ObservationID: value.ObservationID, SourceUpdatedAt: formatTime(value.SourceUpdatedAt), |
| 101 | + ObservationSequence: value.ObservationSequence, ObservedAt: formatTime(value.ObservedAt), |
| 102 | + Source: workflowSourceRefResult(value.Source), DescriptionTruncated: value.DescriptionTruncated, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func workflowSourceRefResult(value domain.SourceRef) cli.WorkflowSourceRefResult { |
| 107 | + return cli.WorkflowSourceRefResult{ |
| 108 | + Source: value.Source, URL: value.URL, CommitSHA: value.CommitSHA, |
| 109 | + ObservedAt: formatTime(value.ObservedAt), AsOf: formatTime(value.AsOf), |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func workflowAuditResults(values []investigation.StatusChange) []cli.WorkflowAuditResult { |
| 114 | + if len(values) == 0 { |
| 115 | + return nil |
| 116 | + } |
| 117 | + result := make([]cli.WorkflowAuditResult, len(values)) |
| 118 | + for index, value := range values { |
| 119 | + result[index] = cli.WorkflowAuditResult{ |
| 120 | + From: value.From, To: value.To, Rationale: value.Rationale, At: formatTime(value.At), |
| 121 | + } |
| 122 | + } |
| 123 | + return result |
| 124 | +} |
| 125 | + |
| 126 | +func workflowSourceRefResults(values []domain.SourceRef) []cli.WorkflowSourceRefResult { |
| 127 | + if len(values) == 0 { |
| 128 | + return nil |
| 129 | + } |
| 130 | + result := make([]cli.WorkflowSourceRefResult, len(values)) |
| 131 | + for index, value := range values { |
| 132 | + result[index] = workflowSourceRefResult(value) |
| 133 | + } |
| 134 | + return result |
| 135 | +} |
| 136 | + |
| 137 | +func workflowLinkResults(values []investigation.Link) []cli.WorkflowLinkResult { |
| 138 | + if len(values) == 0 { |
| 139 | + return nil |
| 140 | + } |
| 141 | + result := make([]cli.WorkflowLinkResult, len(values)) |
| 142 | + for index, value := range values { |
| 143 | + result[index] = cli.WorkflowLinkResult{Kind: value.Kind, Ref: value.Ref, Source: workflowSourceRefResult(value.Source)} |
| 144 | + } |
| 145 | + return result |
| 146 | +} |
0 commit comments