intent-record 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +24 -0
- data/README.md +162 -2
- data/db/migrate/20260920000001_create_search_index.rb +64 -0
- data/lib/intent_record/backfill/backfilled_intent.rb +54 -0
- data/lib/intent_record/backfill/commit_specs.rb +39 -0
- data/lib/intent_record/backfill/inspection.rb +46 -0
- data/lib/intent_record/backfill/predecessor_finder.rb +49 -0
- data/lib/intent_record/backfill/reference_scanner.rb +70 -0
- data/lib/intent_record/backfill.rb +5 -0
- data/lib/intent_record/blame/porcelain.rb +37 -0
- data/lib/intent_record/blame/spans.rb +74 -0
- data/lib/intent_record/cli/blame_input.rb +23 -0
- data/lib/intent_record/cli/dispatch.rb +25 -0
- data/lib/intent_record/cli/usage.rb +26 -1
- data/lib/intent_record/commands/backfill.rb +160 -0
- data/lib/intent_record/commands/blame.rb +74 -0
- data/lib/intent_record/commands/lookup.rb +1 -1
- data/lib/intent_record/commands/search.rb +49 -22
- data/lib/intent_record/match_expression.rb +50 -0
- data/lib/intent_record/models/asset_version.rb +3 -1
- data/lib/intent_record/search_index.rb +7 -0
- data/lib/intent_record/search_membership.rb +62 -0
- data/lib/intent_record/search_ranking.rb +57 -0
- data/lib/intent_record/search_snippet.rb +52 -0
- data/lib/intent_record/search_term.rb +59 -0
- data/lib/intent_record/version.rb +1 -1
- data/lib/intent_record/web/views/_intent_cards.erb +3 -1
- data/lib/intent_record/web/views/search.erb +1 -1
- data/lib/intent_record.rb +9 -0
- metadata +19 -1
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
require_relative "../input_validator"
|
|
2
|
+
|
|
3
|
+
module IntentRecord
|
|
4
|
+
module Blame
|
|
5
|
+
# Reads a blame payload into the spans a reader is answered with.
|
|
6
|
+
#
|
|
7
|
+
# A blame tool answers per line, and a reader asked about a range. Answering
|
|
8
|
+
# per line would bury the useful part in repetition, so neighbouring lines
|
|
9
|
+
# from one change become one span. Neighbouring, not merely equal: the same
|
|
10
|
+
# change either side of someone else's edit is two places in the file, and
|
|
11
|
+
# one span across them would claim a line it does not own.
|
|
12
|
+
#
|
|
13
|
+
# Nothing here reaches the database, so a malformed payload is refused before
|
|
14
|
+
# the store is asked anything.
|
|
15
|
+
class Spans
|
|
16
|
+
Span = Struct.new(:external_id, :from, :to)
|
|
17
|
+
|
|
18
|
+
def initialize(lines)
|
|
19
|
+
@lines = lines
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def call
|
|
23
|
+
collapse(sorted)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def sorted
|
|
29
|
+
raise ValidationError, "At least one line is required" unless @lines.is_a?(Array) && @lines.any?
|
|
30
|
+
|
|
31
|
+
entries = @lines.map { |line| entry(line) }.sort_by(&:first)
|
|
32
|
+
reject_repeats(entries)
|
|
33
|
+
entries
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Sorted, so a repeat is adjacent. The same line attributed to two changes
|
|
37
|
+
# is a contradiction in the payload, and answering it would mean choosing
|
|
38
|
+
# one of them silently.
|
|
39
|
+
def reject_repeats(entries)
|
|
40
|
+
entries.each_cons(2) do |(earlier, _), (later, _)|
|
|
41
|
+
raise ValidationError, "Line #{earlier} is given more than once" if earlier == later
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def entry(line)
|
|
46
|
+
raise ValidationError, "Each line needs a line and an external_id" unless line.is_a?(Hash)
|
|
47
|
+
|
|
48
|
+
[line_number(line["line"]), InputValidator.required_string!(line, "external_id")]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# A string is read in base ten: Integer() on its own reads "010" as
|
|
52
|
+
# octal and answers about line 8, a wrong answer given confidently. A
|
|
53
|
+
# fraction is refused rather than truncated for the same reason.
|
|
54
|
+
def line_number(raw)
|
|
55
|
+
number = raw.is_a?(Integer) ? raw : Integer(raw.to_s, 10, exception: false)
|
|
56
|
+
raise ValidationError, "line must be a positive integer, got #{raw.inspect}" if number.nil? || number < 1
|
|
57
|
+
|
|
58
|
+
number
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Walks in file order, extending the span in hand while the next line both
|
|
62
|
+
# follows the last one and names the same change.
|
|
63
|
+
def collapse(entries)
|
|
64
|
+
entries.each_with_object([]) do |(number, id), spans|
|
|
65
|
+
extends?(spans.last, number, id) ? spans.last.to = number : spans << Span.new(id, number, number)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def extends?(span, number, id)
|
|
70
|
+
!span.nil? && span.external_id == id && span.to == number - 1
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require_relative "../blame/porcelain"
|
|
2
|
+
require_relative "stdin_json"
|
|
3
|
+
|
|
4
|
+
module IntentRecord
|
|
5
|
+
class CLI
|
|
6
|
+
# What `blame` reads from stdin. The JSON shape is the contract; a format
|
|
7
|
+
# names a reader that produces it from what some blame tool already prints.
|
|
8
|
+
module BlameInput
|
|
9
|
+
FORMATS = { "git-porcelain" => Blame::Porcelain }.freeze
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def read(format, stdin)
|
|
14
|
+
return StdinJson.read(stdin) if format.nil?
|
|
15
|
+
|
|
16
|
+
reader = FORMATS[format]
|
|
17
|
+
raise ValidationError, "Unknown --format #{format}. Known formats: #{FORMATS.keys.join(", ")}" if reader.nil?
|
|
18
|
+
|
|
19
|
+
{ "lines" => reader.lines(stdin.read) }
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -10,6 +10,9 @@ require_relative "../commands/by_source"
|
|
|
10
10
|
require_relative "../commands/recent"
|
|
11
11
|
require_relative "../commands/attach"
|
|
12
12
|
require_relative "../commands/systems"
|
|
13
|
+
require_relative "../commands/backfill"
|
|
14
|
+
require_relative "../commands/blame"
|
|
15
|
+
require_relative "blame_input"
|
|
13
16
|
|
|
14
17
|
module IntentRecord
|
|
15
18
|
class CLI
|
|
@@ -67,6 +70,13 @@ module IntentRecord
|
|
|
67
70
|
@parser.take_required_positional(label)
|
|
68
71
|
end
|
|
69
72
|
|
|
73
|
+
def required_flag(name)
|
|
74
|
+
value = @parser.take_flag(name)
|
|
75
|
+
raise ValidationError, "#{name} is required" if value.nil? || value.strip.empty?
|
|
76
|
+
|
|
77
|
+
value
|
|
78
|
+
end
|
|
79
|
+
|
|
70
80
|
def run_record
|
|
71
81
|
finish! { Commands::Record.new.call(stdin_json) }
|
|
72
82
|
end
|
|
@@ -104,6 +114,21 @@ module IntentRecord
|
|
|
104
114
|
finish! { command.call(stdin_json) }
|
|
105
115
|
end
|
|
106
116
|
|
|
117
|
+
def run_backfill
|
|
118
|
+
command = Commands::Backfill.scanning(system: required_flag("--system"),
|
|
119
|
+
pattern: required_flag("--pattern"),
|
|
120
|
+
uri_prefix: @parser.take_flag("--uri-prefix"),
|
|
121
|
+
dry_run: @parser.take_switch?("--dry-run"),
|
|
122
|
+
order: @parser.take_flag("--order") ||
|
|
123
|
+
Commands::Backfill::DEFAULT_ORDER)
|
|
124
|
+
finish! { command.call(stdin_json) }
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def run_blame
|
|
128
|
+
format = @parser.take_flag("--format")
|
|
129
|
+
finish! { Commands::Blame.new.call(BlameInput.read(format, @streams.stdin)) }
|
|
130
|
+
end
|
|
131
|
+
|
|
107
132
|
def run_systems
|
|
108
133
|
finish! { Commands::Systems.new.call }
|
|
109
134
|
end
|
|
@@ -8,10 +8,12 @@ module IntentRecord
|
|
|
8
8
|
attach <intent_id> Link more commits / stakeholder refs / related intents (JSON via stdin)
|
|
9
9
|
show <intent_id> Full intent record
|
|
10
10
|
lookup <commit> [--vcs name] Intents recorded against a commit (full hash or unique prefix)
|
|
11
|
-
search <terms...> [--match all]
|
|
11
|
+
search <terms...> [--match all] Search over summary, body and ticket links, best match first
|
|
12
12
|
by-source <uri> [--contains] Intents linked to a stakeholder uri (Jira ticket, Confluence page, ...)
|
|
13
13
|
recent [--limit N] Newest intents first
|
|
14
14
|
systems Known vcs and stakeholder system names
|
|
15
|
+
blame [--format git-porcelain] Intents for the commits a blame output names (JSON via stdin)
|
|
16
|
+
backfill [options] Recover intents from commit messages (JSON via stdin, see below)
|
|
15
17
|
serve [--port N] Start the local web GUI
|
|
16
18
|
|
|
17
19
|
Options:
|
|
@@ -19,6 +21,29 @@ module IntentRecord
|
|
|
19
21
|
--help, -h Show this help
|
|
20
22
|
--version Show version
|
|
21
23
|
|
|
24
|
+
Options for backfill:
|
|
25
|
+
--system <name> Stakeholder system the found references belong to (required)
|
|
26
|
+
--pattern <regex> What a reference looks like in a message (required).
|
|
27
|
+
A capture group narrows what is appended to the prefix
|
|
28
|
+
--uri-prefix <url> Prefix + key = source uri. Without it the whole match is the uri
|
|
29
|
+
--order <which-first> newest-first (default, as git log prints) or oldest-first
|
|
30
|
+
--dry-run Report what it would write, write nothing
|
|
31
|
+
|
|
32
|
+
Search terms:
|
|
33
|
+
Ranked best first. A plain word also matches its other forms, so `retry`
|
|
34
|
+
finds "retried". A term carrying punctuation is matched literally, so
|
|
35
|
+
`100%` does not match "100 percent". A term may name one field:
|
|
36
|
+
summary:retry, body:retry, uri:ACME-42, title:flaky. The name is read in
|
|
37
|
+
any case. Only those four names count, so a ticket url is searched as itself.
|
|
38
|
+
|
|
39
|
+
Input shape for blame (vcs defaults to git):
|
|
40
|
+
{"vcs": "git", "lines": [{"line": 40, "external_id": "<hash>"}]}
|
|
41
|
+
With --format git-porcelain, `git blame --porcelain` output goes in as-is.
|
|
42
|
+
|
|
43
|
+
Input shape for backfill:
|
|
44
|
+
{"commits": [{"commit": "<hash>", "message": "<commit message>",
|
|
45
|
+
"author": "...", "ref": "<branch name>"}]}
|
|
46
|
+
|
|
22
47
|
Input shape for record/attach (all keys optional except summary and body on record):
|
|
23
48
|
{"summary": "...", "body": "...", "author": "...",
|
|
24
49
|
"commits": ["<git hash>"], "asset_versions": [{"vcs": "perforce", "external_id": "123"}],
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
require_relative "record"
|
|
2
|
+
require_relative "../backfill/backfilled_intent"
|
|
3
|
+
require_relative "../backfill/commit_specs"
|
|
4
|
+
require_relative "../backfill/inspection"
|
|
5
|
+
require_relative "../backfill/predecessor_finder"
|
|
6
|
+
require_relative "../backfill/reference_scanner"
|
|
7
|
+
require_relative "../linkers/stakeholder_linker"
|
|
8
|
+
require_relative "../stakeholder_normalizer"
|
|
9
|
+
require_relative "../models/asset_version"
|
|
10
|
+
require_relative "../asset_version_normalizer"
|
|
11
|
+
|
|
12
|
+
module IntentRecord
|
|
13
|
+
module Commands
|
|
14
|
+
# Recovers intent records from a history of commit messages.
|
|
15
|
+
#
|
|
16
|
+
# One record per commit that names a reference, because `lookup <hash>` is
|
|
17
|
+
# meaningless if a record's summary describes forty other commits. A ticket
|
|
18
|
+
# spanning commits is what `by-source` already assembles from these.
|
|
19
|
+
#
|
|
20
|
+
# Each commit is its own transaction, so one malformed hash in a history of
|
|
21
|
+
# thousands costs that commit rather than the run. A commit that already has
|
|
22
|
+
# an intent is passed over, which is what makes a second run cheap: finding
|
|
23
|
+
# the convention you missed is the normal way to use this.
|
|
24
|
+
class Backfill
|
|
25
|
+
# `git log` prints newest first, which is what a person pipes in without
|
|
26
|
+
# thinking about it, so that is the default. The chain is written oldest
|
|
27
|
+
# to newest either way; this only says which end of the list is which.
|
|
28
|
+
ORDERS = %w[newest-first oldest-first].freeze
|
|
29
|
+
DEFAULT_ORDER = "newest-first".freeze
|
|
30
|
+
|
|
31
|
+
def self.scanning(system:, pattern:, uri_prefix: nil, **)
|
|
32
|
+
new(scanner: IntentRecord::Backfill::ReferenceScanner.new(system: system, pattern: pattern,
|
|
33
|
+
uri_prefix: uri_prefix),
|
|
34
|
+
**)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def initialize(scanner:, dry_run: false, order: DEFAULT_ORDER)
|
|
38
|
+
@scanner = scanner
|
|
39
|
+
@dry_run = dry_run
|
|
40
|
+
@order = validated_order(order)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def call(input)
|
|
44
|
+
specs = chronological(IntentRecord::Backfill::CommitSpecs.from(input))
|
|
45
|
+
seen = IntentRecord::Backfill::Inspection.new(@dry_run)
|
|
46
|
+
report(specs.map { |spec| process(spec, seen) }, seen)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def validated_order(order)
|
|
52
|
+
value = order.to_s.strip.downcase
|
|
53
|
+
return value if ORDERS.include?(value)
|
|
54
|
+
|
|
55
|
+
raise ValidationError, "--order must be one of #{ORDERS.join(", ")}, got #{order.inspect}"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Each record links to the one before it, so the commits are written in
|
|
59
|
+
# the order they were made whichever end of the history the caller gave.
|
|
60
|
+
def chronological(specs)
|
|
61
|
+
@order == DEFAULT_ORDER ? specs.reverse : specs
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# A commit already recorded keeps its record and gains whatever this pass
|
|
65
|
+
# found, because a second pass exists to link the convention the first
|
|
66
|
+
# pattern did not match. Skipping the commit whole would silently drop
|
|
67
|
+
# every reference in it.
|
|
68
|
+
def process(spec, seen)
|
|
69
|
+
references = @scanner.call(searchable(spec))
|
|
70
|
+
return seen.missed(subject(spec)) if references.empty?
|
|
71
|
+
|
|
72
|
+
write(spec, references, seen)
|
|
73
|
+
rescue Error => e
|
|
74
|
+
{ commit: spec[:commit], error: e.message }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Nothing this pass found is new, which is what a repeated pass looks
|
|
78
|
+
# like: neither created nor linked, and its sources are not offered as
|
|
79
|
+
# ones a dry run would add.
|
|
80
|
+
def write(spec, references, seen)
|
|
81
|
+
existing = recorded_intents(spec[:commit])
|
|
82
|
+
return :skipped if nothing_new?(existing, references)
|
|
83
|
+
|
|
84
|
+
seen.noted(references)
|
|
85
|
+
existing.empty? ? create(spec, references) : attach(existing, references)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def nothing_new?(existing, references)
|
|
89
|
+
existing.any? && already_linked?(existing, references)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def attach(existing, references)
|
|
93
|
+
return :linked if @dry_run
|
|
94
|
+
|
|
95
|
+
ActiveRecord::Base.transaction do
|
|
96
|
+
existing.each { |record| Linkers::StakeholderLinker.call(record, "stakeholder_references" => references) }
|
|
97
|
+
end
|
|
98
|
+
:linked
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def already_linked?(existing, references)
|
|
102
|
+
wanted = references.map { |r| StakeholderNormalizer.uri(r["uri"]) }
|
|
103
|
+
existing.all? { |record| (wanted - record.stakeholder_sources.map(&:uri)).empty? }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def create(spec, references)
|
|
107
|
+
@dry_run ? :created : write!(spec, references)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def subject(spec)
|
|
111
|
+
spec[:message].strip.lines.first.to_s.strip
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# The key is as often in the branch name as in the message, and a team
|
|
115
|
+
# that puts it there never puts it in both.
|
|
116
|
+
def searchable(spec)
|
|
117
|
+
[spec[:message], spec[:ref]].compact.join("\n")
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def write!(spec, references)
|
|
121
|
+
ActiveRecord::Base.transaction { Commands::Record.new.call(payload(spec, references)) }
|
|
122
|
+
:created
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def payload(spec, references)
|
|
126
|
+
intent = IntentRecord::Backfill::BackfilledIntent.new(message: spec[:message], author: spec[:author])
|
|
127
|
+
intent.to_h.merge("commits" => [spec[:commit]],
|
|
128
|
+
"stakeholder_references" => references,
|
|
129
|
+
"related_intent_ids" => IntentRecord::Backfill::PredecessorFinder.for(references))
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Asked of the store rather than remembered, so a commit recorded by an
|
|
133
|
+
# earlier run or by hand is treated the same.
|
|
134
|
+
def recorded_intents(commit)
|
|
135
|
+
version = Models::AssetVersion.joins(:vcs_system)
|
|
136
|
+
.find_by(vcs_systems: { name: "git" }, external_id: lookup_id(commit))
|
|
137
|
+
version ? version.intent_records.to_a : []
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# A hash that is not a hash cannot match anything stored, and judging its
|
|
141
|
+
# shape here would refuse it before the per-commit rescue can report it.
|
|
142
|
+
def lookup_id(commit)
|
|
143
|
+
AssetVersionNormalizer.lookup_id("git", commit)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def report(outcomes, seen)
|
|
147
|
+
counts(outcomes).merge(seen.to_h)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def counts(outcomes)
|
|
151
|
+
failures = outcomes.grep(Hash)
|
|
152
|
+
{ "created" => outcomes.count(:created),
|
|
153
|
+
"linked" => outcomes.count(:linked),
|
|
154
|
+
"skipped" => outcomes.count(:skipped),
|
|
155
|
+
"failed" => failures.size,
|
|
156
|
+
"failures" => failures.map { |f| { "commit" => f[:commit], "error" => f[:error] } } }
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
require_relative "../blame/spans"
|
|
2
|
+
require_relative "../blame/porcelain"
|
|
3
|
+
require_relative "../asset_version_normalizer"
|
|
4
|
+
require_relative "../formatter"
|
|
5
|
+
require_relative "../models/asset_version"
|
|
6
|
+
|
|
7
|
+
module IntentRecord
|
|
8
|
+
module Commands
|
|
9
|
+
# Answers "why is this line here" for a range of lines.
|
|
10
|
+
#
|
|
11
|
+
# Blame output goes in on stdin rather than the tool shelling out to git, so
|
|
12
|
+
# the same command answers for Perforce or Mercurial and the store keeps
|
|
13
|
+
# knowing nothing about which repository a commit belongs to.
|
|
14
|
+
class Blame
|
|
15
|
+
DEFAULT_VCS = "git".freeze
|
|
16
|
+
|
|
17
|
+
# Git's all-zero object id, which blame gives a line that is in the working
|
|
18
|
+
# copy and not in the history. It is not a commit, so a reader is told the
|
|
19
|
+
# line is not committed rather than that nothing was recorded against it,
|
|
20
|
+
# and the store is not asked about it at all: the id is a valid shape, so
|
|
21
|
+
# a record can sit under it, and one did, but a line with no history cannot
|
|
22
|
+
# have had anything recorded against it.
|
|
23
|
+
# Forty zeros, or sixty-four in a repository using the sha256 object format.
|
|
24
|
+
UNCOMMITTED = /\A(?:0{40}|0{64})\z/
|
|
25
|
+
|
|
26
|
+
def call(input)
|
|
27
|
+
vcs = AssetVersionNormalizer.vcs_name(input["vcs"] || DEFAULT_VCS)
|
|
28
|
+
spans = IntentRecord::Blame::Spans.new(input["lines"]).call
|
|
29
|
+
{ "spans" => answers(spans, vcs) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def answers(spans, vcs)
|
|
35
|
+
known = versions(spans.reject { |span| uncommitted?(span, vcs) }, vcs)
|
|
36
|
+
spans.map { |span| answer(span, vcs, known[AssetVersionNormalizer.lookup_id(vcs, span.external_id)]) }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Every span is answered, including one whose change the store has never
|
|
40
|
+
# heard of. Leaving it out would read as "this line has no history", which
|
|
41
|
+
# is the opposite of what an empty list of intents says.
|
|
42
|
+
def answer(span, vcs, version)
|
|
43
|
+
answered = {
|
|
44
|
+
"from" => span.from,
|
|
45
|
+
"to" => span.to,
|
|
46
|
+
"asset_version" => { "vcs" => vcs, "external_id" => span.external_id },
|
|
47
|
+
"intents" => intents(version)
|
|
48
|
+
}
|
|
49
|
+
uncommitted?(span, vcs) ? answered.merge("uncommitted" => true) : answered
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def uncommitted?(span, vcs)
|
|
53
|
+
vcs == DEFAULT_VCS && UNCOMMITTED.match?(span.external_id)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def intents(version)
|
|
57
|
+
return [] if version.nil?
|
|
58
|
+
|
|
59
|
+
version.intent_records.map { |record| Formatter.full(record) }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Read once for the whole payload rather than per span. A blamed range is a
|
|
63
|
+
# handful of changes however many lines it covers, and asking per span
|
|
64
|
+
# would make the query count grow with the size of the range.
|
|
65
|
+
def versions(spans, vcs)
|
|
66
|
+
ids = spans.map { |span| AssetVersionNormalizer.lookup_id(vcs, span.external_id) }.uniq
|
|
67
|
+
scope = Models::AssetVersion.joins(:vcs_system)
|
|
68
|
+
.where(vcs_systems: { name: vcs }, external_id: ids)
|
|
69
|
+
.preload(intent_records: Formatter::PRELOADS)
|
|
70
|
+
scope.index_by(&:external_id)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -14,7 +14,7 @@ module IntentRecord
|
|
|
14
14
|
version = AssetVersionResolver.new(external_id: @external_id, vcs: @vcs).call
|
|
15
15
|
{
|
|
16
16
|
"asset_version" => Formatter.asset_version(version),
|
|
17
|
-
"intents" => Formatter.preloaded(version.intent_records
|
|
17
|
+
"intents" => Formatter.preloaded(version.intent_records).map { |r| Formatter.full(r) }
|
|
18
18
|
}
|
|
19
19
|
end
|
|
20
20
|
end
|
|
@@ -1,49 +1,76 @@
|
|
|
1
1
|
require_relative "../formatter"
|
|
2
|
-
require_relative "../
|
|
2
|
+
require_relative "../match_expression"
|
|
3
|
+
require_relative "../search_membership"
|
|
4
|
+
require_relative "../search_ranking"
|
|
5
|
+
require_relative "../search_snippet"
|
|
6
|
+
require_relative "../search_term"
|
|
3
7
|
require_relative "../models/intent_record"
|
|
4
8
|
|
|
5
9
|
module IntentRecord
|
|
6
10
|
module Commands
|
|
7
|
-
#
|
|
8
|
-
#
|
|
11
|
+
# Search over summary, body, and linked stakeholder uris and titles, so a
|
|
12
|
+
# ticket key such as ACME-42 finds the intents built for it.
|
|
13
|
+
#
|
|
14
|
+
# Which records answer is SearchMembership's decision and what order they
|
|
15
|
+
# answer in is SearchRanking's. The two are kept apart because they read a
|
|
16
|
+
# term differently, and only one of them may be trusted with a term that
|
|
17
|
+
# carries punctuation.
|
|
9
18
|
class Search
|
|
10
19
|
LIMIT = 200
|
|
11
|
-
|
|
20
|
+
MATCH_MODES = %w[any all].freeze
|
|
12
21
|
|
|
22
|
+
# Refuses bad input here, before the store is asked anything.
|
|
13
23
|
def initialize(terms:, match: "any")
|
|
14
|
-
|
|
24
|
+
raw = terms.map(&:strip).reject(&:empty?)
|
|
25
|
+
raise ValidationError, "At least one search term is required" if raw.empty?
|
|
26
|
+
raise ValidationError, "--match must be any or all" unless MATCH_MODES.include?(match)
|
|
27
|
+
|
|
28
|
+
@terms = raw.map { |term| SearchTerm.parse(term) }
|
|
15
29
|
@match = match
|
|
16
30
|
end
|
|
17
31
|
|
|
18
32
|
def call
|
|
19
|
-
|
|
20
|
-
raise ValidationError, "--match must be any or all" unless %w[any all].include?(@match)
|
|
21
|
-
|
|
22
|
-
{ "intents" => matching_records.map { |r| Formatter.full(r) } }
|
|
33
|
+
{ "intents" => matching_records.map { |r| formatted(r) } }
|
|
23
34
|
end
|
|
24
35
|
|
|
25
36
|
private
|
|
26
37
|
|
|
38
|
+
# The snippet belongs to the search rather than to the record, so it is
|
|
39
|
+
# merged here instead of in Formatter, which every other command shares.
|
|
40
|
+
def formatted(record)
|
|
41
|
+
snippet = SearchSnippet.text(indexed: record[SearchSnippet::COLUMN], body: record.body)
|
|
42
|
+
Formatter.full(record).merge(SearchSnippet::COLUMN => snippet)
|
|
43
|
+
end
|
|
44
|
+
|
|
27
45
|
def matching_records
|
|
28
|
-
Formatter.preloaded(
|
|
29
|
-
Models::IntentRecord.left_joins(:stakeholder_sources)
|
|
30
|
-
.group("intent_records.id")
|
|
31
|
-
.having(having_sql, *having_binds)
|
|
32
|
-
.order(created_at: :desc).limit(LIMIT)
|
|
33
|
-
)
|
|
46
|
+
Formatter.preloaded(with_ranking(matching_scope))
|
|
34
47
|
end
|
|
35
48
|
|
|
36
|
-
def
|
|
37
|
-
|
|
38
|
-
|
|
49
|
+
def matching_scope
|
|
50
|
+
Models::IntentRecord.left_joins(:stakeholder_sources)
|
|
51
|
+
.group("intent_records.id")
|
|
52
|
+
.having(*SearchMembership.new(terms: @terms, match: @match).condition)
|
|
53
|
+
.limit(LIMIT)
|
|
39
54
|
end
|
|
40
55
|
|
|
41
|
-
|
|
42
|
-
|
|
56
|
+
# Only the terms the index may answer rank anything. A term it may not be
|
|
57
|
+
# trusted with would rank by text the person did not ask for, which is the
|
|
58
|
+
# same reason it does not decide membership either.
|
|
59
|
+
def indexable
|
|
60
|
+
@terms.select(&:indexable?)
|
|
43
61
|
end
|
|
44
62
|
|
|
45
|
-
|
|
46
|
-
|
|
63
|
+
# Joins the relevance and fragment the index found, and orders by them.
|
|
64
|
+
# Terms that hold no searchable token leave nothing to rank, and an empty
|
|
65
|
+
# MATCH is a syntax error rather than an expression matching nothing, so
|
|
66
|
+
# that case orders by the tie-break alone and the body stands in for the
|
|
67
|
+
# fragment.
|
|
68
|
+
def with_ranking(scope)
|
|
69
|
+
expression = MatchExpression.for(indexable)
|
|
70
|
+
return scope.order(Arel.sql(SearchRanking::TIE_BREAK)) if expression.nil?
|
|
71
|
+
|
|
72
|
+
join = Models::IntentRecord.sanitize_sql_array([SearchRanking::JOIN, expression])
|
|
73
|
+
scope.select(SearchRanking::SELECTION).joins(join).order(Arel.sql(SearchRanking::ORDER))
|
|
47
74
|
end
|
|
48
75
|
end
|
|
49
76
|
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module IntentRecord
|
|
2
|
+
# Turns the terms a user typed into an FTS5 MATCH expression.
|
|
3
|
+
#
|
|
4
|
+
# FTS5 reads its right-hand side as an expression language, not as text: `100%`
|
|
5
|
+
# is a syntax error and a term such as `OR` or `NEAR` is an operator. Quoting
|
|
6
|
+
# each term makes it a phrase, which is the only form that carries whatever the
|
|
7
|
+
# user typed through the parser unread.
|
|
8
|
+
#
|
|
9
|
+
# Phrases are joined with OR. The expression is used twice, and OR is right
|
|
10
|
+
# for both: SearchMembership asks the index one term at a time, so an
|
|
11
|
+
# expression there holds a single phrase and the joiner never applies; and
|
|
12
|
+
# SearchRanking asks it about every term at once, where a record is scored on
|
|
13
|
+
# whichever terms it holds rather than dropped for lacking one, because which
|
|
14
|
+
# records answer has already been settled and AND would score some of them at
|
|
15
|
+
# nothing.
|
|
16
|
+
module MatchExpression
|
|
17
|
+
JOINER = " OR ".freeze
|
|
18
|
+
QUOTE = '"'.freeze
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
# Takes SearchTerms, which carry the text and the column the person named.
|
|
23
|
+
def for(terms)
|
|
24
|
+
phrases = terms.map { |term| phrase(term) }.compact
|
|
25
|
+
return nil if phrases.empty?
|
|
26
|
+
|
|
27
|
+
phrases.join(JOINER)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def phrase(term)
|
|
31
|
+
# A term of pure punctuation tokenises to nothing, and an empty phrase is
|
|
32
|
+
# a syntax error rather than a phrase that matches nothing.
|
|
33
|
+
return nil unless term.text.match?(/[[:alnum:]]/)
|
|
34
|
+
|
|
35
|
+
column(term.index_column) + quoted(term.text)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# FTS5 reads `summary:"retry"` as that phrase in that column only. A term
|
|
39
|
+
# naming no column is left unprefixed and is answered over all of them.
|
|
40
|
+
def column(name)
|
|
41
|
+
name.nil? ? "" : "#{name}:"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def quoted(text)
|
|
45
|
+
"#{QUOTE}#{text.gsub(QUOTE, QUOTE * 2)}#{QUOTE}"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private_class_method :phrase, :column, :quoted
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -4,7 +4,9 @@ module IntentRecord
|
|
|
4
4
|
class AssetVersion < ApplicationRecord
|
|
5
5
|
belongs_to :vcs_system
|
|
6
6
|
has_many :intent_record_asset_versions, dependent: :destroy
|
|
7
|
-
|
|
7
|
+
# Oldest first, then id, so a commit carrying several intents reads as a
|
|
8
|
+
# story and the order is the query's rather than the plan's or Ruby's.
|
|
9
|
+
has_many :intent_records, -> { order(:created_at, :id) }, through: :intent_record_asset_versions
|
|
8
10
|
|
|
9
11
|
validates :external_id, presence: true, uniqueness: { scope: :vcs_system_id }
|
|
10
12
|
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
require_relative "like_pattern"
|
|
2
|
+
require_relative "match_expression"
|
|
3
|
+
require_relative "search_ranking"
|
|
4
|
+
require_relative "search_term"
|
|
5
|
+
|
|
6
|
+
module IntentRecord
|
|
7
|
+
# Which records a search answers with.
|
|
8
|
+
#
|
|
9
|
+
# Two ways of matching sit behind one condition. Substring matching reads a
|
|
10
|
+
# term as the literal it is, which is what a person typing `100%` or `ACME-4`
|
|
11
|
+
# is asking for. The index reads it as a word, which is what finds `retried`
|
|
12
|
+
# for someone who typed `retry`.
|
|
13
|
+
#
|
|
14
|
+
# They are not interchangeable, and the index is the narrower of the two in
|
|
15
|
+
# one direction and the wider in the other. It cannot see inside a word, so it
|
|
16
|
+
# would lose `ACME-4` in `ACME-42`; and it drops punctuation, so, measured, it
|
|
17
|
+
# matches `100 percent` for `100%` and `done now` for `done_now`. A term the
|
|
18
|
+
# index may not be trusted with therefore gets the substring arm alone, which
|
|
19
|
+
# SearchTerm#indexable? decides.
|
|
20
|
+
class SearchMembership
|
|
21
|
+
JOINERS = { "all" => " AND ", "any" => " OR " }.freeze
|
|
22
|
+
|
|
23
|
+
def initialize(terms:, match:)
|
|
24
|
+
@terms = terms
|
|
25
|
+
@match = match
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Ready for `having`: the condition followed by its binds, in order.
|
|
29
|
+
def condition
|
|
30
|
+
clauses = @terms.map { |term| clause(term) }
|
|
31
|
+
[clauses.map(&:first).join(JOINERS.fetch(@match)), *clauses.flat_map { |c| c.drop(1) }]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def clause(term)
|
|
37
|
+
arms = [substring_arm(term), *index_arm(term)]
|
|
38
|
+
["(#{arms.join(" OR ")})", *substring_binds(term), *index_binds(term)]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Counted rather than tested for, because the join to stakeholder_sources
|
|
42
|
+
# gives a record one row per source and the condition is read after grouping.
|
|
43
|
+
def substring_arm(term)
|
|
44
|
+
"SUM(CASE WHEN #{term.fields.map { |f| LikePattern.contains(f) }.join(" OR ")} THEN 1 ELSE 0 END) > 0"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def substring_binds(term)
|
|
48
|
+
[LikePattern.contains_bind(term.text)] * term.fields.size
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def index_arm(term)
|
|
52
|
+
return [] unless term.indexable?
|
|
53
|
+
|
|
54
|
+
["EXISTS (SELECT 1 FROM #{SearchRanking::TABLE} " \
|
|
55
|
+
"WHERE #{SearchRanking::TABLE} MATCH ? AND #{SearchRanking::TABLE}.rowid = intent_records.id)"]
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def index_binds(term)
|
|
59
|
+
term.indexable? ? [MatchExpression.for([term])] : []
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|