Skip to content

Commit 41c3aea

Browse files
authored
Merge pull request #131 from unisoncomputing/cp/list-deps
Add endpoint to list dependencies of a definition
2 parents e51c124 + f8aa44f commit 41c3aea

22 files changed

+845
-80
lines changed

src/Share/BackgroundJobs/Search/DefinitionSync.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ syncTerms codebase namesPerspective rootBranchHashId termsCursor = do
216216
(ns :| rest) -> ns :| take 1 rest
217217
& Name.fromReverseSegments
218218
-- TODO: batchify this
219-
termSummary <- lift $ Summary.termSummaryForReferent ref typ (Just displayName) rootBranchHashId Nothing Nothing
219+
termSummary <- lift $ Summary.termSummaryForReferent ref typ (Just displayName) namesPerspective Nothing
220220
let sh = Referent.toShortHash ref
221221
let (refTokens, arity) = tokensForTerm fqn ref typ termSummary
222222
let dd =

src/Share/Web/Share/Branches/Impl.hs

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import Share.Postgres qualified as PG
2222
import Share.Postgres.Causal.Queries qualified as CausalQ
2323
import Share.Postgres.Contributions.Queries qualified as ContributionsQ
2424
import Share.Postgres.IDs (CausalId)
25+
import Share.Postgres.NameLookups.Types (pathToPathSegments)
26+
import Share.Postgres.NamesPerspective.Ops qualified as NP
2527
import Share.Postgres.Queries qualified as Q
2628
import Share.Postgres.Users.Queries qualified as UserQ
2729
import Share.Postgres.Users.Queries qualified as UsersQ
@@ -107,6 +109,8 @@ branchCodeBrowsingServer session handle projectSlug branchShortHand =
107109
:<|> projectBranchDefinitionsByHashEndpoint session handle projectSlug branchShortHand
108110
:<|> projectBranchTermSummaryEndpoint session handle projectSlug branchShortHand
109111
:<|> projectBranchTypeSummaryEndpoint session handle projectSlug branchShortHand
112+
:<|> projectBranchDefinitionDependenciesByNameEndpoint session handle projectSlug branchShortHand
113+
:<|> projectBranchDefinitionDependenciesByHashEndpoint session handle projectSlug branchShortHand
110114
:<|> projectBranchFindEndpoint session handle projectSlug branchShortHand
111115
:<|> projectBranchNamespacesByNameEndpoint session handle projectSlug branchShortHand
112116
)
@@ -154,7 +158,7 @@ projectBranchDefinitionsByNameEndpoint (AuthN.MaybeAuthedUserID callerUserId) us
154158
Codebase.cachedCodebaseResponse authZReceipt codebaseLoc "project-branch-definitions-by-name" cacheParams causalId $ do
155159
PG.runTransactionMode PG.ReadCommitted PG.ReadWrite $ do
156160
CR.withCodebaseRuntime codebase unisonRuntime \rt ->
157-
ShareBackend.definitionForHQName codebase (fromMaybe mempty relativeTo) causalId renderWidth (Suffixify False) rt name
161+
ShareBackend.displayDefinitionByHQName codebase (fromMaybe mempty relativeTo) causalId renderWidth (Suffixify False) rt name
158162
where
159163
projectBranchShortHand = ProjectBranchShortHand {userHandle, projectSlug, contributorHandle, branchName}
160164
cacheParams = [IDs.toText projectBranchShortHand, HQ.toTextWith Name.toText name, tShow $ fromMaybe mempty relativeTo, foldMap toUrlPiece renderWidth]
@@ -181,7 +185,7 @@ projectBranchDefinitionsByHashEndpoint (AuthN.MaybeAuthedUserID callerUserId) us
181185
Codebase.cachedCodebaseResponse authZReceipt codebaseLoc "project-branch-definitions-by-hash" cacheParams causalId $ do
182186
PG.runTransactionMode PG.ReadCommitted PG.ReadWrite $ do
183187
CR.withCodebaseRuntime codebase unisonRuntime \rt ->
184-
ShareBackend.definitionForHQName codebase (fromMaybe mempty relativeTo) causalId renderWidth (Suffixify False) rt query
188+
ShareBackend.displayDefinitionByHQName codebase (fromMaybe mempty relativeTo) causalId renderWidth (Suffixify False) rt query
185189
where
186190
projectBranchShortHand = ProjectBranchShortHand {userHandle, projectSlug, contributorHandle, branchName}
187191
cacheParams = [IDs.toText projectBranchShortHand, toUrlPiece referent, tShow $ fromMaybe mempty relativeTo, foldMap toUrlPiece renderWidth]
@@ -205,7 +209,9 @@ projectBranchTermSummaryEndpoint (AuthN.MaybeAuthedUserID callerUserId) userHand
205209
causalId <- resolveRootHash codebase branchHead rootHash
206210
Codebase.cachedCodebaseResponse authZReceipt codebaseLoc "project-branch-term-summary" cacheParams causalId $ do
207211
PG.runTransactionMode PG.ReadCommitted PG.ReadWrite $ do
208-
serveTermSummary codebase ref mayName causalId relativeTo renderWidth
212+
rootBHId <- CausalQ.expectNamespaceIdsByCausalIdsOf id causalId
213+
np <- NP.namesPerspectiveForRootAndPath rootBHId (maybe mempty pathToPathSegments relativeTo)
214+
serveTermSummary codebase ref mayName np renderWidth
209215
where
210216
projectBranchShortHand = ProjectBranchShortHand {userHandle, projectSlug, contributorHandle, branchName}
211217
cacheParams = [IDs.toText projectBranchShortHand, toUrlPiece ref, maybe "" Name.toText mayName, tShow $ fromMaybe mempty relativeTo, foldMap toUrlPiece renderWidth]
@@ -234,6 +240,62 @@ projectBranchTypeSummaryEndpoint (AuthN.MaybeAuthedUserID callerUserId) userHand
234240
projectBranchShortHand = ProjectBranchShortHand {userHandle, projectSlug, contributorHandle, branchName}
235241
cacheParams = [IDs.toText projectBranchShortHand, toUrlPiece ref, maybe "" Name.toText mayName, tShow $ fromMaybe mempty relativeTo, foldMap toUrlPiece renderWidth]
236242

243+
projectBranchDefinitionDependenciesByNameEndpoint ::
244+
Maybe Session ->
245+
UserHandle ->
246+
ProjectSlug ->
247+
BranchShortHand ->
248+
HQ.HashQualified Name ->
249+
Maybe Path.Path ->
250+
Maybe Pretty.Width ->
251+
Maybe CausalHash ->
252+
WebApp (Cached JSON DefinitionSearchResults)
253+
projectBranchDefinitionDependenciesByNameEndpoint (AuthN.MaybeAuthedUserID callerUserId) userHandle projectSlug bsh@(BranchShortHand {contributorHandle, branchName}) name relativeTo renderWidth rootHash = do
254+
(Project {ownerUserId = projectOwnerUserId, projectId}, Branch {causal = branchHead, contributorId}) <- getProjectBranch projectBranchShortHand
255+
authZReceipt <- AuthZ.permissionGuard $ AuthZ.checkProjectBranchRead callerUserId projectId
256+
let codebaseLoc = Codebase.codebaseLocationForProjectBranchCodebase projectOwnerUserId contributorId
257+
let codebase = Codebase.codebaseEnv authZReceipt codebaseLoc
258+
causalId <- resolveRootHash codebase branchHead rootHash
259+
Codebase.cachedCodebaseResponse authZReceipt codebaseLoc "project-branch-definition-dependencies-by-name" cacheParams causalId $ do
260+
PG.runTransactionMode PG.ReadCommitted PG.ReadWrite $ do
261+
rootBranchHashId <- CausalQ.expectNamespaceIdsByCausalIdsOf id causalId
262+
np <- NP.namesPerspectiveForRootAndPath rootBranchHashId (maybe mempty pathToPathSegments relativeTo)
263+
DefinitionSearchResults <$> ShareBackend.definitionDependencyResults codebase name projectShorthand branchOrReleaseShortHand np renderWidth
264+
where
265+
branchOrReleaseShortHand = IDs.IsBranchShortHand bsh
266+
projectShorthand = ProjectShortHand {userHandle, projectSlug}
267+
projectBranchShortHand = ProjectBranchShortHand {userHandle, projectSlug, contributorHandle, branchName}
268+
cacheParams = [IDs.toText projectBranchShortHand, HQ.toTextWith Name.toText name, tShow $ fromMaybe mempty relativeTo, foldMap toUrlPiece renderWidth]
269+
270+
projectBranchDefinitionDependenciesByHashEndpoint ::
271+
Maybe Session ->
272+
UserHandle ->
273+
ProjectSlug ->
274+
BranchShortHand ->
275+
Referent ->
276+
Maybe Path.Path ->
277+
Maybe Pretty.Width ->
278+
Maybe CausalHash ->
279+
WebApp (Cached JSON DefinitionSearchResults)
280+
projectBranchDefinitionDependenciesByHashEndpoint (AuthN.MaybeAuthedUserID callerUserId) userHandle projectSlug bsh@(BranchShortHand {contributorHandle, branchName}) referent relativeTo renderWidth rootHash = do
281+
(Project {ownerUserId = projectOwnerUserId, projectId}, Branch {causal = branchHead, contributorId}) <- getProjectBranch projectBranchShortHand
282+
authZReceipt <- AuthZ.permissionGuard $ AuthZ.checkProjectBranchRead callerUserId projectId
283+
let codebaseLoc = Codebase.codebaseLocationForProjectBranchCodebase projectOwnerUserId contributorId
284+
let codebase = Codebase.codebaseEnv authZReceipt codebaseLoc
285+
let shortHash = Referent.toShortHash referent
286+
let query = HQ.HashOnly shortHash
287+
causalId <- resolveRootHash codebase branchHead rootHash
288+
Codebase.cachedCodebaseResponse authZReceipt codebaseLoc "project-branch-definition-dependencies-by-hash" (cacheParams query) causalId $ do
289+
PG.runTransactionMode PG.ReadCommitted PG.ReadWrite $ do
290+
rootBranchHashId <- CausalQ.expectNamespaceIdsByCausalIdsOf id causalId
291+
np <- NP.namesPerspectiveForRootAndPath rootBranchHashId (maybe mempty pathToPathSegments relativeTo)
292+
DefinitionSearchResults <$> ShareBackend.definitionDependencyResults codebase query projectShorthand branchOrReleaseShortHand np renderWidth
293+
where
294+
branchOrReleaseShortHand = IDs.IsBranchShortHand bsh
295+
projectShorthand = ProjectShortHand {userHandle, projectSlug}
296+
projectBranchShortHand = ProjectBranchShortHand {userHandle, projectSlug, contributorHandle, branchName}
297+
cacheParams query = [IDs.toText projectBranchShortHand, HQ.toTextWith Name.toText query, tShow $ fromMaybe mempty relativeTo, foldMap toUrlPiece renderWidth]
298+
237299
projectBranchFindEndpoint ::
238300
Maybe Session ->
239301
UserHandle ->

src/Share/Web/Share/CodeBrowsing/API.hs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Data.Text (Text)
77
import Servant
88
import Share.Utils.Caching
99
import Share.Utils.Servant (OptionalCapture, RequiredQueryParam)
10+
import Share.Web.Share.Types (DefinitionSearchResults)
1011
import U.Codebase.HashTags (CausalHash)
1112
import Unison.Codebase.Path qualified as Path
1213
import Unison.HashQualified qualified as HQ
@@ -26,6 +27,8 @@ type CodeBrowseAPI =
2627
:<|> ("definitions" :> "by-hash" :> DefinitionsByHashEndpoint)
2728
:<|> ("definitions" :> "terms" :> "by-hash" :> TermSummaryEndpoint)
2829
:<|> ("definitions" :> "types" :> "by-hash" :> TypeSummaryEndpoint)
30+
:<|> ("definitions" :> "dependencies" :> "by-name" :> DefinitionDependenciesByNameEndpoint)
31+
:<|> ("definitions" :> "dependencies" :> "by-hash" :> DefinitionDependenciesByHashEndpoint)
2932
:<|> ("find" :> FindEndpoint)
3033
:<|> ("namespaces" :> "by-name" :> NamespaceByNameEndpoint)
3134
)
@@ -69,6 +72,34 @@ type DefinitionsByHashEndpoint =
6972
:> QueryParam "rootHash" CausalHash
7073
:> Get '[JSON] (Cached JSON DefinitionDisplayResults)
7174

75+
type DefinitionDependenciesByNameEndpoint =
76+
Capture "name" (HQ.HashQualified Name)
77+
:> QueryParam "relativeTo" Path.Path
78+
:> QueryParam "renderWidth" Width
79+
:> QueryParam "rootHash" CausalHash
80+
:> Get '[JSON] (Cached JSON DefinitionSearchResults)
81+
82+
type DefinitionDependenciesByHashEndpoint =
83+
Capture "hash" Referent
84+
:> QueryParam "relativeTo" Path.Path
85+
:> QueryParam "renderWidth" Width
86+
:> QueryParam "rootHash" CausalHash
87+
:> Get '[JSON] (Cached JSON DefinitionSearchResults)
88+
89+
type DefinitionDependentsByNameEndpoint =
90+
Capture "name" (HQ.HashQualified Name)
91+
:> QueryParam "relativeTo" Path.Path
92+
:> QueryParam "renderWidth" Width
93+
:> QueryParam "rootHash" CausalHash
94+
:> Get '[JSON] (Cached JSON DefinitionSearchResults)
95+
96+
type DefinitionDependentsByHashEndpoint =
97+
Capture "hash" Referent
98+
:> QueryParam "relativeTo" Path.Path
99+
:> QueryParam "renderWidth" Width
100+
:> QueryParam "rootHash" CausalHash
101+
:> Get '[JSON] (Cached JSON DefinitionSearchResults)
102+
72103
type FindEndpoint =
73104
QueryParam "relativeTo" Path.Path
74105
:> QueryParam "limit" Int

src/Share/Web/Share/Impl.hs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import Share.Postgres qualified as PG
2626
import Share.Postgres.Authorization.Queries qualified as AuthZQ
2727
import Share.Postgres.Causal.Queries qualified as CausalQ
2828
import Share.Postgres.IDs (CausalHash)
29+
import Share.Postgres.NameLookups.Types (pathToPathSegments)
30+
import Share.Postgres.NamesPerspective.Ops qualified as NP
2931
import Share.Postgres.Ops qualified as PGO
3032
import Share.Postgres.Projects.Queries qualified as PQ
3133
import Share.Postgres.Queries qualified as Q
@@ -83,6 +85,8 @@ userCodebaseServer session handle =
8385
:<|> definitionsByHashEndpoint session handle
8486
:<|> termSummaryEndpoint session handle
8587
:<|> typeSummaryEndpoint session handle
88+
:<|> definitionDependenciesByNameEndpoint session handle
89+
:<|> definitionDependenciesByHashEndpoint session handle
8690
:<|> findEndpoint session handle
8791
:<|> namespacesByNameEndpoint session handle
8892
)
@@ -160,7 +164,7 @@ definitionsByNameEndpoint (AuthN.MaybeAuthedUserID callerUserId) userHandle name
160164
Codebase.cachedCodebaseResponse authZReceipt codebaseLoc "definitions-by-name" cacheParams rootCausalId $ do
161165
PG.runTransactionMode PG.ReadCommitted PG.ReadWrite $ do
162166
CR.withCodebaseRuntime codebase unisonRuntime \rt -> do
163-
ShareBackend.definitionForHQName codebase (fromMaybe mempty relativeTo) rootCausalId renderWidth (Suffixify False) rt query
167+
ShareBackend.displayDefinitionByHQName codebase (fromMaybe mempty relativeTo) rootCausalId renderWidth (Suffixify False) rt query
164168
where
165169
cacheParams = [HQ.toTextWith Name.toText name, tShow $ fromMaybe mempty relativeTo, foldMap toUrlPiece renderWidth]
166170
authPath :: Path.Path
@@ -191,7 +195,7 @@ definitionsByHashEndpoint (AuthN.MaybeAuthedUserID callerUserId) userHandle refe
191195
Codebase.cachedCodebaseResponse authZReceipt codebaseLoc "definitions-by-hash" cacheParams rootCausalId $ do
192196
PG.runTransactionMode PG.ReadCommitted PG.ReadWrite $ do
193197
CR.withCodebaseRuntime codebase unisonRuntime \rt -> do
194-
ShareBackend.definitionForHQName codebase (fromMaybe mempty relativeTo) rootCausalId renderWidth (Suffixify False) rt query
198+
ShareBackend.displayDefinitionByHQName codebase (fromMaybe mempty relativeTo) rootCausalId renderWidth (Suffixify False) rt query
195199
where
196200
cacheParams = [toUrlPiece referent, tShow $ fromMaybe mempty relativeTo, foldMap toUrlPiece renderWidth]
197201

@@ -213,7 +217,9 @@ termSummaryEndpoint (AuthN.MaybeAuthedUserID callerUserId) userHandle ref mayNam
213217
(rootCausalId, _rootCausalHash) <- PG.runTransactionMode PG.ReadCommitted PG.Read $ Codebase.expectLooseCodeRoot codebase
214218
Codebase.cachedCodebaseResponse authZReceipt codebaseLoc "term-summary" cacheParams rootCausalId $ do
215219
PG.runTransactionMode PG.ReadCommitted PG.ReadWrite $ do
216-
serveTermSummary codebase ref mayName rootCausalId relativeTo renderWidth
220+
rootBranchHashId <- CausalQ.expectNamespaceIdsByCausalIdsOf id rootCausalId
221+
np <- NP.namesPerspectiveForRootAndPath rootBranchHashId (maybe mempty pathToPathSegments relativeTo)
222+
serveTermSummary codebase ref mayName np renderWidth
217223
where
218224
cacheParams = [toUrlPiece ref, maybe "" Name.toText mayName, tShow $ fromMaybe mempty relativeTo, foldMap toUrlPiece renderWidth]
219225
authPath :: Path.Path
@@ -249,6 +255,30 @@ typeSummaryEndpoint (AuthN.MaybeAuthedUserID callerUserId) userHandle ref mayNam
249255
suffix = maybe mempty Path.fromName mayName
250256
in prefix <> suffix
251257

258+
definitionDependenciesByNameEndpoint ::
259+
Maybe Session ->
260+
UserHandle ->
261+
HQ.HashQualified Name ->
262+
Maybe Path.Path ->
263+
Maybe Pretty.Width ->
264+
Maybe CausalHash ->
265+
WebApp (Cached JSON DefinitionSearchResults)
266+
definitionDependenciesByNameEndpoint _ _userHandle _name _relativeTo _renderWidth _rootHash = do
267+
-- The user endpoints are deprecated, no point maintaining them.
268+
respondError Unimplemented
269+
270+
definitionDependenciesByHashEndpoint ::
271+
Maybe Session ->
272+
UserHandle ->
273+
Referent ->
274+
Maybe Path.Path ->
275+
Maybe Pretty.Width ->
276+
Maybe CausalHash ->
277+
WebApp (Cached JSON DefinitionSearchResults)
278+
definitionDependenciesByHashEndpoint _ _userHandle _referent _relativeTo _renderWidth _rootHash = do
279+
-- The user endpoints are deprecated, no point maintaining them.
280+
respondError Unimplemented
281+
252282
findEndpoint ::
253283
Maybe Session ->
254284
UserHandle ->

0 commit comments

Comments
 (0)