Skip to content

Commit bec43e3

Browse files
jsamPanaetius
authored andcommitted
fix(service): push to protected branches (#1614)
1 parent 83a971e commit bec43e3

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
from tests.utils import make_dataset_add_payload
4747

4848
IT_PROTECTED_REMOTE_REPO_URL = os.getenv(
49-
"IT_PROTECTED_REMOTE_REPO", "https://dev.renku.ch/gitlab/renku-qa/core-integration-test"
49+
"IT_PROTECTED_REMOTE_REPO", "https://dev.renku.ch/gitlab/renku-qa/core-it-protected.git"
5050
)
5151

5252
IT_REMOTE_REPO_URL = os.getenv("IT_REMOTE_REPOSITORY", "https://dev.renku.ch/gitlab/renku-qa/core-integration-test")
@@ -962,6 +962,9 @@ def svc_protected_repo(svc_client):
962962

963963
response = svc_client.post("/cache.project_clone", data=json.dumps(payload), headers=headers)
964964

965+
project_id = response.json["result"]["project_id"]
966+
_ = svc_client.post("/cache.migrate", data=json.dumps(dict(project_id=project_id)), headers=headers)
967+
965968
yield svc_client, headers, payload, response
966969

967970

renku/core/commands/save.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,19 @@ def repo_sync(repo, message=None, remote=None, paths=None):
9797
raise errors.GitError("Cannot commit changes") from e
9898

9999
try:
100-
# push local changes to remote branch
100+
# NOTE: Push local changes to remote branch.
101101
if origin.refs and repo.active_branch in origin.refs:
102102
origin.fetch()
103103
origin.pull(repo.active_branch)
104104

105-
origin.push(repo.active_branch)
105+
result = origin.push(repo.active_branch)
106+
if result and "[remote rejected] (pre-receive hook declined)" in result[0].summary:
107+
# NOTE: Push to new remote branch if original one is protected.
108+
pushed_branch = uuid4().hex
109+
repo.create_head(pushed_branch)
110+
repo.remote().push(pushed_branch)
111+
106112
except git.exc.GitCommandError as e:
107-
if "protected branches" not in e.stderr:
108-
raise errors.GitError("Cannot push changes") from e
109-
# push to new remote branch if original one is protected
110-
pushed_branch = uuid4().hex
111-
origin = repo.create_remote(pushed_branch, remote)
112-
origin.push(repo.active_branch)
113+
raise errors.GitError("Cannot push changes") from e
113114

114115
return saved_paths, pushed_branch

tests/service/views/test_dataset_views.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,8 @@ def test_edit_datasets_view(svc_client_with_repo):
945945
def test_protected_branch(svc_protected_repo):
946946
"""Test adding a file to protected branch."""
947947
svc_client, headers, payload, response = svc_protected_repo
948-
assert response
948+
949+
assert_rpc_response(response)
949950
assert {"result"} == set(response.json.keys())
950951

951952
payload = {
@@ -954,11 +955,8 @@ def test_protected_branch(svc_protected_repo):
954955
}
955956

956957
response = svc_client.post("/datasets.create", data=json.dumps(payload), headers=headers,)
957-
assert response
958958

959-
if "error" in response.json.keys() and response.json["error"]["migration_required"]:
960-
# TODO: Fix this test to work with new project versions
961-
return
959+
assert_rpc_response(response)
962960
assert {"result"} == set(response.json.keys())
963961
assert "master" != response.json["result"]["remote_branch"]
964962

0 commit comments

Comments
 (0)