diff --git a/docs/content/api/modules.json.gz b/docs/content/api/modules.json.gz
index e5fd58845027d..3930da2c16840 100644
Binary files a/docs/content/api/modules.json.gz and b/docs/content/api/modules.json.gz differ
diff --git a/docs/content/api/searchindex.json.gz b/docs/content/api/searchindex.json.gz
index 2be246471f0fa..1faf44f410954 100644
Binary files a/docs/content/api/searchindex.json.gz and b/docs/content/api/searchindex.json.gz differ
diff --git a/docs/content/api/sections.json.gz b/docs/content/api/sections.json.gz
index 7b8b3a74b3ed2..aa1367b7d9f80 100644
Binary files a/docs/content/api/sections.json.gz and b/docs/content/api/sections.json.gz differ
diff --git a/docs/content/guides/dagster/code-references.mdx b/docs/content/guides/dagster/code-references.mdx
index 5657ac6d764fd..52e546c9eb490 100644
--- a/docs/content/guides/dagster/code-references.mdx
+++ b/docs/content/guides/dagster/code-references.mdx
@@ -71,7 +71,7 @@ Dagster's dbt integration can automatically attach references to the SQL files b
In some cases, you may want to manually attach code references to your asset definitions. Some assets may have a more complex source structure, such as an asset whose definition is spread across multiple Python source files or an asset which is partially defined with a `.sql` model file.
-To manually attach code references to an asset definition, use `CodeReferencesMetadataValue`. You can then choose to augment these manual references with `with_source_code_references`:
+To manually attach code references to an asset definition, use . You can then choose to augment these manual references with :
```python file=/guides/dagster/code_references/manual_references.py
import os
@@ -165,7 +165,7 @@ defs = Definitions(
### In any Dagster environment
-The utility allows you to convert local file code references to source control links. You'll need to provide the base URL of your git repository, the branch or commit hash, and a path to the repository root locally.
+The utility allows you to convert local file code references to source control links. You'll need to provide the base URL of your git repository, the branch or commit hash, and a which tells Dagster how to convert local file paths to paths in the repository. The simplest way to do so is with an , which uses a local file path and the corresponding path in the repository to infer the mapping for other files.
```python file=/guides/dagster/code_references/link_to_source_control.py
from pathlib import Path
diff --git a/docs/next/public/objects.inv b/docs/next/public/objects.inv
index 5e85237fa1119..048165c370bad 100644
Binary files a/docs/next/public/objects.inv and b/docs/next/public/objects.inv differ
diff --git a/docs/sphinx/sections/api/apidocs/metadata.rst b/docs/sphinx/sections/api/apidocs/metadata.rst
index 79f60135b532f..7c3e8b7035454 100644
--- a/docs/sphinx/sections/api/apidocs/metadata.rst
+++ b/docs/sphinx/sections/api/apidocs/metadata.rst
@@ -47,6 +47,8 @@ All metadata types inherit from `MetadataValue`. The following types are defined
.. autoclass:: UrlMetadataValue
+.. autoclass:: CodeReferencesMetadataValue
+
Tables
^^^^^^
@@ -76,3 +78,7 @@ For more information, refer to the `Linking to asset definition code with code r
.. autofunction:: with_source_code_references
.. autofunction:: link_code_references_to_git
+
+.. autoclass:: FilePathMapping
+
+.. autoclass:: AnchorBasedFilePathMapping
diff --git a/examples/quickstart_etl/quickstart_etl/definitions.py b/examples/quickstart_etl/quickstart_etl/definitions.py
index 3070b3e358ee9..af2842d291c9b 100644
--- a/examples/quickstart_etl/quickstart_etl/definitions.py
+++ b/examples/quickstart_etl/quickstart_etl/definitions.py
@@ -1,9 +1,16 @@
+from pathlib import Path
+
from dagster import (
Definitions,
ScheduleDefinition,
define_asset_job,
+ graph_asset,
+ link_code_references_to_git,
load_assets_from_package_module,
+ op,
+ with_source_code_references,
)
+from dagster._core.definitions.metadata.source_code import AnchorBasedFilePathMapping
from . import assets
@@ -11,6 +18,35 @@
job=define_asset_job(name="all_assets_job"), cron_schedule="0 0 * * *"
)
+
+@op
+def foo_op():
+ return 5
+
+
+@graph_asset
+def my_asset():
+ return foo_op()
+
+
+my_assets = with_source_code_references(
+ [
+ my_asset,
+ *load_assets_from_package_module(assets),
+ ]
+)
+
+my_assets = link_code_references_to_git(
+ assets_defs=my_assets,
+ git_url="https://github.com/dagster-io/dagster/",
+ git_branch="master",
+ file_path_mapping=AnchorBasedFilePathMapping(
+ local_file_anchor=Path(__file__).parent,
+ file_anchor_path_in_repository="examples/quickstart_etl/quickstart_etl/",
+ ),
+)
+
defs = Definitions(
- assets=load_assets_from_package_module(assets), schedules=[daily_refresh_schedule]
+ assets=my_assets,
+ schedules=[daily_refresh_schedule],
)
diff --git a/python_modules/dagster/dagster/_core/definitions/metadata/source_code.py b/python_modules/dagster/dagster/_core/definitions/metadata/source_code.py
index a7e4ccb08f08c..c9bfe996afe81 100644
--- a/python_modules/dagster/dagster/_core/definitions/metadata/source_code.py
+++ b/python_modules/dagster/dagster/_core/definitions/metadata/source_code.py
@@ -6,7 +6,7 @@
from typing import TYPE_CHECKING, Any, Callable, List, Optional, Sequence, Union
import dagster._check as check
-from dagster._annotations import experimental
+from dagster._annotations import experimental, public
from dagster._model import DagsterModel
from dagster._serdes import whitelist_for_serdes
@@ -160,8 +160,17 @@ class FilePathMapping(ABC):
mapping function can be provided to handle these cases.
"""
+ @public
@abstractmethod
- def convert_to_source_control_path(self, local_path: Path) -> str: ...
+ def convert_to_source_control_path(self, local_path: Path) -> str:
+ """Maps a local file path to the corresponding path in a source control repository.
+
+ Args:
+ local_path (Path): The local file path to map.
+
+ Returns:
+ str: The corresponding path in the hosted source control repository, relative to the repository root.
+ """
@experimental
@@ -192,7 +201,17 @@ class AnchorBasedFilePathMapping(FilePathMapping):
local_file_anchor: Path
file_anchor_path_in_repository: str
+ @public
def convert_to_source_control_path(self, local_path: Path) -> str:
+ """Maps a local file path to the corresponding path in a source control repository
+ based on the anchor file and its corresponding path in the repository.
+
+ Args:
+ local_path (Path): The local file path to map.
+
+ Returns:
+ str: The corresponding path in the hosted source control repository, relative to the repository root.
+ """
path_from_anchor_to_target = os.path.relpath(
local_path,
self.local_file_anchor,