git-contribution-analyzer 0.8.0__py3-none-any.whl
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.
- git_contribution_analyzer/__init__.py +3 -0
- git_contribution_analyzer/adapters/__init__.py +1 -0
- git_contribution_analyzer/adapters/git/__init__.py +1 -0
- git_contribution_analyzer/adapters/git/local_identity.py +51 -0
- git_contribution_analyzer/adapters/git/native_git.py +353 -0
- git_contribution_analyzer/adapters/git/pydriller_history.py +57 -0
- git_contribution_analyzer/adapters/git/repository_discovery.py +75 -0
- git_contribution_analyzer/adapters/gui/__init__.py +1 -0
- git_contribution_analyzer/adapters/gui/app.py +63 -0
- git_contribution_analyzer/adapters/gui/controller.py +1084 -0
- git_contribution_analyzer/adapters/gui/models/__init__.py +3 -0
- git_contribution_analyzer/adapters/gui/models/base_table.py +108 -0
- git_contribution_analyzer/adapters/gui/settings.py +53 -0
- git_contribution_analyzer/adapters/gui/task_runner.py +196 -0
- git_contribution_analyzer/adapters/llm/__init__.py +1 -0
- git_contribution_analyzer/adapters/llm/anthropic.py +52 -0
- git_contribution_analyzer/adapters/llm/base.py +142 -0
- git_contribution_analyzer/adapters/llm/cache.py +148 -0
- git_contribution_analyzer/adapters/llm/claude_cli.py +52 -0
- git_contribution_analyzer/adapters/llm/codex_cli.py +52 -0
- git_contribution_analyzer/adapters/llm/command_base.py +153 -0
- git_contribution_analyzer/adapters/llm/mock.py +82 -0
- git_contribution_analyzer/adapters/llm/ollama.py +53 -0
- git_contribution_analyzer/adapters/llm/openai_compatible.py +61 -0
- git_contribution_analyzer/adapters/llm/registry.py +117 -0
- git_contribution_analyzer/adapters/mcp/__init__.py +13 -0
- git_contribution_analyzer/adapters/mcp/installer.py +175 -0
- git_contribution_analyzer/adapters/mcp/prompts_resources.py +218 -0
- git_contribution_analyzer/adapters/mcp/protocol.py +361 -0
- git_contribution_analyzer/adapters/mcp/server.py +52 -0
- git_contribution_analyzer/adapters/mcp/tools.py +621 -0
- git_contribution_analyzer/adapters/outcomes/__init__.py +1 -0
- git_contribution_analyzer/adapters/outcomes/yaml_loader.py +55 -0
- git_contribution_analyzer/adapters/outcomes/yaml_store.py +104 -0
- git_contribution_analyzer/adapters/reporting/__init__.py +1 -0
- git_contribution_analyzer/adapters/reporting/brag_markdown.py +115 -0
- git_contribution_analyzer/adapters/reporting/html_report.py +1413 -0
- git_contribution_analyzer/adapters/reporting/markdown.py +217 -0
- git_contribution_analyzer/adapters/reporting/portfolio_markdown.py +167 -0
- git_contribution_analyzer/adapters/reporting/recap_markdown.py +212 -0
- git_contribution_analyzer/adapters/reporting/resume_markdown.py +37 -0
- git_contribution_analyzer/adapters/reporting/work_assessment_csv.py +117 -0
- git_contribution_analyzer/adapters/reporting/work_assessment_markdown.py +90 -0
- git_contribution_analyzer/adapters/reporting/work_assessment_series_markdown.py +31 -0
- git_contribution_analyzer/adapters/storage/__init__.py +1 -0
- git_contribution_analyzer/adapters/storage/sqlite/__init__.py +1 -0
- git_contribution_analyzer/adapters/storage/sqlite/analysis.py +708 -0
- git_contribution_analyzer/adapters/storage/sqlite/database.py +98 -0
- git_contribution_analyzer/adapters/storage/sqlite/git_index.py +482 -0
- git_contribution_analyzer/adapters/storage/sqlite/identity_merge.py +373 -0
- git_contribution_analyzer/adapters/storage/sqlite/llm_audit.py +109 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/env.py +34 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/script.py.mako +22 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0001_initial.py +51 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0002_git_index.py +146 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0003_analysis.py +79 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0004_llm_audit.py +69 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0005_run_types.py +42 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0006_identity_merges.py +83 -0
- git_contribution_analyzer/adapters/storage/sqlite/migrations/versions/0007_structural_baselines.py +173 -0
- git_contribution_analyzer/adapters/storage/sqlite/models.py +354 -0
- git_contribution_analyzer/adapters/storage/sqlite/structural_baseline.py +841 -0
- git_contribution_analyzer/adapters/workspace/__init__.py +1 -0
- git_contribution_analyzer/adapters/workspace/config.py +135 -0
- git_contribution_analyzer/adapters/workspace/git_exclude.py +32 -0
- git_contribution_analyzer/adapters/workspace/layout.py +40 -0
- git_contribution_analyzer/adapters/workspace/locks.py +27 -0
- git_contribution_analyzer/adapters/workspace/portfolio_workspace.py +380 -0
- git_contribution_analyzer/adapters/workspace/ranking_config.py +17 -0
- git_contribution_analyzer/application/__init__.py +1 -0
- git_contribution_analyzer/application/dto/__init__.py +19 -0
- git_contribution_analyzer/application/dto/gui.py +86 -0
- git_contribution_analyzer/application/dto/results.py +21 -0
- git_contribution_analyzer/application/facades/__init__.py +3 -0
- git_contribution_analyzer/application/facades/gui.py +568 -0
- git_contribution_analyzer/application/ports/__init__.py +22 -0
- git_contribution_analyzer/application/ports/cancellation.py +50 -0
- git_contribution_analyzer/application/ports/git_history.py +22 -0
- git_contribution_analyzer/application/ports/llm.py +44 -0
- git_contribution_analyzer/application/ports/portfolio_store.py +34 -0
- git_contribution_analyzer/application/ports/progress.py +43 -0
- git_contribution_analyzer/application/ports/structural_baseline.py +93 -0
- git_contribution_analyzer/application/services/__init__.py +1 -0
- git_contribution_analyzer/application/services/assessment_explanation.py +127 -0
- git_contribution_analyzer/application/services/default_person.py +61 -0
- git_contribution_analyzer/application/services/evidence_snapshot.py +64 -0
- git_contribution_analyzer/application/services/person_selection.py +88 -0
- git_contribution_analyzer/application/services/resume_generation.py +318 -0
- git_contribution_analyzer/application/services/semantic_enhancement.py +157 -0
- git_contribution_analyzer/application/services/time_boundaries.py +49 -0
- git_contribution_analyzer/application/use_cases/__init__.py +1 -0
- git_contribution_analyzer/application/use_cases/analyze_contributions.py +276 -0
- git_contribution_analyzer/application/use_cases/analyze_project.py +295 -0
- git_contribution_analyzer/application/use_cases/assess_portfolio.py +327 -0
- git_contribution_analyzer/application/use_cases/assess_work.py +602 -0
- git_contribution_analyzer/application/use_cases/assess_work_series.py +212 -0
- git_contribution_analyzer/application/use_cases/compare_assessment_runs.py +121 -0
- git_contribution_analyzer/application/use_cases/export_report.py +49 -0
- git_contribution_analyzer/application/use_cases/generate_brag.py +308 -0
- git_contribution_analyzer/application/use_cases/generate_portfolio_recap.py +244 -0
- git_contribution_analyzer/application/use_cases/generate_portfolio_resume.py +152 -0
- git_contribution_analyzer/application/use_cases/generate_recap.py +316 -0
- git_contribution_analyzer/application/use_cases/generate_report.py +32 -0
- git_contribution_analyzer/application/use_cases/generate_resume.py +209 -0
- git_contribution_analyzer/application/use_cases/get_portfolio_status.py +83 -0
- git_contribution_analyzer/application/use_cases/get_run.py +16 -0
- git_contribution_analyzer/application/use_cases/get_status.py +69 -0
- git_contribution_analyzer/application/use_cases/index_repository.py +267 -0
- git_contribution_analyzer/application/use_cases/init_portfolio.py +89 -0
- git_contribution_analyzer/application/use_cases/init_project.py +71 -0
- git_contribution_analyzer/application/use_cases/list_identities.py +16 -0
- git_contribution_analyzer/application/use_cases/list_providers.py +29 -0
- git_contribution_analyzer/application/use_cases/list_runs.py +17 -0
- git_contribution_analyzer/application/use_cases/manage_identity_merges.py +48 -0
- git_contribution_analyzer/application/use_cases/manage_outcomes.py +189 -0
- git_contribution_analyzer/application/use_cases/manage_structural_baselines.py +262 -0
- git_contribution_analyzer/application/use_cases/map_identity.py +28 -0
- git_contribution_analyzer/application/use_cases/recover_runs.py +22 -0
- git_contribution_analyzer/application/use_cases/run_doctor.py +81 -0
- git_contribution_analyzer/application/use_cases/sync_portfolio.py +232 -0
- git_contribution_analyzer/application/use_cases/test_provider.py +22 -0
- git_contribution_analyzer/application/use_cases/uninit_project.py +23 -0
- git_contribution_analyzer/application/use_cases/verify_portfolio.py +165 -0
- git_contribution_analyzer/cli/__init__.py +1 -0
- git_contribution_analyzer/cli/app.py +1430 -0
- git_contribution_analyzer/cli/exit_codes.py +13 -0
- git_contribution_analyzer/cli/output.py +26 -0
- git_contribution_analyzer/cli/portfolio_commands.py +629 -0
- git_contribution_analyzer/domain/__init__.py +1 -0
- git_contribution_analyzer/domain/errors.py +55 -0
- git_contribution_analyzer/domain/models/__init__.py +3 -0
- git_contribution_analyzer/domain/models/analysis.py +29 -0
- git_contribution_analyzer/domain/models/assessment.py +12 -0
- git_contribution_analyzer/domain/models/brag.py +148 -0
- git_contribution_analyzer/domain/models/contribution.py +55 -0
- git_contribution_analyzer/domain/models/git_history.py +69 -0
- git_contribution_analyzer/domain/models/person_selection.py +46 -0
- git_contribution_analyzer/domain/models/portfolio.py +498 -0
- git_contribution_analyzer/domain/models/ranking.py +48 -0
- git_contribution_analyzer/domain/models/recap.py +235 -0
- git_contribution_analyzer/domain/models/repository.py +10 -0
- git_contribution_analyzer/domain/models/resume.py +59 -0
- git_contribution_analyzer/domain/models/run.py +10 -0
- git_contribution_analyzer/domain/models/semantic.py +34 -0
- git_contribution_analyzer/domain/models/snapshot.py +208 -0
- git_contribution_analyzer/domain/models/structural_baseline.py +120 -0
- git_contribution_analyzer/domain/models/work_assessment.py +81 -0
- git_contribution_analyzer/domain/services/assessment_periods.py +182 -0
- git_contribution_analyzer/domain/services/brag_rules.py +250 -0
- git_contribution_analyzer/domain/services/capability_rules.py +91 -0
- git_contribution_analyzer/domain/services/contribution_clustering.py +98 -0
- git_contribution_analyzer/domain/services/contribution_rules.py +79 -0
- git_contribution_analyzer/domain/services/difficulty_rules.py +191 -0
- git_contribution_analyzer/domain/services/dimensional_summary.py +117 -0
- git_contribution_analyzer/domain/services/portfolio_aggregation.py +250 -0
- git_contribution_analyzer/domain/services/portfolio_resume_rules.py +265 -0
- git_contribution_analyzer/domain/services/portfolio_verifier.py +238 -0
- git_contribution_analyzer/domain/services/ranking_rules.py +226 -0
- git_contribution_analyzer/domain/services/recap_rules.py +268 -0
- git_contribution_analyzer/domain/services/resume_claim_rules.py +70 -0
- git_contribution_analyzer/domain/services/structural_baseline.py +317 -0
- git_contribution_analyzer/domain/services/structural_signals.py +151 -0
- git_contribution_analyzer/domain/services/workload_rules.py +185 -0
- git_contribution_analyzer/gui/__init__.py +1 -0
- git_contribution_analyzer/gui/qml/Main.qml +1141 -0
- git_contribution_analyzer/gui/qml/__init__.py +1 -0
- git_contribution_analyzer/prompts/__init__.py +1 -0
- git_contribution_analyzer/prompts/assessment/__init__.py +1 -0
- git_contribution_analyzer/prompts/assessment/v1/__init__.py +1 -0
- git_contribution_analyzer/prompts/assessment/v1/explanation.txt +4 -0
- git_contribution_analyzer/prompts/resume/__init__.py +1 -0
- git_contribution_analyzer/prompts/resume/v1/__init__.py +1 -0
- git_contribution_analyzer/prompts/resume/v1/style-concise.txt +1 -0
- git_contribution_analyzer/prompts/resume/v1/style-star.txt +1 -0
- git_contribution_analyzer/prompts/resume/v1/style-xyz.txt +1 -0
- git_contribution_analyzer/prompts/resume/v1/system.txt +3 -0
- git_contribution_analyzer/prompts/v1/__init__.py +1 -0
- git_contribution_analyzer/prompts/v1/capability_explanation.txt +2 -0
- git_contribution_analyzer/prompts/v1/contribution_summary.txt +2 -0
- git_contribution_analyzer/prompts/v1/overall_summary.txt +2 -0
- git_contribution_analyzer/prompts/v1/resume_bullet.txt +2 -0
- git_contribution_analyzer-0.8.0.dist-info/METADATA +667 -0
- git_contribution_analyzer-0.8.0.dist-info/RECORD +187 -0
- git_contribution_analyzer-0.8.0.dist-info/WHEEL +4 -0
- git_contribution_analyzer-0.8.0.dist-info/entry_points.txt +3 -0
- git_contribution_analyzer-0.8.0.dist-info/licenses/LICENSE +201 -0
- git_contribution_analyzer-0.8.0.dist-info/licenses/NOTICE +6 -0
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from datetime import UTC, datetime
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Any
|
|
7
|
+
from uuid import uuid4
|
|
8
|
+
|
|
9
|
+
from sqlalchemy import Connection, func, insert, select, update
|
|
10
|
+
|
|
11
|
+
from git_contribution_analyzer.adapters.storage.sqlite.database import create_database_engine
|
|
12
|
+
from git_contribution_analyzer.adapters.storage.sqlite.models import (
|
|
13
|
+
analysis_runs,
|
|
14
|
+
identity_aliases,
|
|
15
|
+
identity_merge_events,
|
|
16
|
+
persons,
|
|
17
|
+
repositories,
|
|
18
|
+
)
|
|
19
|
+
from git_contribution_analyzer.domain.errors import IdentityResolutionError, WorkspaceError
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def reassign_alias(
|
|
23
|
+
connection: Connection,
|
|
24
|
+
repository_id: str,
|
|
25
|
+
alias_id: str,
|
|
26
|
+
target_person_id: str,
|
|
27
|
+
) -> str | None:
|
|
28
|
+
alias = connection.execute(
|
|
29
|
+
select(identity_aliases).where(
|
|
30
|
+
identity_aliases.c.id == alias_id,
|
|
31
|
+
identity_aliases.c.repository_id == repository_id,
|
|
32
|
+
)
|
|
33
|
+
).mappings().one_or_none()
|
|
34
|
+
if alias is None:
|
|
35
|
+
raise WorkspaceError(f"Identity alias not found: {alias_id}")
|
|
36
|
+
source_person_id = str(alias["person_id"])
|
|
37
|
+
if source_person_id == target_person_id:
|
|
38
|
+
return None
|
|
39
|
+
|
|
40
|
+
source = connection.execute(
|
|
41
|
+
select(persons).where(
|
|
42
|
+
persons.c.id == source_person_id,
|
|
43
|
+
persons.c.repository_id == repository_id,
|
|
44
|
+
)
|
|
45
|
+
).mappings().one()
|
|
46
|
+
target_exists = connection.execute(
|
|
47
|
+
select(func.count()).select_from(persons).where(
|
|
48
|
+
persons.c.id == target_person_id,
|
|
49
|
+
persons.c.repository_id == repository_id,
|
|
50
|
+
)
|
|
51
|
+
).scalar_one()
|
|
52
|
+
if not target_exists:
|
|
53
|
+
raise WorkspaceError(f"Identity map target not found: {target_person_id}")
|
|
54
|
+
|
|
55
|
+
source_snapshot = {
|
|
56
|
+
"person": {
|
|
57
|
+
"id": source_person_id,
|
|
58
|
+
"name": str(source["canonical_name"]),
|
|
59
|
+
"email": str(source["canonical_email"]),
|
|
60
|
+
"kind": str(source["kind"]),
|
|
61
|
+
"confirmed": bool(source["confirmed"]),
|
|
62
|
+
"active": bool(source["active"]),
|
|
63
|
+
"mergedIntoPersonId": source["merged_into_person_id"],
|
|
64
|
+
},
|
|
65
|
+
"aliasIds": [alias_id],
|
|
66
|
+
}
|
|
67
|
+
event_id = str(uuid4())
|
|
68
|
+
connection.execute(
|
|
69
|
+
update(identity_aliases)
|
|
70
|
+
.where(identity_aliases.c.id == alias_id)
|
|
71
|
+
.values(person_id=target_person_id)
|
|
72
|
+
)
|
|
73
|
+
remaining = connection.execute(
|
|
74
|
+
select(func.count()).select_from(identity_aliases).where(
|
|
75
|
+
identity_aliases.c.person_id == source_person_id
|
|
76
|
+
)
|
|
77
|
+
).scalar_one()
|
|
78
|
+
if remaining == 0:
|
|
79
|
+
connection.execute(
|
|
80
|
+
update(persons)
|
|
81
|
+
.where(persons.c.id == source_person_id)
|
|
82
|
+
.values(active=False, merged_into_person_id=target_person_id)
|
|
83
|
+
)
|
|
84
|
+
connection.execute(
|
|
85
|
+
insert(identity_merge_events).values(
|
|
86
|
+
id=event_id,
|
|
87
|
+
repository_id=repository_id,
|
|
88
|
+
event_type="ALIAS_MAP",
|
|
89
|
+
target_person_id=target_person_id,
|
|
90
|
+
source_person_ids_json=_canonical_json([source_person_id]),
|
|
91
|
+
moved_alias_ids_json=_canonical_json([alias_id]),
|
|
92
|
+
source_snapshots_json=_canonical_json([source_snapshot]),
|
|
93
|
+
status="ACTIVE",
|
|
94
|
+
)
|
|
95
|
+
)
|
|
96
|
+
return event_id
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class SqliteIdentityMergeStore:
|
|
100
|
+
def __init__(self, database_path: Path, repository_root: str) -> None:
|
|
101
|
+
self.database_path = database_path
|
|
102
|
+
self.repository_root = repository_root
|
|
103
|
+
|
|
104
|
+
def preview(self, sources: tuple[str, ...], target: str) -> dict[str, Any]:
|
|
105
|
+
engine = create_database_engine(self.database_path)
|
|
106
|
+
try:
|
|
107
|
+
with engine.connect() as connection:
|
|
108
|
+
repository_id = self._repository_id(connection)
|
|
109
|
+
return self._preview(connection, repository_id, sources, target)
|
|
110
|
+
finally:
|
|
111
|
+
engine.dispose()
|
|
112
|
+
|
|
113
|
+
def merge(self, sources: tuple[str, ...], target: str) -> dict[str, Any]:
|
|
114
|
+
engine = create_database_engine(self.database_path)
|
|
115
|
+
try:
|
|
116
|
+
with engine.begin() as connection:
|
|
117
|
+
repository_id = self._repository_id(connection)
|
|
118
|
+
preview = self._preview(connection, repository_id, sources, target)
|
|
119
|
+
event_id = str(uuid4())
|
|
120
|
+
target_id = str(preview["target"]["id"])
|
|
121
|
+
source_snapshots = [
|
|
122
|
+
{
|
|
123
|
+
"person": source,
|
|
124
|
+
"aliasIds": [alias["id"] for alias in source["aliases"]],
|
|
125
|
+
}
|
|
126
|
+
for source in preview["sources"]
|
|
127
|
+
]
|
|
128
|
+
moved_alias_ids = sorted(
|
|
129
|
+
alias_id
|
|
130
|
+
for snapshot in source_snapshots
|
|
131
|
+
for alias_id in snapshot["aliasIds"]
|
|
132
|
+
)
|
|
133
|
+
if moved_alias_ids:
|
|
134
|
+
connection.execute(
|
|
135
|
+
update(identity_aliases)
|
|
136
|
+
.where(identity_aliases.c.id.in_(moved_alias_ids))
|
|
137
|
+
.values(person_id=target_id)
|
|
138
|
+
)
|
|
139
|
+
source_ids = sorted(str(source["id"]) for source in preview["sources"])
|
|
140
|
+
connection.execute(
|
|
141
|
+
update(persons)
|
|
142
|
+
.where(persons.c.id.in_(source_ids))
|
|
143
|
+
.values(active=False, merged_into_person_id=target_id)
|
|
144
|
+
)
|
|
145
|
+
connection.execute(
|
|
146
|
+
insert(identity_merge_events).values(
|
|
147
|
+
id=event_id,
|
|
148
|
+
repository_id=repository_id,
|
|
149
|
+
event_type="PERSON_MERGE",
|
|
150
|
+
target_person_id=target_id,
|
|
151
|
+
source_person_ids_json=_canonical_json(source_ids),
|
|
152
|
+
moved_alias_ids_json=_canonical_json(moved_alias_ids),
|
|
153
|
+
source_snapshots_json=_canonical_json(source_snapshots),
|
|
154
|
+
status="ACTIVE",
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
return {"mergeId": event_id, "status": "ACTIVE", **preview}
|
|
158
|
+
finally:
|
|
159
|
+
engine.dispose()
|
|
160
|
+
|
|
161
|
+
def list_events(self, status: str = "ALL") -> list[dict[str, Any]]:
|
|
162
|
+
engine = create_database_engine(self.database_path)
|
|
163
|
+
try:
|
|
164
|
+
with engine.connect() as connection:
|
|
165
|
+
repository_id = self._repository_id(connection)
|
|
166
|
+
statement = select(identity_merge_events).where(
|
|
167
|
+
identity_merge_events.c.repository_id == repository_id
|
|
168
|
+
)
|
|
169
|
+
normalized = status.upper()
|
|
170
|
+
if normalized != "ALL":
|
|
171
|
+
statement = statement.where(
|
|
172
|
+
identity_merge_events.c.status == normalized
|
|
173
|
+
)
|
|
174
|
+
rows = connection.execute(
|
|
175
|
+
statement.order_by(identity_merge_events.c.created_at.desc())
|
|
176
|
+
).mappings()
|
|
177
|
+
return [self._serialize_event(dict(row)) for row in rows]
|
|
178
|
+
finally:
|
|
179
|
+
engine.dispose()
|
|
180
|
+
|
|
181
|
+
def unmerge(self, merge_id: str) -> dict[str, Any]:
|
|
182
|
+
engine = create_database_engine(self.database_path)
|
|
183
|
+
try:
|
|
184
|
+
with engine.begin() as connection:
|
|
185
|
+
repository_id = self._repository_id(connection)
|
|
186
|
+
row = connection.execute(
|
|
187
|
+
select(identity_merge_events).where(
|
|
188
|
+
identity_merge_events.c.id == merge_id,
|
|
189
|
+
identity_merge_events.c.repository_id == repository_id,
|
|
190
|
+
)
|
|
191
|
+
).mappings().one_or_none()
|
|
192
|
+
if row is None:
|
|
193
|
+
raise WorkspaceError(f"Identity merge event not found: {merge_id}")
|
|
194
|
+
if row["status"] != "ACTIVE":
|
|
195
|
+
raise WorkspaceError(f"Identity merge event is not ACTIVE: {merge_id}")
|
|
196
|
+
target_id = str(row["target_person_id"])
|
|
197
|
+
snapshots = json.loads(str(row["source_snapshots_json"]))
|
|
198
|
+
for snapshot in snapshots:
|
|
199
|
+
source_id = str(snapshot["person"]["id"])
|
|
200
|
+
source_active = bool(snapshot["person"].get("active", True))
|
|
201
|
+
source_redirect = snapshot["person"].get("mergedIntoPersonId")
|
|
202
|
+
alias_ids = [str(value) for value in snapshot["aliasIds"]]
|
|
203
|
+
if alias_ids:
|
|
204
|
+
current = connection.execute(
|
|
205
|
+
select(identity_aliases.c.id, identity_aliases.c.person_id).where(
|
|
206
|
+
identity_aliases.c.id.in_(alias_ids)
|
|
207
|
+
)
|
|
208
|
+
).all()
|
|
209
|
+
current_map = {
|
|
210
|
+
str(alias_id): str(person_id)
|
|
211
|
+
for alias_id, person_id in current
|
|
212
|
+
}
|
|
213
|
+
conflicts = [
|
|
214
|
+
alias_id
|
|
215
|
+
for alias_id in alias_ids
|
|
216
|
+
if current_map.get(alias_id) != target_id
|
|
217
|
+
]
|
|
218
|
+
if conflicts:
|
|
219
|
+
raise WorkspaceError(
|
|
220
|
+
"Cannot unmerge aliases changed after merge: "
|
|
221
|
+
+ ", ".join(sorted(conflicts))
|
|
222
|
+
)
|
|
223
|
+
connection.execute(
|
|
224
|
+
update(identity_aliases)
|
|
225
|
+
.where(identity_aliases.c.id.in_(alias_ids))
|
|
226
|
+
.values(person_id=source_id)
|
|
227
|
+
)
|
|
228
|
+
connection.execute(
|
|
229
|
+
update(persons)
|
|
230
|
+
.where(persons.c.id == source_id)
|
|
231
|
+
.values(
|
|
232
|
+
active=source_active,
|
|
233
|
+
merged_into_person_id=source_redirect,
|
|
234
|
+
)
|
|
235
|
+
)
|
|
236
|
+
reverted_at = datetime.now(UTC)
|
|
237
|
+
connection.execute(
|
|
238
|
+
update(identity_merge_events)
|
|
239
|
+
.where(identity_merge_events.c.id == merge_id)
|
|
240
|
+
.values(status="REVERTED", reverted_at=reverted_at)
|
|
241
|
+
)
|
|
242
|
+
return {
|
|
243
|
+
"mergeId": merge_id,
|
|
244
|
+
"status": "REVERTED",
|
|
245
|
+
"revertedAt": reverted_at.isoformat(),
|
|
246
|
+
}
|
|
247
|
+
finally:
|
|
248
|
+
engine.dispose()
|
|
249
|
+
|
|
250
|
+
def _preview(
|
|
251
|
+
self,
|
|
252
|
+
connection: Connection,
|
|
253
|
+
repository_id: str,
|
|
254
|
+
source_selectors: tuple[str, ...],
|
|
255
|
+
target_selector: str,
|
|
256
|
+
) -> dict[str, Any]:
|
|
257
|
+
if not source_selectors:
|
|
258
|
+
raise IdentityResolutionError("At least one --source selector is required")
|
|
259
|
+
target = self._resolve_raw(connection, repository_id, target_selector)
|
|
260
|
+
sources = [
|
|
261
|
+
self._resolve_raw(connection, repository_id, selector)
|
|
262
|
+
for selector in source_selectors
|
|
263
|
+
]
|
|
264
|
+
source_ids = [str(source["id"]) for source in sources]
|
|
265
|
+
if len(source_ids) != len(set(source_ids)):
|
|
266
|
+
raise IdentityResolutionError("Multiple source selectors resolve to the same person")
|
|
267
|
+
if str(target["id"]) in source_ids:
|
|
268
|
+
raise IdentityResolutionError("Merge target cannot also be a source")
|
|
269
|
+
if not target["active"] or not target["confirmed"] or target["kind"] != "HUMAN":
|
|
270
|
+
raise IdentityResolutionError("Merge target must be active, confirmed, and HUMAN")
|
|
271
|
+
for source in sources:
|
|
272
|
+
if not source["active"]:
|
|
273
|
+
raise IdentityResolutionError(
|
|
274
|
+
f"Merge source is inactive and redirects to {source['mergedIntoPersonId']}"
|
|
275
|
+
)
|
|
276
|
+
if source["kind"] != "HUMAN":
|
|
277
|
+
raise IdentityResolutionError("Merge sources must be HUMAN")
|
|
278
|
+
source["historicalRunCount"] = int(
|
|
279
|
+
connection.execute(
|
|
280
|
+
select(func.count()).select_from(analysis_runs).where(
|
|
281
|
+
analysis_runs.c.repository_id == repository_id,
|
|
282
|
+
analysis_runs.c.person_id == source["id"],
|
|
283
|
+
)
|
|
284
|
+
).scalar_one()
|
|
285
|
+
)
|
|
286
|
+
return {
|
|
287
|
+
"target": target,
|
|
288
|
+
"sources": sources,
|
|
289
|
+
"movedAliases": [
|
|
290
|
+
alias for source in sources for alias in source["aliases"]
|
|
291
|
+
],
|
|
292
|
+
"selectorRedirects": [
|
|
293
|
+
{"selector": selector, "targetPersonId": target["id"]}
|
|
294
|
+
for selector in source_selectors
|
|
295
|
+
],
|
|
296
|
+
"safeToExecute": True,
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
def _resolve_raw(
|
|
300
|
+
self, connection: Connection, repository_id: str, selector: str
|
|
301
|
+
) -> dict[str, Any]:
|
|
302
|
+
rows = connection.execute(
|
|
303
|
+
select(persons).where(persons.c.repository_id == repository_id)
|
|
304
|
+
).mappings().all()
|
|
305
|
+
needle = selector.strip().casefold()
|
|
306
|
+
matches: list[dict[str, Any]] = []
|
|
307
|
+
for row in rows:
|
|
308
|
+
aliases = connection.execute(
|
|
309
|
+
select(identity_aliases).where(identity_aliases.c.person_id == row["id"])
|
|
310
|
+
).mappings().all()
|
|
311
|
+
values = {
|
|
312
|
+
str(row["id"]).casefold(),
|
|
313
|
+
str(row["canonical_name"]).casefold(),
|
|
314
|
+
str(row["canonical_email"]).casefold(),
|
|
315
|
+
*(
|
|
316
|
+
str(value).casefold()
|
|
317
|
+
for alias in aliases
|
|
318
|
+
for value in (alias["name"], alias["email"])
|
|
319
|
+
),
|
|
320
|
+
}
|
|
321
|
+
if needle in values:
|
|
322
|
+
matches.append(
|
|
323
|
+
{
|
|
324
|
+
"id": str(row["id"]),
|
|
325
|
+
"name": str(row["canonical_name"]),
|
|
326
|
+
"email": str(row["canonical_email"]),
|
|
327
|
+
"kind": str(row["kind"]),
|
|
328
|
+
"confirmed": bool(row["confirmed"]),
|
|
329
|
+
"active": bool(row["active"]),
|
|
330
|
+
"mergedIntoPersonId": row["merged_into_person_id"],
|
|
331
|
+
"aliases": [
|
|
332
|
+
{
|
|
333
|
+
"id": str(alias["id"]),
|
|
334
|
+
"name": str(alias["name"]),
|
|
335
|
+
"email": str(alias["email"]),
|
|
336
|
+
}
|
|
337
|
+
for alias in aliases
|
|
338
|
+
],
|
|
339
|
+
}
|
|
340
|
+
)
|
|
341
|
+
unique = {str(match["id"]): match for match in matches}
|
|
342
|
+
if len(unique) != 1:
|
|
343
|
+
raise IdentityResolutionError(
|
|
344
|
+
f"Person selector must match exactly one identity: {selector}"
|
|
345
|
+
)
|
|
346
|
+
return next(iter(unique.values()))
|
|
347
|
+
|
|
348
|
+
def _repository_id(self, connection: Connection) -> str:
|
|
349
|
+
value = connection.execute(
|
|
350
|
+
select(repositories.c.id).where(repositories.c.root_path == self.repository_root)
|
|
351
|
+
).scalar_one_or_none()
|
|
352
|
+
if value is None:
|
|
353
|
+
raise WorkspaceError(f"Repository is not registered: {self.repository_root}")
|
|
354
|
+
return str(value)
|
|
355
|
+
|
|
356
|
+
@staticmethod
|
|
357
|
+
def _serialize_event(row: dict[str, Any]) -> dict[str, Any]:
|
|
358
|
+
return {
|
|
359
|
+
"mergeId": str(row["id"]),
|
|
360
|
+
"eventType": str(row["event_type"]),
|
|
361
|
+
"targetPersonId": str(row["target_person_id"]),
|
|
362
|
+
"sourcePersonIds": json.loads(str(row["source_person_ids_json"])),
|
|
363
|
+
"movedAliasIds": json.loads(str(row["moved_alias_ids_json"])),
|
|
364
|
+
"status": str(row["status"]),
|
|
365
|
+
"createdAt": row["created_at"].isoformat(),
|
|
366
|
+
"revertedAt": (
|
|
367
|
+
row["reverted_at"].isoformat() if row["reverted_at"] else None
|
|
368
|
+
),
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def _canonical_json(value: object) -> str:
|
|
373
|
+
return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Any
|
|
6
|
+
from uuid import uuid4
|
|
7
|
+
|
|
8
|
+
from sqlalchemy import func, insert, select, update
|
|
9
|
+
from sqlalchemy.dialects.sqlite import insert as sqlite_insert
|
|
10
|
+
|
|
11
|
+
from git_contribution_analyzer.adapters.storage.sqlite.database import create_database_engine
|
|
12
|
+
from git_contribution_analyzer.adapters.storage.sqlite.models import llm_cache, llm_invocations
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class SqliteLlmAuditStore:
|
|
16
|
+
def __init__(self, database_path: Path) -> None:
|
|
17
|
+
self.database_path = database_path
|
|
18
|
+
|
|
19
|
+
def get_cached(self, request_hash: str) -> dict[str, Any] | None:
|
|
20
|
+
engine = create_database_engine(self.database_path)
|
|
21
|
+
try:
|
|
22
|
+
with engine.begin() as connection:
|
|
23
|
+
value = connection.execute(
|
|
24
|
+
select(llm_cache.c.response_json).where(
|
|
25
|
+
llm_cache.c.request_hash == request_hash
|
|
26
|
+
)
|
|
27
|
+
).scalar_one_or_none()
|
|
28
|
+
if value is not None:
|
|
29
|
+
connection.execute(
|
|
30
|
+
update(llm_cache)
|
|
31
|
+
.where(llm_cache.c.request_hash == request_hash)
|
|
32
|
+
.values(last_used_at=func.now())
|
|
33
|
+
)
|
|
34
|
+
finally:
|
|
35
|
+
engine.dispose()
|
|
36
|
+
return dict(json.loads(str(value))) if value is not None else None
|
|
37
|
+
|
|
38
|
+
def put_cached(
|
|
39
|
+
self,
|
|
40
|
+
*,
|
|
41
|
+
request_hash: str,
|
|
42
|
+
provider_id: str,
|
|
43
|
+
model: str,
|
|
44
|
+
prompt_version: str,
|
|
45
|
+
schema_version: str,
|
|
46
|
+
response: dict[str, Any],
|
|
47
|
+
) -> None:
|
|
48
|
+
engine = create_database_engine(self.database_path)
|
|
49
|
+
statement = sqlite_insert(llm_cache).values(
|
|
50
|
+
request_hash=request_hash,
|
|
51
|
+
provider_id=provider_id,
|
|
52
|
+
model=model,
|
|
53
|
+
prompt_version=prompt_version,
|
|
54
|
+
schema_version=schema_version,
|
|
55
|
+
response_json=_json(response),
|
|
56
|
+
)
|
|
57
|
+
try:
|
|
58
|
+
with engine.begin() as connection:
|
|
59
|
+
connection.execute(
|
|
60
|
+
statement.on_conflict_do_update(
|
|
61
|
+
index_elements=[llm_cache.c.request_hash],
|
|
62
|
+
set_={"response_json": _json(response)},
|
|
63
|
+
)
|
|
64
|
+
)
|
|
65
|
+
finally:
|
|
66
|
+
engine.dispose()
|
|
67
|
+
|
|
68
|
+
def record_invocation(
|
|
69
|
+
self,
|
|
70
|
+
*,
|
|
71
|
+
run_id: str | None,
|
|
72
|
+
request_hash: str,
|
|
73
|
+
provider_id: str,
|
|
74
|
+
model: str,
|
|
75
|
+
prompt_version: str,
|
|
76
|
+
schema_version: str,
|
|
77
|
+
attempts: int,
|
|
78
|
+
latency_ms: int,
|
|
79
|
+
status: str,
|
|
80
|
+
error_code: str | None = None,
|
|
81
|
+
error_message: str | None = None,
|
|
82
|
+
cache_hit: bool = False,
|
|
83
|
+
) -> None:
|
|
84
|
+
engine = create_database_engine(self.database_path)
|
|
85
|
+
try:
|
|
86
|
+
with engine.begin() as connection:
|
|
87
|
+
connection.execute(
|
|
88
|
+
insert(llm_invocations).values(
|
|
89
|
+
id=str(uuid4()),
|
|
90
|
+
run_id=run_id,
|
|
91
|
+
request_hash=request_hash,
|
|
92
|
+
provider_id=provider_id,
|
|
93
|
+
model=model,
|
|
94
|
+
prompt_version=prompt_version,
|
|
95
|
+
schema_version=schema_version,
|
|
96
|
+
attempts=attempts,
|
|
97
|
+
latency_ms=max(0, latency_ms),
|
|
98
|
+
status=status,
|
|
99
|
+
error_code=error_code,
|
|
100
|
+
error_message=error_message,
|
|
101
|
+
cache_hit=cache_hit,
|
|
102
|
+
)
|
|
103
|
+
)
|
|
104
|
+
finally:
|
|
105
|
+
engine.dispose()
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _json(value: Any) -> str:
|
|
109
|
+
return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from alembic import context
|
|
4
|
+
from sqlalchemy import engine_from_config, pool
|
|
5
|
+
|
|
6
|
+
from git_contribution_analyzer.adapters.storage.sqlite.models import metadata
|
|
7
|
+
|
|
8
|
+
config = context.config
|
|
9
|
+
target_metadata = metadata
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def run_migrations_offline() -> None:
|
|
13
|
+
url = config.get_main_option("sqlalchemy.url")
|
|
14
|
+
context.configure(url=url, target_metadata=target_metadata, literal_binds=True)
|
|
15
|
+
with context.begin_transaction():
|
|
16
|
+
context.run_migrations()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def run_migrations_online() -> None:
|
|
20
|
+
connectable = engine_from_config(
|
|
21
|
+
config.get_section(config.config_ini_section) or {},
|
|
22
|
+
prefix="sqlalchemy.",
|
|
23
|
+
poolclass=pool.NullPool,
|
|
24
|
+
)
|
|
25
|
+
with connectable.connect() as connection:
|
|
26
|
+
context.configure(connection=connection, target_metadata=target_metadata)
|
|
27
|
+
with context.begin_transaction():
|
|
28
|
+
context.run_migrations()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
if context.is_offline_mode():
|
|
32
|
+
run_migrations_offline()
|
|
33
|
+
else:
|
|
34
|
+
run_migrations_online()
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""${message}
|
|
2
|
+
|
|
3
|
+
Revision ID: ${up_revision}
|
|
4
|
+
Revises: ${down_revision | comma,n}
|
|
5
|
+
Create Date: ${create_date}
|
|
6
|
+
"""
|
|
7
|
+
from alembic import op
|
|
8
|
+
import sqlalchemy as sa
|
|
9
|
+
${imports if imports else ""}
|
|
10
|
+
|
|
11
|
+
revision = ${repr(up_revision)}
|
|
12
|
+
down_revision = ${repr(down_revision)}
|
|
13
|
+
branch_labels = ${repr(branch_labels)}
|
|
14
|
+
depends_on = ${repr(depends_on)}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def upgrade() -> None:
|
|
18
|
+
${upgrades if upgrades else "pass"}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def downgrade() -> None:
|
|
22
|
+
${downgrades if downgrades else "pass"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Create the phase-one workspace schema."""
|
|
2
|
+
|
|
3
|
+
import sqlalchemy as sa
|
|
4
|
+
from alembic import op
|
|
5
|
+
|
|
6
|
+
revision = "0001_initial"
|
|
7
|
+
down_revision = None
|
|
8
|
+
branch_labels = None
|
|
9
|
+
depends_on = None
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def upgrade() -> None:
|
|
13
|
+
op.create_table(
|
|
14
|
+
"repositories",
|
|
15
|
+
sa.Column("id", sa.String(length=36), primary_key=True),
|
|
16
|
+
sa.Column("root_path", sa.String(), nullable=False, unique=True),
|
|
17
|
+
sa.Column("git_dir", sa.String(), nullable=False),
|
|
18
|
+
sa.Column("default_branch", sa.String(), nullable=False),
|
|
19
|
+
sa.Column(
|
|
20
|
+
"created_at",
|
|
21
|
+
sa.DateTime(timezone=True),
|
|
22
|
+
server_default=sa.func.now(),
|
|
23
|
+
nullable=False,
|
|
24
|
+
),
|
|
25
|
+
sa.Column(
|
|
26
|
+
"updated_at",
|
|
27
|
+
sa.DateTime(timezone=True),
|
|
28
|
+
server_default=sa.func.now(),
|
|
29
|
+
nullable=False,
|
|
30
|
+
),
|
|
31
|
+
)
|
|
32
|
+
op.create_table(
|
|
33
|
+
"analysis_runs",
|
|
34
|
+
sa.Column("id", sa.String(length=36), primary_key=True),
|
|
35
|
+
sa.Column("repository_id", sa.String(length=36), nullable=False),
|
|
36
|
+
sa.Column("status", sa.String(length=20), nullable=False),
|
|
37
|
+
sa.Column("schema_version", sa.String(length=20), nullable=False),
|
|
38
|
+
sa.Column(
|
|
39
|
+
"started_at",
|
|
40
|
+
sa.DateTime(timezone=True),
|
|
41
|
+
server_default=sa.func.now(),
|
|
42
|
+
nullable=False,
|
|
43
|
+
),
|
|
44
|
+
sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True),
|
|
45
|
+
sa.ForeignKeyConstraint(["repository_id"], ["repositories.id"]),
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def downgrade() -> None:
|
|
50
|
+
op.drop_table("analysis_runs")
|
|
51
|
+
op.drop_table("repositories")
|