Skip to content

Add support for rm_file in GitHubFileSystem implementation #1839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions fsspec/implementations/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,33 @@ def _open(
cache_options=cache_options,
**kwargs,
)

def _rm(self, path):
if not self.username:
raise ValueError("Authentication required")

path = self._strip_protocol(path).lstrip("/")

# Get the file SHA
url = self.content_url.format(
org=self.org, repo=self.repo, path=path, sha=self.root
)
r = requests.get(url, timeout=self.timeout, **self.kw)
if r.status_code == 404:
raise FileNotFoundError(path)
r.raise_for_status()
content_json = r.json()
sha = content_json["sha"]

# Delete the file using GitHub API
delete_url = url
data = {
"message": f"Delete {path}",
"sha": sha,
"branch": self.root,
}
r = requests.delete(delete_url, json=data, timeout=self.timeout, **self.kw)
if r.status_code not in (200, 204):
r.raise_for_status()

self.invalidate_cache(path)
15 changes: 15 additions & 0 deletions fsspec/implementations/tests/test_github.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fsspec
import pytest


def test_github_open_small_file():
Expand Down Expand Up @@ -46,3 +47,17 @@ def test_github_ls():
expected = {"brain_networks.csv", "mpg.csv", "penguins.csv", "README.md", "raw"}
# check if the result is a subset of the expected files
assert expected.issubset(ls_result)


def test_github_rm():
# trying to remove a file without passing authentication should raise ValueError
fs = fsspec.filesystem("github", org="mwaskom", repo="seaborn-data")
with pytest.raises(ValueError):
fs.rm("mpg.csv")

# trying to remove a file which doesn't exist should raise FineNotFoundError
fs = fsspec.filesystem(
"github", org="mwaskom", repo="seaborn-data", username="user", token="token"
)
with pytest.raises(FileNotFoundError):
fs.rm("/this-file-doesnt-exist")