diff --git a/gitlab_deps/hooks.py b/gitlab_deps/hooks.py index b97b3d4643e573bdc2eb80f045275577dbd1dbe6..c10727c02799e7710088310d668b5d681c5ad7f8 100644 --- a/gitlab_deps/hooks.py +++ b/gitlab_deps/hooks.py @@ -18,3 +18,12 @@ def check_hook(gl, hook_url, webhook_token, project_path, dry_run): 'pipeline_events': True, 'token': webhook_token, }) + + +def clear_hooks(gl, project_path, dry_run): + project = gl.projects.get(project_path) + for h in project.hooks.list(): + logging.info('%s: removing hook %s', project_path, h.url) + if not dry_run: + h.delete() + diff --git a/gitlab_deps/main.py b/gitlab_deps/main.py index f3940a64586ce600e66c21d33cf3123caa94934f..8bdf1491d5c7114c368ae761ccb7760a3741596b 100644 --- a/gitlab_deps/main.py +++ b/gitlab_deps/main.py @@ -7,7 +7,7 @@ from urllib.parse import urlsplit from .deps import get_branches, list_projects, list_deps, \ split_project_branch, read_deps -from .hooks import check_hook +from .hooks import check_hook, clear_hooks from .rebuild import rebuild_deps from .server import run_app @@ -105,6 +105,14 @@ from standard input if a filename is omitted or specified as '-'. type=argparse.FileType('r'), nargs='?', default=sys.stdin) + clear_hooks_parser = subparsers.add_parser( + 'clear-hooks', + parents=[common_parser]) + clear_hooks_parser.add_argument( + 'projects_list', + type=argparse.FileType('r'), + nargs='?', default=sys.stdin) + # Setup pipeline hooks on the specified projects. set_hooks_parser = subparsers.add_parser( 'set-hooks', @@ -264,6 +272,16 @@ specified as '-'. logging.error('error checking project %s: %s', project_path, e) + elif cmd == 'clear-hooks': + projects = set(y[0] for y in ( + split_project_branch(x.strip()) for x in args.projects_list)) + for project_path in projects: + try: + clear_hooks(gl, project_path, args.dry_run) + except Exception as e: + logging.error('error clearing hooks for project %s: %s', + project_path, e) + elif cmd == 'server': deps = read_deps(args.dependencies_list) run_app(gl, deps, args.bind_host, args.bind_port,