Skip to content

Commit

Permalink
Correctly set branch when commit hash is not given (#2083)
Browse files Browse the repository at this point in the history
* Correctly set branch when commit hash is not given

* Adding unit test for syncing starters without a commit hash
  • Loading branch information
pt2302 authored Jan 30, 2024
1 parent 72a1ed2 commit 7837124
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion content_sync/apis/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def sync_starter_configs( # pylint:disable=too-many-locals
if commit:
git_file = repo.get_contents(config_file, commit)
else:
git_file = repo.get_contents(config_file)
git_file = repo.get_contents(config_file, ref=branch.name)
slug = (
git_file.path.split("/")[0]
if git_file.path != settings.OCW_STUDIO_SITE_CONFIG_FILE
Expand Down
34 changes: 33 additions & 1 deletion content_sync/apis/github_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ def test_sync_starter_configs_webhook_branch_hash_match(mocker, mock_github):
config_filenames = ["site-1/ocw-studio.yaml", "site-2/ocw-studio.yaml"]
fake_commit = "abc123"
release_branch = "release"
config_content = b"---\ncollections: []"
config_content = b"---\nroot-url-path: sites\ncollections: []"
mock_github.return_value.get_organization.return_value.get_repo.return_value.get_contents.side_effect = [
mocker.Mock(path=config_filenames[0], decoded_content=config_content),
mocker.Mock(path=config_filenames[1], decoded_content=config_content),
Expand All @@ -911,6 +911,38 @@ def test_sync_starter_configs_webhook_branch_hash_match(mocker, mock_github):
)


def test_sync_starter_configs_webhook_branch_nohash(mocker, mock_github):
"""
Sync starter with a non-main settings.GITHUB_WEBHOOK_BRANCH should trigger
getting that branch without needing a commit hash
"""
git_url = "https://github.com/testorg/ocws-configs"
config_filenames = ["site-1/ocw-studio.yaml", "site-2/ocw-studio.yaml"]
release_branch = "release"
config_content = b"---\nroot-url-path: sites\ncollections: []"

mock_github.reset_mock()
mock_github.return_value.get_organization.return_value.get_repo.return_value.get_contents.side_effect = [
mocker.Mock(path=config_filenames[0], decoded_content=config_content),
mocker.Mock(path=config_filenames[1], decoded_content=config_content),
]
settings.GITHUB_WEBHOOK_BRANCH = release_branch
mock_github.return_value.get_organization.return_value.get_repo.return_value.get_branch.return_value.name = (
release_branch
)

sync_starter_configs(git_url, config_filenames)

mock_github.return_value.get_organization.return_value.get_repo.return_value.get_branch.assert_called_once_with(
release_branch
)

for config_file in config_filenames:
mock_github.return_value.get_organization.return_value.get_repo.return_value.get_contents.assert_any_call(
config_file, ref=release_branch
)


def test_get_all_file_paths(mocker, mock_api_wrapper):
"""get_all_file_paths should yield all file paths in the repo"""
mock_api_wrapper.org.get_repo.return_value.get_contents.side_effect = [
Expand Down

0 comments on commit 7837124

Please sign in to comment.