Skip to content

Commit

Permalink
Merge pull request #199 from aslheyrr/master
Browse files Browse the repository at this point in the history
Pass the artifacts checksums in the headers of the deploy request
  • Loading branch information
anancarv authored Nov 12, 2024
2 parents 320a00c + c6b705d commit b9cc431
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ This library enables you to manage Artifactory resources such as users, groups,
+ [Get the information about a file or folder](#get-the-information-about-a-file-or-folder)
+ [Deploy an artifact](#deploy-an-artifact)
+ [Deploy an artifact with properties](#deploy-an-artifact-with-properties)
+ [Deploy an artifact with checksums](#deploy-an-artifact-with-checksums)
+ [Deploy an artifact by checksums](#deploy-an-artifact-by-checksums)
+ [Download an artifact](#download-an-artifact)
+ [Retrieve artifact list](#retrieve-artifact-list)
+ [Retrieve artifact properties](#retrieve-artifact-properties)
Expand Down Expand Up @@ -424,7 +424,7 @@ artifact = art.artifacts.deploy("<LOCAL_FILE_LOCATION>", "<ARTIFACT_PATH_IN_ARTI
# artifact = art.artifacts.deploy("Desktop/myNewFile.txt", "my-repository/my/new/artifact/directory/file.txt", {"retention": ["30"]})
```

#### Deploy an artifact with checksums
#### Deploy an artifact by checksums

```python
artifact = art.artifacts.deploy("<LOCAL_FILE_LOCATION>", "<ARTIFACT_PATH_IN_ARTIFACTORY>", checksum_enabled=True)
Expand Down
2 changes: 2 additions & 0 deletions pyartifactory/models/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class OriginalChecksums(BaseModel):
"""Models original checksums."""

sha256: str
sha1: str
md5: str


class Child(BaseModel):
Expand Down
28 changes: 15 additions & 13 deletions pyartifactory/objects/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,22 @@ def deploy(
for file in files:
self.deploy(Path(f"{root}/{file}"), Path(f"{new_root}/{file}"), properties, checksum_enabled)
else:
properties_param_str = ""
if properties is not None:
properties_param_str = ";".join(f"{k}={value}" for k, values in properties.items() for value in values)
route = ";".join(s for s in [artifact_folder.as_posix(), properties_param_str] if s)
artifact_check_sums = Checksums.generate(local_file)
headers = {
"X-Checksum-Sha1": artifact_check_sums.sha1,
"X-Checksum-Sha256": artifact_check_sums.sha256,
"X-Checksum": artifact_check_sums.md5,
}
if checksum_enabled:
artifact_check_sums = Checksums.generate(local_file)
headers["X-Checksum-Deploy"] = "true"
try:
self._put(
route=artifact_folder.as_posix(),
headers={
"X-Checksum-Deploy": "true",
"X-Checksum-Sha1": artifact_check_sums.sha1,
"X-Checksum-Sha256": artifact_check_sums.sha256,
"X-Checksum": artifact_check_sums.md5,
},
route=route,
headers=headers,
)
except requests.exceptions.HTTPError as error:
if error.response.status_code == 404:
Expand All @@ -120,12 +125,9 @@ def deploy(
raise ArtifactNotFoundError(message)
raise ArtifactoryError from error
else:
headers["X-Checksum-Deploy"] = "false"
with local_file.open("rb") as stream:
properties_param_str = ""
if properties is not None:
properties_param_str = self._format_properties(properties)
route = ";".join(s for s in [artifact_folder.as_posix(), properties_param_str] if s)
self._put(route, data=stream)
self._put(route=route, headers=headers, data=stream)

logger.debug("Artifact %s successfully deployed", local_file)
return self.info(artifact_folder)
Expand Down
13 changes: 8 additions & 5 deletions tests/test_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@
"md5": "4cf609e0fe1267df8815bc650f5851e9",
"sha256": "396cf16e8ce000342c95ffc7feb2a15701d0994b70c1b13fea7112f85ac8e858",
},
"originalChecksums": {"sha256": "396cf16e8ce000342c95ffc7feb2a15701d0994b70c1b13fea7112f85ac8e858"},
"originalChecksums": {
"sha1": "962c287c760e03b03c17eb920f5358d05f44dd3b",
"md5": "4cf609e0fe1267df8815bc650f5851e9",
"sha256": "396cf16e8ce000342c95ffc7feb2a15701d0994b70c1b13fea7112f85ac8e858",
},
"uri": f"{URL}/api/storage/{ARTIFACT_PATH}",
}
FILE_INFO = ArtifactFileInfoResponse(**FILE_INFO_RESPONSE)
Expand Down Expand Up @@ -460,10 +464,9 @@ def test_deploy_artifact_with_properties_success():

@responses.activate
def test_deploy_artifact_with_multiple_properties_success():
properties_param_str = ""
for k, v in ARTIFACT_MULTIPLE_PROPERTIES.properties.items():
values_str = ",".join(list(map(urllib.parse.quote, v)))
properties_param_str += f"{k}={values_str};"
properties_param_str = ";".join(
f"{k}={value}" for k, values in ARTIFACT_MULTIPLE_PROPERTIES.properties.items() for value in values
)
responses.add(
responses.PUT,
f"{URL}/{ARTIFACT_PATH};{properties_param_str.rstrip(';')}",
Expand Down

0 comments on commit b9cc431

Please sign in to comment.