Skip to content

Commit 1ee0ba7

Browse files
committed
Disable the creation of the global webhook in the batch-create command
Signed-off-by: tdruez <[email protected]>
1 parent 6462c84 commit 1ee0ba7

File tree

8 files changed

+116
-24
lines changed

8 files changed

+116
-24
lines changed

Diff for: CHANGELOG.rst

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ v34.9.6 (unreleased)
2020
- Upgrade Bulma CSS library to version 1.0.2
2121
https://github.com/aboutcode-org/scancode.io/pull/1268
2222

23+
- Disable the creation of the global webhook in the ``batch-create`` command by default.
24+
The global webhook can be created by providing the ``--create-global-webhook`` option.
25+
A ``--no-global-webhook`` option was also added to the ``create-project`` command to
26+
provide the ability to skip the global webhook creation.
27+
2328
v34.9.5 (2025-02-19)
2429
--------------------
2530

Diff for: docs/command-line-interface.rst

+8-2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ Optional arguments:
105105

106106
- ``--pipeline PIPELINES`` Pipelines names to add on the project.
107107

108+
.. warning::
109+
Pipelines are added and are executed in order.
110+
108111
.. tip::
109112
Use the "pipeline_name:option1,option2" syntax to select optional steps:
110113

@@ -137,8 +140,8 @@ Optional arguments:
137140
of running in the current thread.
138141
Applies only when ``--execute`` is provided.
139142

140-
.. warning::
141-
Pipelines are added and are executed in order.
143+
- ``--no-global-webhook`` Skip the creation of the global webhook. This option is
144+
only useful if a global webhook is defined in the settings.
142145

143146
.. _cli_batch_create:
144147

@@ -196,6 +199,9 @@ Optional arguments:
196199
of running in the current thread.
197200
Applies only when ``--execute`` is provided.
198201

202+
- ``--create-global-webhook`` Create the global webhook for each project, if enabled in
203+
the settings. If not provided, the global webhook subscriptions are not created.
204+
199205
Example: Processing Multiple Docker Images
200206
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
201207

Diff for: scanpipe/management/commands/__init__.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ def create_project(
441441
labels=None,
442442
execute=False,
443443
run_async=False,
444+
create_global_webhook=True,
444445
command=None,
445446
):
446447
verbosity = getattr(command, "verbosity", 1)
@@ -462,7 +463,11 @@ def create_project(
462463
pipelines=pipelines, input_files=input_files, copy_from=copy_from
463464
)
464465

465-
project.save()
466+
save_kwargs = {}
467+
if not create_global_webhook:
468+
save_kwargs = {"skip_global_webhook": True}
469+
470+
project.save(**save_kwargs)
466471

467472
if labels:
468473
project.labels.add(*labels)
@@ -520,6 +525,15 @@ def add_arguments(self, parser):
520525
default=list(),
521526
help="Optional labels for the project.",
522527
)
528+
parser.add_argument(
529+
"--no-global-webhook",
530+
action="store_true",
531+
help=(
532+
"Skip the creation of the global webhook. "
533+
"This option is only useful if a global webhook is defined in the "
534+
"settings."
535+
),
536+
)
523537

524538
def create_project(
525539
self,
@@ -532,6 +546,7 @@ def create_project(
532546
labels=None,
533547
execute=False,
534548
run_async=False,
549+
create_global_webhook=True,
535550
):
536551
if execute and not pipelines:
537552
raise CommandError("The --execute option requires one or more pipelines.")
@@ -546,5 +561,6 @@ def create_project(
546561
labels=labels,
547562
execute=execute,
548563
run_async=run_async,
564+
create_global_webhook=create_global_webhook,
549565
command=self,
550566
)

Diff for: scanpipe/management/commands/batch-create.py

+12
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ def add_arguments(self, parser):
6868
"a timestamp (in the format [YYMMDD_HHMMSS]) will be used."
6969
),
7070
)
71+
parser.add_argument(
72+
"--create-global-webhook",
73+
action="store_true",
74+
default=False,
75+
help=(
76+
"Create the global webhook for each project, "
77+
"if enabled in the settings. "
78+
"If not provided, the global webhook subscriptions are not created."
79+
),
80+
)
7181

7282
def handle(self, *args, **options):
7383
self.verbosity = options["verbosity"]
@@ -110,6 +120,7 @@ def handle_input_directory(self, **options):
110120
labels=options["labels"],
111121
execute=options["execute"],
112122
run_async=options["async"],
123+
create_global_webhook=options["create_global_webhook"],
113124
)
114125
self.created_project_count += 1
115126

@@ -143,6 +154,7 @@ def handle_input_list(self, **options):
143154
labels=options["labels"],
144155
execute=options["execute"],
145156
run_async=options["async"],
157+
create_global_webhook=options["create_global_webhook"],
146158
)
147159
self.created_project_count += 1
148160

Diff for: scanpipe/management/commands/create-project.py

+10-19
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,15 @@ def add_arguments(self, parser):
3939

4040
def handle(self, *args, **options):
4141
self.verbosity = options["verbosity"]
42-
name = options["name"]
43-
pipelines = options["pipelines"]
44-
input_files = options["input_files"]
45-
input_urls = options["input_urls"]
46-
copy_from = options["copy_codebase"]
47-
notes = options["notes"]
48-
labels = options["labels"]
49-
execute = options["execute"]
50-
run_async = options["async"]
51-
5242
self.create_project(
53-
name=name,
54-
pipelines=pipelines,
55-
input_files=input_files,
56-
input_urls=input_urls,
57-
copy_from=copy_from,
58-
notes=notes,
59-
labels=labels,
60-
execute=execute,
61-
run_async=run_async,
43+
name=options["name"],
44+
pipelines=options["pipelines"],
45+
input_files=options["input_files"],
46+
input_urls=options["input_urls"],
47+
copy_from=options["copy_codebase"],
48+
notes=options["notes"],
49+
labels=options["labels"],
50+
execute=options["execute"],
51+
run_async=options["async"],
52+
create_global_webhook=not options["no_global_webhook"],
6253
)

Diff for: scanpipe/models.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,8 @@ def save(self, *args, **kwargs):
607607
is_new = self._state.adding
608608
# True if the new instance is a clone of an existing one.
609609
is_clone = kwargs.pop("is_clone", False)
610+
# True if the global webhook creation should be skipped.
611+
skip_global_webhook = kwargs.pop("skip_global_webhook", False)
610612

611613
if not self.slug:
612614
self.slug = get_project_slug(project=self)
@@ -617,7 +619,8 @@ def save(self, *args, **kwargs):
617619

618620
super().save(*args, **kwargs)
619621

620-
if settings.SCANCODEIO_GLOBAL_WEBHOOK and is_new and not is_clone:
622+
global_webhook = settings.SCANCODEIO_GLOBAL_WEBHOOK
623+
if global_webhook and is_new and not is_clone and not skip_global_webhook:
621624
self.setup_global_webhook()
622625

623626
def setup_global_webhook(self):

Diff for: scanpipe/tests/test_commands.py

+54
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,22 @@ def test_scanpipe_management_command_create_project_execute(self):
199199
"Project other_project created with work directory", out.getvalue()
200200
)
201201

202+
@override_settings(SCANCODEIO_GLOBAL_WEBHOOK={"target_url": "https://webhook.url"})
203+
@mock.patch.object(Project, "setup_global_webhook")
204+
def test_scanpipe_management_command_create_project_no_global_webhook(
205+
self, mock_setup_webhook
206+
):
207+
out = StringIO()
208+
options = ["--no-global-webhook"]
209+
call_command("create-project", "my_project", *options, stdout=out)
210+
self.assertIn("Project my_project created", out.getvalue())
211+
mock_setup_webhook.assert_not_called()
212+
213+
options = []
214+
call_command("create-project", "my_project_v2", *options, stdout=out)
215+
self.assertIn("Project my_project_v2 created", out.getvalue())
216+
mock_setup_webhook.assert_called()
217+
202218
def test_scanpipe_management_command_batch_create(self):
203219
expected = "You must provide either --input-directory or --input-list as input."
204220
with self.assertRaisesMessage(CommandError, expected):
@@ -277,6 +293,28 @@ def test_scanpipe_management_command_batch_create_input_list_csv(self):
277293
self.assertEqual("", input_source3.tag)
278294
self.assertFalse(input_source3.exists())
279295

296+
@override_settings(SCANCODEIO_GLOBAL_WEBHOOK={"target_url": "https://webhook.url"})
297+
@mock.patch.object(Project, "setup_global_webhook")
298+
def test_scanpipe_management_command_batch_create_global_webhook(
299+
self, mock_setup_webhook
300+
):
301+
input_directory = self.data / "commands" / "batch-create-directory"
302+
options = ["--input-directory", str(input_directory)]
303+
out = StringIO()
304+
call_command("batch-create", *options, stdout=out)
305+
self.assertIn("2 projects created.", out.getvalue())
306+
mock_setup_webhook.assert_not_called()
307+
308+
options += [
309+
"--create-global-webhook",
310+
"--project-name-suffix",
311+
"with-webhook",
312+
]
313+
out = StringIO()
314+
call_command("batch-create", *options, stdout=out)
315+
self.assertIn("2 projects created.", out.getvalue())
316+
mock_setup_webhook.assert_called()
317+
280318
def test_scanpipe_management_command_add_input_file(self):
281319
out = StringIO()
282320

@@ -1288,3 +1326,19 @@ def test_scanpipe_management_command_mixin_create_project_execute(self):
12881326
execute=True,
12891327
run_async=True,
12901328
)
1329+
1330+
@override_settings(SCANCODEIO_GLOBAL_WEBHOOK={"target_url": "https://webhook.url"})
1331+
@mock.patch.object(Project, "setup_global_webhook")
1332+
def test_scanpipe_management_command_mixin_create_project_no_global_webhook(
1333+
self, mock_setup_webhook
1334+
):
1335+
project = self.create_project_command.create_project(
1336+
name="no global webhook",
1337+
create_global_webhook=False,
1338+
)
1339+
self.assertTrue(project.pk)
1340+
mock_setup_webhook.assert_not_called()
1341+
1342+
project = self.create_project_command.create_project(name="with global webhook")
1343+
self.assertTrue(project.pk)
1344+
mock_setup_webhook.assert_called()

Diff for: scanpipe/tests/test_models.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,12 @@ def test_scanpipe_project_model_call_setup_global_webhook(self, mock_setup_webho
868868
project.save(is_clone=True)
869869
mock_setup_webhook.assert_not_called()
870870

871-
# Case 3: Global webhook is disabled (Webhook should NOT be called)
871+
# Case 3: Skip global webhook (Webhook should NOT be called)
872+
project = Project(name="Project with skip")
873+
project.save(skip_global_webhook=True)
874+
mock_setup_webhook.assert_not_called()
875+
876+
# Case 4: Global webhook is disabled (Webhook should NOT be called)
872877
with override_settings(SCANCODEIO_GLOBAL_WEBHOOK=None):
873878
project = Project(name="No Webhook Project")
874879
project.save()

0 commit comments

Comments
 (0)