Repository: wandb/wandb

Issue:
[CLI]: 
### Describe the bug

<!--- Description of the issue below  -->

I'm calling wandb.init in a python file in a nested directory structure. It's failing with the following error:

```ERROR   SenderThread:15661 [sender.py:send_artifact():1417] send_artifact: failed for artifact "job/job-git_github.com_BambooHR_bhr-ai-labs-ml-ops.git_src_com_bamboohr_ai_labs_ml_ops_annotation_projects_enps_sentiment_enps_auto_tag_to_wandb_loader.py": 400 Client Error: Bad Request for url: https://api.wandb.ai/graphql```

<!--- A minimal code snippet between the quotes below  -->
```python
run = wandb.init(
            project=project,
            config={
                "tag": classification_tag,
                "origin": "spreadsheet",
                "labeler": "HR",
            },
        )
```

<!--- A full traceback of the exception in the quotes below -->
```shell

```


### Additional Files

[run.zip](https://github.com/wandb/wandb/files/10562780/run.zip)


### Environment

WandB version: 0.13.9

OS: macos Monterey 12.6.2

Python version: 3.9.7

Versions of relevant libraries:


### Additional Context

_No response_


Discussion:
There were a few errors in the log - this one is somewhat more descriptive:

`2023-02-01 15:46:57,582 ERROR   SenderThread:15661 [internal_api.py:execute():245] {"errors":[{"message":"invalid artifact name job-git_github.com_BambooHR_bhr-ai-labs-ml-ops.git_src_com_bamboohr_ai_labs_ml_ops_annotation_projects_enps_sentiment_enps_auto_tag_to_wandb_loader.py, must be 128 characters or less","path":["createArtifact"]}],"data":{"createArtifact":null}}`

Resolving patch:
diff --git a/wandb/sdk/internal/job_builder.py b/wandb/sdk/internal/job_builder.py
--- a/wandb/sdk/internal/job_builder.py
+++ b/wandb/sdk/internal/job_builder.py
@@ -144,8 +144,7 @@ def _build_artifact_job(
             ],
             "artifact": f"wandb-artifact://_id/{self._logged_code_artifact['id']}",
         }
-
-        name = f"job-{self._logged_code_artifact['name']}"
+        name = make_artifact_name_safe(f"job-{self._logged_code_artifact['name']}")
 
         artifact = Artifact(name, JOB_ARTIFACT_TYPE)
         return artifact, source
diff --git a/wandb/util.py b/wandb/util.py
--- a/wandb/util.py
+++ b/wandb/util.py
@@ -1872,7 +1872,11 @@ def ensure_text(
 def make_artifact_name_safe(name: str) -> str:
     """Make an artifact name safe for use in artifacts"""
     # artifact names may only contain alphanumeric characters, dashes, underscores, and dots.
-    return re.sub(r"[^a-zA-Z0-9_\-.]", "_", name)
+    cleaned = re.sub(r"[^a-zA-Z0-9_\-.]", "_", name)
+    if len(cleaned) <= 128:
+        return cleaned
+    # truncate with dots in the middle using regex
+    return re.sub(r"(^.{63}).*(.{63}$)", r"\g<1>..\g<2>", cleaned)
 
 
 def make_docker_image_name_safe(name: str) -> str:


Test patch:
diff --git a/tests/pytest_tests/unit_tests/test_job_builder.py b/tests/pytest_tests/unit_tests/test_job_builder.py
--- a/tests/pytest_tests/unit_tests/test_job_builder.py
+++ b/tests/pytest_tests/unit_tests/test_job_builder.py
@@ -1,13 +1,20 @@
 import json
+import random
+import string
 
 from wandb.sdk.internal.job_builder import JobBuilder
 from wandb.sdk.internal.settings_static import SettingsStatic
+from wandb.util import make_artifact_name_safe
 
 
-def test_build_repo_job(runner):
+def str_of_length(n):
+    return "".join(random.choices(string.ascii_uppercase + string.digits, k=n))
+
 
+def test_build_repo_job(runner):
+    remote_name = str_of_length(129)
     metadata = {
-        "git": {"remote": "testtest", "commit": "testtestcommit"},
+        "git": {"remote": remote_name, "commit": "testtestcommit"},
         "codePath": "blah/test.py",
         "args": ["--test", "test"],
         "python": "3.7",
@@ -22,7 +29,9 @@ def test_build_repo_job(runner):
         job_builder = JobBuilder(settings)
         artifact = job_builder.build()
         assert artifact is not None
-        assert artifact.name == "job-testtest_blah_test.py"
+        assert artifact.name == make_artifact_name_safe(
+            f"job-{remote_name}_blah_test.py"
+        )
         assert artifact.type == "job"
         assert artifact._manifest.entries["wandb-job.json"]
         assert artifact._manifest.entries["requirements.frozen.txt"]
@@ -34,6 +43,7 @@ def test_build_artifact_job(runner):
         "args": ["--test", "test"],
         "python": "3.7",
     }
+    artifact_name = str_of_length(129)
     with runner.isolated_filesystem():
         with open("requirements.txt", "w") as f:
             f.write("numpy==1.19.0")
@@ -42,21 +52,25 @@ def test_build_artifact_job(runner):
             f.write(json.dumps(metadata))
         settings = SettingsStatic({"files_dir": "./"})
         job_builder = JobBuilder(settings)
-        job_builder._logged_code_artifact = {"id": "testtest", "name": "artifact_name"}
+        job_builder._logged_code_artifact = {
+            "id": "testtest",
+            "name": artifact_name,
+        }
         artifact = job_builder.build()
         assert artifact is not None
-        assert artifact.name == "job-artifact_name"
+        assert artifact.name == make_artifact_name_safe(f"job-{artifact_name}")
         assert artifact.type == "job"
         assert artifact._manifest.entries["wandb-job.json"]
         assert artifact._manifest.entries["requirements.frozen.txt"]
 
 
 def test_build_image_job(runner):
+    image_name = str_of_length(129)
     metadata = {
         "codePath": "blah/test.py",
         "args": ["--test", "test"],
         "python": "3.7",
-        "docker": "mydockerimage",
+        "docker": image_name,
     }
     with runner.isolated_filesystem():
         with open("requirements.txt", "w") as f:
@@ -68,7 +82,7 @@ def test_build_image_job(runner):
         job_builder = JobBuilder(settings)
         artifact = job_builder.build()
         assert artifact is not None
-        assert artifact.name == "job-mydockerimage"
+        assert artifact.name == make_artifact_name_safe(f"job-{image_name}")
         assert artifact.type == "job"
         assert artifact._manifest.entries["wandb-job.json"]
         assert artifact._manifest.entries["requirements.frozen.txt"]