Skip to content

Commit

Permalink
Build cancel command (#1103)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dhilmathy committed Oct 26, 2022
1 parent 53cab54 commit 81abbb9
Show file tree
Hide file tree
Showing 4 changed files with 504 additions and 0 deletions.
18 changes: 18 additions & 0 deletions azure-devops/azext_devops/dev/pipelines/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ def build_list(definition_ids=None, branch=None, organization=None, project=None
return builds


def build_cancel(build_id, open=False, organization=None, project=None, detect=None):
"""Cancels if build is running.
:param build_id: ID of the build.
:type build_id: int
:param open: Open the build results page in your web browser.
:type open: bool
:rtype: :class:`<Build> <v5_0.build.models.Build>`
"""
organization, project = resolve_instance_and_project(
detect=detect, organization=organization, project=project)
client = get_build_client(organization)
build = Build(status="Cancelling")
build = client.update_build(build=build, project=project, build_id=build_id)
if open:
_open_build(build, organization)
return build


def add_build_tags(build_id, tags, organization=None, project=None, detect=None):
"""Add tag(s) for a build.
:param build_id: ID of the build.
Expand Down
1 change: 1 addition & 0 deletions azure-devops/azext_devops/dev/pipelines/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def load_build_commands(self, _):
g.command('list', 'build_list', table_transformer=transform_builds_table_output)
g.command('queue', 'build_queue', table_transformer=transform_build_table_output)
g.show_command('show', 'build_show', table_transformer=transform_build_table_output)
g.command('cancel', 'build_cancel', table_transformer=transform_build_table_output)

with self.command_group('pipelines build tag', command_type=buildOps) as g:
# basic build tag commands
Expand Down
454 changes: 454 additions & 0 deletions tests/recordings/test_build_cancel.yaml

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions tests/test_pipelinesBuildCancelTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import os
import unittest

from azure_devtools.scenario_tests import AllowLargeResponse
from .utilities.helper import DevopsScenarioTest, disable_telemetry, set_authentication, get_test_org_from_env_variable

DEVOPS_CLI_TEST_ORGANIZATION = get_test_org_from_env_variable() or 'Https://dev.azure.com/dhilmathy'

class PipelinesBuildCancelTests(DevopsScenarioTest):
@AllowLargeResponse(size_kb=3072)
@disable_telemetry
@set_authentication
def test_build_cancel(self):
self.cmd('az devops configure --defaults organization=' + DEVOPS_CLI_TEST_ORGANIZATION + ' project=buildtests')

build_definition_name = 'BuildTests Definition1'

#QueueBuild to get a build ID
queue_build_command = 'az pipelines build queue --definition-name "' + build_definition_name + '" --detect false --output json'
queue_build_output = self.cmd(queue_build_command).get_output_in_json()
queued_build_id = queue_build_output["id"]

#Cancel the running build
cancel_running_build_command = 'az pipelines build cancel --id ' + str(queued_build_id) + ' --detect false --output json'
cancel_running_build_output = self.cmd(cancel_running_build_command).get_output_in_json()
assert cancel_running_build_output["status"] == "cancelling"

0 comments on commit 81abbb9

Please sign in to comment.