Git & Gitlab support¶
arkindex_worker.git ¶
Helper classes for workers that interact with Git repositories and the GitLab API.
Classes¶
GitlabHelper ¶
GitlabHelper(
project_id,
gitlab_url,
gitlab_token,
branch,
rebase_wait_period=1,
delete_source_branch=True,
max_rebase_tries=10,
)
Helper class to save files to GitLab repository
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_id |
str
|
the id of the gitlab project |
required |
gitlab_url |
str
|
gitlab server url |
required |
gitlab_token |
str
|
gitlab private token of user with permission to accept merge requests |
required |
branch |
str
|
name of the branch to where the exported branch will be merged |
required |
rebase_wait_period |
Optional[int]
|
seconds to wait between each poll to check whether rebase has finished |
1
|
delete_source_branch |
Optional[bool]
|
should delete the source branch after merging? |
True
|
max_rebase_tries |
Optional[int]
|
max number of tries to rebase when merging before giving up |
10
|
Source code in arkindex_worker/git.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
Functions¶
merge ¶
merge(branch_name, title)
Create a merge request and try to merge. Always rebase first to avoid conflicts from MRs made in parallel
Parameters:
Name | Type | Description | Default |
---|---|---|---|
branch_name |
str
|
Source branch name |
required |
title |
str
|
Title of the merge request |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the branch was successfully merged |
Source code in arkindex_worker/git.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
GitHelper ¶
GitHelper(
repo_url,
git_dir,
export_path,
workflow_id,
gitlab_helper,
git_clone_wait_period=1,
)
A helper class for running git commands
At the beginning of the workflow call run_clone_in_background. When all the files are ready to be added to git then call save_files to move the files in to the git repository and try to push them.
Examples¶
in worker.configure() configure the git helper and start the cloning:
gitlab = GitlabHelper(...)
prepare_git_key(...)
self.git_helper = GitHelper(workflow_id=workflow_id, gitlab_helper=gitlab, ...)
self.git_helper.run_clone_in_background()
at the end of the workflow (at the end of worker.run()) push the files to git:
self.git_helper.save_files(self.out_dir)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo_url |
the url of the git repository where the export will be pushed |
required | |
git_dir |
the directory where to clone the git repository |
required | |
export_path |
the path inside the git repository where to put the exported files |
required | |
workflow_id |
the process id to see the workflow graph in the frontend |
required | |
gitlab_helper |
GitlabHelper
|
helper for gitlab |
required |
git_clone_wait_period |
check if clone has finished every N seconds at the end of the workflow |
1
|
Source code in arkindex_worker/git.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
|
Functions¶
run_clone_in_background ¶
run_clone_in_background()
Clones the git repository in the background in to the self.git_dir directory.
self.is_clone_finished
can be used to know whether the cloning has finished
or not.
Source code in arkindex_worker/git.py
286 287 288 289 290 291 292 293 294 295 296 297 298 |
|
save_files ¶
save_files(export_out_dir)
Move files in export_out_dir to the cloned git repository and try to merge the created files if possible.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
export_out_dir |
Path
|
Path to the files to be saved |
required |
Raises:
Type | Description |
---|---|
sh.ErrorReturnCode
|
description |
Exception
|
description |
Source code in arkindex_worker/git.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
|
Functions¶
make_backup ¶
make_backup(path)
Create a backup file in the same directory with timestamp as suffix “.bak_{timestamp}”
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Path to the file to be backed up |
required |
Source code in arkindex_worker/git.py
154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
prepare_git_key ¶
prepare_git_key(
private_key,
known_hosts,
private_key_path="~/.ssh/id_ed25519",
known_hosts_path="~/.ssh/known_hosts",
)
Prepare the git keys (put them in to the correct place) so that git could be used. Fixes some whitespace problems that come from arkindex secrets store (Django admin).
Also creates a backup of the previous keys if they exist, to avoid losing the original keys of the developers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
private_key |
str
|
git private key contents |
required |
known_hosts |
str
|
git known_hosts contents |
required |
private_key_path |
Optional[str]
|
path where to put the private key |
'~/.ssh/id_ed25519'
|
known_hosts_path |
Optional[str]
|
path where to put the known_hosts |
'~/.ssh/known_hosts'
|
Source code in arkindex_worker/git.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|