Skip to content

Commit b510e30

Browse files
authored
[FSTORE-1480] Deduplicate git* (logicalclocks#246)
* Move git* files to hopsworks_common * Adapt git* files * Fix the alias for git_commit in hopsworks
1 parent 552bc65 commit b510e30

20 files changed

+1563
-1323
lines changed

python/hopsworks/core/git_api.py

+6-484
Large diffs are not rendered by default.
+7-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright 2022 Logical Clocks AB
2+
# Copyright 2024 Hopsworks AB
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -14,33 +14,11 @@
1414
# limitations under the License.
1515
#
1616

17-
from hopsworks import client, git_op_execution
17+
from hopsworks_common.core.git_op_execution_api import (
18+
GitOpExecutionApi,
19+
)
1820

1921

20-
class GitOpExecutionApi:
21-
def __init__(
22-
self,
23-
project_id,
24-
project_name,
25-
):
26-
self._project_id = project_id
27-
self._project_name = project_name
28-
29-
def _get_execution(self, repo_id, execution_id):
30-
_client = client.get_instance()
31-
path_params = [
32-
"project",
33-
self._project_id,
34-
"git",
35-
"repository",
36-
str(repo_id),
37-
"execution",
38-
str(execution_id),
39-
]
40-
query_params = {"expand": "repository"}
41-
42-
return git_op_execution.GitOpExecution.from_response_json(
43-
_client._send_request("GET", path_params, query_params=query_params),
44-
self._project_id,
45-
self._project_name,
46-
)
22+
__all__ = [
23+
GitOpExecutionApi,
24+
]
+7-76
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright 2022 Logical Clocks AB
2+
# Copyright 2024 Hopsworks AB
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -14,80 +14,11 @@
1414
# limitations under the License.
1515
#
1616

17-
import json
17+
from hopsworks_common.core.git_provider_api import (
18+
GitProviderApi,
19+
)
1820

19-
from hopsworks import client, git_provider
20-
from hopsworks.client.exceptions import GitException
21-
from hopsworks.engine import git_engine
2221

23-
24-
class GitProviderApi:
25-
def __init__(
26-
self,
27-
project_id,
28-
project_name,
29-
):
30-
self._git_engine = git_engine.GitEngine(project_id, project_name)
31-
self._project_id = project_id
32-
self._project_name = project_name
33-
34-
def _get_providers(self):
35-
_client = client.get_instance()
36-
path_params = ["users", "git", "provider"]
37-
38-
return git_provider.GitProvider.from_response_json(
39-
_client._send_request("GET", path_params),
40-
self._project_id,
41-
self._project_name,
42-
)
43-
44-
def _get_default_configured_provider(self):
45-
providers = self._get_providers()
46-
if providers is None or len(providers) == 0:
47-
raise GitException("No git provider is configured")
48-
elif len(providers) == 1:
49-
return providers[0].git_provider
50-
else:
51-
raise GitException(
52-
"Multiple git providers are configured. Set the provider keyword to specify the provider to use"
53-
)
54-
55-
def _get_provider(self, provider: str):
56-
_client = client.get_instance()
57-
path_params = ["users", "git", "provider"]
58-
59-
providers = git_provider.GitProvider.from_response_json(
60-
_client._send_request("GET", path_params),
61-
self._project_id,
62-
self._project_name,
63-
)
64-
for p in providers:
65-
if p.git_provider.lower() == provider.lower():
66-
return p
67-
raise GitException("No git provider configured for {}".format(provider))
68-
69-
def _set_provider(self, provider: str, username: str, token: str):
70-
_client = client.get_instance()
71-
path_params = ["users", "git", "provider"]
72-
73-
provider_config = {
74-
"gitProvider": provider,
75-
"username": username,
76-
"token": token,
77-
}
78-
79-
headers = {"content-type": "application/json"}
80-
return git_provider.GitProvider.from_response_json(
81-
_client._send_request(
82-
"POST", path_params, headers=headers, data=json.dumps(provider_config)
83-
),
84-
self._project_id,
85-
self._project_name,
86-
)
87-
88-
def _delete_provider(self, provider: str):
89-
_client = client.get_instance()
90-
path_params = ["users", "secrets", "{}_token".format(provider.lower())]
91-
_client._send_request("DELETE", path_params)
92-
path_params = ["users", "secrets", "{}_username".format(provider.lower())]
93-
_client._send_request("DELETE", path_params)
22+
__all__ = [
23+
GitProviderApi,
24+
]
+7-104
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright 2022 Logical Clocks AB
2+
# Copyright 2024 Hopsworks AB
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -14,108 +14,11 @@
1414
# limitations under the License.
1515
#
1616

17-
from hopsworks import client, git_op_execution, git_remote
18-
from hopsworks.engine import git_engine
17+
from hopsworks_common.core.git_remote_api import (
18+
GitRemoteApi,
19+
)
1920

2021

21-
class GitRemoteApi:
22-
def __init__(self, project_id, project_name):
23-
self._git_engine = git_engine.GitEngine(project_id, project_name)
24-
self._project_id = project_id
25-
self._project_name = project_name
26-
27-
def _get(self, repo_id, name: str):
28-
_client = client.get_instance()
29-
path_params = [
30-
"project",
31-
self._project_id,
32-
"git",
33-
"repository",
34-
str(repo_id),
35-
"remote",
36-
str(name),
37-
]
38-
39-
remote = git_remote.GitRemote.from_response_json(
40-
_client._send_request("GET", path_params),
41-
self._project_id,
42-
self._project_name,
43-
)
44-
remote._repo_id = repo_id
45-
return remote
46-
47-
def _get_remotes(self, repo_id):
48-
_client = client.get_instance()
49-
path_params = [
50-
"project",
51-
self._project_id,
52-
"git",
53-
"repository",
54-
str(repo_id),
55-
"remote",
56-
]
57-
58-
remotes = git_remote.GitRemote.from_response_json(
59-
_client._send_request("GET", path_params),
60-
self._project_id,
61-
self._project_name,
62-
)
63-
for remote in remotes:
64-
remote._repo_id = repo_id
65-
return remotes
66-
67-
def _add(self, repo_id, name: str, url: str):
68-
_client = client.get_instance()
69-
path_params = [
70-
"project",
71-
self._project_id,
72-
"git",
73-
"repository",
74-
str(repo_id),
75-
"remote",
76-
]
77-
78-
query_params = {
79-
"action": "ADD",
80-
"url": url,
81-
"name": name,
82-
"expand": ["repository", "user"],
83-
}
84-
85-
headers = {"content-type": "application/json"}
86-
git_op = git_op_execution.GitOpExecution.from_response_json(
87-
_client._send_request(
88-
"POST", path_params, headers=headers, query_params=query_params
89-
),
90-
self._project_id,
91-
self._project_name,
92-
)
93-
_ = self._git_engine.execute_op_blocking(git_op, "ADD_REMOTE")
94-
return self._get(repo_id, name)
95-
96-
def _delete(self, repo_id, name: str):
97-
_client = client.get_instance()
98-
path_params = [
99-
"project",
100-
self._project_id,
101-
"git",
102-
"repository",
103-
str(repo_id),
104-
"remote",
105-
]
106-
107-
query_params = {
108-
"action": "DELETE",
109-
"name": name,
110-
"expand": ["repository", "user"],
111-
}
112-
113-
headers = {"content-type": "application/json"}
114-
git_op = git_op_execution.GitOpExecution.from_response_json(
115-
_client._send_request(
116-
"POST", path_params, headers=headers, query_params=query_params
117-
),
118-
self._project_id,
119-
self._project_name,
120-
)
121-
_ = self._git_engine.execute_op_blocking(git_op, "DELETE_REMOTE")
22+
__all__ = [
23+
GitRemoteApi,
24+
]

python/hopsworks/engine/git_engine.py

+7-46
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright 2022 Logical Clocks AB
2+
# Copyright 2024 Hopsworks AB
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -14,50 +14,11 @@
1414
# limitations under the License.
1515
#
1616

17-
import logging
18-
import time
17+
from hopsworks_common.engine.git_engine import (
18+
GitEngine,
19+
)
1920

20-
from hopsworks.client.exceptions import GitException
21-
from hopsworks.core import git_op_execution_api
2221

23-
24-
class GitEngine:
25-
def __init__(
26-
self,
27-
project_id,
28-
project_name,
29-
):
30-
self._git_op_execution_api = git_op_execution_api.GitOpExecutionApi(
31-
project_id, project_name
32-
)
33-
self._log = logging.getLogger(__name__)
34-
35-
def execute_op_blocking(self, git_op, command):
36-
"""Poll a git execution status until it reaches a terminal state
37-
:param git_op: git execution to monitor
38-
:type git_op: GitOpExecution
39-
:param command: git operation running
40-
:type command: str
41-
:return: The final GitOpExecution object
42-
:rtype: GitOpExecution
43-
"""
44-
45-
while git_op.success is None:
46-
self._log.info(
47-
"Running command {}, current status {}".format(command, git_op.state)
48-
)
49-
git_op = self._git_op_execution_api._get_execution(
50-
git_op.repository.id, git_op.id
51-
)
52-
time.sleep(5)
53-
54-
if git_op.success is False:
55-
raise GitException(
56-
"Git command failed with the message: {}".format(
57-
git_op.command_result_message
58-
)
59-
)
60-
else:
61-
self._log.info("Git command {} finished".format(command))
62-
63-
return git_op
22+
__all__ = [
23+
GitEngine,
24+
]

0 commit comments

Comments
 (0)