Skip to content

Commit

Permalink
add gcs listing and test release support
Browse files Browse the repository at this point in the history
* code migrated under gcs api service

* debug log added

* testing for list files with prefix added

* update debug on list files function

* fix unit test

* yaml for testpypi updated

* api token test added

---------

Co-authored-by: mab <[email protected]>
  • Loading branch information
kaanHapptiq and Fattouh92 authored Sep 17, 2024
1 parent ab12495 commit 619d79b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/publish_pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,29 @@ jobs:
GITHUB_TOKEN: ${{ github.token }}
run: |
gh release upload '${{ github.ref_name }}' dist/** --repo '${{ github.repository }}'
publish-to-testpypi:
name: Publish Python 🐍 distribution 📦 to TestPyPI
if: startsWith(github.ref, 'refs/tags/')
needs:
- build
runs-on: ubuntu-latest

environment:
name: testpypi
url: https://test.pypi.org/p/${{ matrix.package }}

permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing

steps:
- name: Download all the dists
uses: actions/download-artifact@v3
with:
name: python-package-distributions
path: dist/
- name: Publish distribution 📦 to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ def upload_file(self, file_path: bytes, bucket_name: str, destination: str, con
blob.upload_from_filename(file_path, content_type=content_type, timeout=self.timeout)
self.logger.info(f"File uploaded to {destination}.")
return f"gs://{bucket_name}/{destination}"

def list_files_with_prefix(self, bucket_name: str, prefix: str):
self.logger.info(f"Listing files in bucket {bucket_name} with prefix {prefix}")
bucket = self.client.bucket(bucket_name)
blobs = bucket.list_blobs(prefix=prefix)

file_list = [blob.name for blob in blobs]
self.logger.debug(f"Files in bucket {bucket_name} with prefix {prefix}: {file_list}")
self.logger.info(f"found {len(file_list)} files in bucket {bucket_name} with prefix {prefix}")

return file_list
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,25 @@ def test_upload_file_gzipped(gcs_api_service, monkeypatch):
gcs_api_service.client.bucket().blob.assert_called_once_with(destination)
gcs_api_service.client.bucket().blob(destination).upload_from_filename.assert_called_once_with(file_path, content_type=content_type, timeout=30)
gcs_api_service.client.bucket().blob(destination).content_encoding = "gzip"

def test_list_files_with_prefix(gcs_api_service, monkeypatch):
mock_client = MagicMock()
mock_bucket = MagicMock()
mock_blob1 = MagicMock()
mock_blob1.name = "file1.txt"
mock_blob2 = MagicMock()
mock_blob2.name = "file2.txt"
mock_blobs = [mock_blob1,mock_blob2]

mock_client.bucket.return_value = mock_bucket
mock_bucket.list_blobs.return_value = mock_blobs
monkeypatch.setattr(gcs_api_service, "client", mock_client)

bucket_name = "test-bucket"
prefix = "test-prefix/"

result = gcs_api_service.list_files_with_prefix(bucket_name, prefix)

assert result == ["file1.txt", "file2.txt"]
mock_client.bucket.assert_called_once_with(bucket_name)
mock_bucket.list_blobs.assert_called_once_with(prefix=prefix)

0 comments on commit 619d79b

Please sign in to comment.