38
38
)
39
39
from .patterns import PROJECT_SUBST , COMMAND_PROPERTY
40
40
from .utils import is_exe , check_create_dir , get_int , is_web_uri , get_bool
41
- from .opengrok import get_repos , get_repo_type , get_uri
41
+ from .opengrok import get_repos , get_repo_type , get_uri , delete_project_data
42
42
from .hook import run_hook
43
43
from .command import Command
44
44
from .restful import call_rest_api , do_api_call
64
64
DISABLED_CMD_PROPERTY = 'disabled_command'
65
65
HOOK_PRE_PROPERTY = "pre"
66
66
HOOK_POST_PROPERTY = "post"
67
+ STRIP_OUTGOING_PROPERTY = "strip_outgoing"
67
68
68
69
69
70
def get_repos_for_project (project_name , uri , source_root ,
@@ -177,6 +178,7 @@ def get_project_properties(project_config, project_name, hookdir):
177
178
use_proxy = False
178
179
ignored_repos = None
179
180
check_changes = None
181
+ check_outgoing = None
180
182
ignore_errors = None
181
183
182
184
logger = logging .getLogger (__name__ )
@@ -230,6 +232,12 @@ def get_project_properties(project_config, project_name, hookdir):
230
232
project_config .get (INCOMING_PROPERTY ))
231
233
logger .debug ("incoming check = {}" .format (check_changes ))
232
234
235
+ if project_config .get (STRIP_OUTGOING_PROPERTY ) is not None :
236
+ check_outgoing = get_bool (logger , ("outgoing check for project {}" .
237
+ format (project_name )),
238
+ project_config .get (STRIP_OUTGOING_PROPERTY ))
239
+ logger .debug ("outgoing check = {}" .format (check_changes ))
240
+
233
241
if project_config .get (IGNORE_ERR_PROPERTY ) is not None :
234
242
ignore_errors = get_bool (logger , ("ignore errors for project {}" .
235
243
format (project_name )),
@@ -240,7 +248,7 @@ def get_project_properties(project_config, project_name, hookdir):
240
248
ignored_repos = []
241
249
242
250
return prehook , posthook , hook_timeout , command_timeout , \
243
- use_proxy , ignored_repos , check_changes , ignore_errors
251
+ use_proxy , ignored_repos , check_changes , check_outgoing , ignore_errors
244
252
245
253
246
254
def process_hook (hook_ident , hook , source_root , project_name , proxy ,
@@ -342,7 +350,7 @@ def run_command(cmd, project_name):
342
350
343
351
344
352
def handle_disabled_project (config , project_name , disabled_msg , headers = None ,
345
- timeout = None ):
353
+ timeout = None , api_timeout = None ):
346
354
disabled_command = config .get (DISABLED_CMD_PROPERTY )
347
355
if disabled_command :
348
356
logger = logging .getLogger (__name__ )
@@ -365,7 +373,7 @@ def handle_disabled_project(config, project_name, disabled_msg, headers=None,
365
373
366
374
try :
367
375
call_rest_api (disabled_command , {PROJECT_SUBST : project_name },
368
- http_headers = headers , timeout = timeout )
376
+ http_headers = headers , timeout = timeout , api_timeout = api_timeout )
369
377
except RequestException as e :
370
378
logger .error ("API call failed for disabled command of "
371
379
"project '{}': {}" .
@@ -393,18 +401,58 @@ def get_mirror_retcode(ignore_errors, value):
393
401
return value
394
402
395
403
396
- def mirror_project (config , project_name , check_changes , uri ,
397
- source_root , headers = None , timeout = None ):
404
+ def process_outgoing (repos , project_name ):
405
+ """
406
+ Detect and strip any outgoing changes for the repositories.
407
+ :param repos: list of repository objects
408
+ :param project_name: name of the project
409
+ :return: if any of the repositories had to be reset
410
+ """
411
+
412
+ logger = logging .getLogger (__name__ )
413
+
414
+ ret = False
415
+ for repo in repos :
416
+ if repo .strip_outgoing ():
417
+ logger .debug ('Repository {} in project {} had outgoing changes stripped' .
418
+ format (repo , project_name ))
419
+ ret = True
420
+
421
+ return ret
422
+
423
+
424
+ def wipe_project_data (project_name , uri , headers = None , timeout = None , api_timeout = None ):
425
+ """
426
+ Remove data for the project and mark it as not indexed.
427
+ :param project_name: name of the project
428
+ :param uri: URI of the webapp
429
+ :param headers: HTTP headers
430
+ :param timeout: connect timeout
431
+ :param api_timeout: asynchronous API timeout
432
+ """
433
+
434
+ logger = logging .getLogger (__name__ )
435
+
436
+ logger .info ("removing data for project {}" .format (project_name ))
437
+ delete_project_data (logger , project_name , uri ,
438
+ headers = headers , timeout = timeout , api_timeout = api_timeout )
439
+
440
+
441
+ def mirror_project (config , project_name , check_changes , check_outgoing , uri ,
442
+ source_root , headers = None , timeout = None , api_timeout = None ):
398
443
"""
399
444
Mirror the repositories of single project.
400
445
:param config global configuration dictionary
401
446
:param project_name: name of the project
402
447
:param check_changes: check for changes in the project or its repositories
403
448
and terminate if no change is found
449
+ :param check_outgoing: check for outgoing changes in the repositories of the project,
450
+ strip the changes and wipe project data if such changes were found
404
451
:param uri web application URI
405
452
:param source_root source root
406
453
:param headers: optional dictionary of HTTP headers
407
- :param timeout: optional timeout in seconds for API call response
454
+ :param timeout: connect timeout
455
+ :param api_timeout: optional timeout in seconds for API call response
408
456
:return exit code
409
457
"""
410
458
@@ -416,6 +464,7 @@ def mirror_project(config, project_name, check_changes, uri,
416
464
prehook , posthook , hook_timeout , command_timeout , use_proxy , \
417
465
ignored_repos , \
418
466
check_changes_proj , \
467
+ check_outgoing_proj , \
419
468
ignore_errors_proj = get_project_properties (project_config ,
420
469
project_name ,
421
470
config .
@@ -431,6 +480,11 @@ def mirror_project(config, project_name, check_changes, uri,
431
480
else :
432
481
check_changes_config = check_changes_proj
433
482
483
+ if check_outgoing_proj is None :
484
+ check_outgoing_config = config .get (STRIP_OUTGOING_PROPERTY )
485
+ else :
486
+ check_outgoing_config = check_outgoing_proj
487
+
434
488
if ignore_errors_proj is None :
435
489
ignore_errors = config .get (IGNORE_ERR_PROPERTY )
436
490
else :
@@ -446,7 +500,8 @@ def mirror_project(config, project_name, check_changes, uri,
446
500
project_config .
447
501
get (DISABLED_REASON_PROPERTY ),
448
502
headers = headers ,
449
- timeout = timeout )
503
+ timeout = timeout ,
504
+ api_timeout = api_timeout )
450
505
logger .info ("Project '{}' disabled, exiting" .
451
506
format (project_name ))
452
507
return CONTINUE_EXITVAL
@@ -473,6 +528,20 @@ def mirror_project(config, project_name, check_changes, uri,
473
528
if check_changes_config is not None :
474
529
check_changes = check_changes_config
475
530
531
+ if check_outgoing_config is not None :
532
+ check_outgoing = check_outgoing_config
533
+
534
+ if check_outgoing :
535
+ try :
536
+ r = process_outgoing (repos , project_name )
537
+ except RepositoryException as exc :
538
+ logger .error ('Failed to handle outgoing changes for '
539
+ 'a repository in project {}: {}' .format (project_name , exc ))
540
+ return get_mirror_retcode (ignore_errors , FAILURE_EXITVAL )
541
+ if r :
542
+ wipe_project_data (project_name , uri , headers = headers ,
543
+ timeout = timeout , api_timeout = api_timeout )
544
+
476
545
# Check if the project or any of its repositories have changed.
477
546
if check_changes :
478
547
r = process_changes (repos , project_name , uri , headers = headers )
@@ -522,7 +591,7 @@ def check_project_configuration(multiple_project_config, hookdir=False,
522
591
HOOK_TIMEOUT_PROPERTY , PROXY_PROPERTY ,
523
592
IGNORED_REPOS_PROPERTY , HOOKS_PROPERTY ,
524
593
DISABLED_REASON_PROPERTY , INCOMING_PROPERTY ,
525
- IGNORE_ERR_PROPERTY ]
594
+ IGNORE_ERR_PROPERTY , STRIP_OUTGOING_PROPERTY ]
526
595
527
596
if not multiple_project_config :
528
597
return True
@@ -640,7 +709,7 @@ def check_configuration(config):
640
709
COMMANDS_PROPERTY , PROJECTS_PROPERTY ,
641
710
HOOK_TIMEOUT_PROPERTY , CMD_TIMEOUT_PROPERTY ,
642
711
DISABLED_CMD_PROPERTY , INCOMING_PROPERTY ,
643
- IGNORE_ERR_PROPERTY ]
712
+ IGNORE_ERR_PROPERTY , STRIP_OUTGOING_PROPERTY ]
644
713
645
714
diff = set (config .keys ()).difference (global_tunables )
646
715
if diff :
0 commit comments