• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SwissDataScienceCenter / renku-python / 9058668052

13 May 2024 07:05AM UTC coverage: 77.713% (-8.4%) from 86.115%
9058668052

Pull #3727

github

web-flow
Merge 128d38387 into 050ed61bf
Pull Request #3727: fix: don't fail session launch when gitlab couldn't be reached

15 of 29 new or added lines in 3 files covered. (51.72%)

2594 existing lines in 125 files now uncovered.

23893 of 30745 relevant lines covered (77.71%)

3.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.94
/renku/command/command_builder/repo.py
1
# Copyright Swiss Data Science Center (SDSC). A partnership between
2
# École Polytechnique Fédérale de Lausanne (EPFL) and
3
# Eidgenössische Technische Hochschule Zürich (ETHZ).
4
#
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
8
#
9
#     http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
"""Command builder for repository."""
6✔
17

18
from pathlib import Path
6✔
19
from typing import List, Optional, Union
6✔
20

21
from renku.command.command_builder.command import Command, CommandResult, check_finalized
6✔
22
from renku.core import errors
6✔
23
from renku.core.git import ensure_clean
6✔
24
from renku.core.login import ensure_login
6✔
25
from renku.domain_model.project_context import project_context
6✔
26

27

28
class Commit(Command):
6✔
29
    """Builder for commands that create a commit."""
30

31
    HOOK_ORDER = 4
6✔
32

33
    def __init__(
6✔
34
        self,
35
        builder: Command,
36
        message: Optional[str] = None,
37
        commit_if_empty: Optional[bool] = False,
38
        raise_if_empty: Optional[bool] = False,
39
        commit_only: Optional[Union[str, List[Union[str, Path]]]] = None,
40
        skip_staging: bool = False,
41
        skip_dirty_checks: bool = False,
42
    ) -> None:
43
        """__init__ of Commit.
44

45
        Args:
46
            builder(Command): The current ``CommandBuilder``.
47
            message (str): The commit message. Auto-generated if left empty (Default value = None).
48
            commit_if_empty (bool): Whether to commit if there are no modified files (Default value = None).
49
            raise_if_empty (bool): Whether to raise an exception if there are no modified files (Default value = None).
50
            commit_only (bool): Only commit the supplied paths (Default value = None).
51
            skip_staging(bool): Don't commit staged files.
52
            skip_dirty_checks(bool): Don't check if paths are dirty or staged.
53
        """
54
        self._builder = builder
6✔
55
        self._message = message
6✔
56
        self._commit_if_empty = commit_if_empty
6✔
57
        self._raise_if_empty = raise_if_empty
6✔
58
        self._commit_filter_paths = commit_only
6✔
59
        self._skip_staging: bool = skip_staging
6✔
60
        self._skip_dirty_checks: bool = skip_dirty_checks
6✔
61

62
    def _pre_hook(self, builder: Command, context: dict, *args, **kwargs) -> None:
6✔
63
        """Hook to create a commit transaction.
64

65
        Args:
66
            builder(Command): The current ``CommandBuilder``.
67
            context(dict): The current context object.
68
        """
69
        from renku.core.util.git import prepare_commit
6✔
70

71
        if "stack" not in context:
6✔
72
            raise ValueError("Commit builder needs a stack to be set.")
×
73

74
        self.diff_before = prepare_commit(
6✔
75
            repository=project_context.repository,
76
            commit_only=self._commit_filter_paths,
77
            skip_staging=self._skip_staging,
78
            skip_dirty_checks=self._skip_dirty_checks,
79
        )
80

81
    def _post_hook(self, builder: Command, context: dict, result: CommandResult, *args, **kwargs):
6✔
82
        """Hook that commits changes.
83

84
        Args:
85
            builder(Command):The current ``CommandBuilder``.
86
            context(dict): The current context object.
87
            result(CommandResult): The result of the command execution.
88
        """
89
        from renku.core.util.git import finalize_commit
6✔
90

91
        if result.error:
6✔
92
            # TODO: Cleanup repo
93
            return
4✔
94

95
        try:
6✔
96
            finalize_commit(
6✔
97
                diff_before=self.diff_before,
98
                repository=project_context.repository,
99
                transaction_id=project_context.transaction_id,
100
                commit_only=self._commit_filter_paths,
101
                commit_empty=self._commit_if_empty,
102
                raise_if_empty=self._raise_if_empty,
103
                commit_message=self._message,
104
                skip_staging=self._skip_staging,
105
            )
UNCOV
106
        except errors.RenkuException as e:
×
UNCOV
107
            result.error = e
×
108

109
    @check_finalized
6✔
110
    def build(self) -> Command:
6✔
111
        """Build the command.
112

113
        Returns:
114
            Command: Finalized version of this command.
115
        """
116
        self._builder.add_pre_hook(self.HOOK_ORDER, self._pre_hook)
6✔
117
        self._builder.add_post_hook(self.HOOK_ORDER, self._post_hook)
6✔
118

119
        return self._builder.build()
6✔
120

121
    @check_finalized
6✔
122
    def with_commit_message(self, message: str) -> Command:
6✔
123
        """Set a new commit message.
124

125
        Args:
126
            message(str): Commit message to set.
127

128
        Returns:
129
            Command: This command with commit message applied.
130
        """
131
        self._message = message
2✔
132

133
        return self
2✔
134

135

136
class RequireClean(Command):
6✔
137
    """Builder to check if repo is clean."""
138

139
    HOOK_ORDER = 4
6✔
140

141
    def __init__(self, builder: Command) -> None:
6✔
142
        """__init__ of RequireClean."""
143
        self._builder = builder
6✔
144

145
    def _pre_hook(self, builder: Command, context: dict, *args, **kwargs) -> None:
6✔
146
        """Check if repo is clean.
147

148
        Args:
149
            builder(Command): Current ``CommandBuilder``.
150
            context(dict): Current context.
151
        """
152
        if not project_context.has_context():
6✔
153
            raise ValueError("Commit builder needs a ProjectContext to be set.")
×
154

155
        ensure_clean(ignore_std_streams=not builder._track_std_streams)
6✔
156

157
    @check_finalized
6✔
158
    def build(self) -> Command:
6✔
159
        """Build the command.
160

161
        Returns:
162
            Command: Finalized version of this command.
163
        """
164
        self._builder.add_pre_hook(self.HOOK_ORDER, self._pre_hook)
6✔
165

166
        return self._builder.build()
6✔
167

168

169
class RequireLogin(Command):
6✔
170
    """Builder to check if a user is logged in."""
171

172
    HOOK_ORDER = 4
6✔
173

174
    def __init__(self, builder: Command) -> None:
6✔
175
        """__init__ of RequireLogin."""
176
        self._builder = builder
×
177

178
    def _pre_hook(self, builder: Command, context: dict, *args, **kwargs) -> None:
6✔
179
        """Check if the user is logged in.
180

181
        Args:
182
            builder(Command): Current ``CommandBuilder``.
183
            context(dict): Current context.
184
        """
185
        if not project_context.has_context():
×
186
            raise ValueError("RequireLogin builder needs a ProjectContext to be set.")
×
187

188
        ensure_login()
×
189

190
    @check_finalized
6✔
191
    def build(self) -> Command:
6✔
192
        """Build the command.
193

194
        Returns:
195
            Command: Finalized version of this command.
196
        """
197
        self._builder.add_pre_hook(self.HOOK_ORDER, self._pre_hook)
×
198

199
        return self._builder.build()
×
200

201

202
class Isolation(Command):
6✔
203
    """Builder to run a command in git isolation."""
204

205
    HOOK_ORDER = 3
6✔
206

207
    def __init__(
6✔
208
        self,
209
        builder: Command,
210
    ) -> None:
211
        """__init__ of Commit."""
212
        self._builder = builder
3✔
213

214
    def _injection_pre_hook(self, builder: Command, context: dict, *args, **kwargs) -> None:
6✔
215
        """Hook to setup dependency injection for commit transaction.
216

217
        Args:
218
            builder(Command): Current ``CommandBuilder``.
219
            context(dict): Current context.
220
        """
UNCOV
221
        from renku.core.git import prepare_worktree
×
222

UNCOV
223
        if not project_context.has_context():
×
224
            raise ValueError("Commit builder needs a ProjectContext to be set.")
×
225

UNCOV
226
        _, self.isolation, self.path, self.branch_name = prepare_worktree(path=None, branch_name=None, commit=None)
×
227

228
    def _post_hook(self, builder: Command, context: dict, result: CommandResult, *args, **kwargs):
6✔
229
        """Hook that commits changes.
230

231
        Args:
232
            builder(Command): Current ``CommandBuilder``.
233
            context(dict): Current context.
234
        """
UNCOV
235
        from renku.core.git import finalize_worktree
×
236

UNCOV
237
        try:
×
UNCOV
238
            finalize_worktree(
×
239
                isolation=self.isolation,
240
                path=self.path,
241
                branch_name=self.branch_name,
242
                delete=True,
243
                new_branch=True,
244
                exception=result.error,
245
            )
246
        except errors.RenkuException as e:
×
247
            if not result.error:
×
248
                result.error = e
×
249

250
    @check_finalized
6✔
251
    def build(self) -> Command:
6✔
252
        """Build the command.
253

254
        Returns:
255
            Command: Finalized version of this command.
256
        """
UNCOV
257
        self._builder.add_injection_pre_hook(self.HOOK_ORDER, self._injection_pre_hook)
×
UNCOV
258
        self._builder.add_post_hook(self.HOOK_ORDER, self._post_hook)
×
259

UNCOV
260
        return self._builder.build()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc