Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build cancel command #1103

Merged
merged 8 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
atbagga marked this conversation as resolved.
Show resolved Hide resolved

#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"