Skip to content

Commit 81abbb9

Browse files
authored
Build cancel command (#1103)
* Added build cancel command * Added test case for build cancel command * Reverted local vs code launch settings * Resolved merge conflicts * Fixed styling issue * Updated parameter name for build cancel method
1 parent 53cab54 commit 81abbb9

File tree

4 files changed

+504
-0
lines changed

4 files changed

+504
-0
lines changed

azure-devops/azext_devops/dev/pipelines/build.py

+18
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ def build_list(definition_ids=None, branch=None, organization=None, project=None
123123
return builds
124124

125125

126+
def build_cancel(build_id, open=False, organization=None, project=None, detect=None):
127+
"""Cancels if build is running.
128+
:param build_id: ID of the build.
129+
:type build_id: int
130+
:param open: Open the build results page in your web browser.
131+
:type open: bool
132+
:rtype: :class:`<Build> <v5_0.build.models.Build>`
133+
"""
134+
organization, project = resolve_instance_and_project(
135+
detect=detect, organization=organization, project=project)
136+
client = get_build_client(organization)
137+
build = Build(status="Cancelling")
138+
build = client.update_build(build=build, project=project, build_id=build_id)
139+
if open:
140+
_open_build(build, organization)
141+
return build
142+
143+
126144
def add_build_tags(build_id, tags, organization=None, project=None, detect=None):
127145
"""Add tag(s) for a build.
128146
:param build_id: ID of the build.

azure-devops/azext_devops/dev/pipelines/commands.py

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def load_build_commands(self, _):
116116
g.command('list', 'build_list', table_transformer=transform_builds_table_output)
117117
g.command('queue', 'build_queue', table_transformer=transform_build_table_output)
118118
g.show_command('show', 'build_show', table_transformer=transform_build_table_output)
119+
g.command('cancel', 'build_cancel', table_transformer=transform_build_table_output)
119120

120121
with self.command_group('pipelines build tag', command_type=buildOps) as g:
121122
# basic build tag commands

tests/recordings/test_build_cancel.yaml

+454
Large diffs are not rendered by default.
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
# --------------------------------------------------------------------------------------------
5+
6+
import os
7+
import unittest
8+
9+
from azure_devtools.scenario_tests import AllowLargeResponse
10+
from .utilities.helper import DevopsScenarioTest, disable_telemetry, set_authentication, get_test_org_from_env_variable
11+
12+
DEVOPS_CLI_TEST_ORGANIZATION = get_test_org_from_env_variable() or 'Https://dev.azure.com/dhilmathy'
13+
14+
class PipelinesBuildCancelTests(DevopsScenarioTest):
15+
@AllowLargeResponse(size_kb=3072)
16+
@disable_telemetry
17+
@set_authentication
18+
def test_build_cancel(self):
19+
self.cmd('az devops configure --defaults organization=' + DEVOPS_CLI_TEST_ORGANIZATION + ' project=buildtests')
20+
21+
build_definition_name = 'BuildTests Definition1'
22+
23+
#QueueBuild to get a build ID
24+
queue_build_command = 'az pipelines build queue --definition-name "' + build_definition_name + '" --detect false --output json'
25+
queue_build_output = self.cmd(queue_build_command).get_output_in_json()
26+
queued_build_id = queue_build_output["id"]
27+
28+
#Cancel the running build
29+
cancel_running_build_command = 'az pipelines build cancel --id ' + str(queued_build_id) + ' --detect false --output json'
30+
cancel_running_build_output = self.cmd(cancel_running_build_command).get_output_in_json()
31+
assert cancel_running_build_output["status"] == "cancelling"

0 commit comments

Comments
 (0)