Skip to content

Commit

Permalink
added check to make sure repo is a valid uri before getting updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Bowden committed Jun 21, 2024
1 parent bd7384a commit 7964d42
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
13 changes: 13 additions & 0 deletions secureli/modules/shared/abstractions/pre_commit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
from pathlib import Path
import shutil
from urllib.parse import urlparse

# Note that this import is pulling from the pre-commit tool's internals.
# A cleaner approach would be to update pre-commit
Expand Down Expand Up @@ -189,8 +190,13 @@ def check_for_hook_updates(
"repo": repo_config.url
} # PreCommitSettings uses "url" instead of "repo", so we need to copy that value over
old_rev_info = HookRepoRevInfo.from_config(repo_config_dict)

# if the repo isn't a valid uri, don't try to download updates for it
if not self.is_valid_uri(old_rev_info.repo):
continue
# if the revision currently specified in .pre-commit-config.yaml looks like a full git SHA
# (40-character hex string), then set freeze to True

freeze = (
bool(git_commit_sha_pattern.fullmatch(repo_config.rev))
if freeze is None
Expand Down Expand Up @@ -425,3 +431,10 @@ def _get_outdated_repos(
repos = [key for key in outdated_repos.keys()]

return repos

def is_valid_uri(self, uri_string):
try:
result = urlparse(uri_string)
return all([result.scheme, result.netloc])
except Exception:
return False
39 changes: 35 additions & 4 deletions tests/modules/shared/abstractions/test_pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,10 +550,14 @@ def test_check_for_hook_updates_infers_freeze_param_when_not_provided(
pre_commit_config = RepositoryModels.PreCommitSettings(
repos=[pre_commit_config_repo]
)
rev_info_mock = MagicMock(rev=pre_commit_config_repo.rev)
rev_info_mock = MagicMock(
rev=pre_commit_config_repo.rev, repo="http://example-repo.com/"
)
mock_hook_repo_rev_info.return_value = rev_info_mock
rev_info_mock.update.return_value = rev_info_mock # Returning the same revision info on update means the hook will be considered up to date
pre_commit.check_for_hook_updates(pre_commit_config)
pre_commit.check_for_hook_updates(
pre_commit_config,
)
rev_info_mock.update.assert_called_with(tags_only=True, freeze=rev_is_sha)


Expand All @@ -575,7 +579,9 @@ def test_check_for_hook_updates_respects_freeze_param_when_false(
pre_commit_config = RepositoryModels.PreCommitSettings(
repos=[pre_commit_config_repo]
)
rev_info_mock = MagicMock(rev=pre_commit_config_repo.rev)
rev_info_mock = MagicMock(
rev=pre_commit_config_repo.rev, repo="http://example-repo.com/"
)
mock_hook_repo_rev_info.return_value = rev_info_mock
rev_info_mock.update.return_value = rev_info_mock # Returning the same revision info on update means the hook will be considered up to date
pre_commit.check_for_hook_updates(pre_commit_config, freeze=False)
Expand All @@ -596,7 +602,9 @@ def test_check_for_hook_updates_respects_freeze_param_when_true(
pre_commit_config = RepositoryModels.PreCommitSettings(
repos=[pre_commit_config_repo]
)
rev_info_mock = MagicMock(rev=pre_commit_config_repo.rev)
rev_info_mock = MagicMock(
rev=pre_commit_config_repo.rev, repo="http://example-repo.com/"
)
mock_hook_repo_rev_info.return_value = rev_info_mock
rev_info_mock.update.return_value = rev_info_mock # Returning the same revision info on update means the hook will be considered up to date
pre_commit.check_for_hook_updates(pre_commit_config, freeze=True)
Expand Down Expand Up @@ -638,6 +646,29 @@ def test_check_for_hook_updates_returns_repos_with_new_revs(
assert updated_repos[repo_urls[0]].newRev == "tag2"


def test_check_for_hook_updates_does_not_updated_repos_with_urls(
pre_commit: PreCommitAbstractionModels.PreCommitAbstraction,
):
with um.patch(
"secureli.modules.shared.abstractions.pre_commit.HookRepoRevInfo.from_config"
) as mock_hook_repo_rev_info:
pre_commit_config_repo = RepositoryModels.PreCommitRepo(
repo="local",
rev="tag1",
hooks=[RepositoryModels.PreCommitHook(id="hook-id")],
)
pre_commit_config = RepositoryModels.PreCommitSettings(
repos=[pre_commit_config_repo]
)
rev_info_mock = MagicMock(
rev=pre_commit_config_repo.rev, repo="http://example-repo.com/"
)
mock_hook_repo_rev_info.return_value = rev_info_mock
rev_info_mock.update.return_value = rev_info_mock # Returning the same revision info on update means the hook will be considered up to date
pre_commit.check_for_hook_updates(pre_commit_config, freeze=True)
rev_info_mock.update.assert_called_with(tags_only=True, freeze=True)


def test_pre_commit_config_exists(
pre_commit: PreCommitAbstractionModels.PreCommitAbstraction,
):
Expand Down

0 comments on commit 7964d42

Please sign in to comment.