SuggestBranchName

The script gets the pack name as input and suggests an available branch name, for example: pack name is "MyPack" the branch name will be "MyPack". If a branch with the name "MyPack" exists, the script return "MyPack_1".

python · XSOAR CI/CD

Details

IDSuggestBranchName
Languagepython
From Version6.0.0
Docker Imagedemisto/python3:3.12.13.10116658

README

The script gets the pack name as input and suggests an available branch name, for example:
pack name is “MyPack” the branch name will be “MyPack”.
If a branch with the name “MyPack” exists, the script return “MyPack_1”.

Script Data


Name Description
Script Type python3
Cortex XSOAR Version 6.0.0

Used In


This script is used in the following playbooks and scripts.

  • Pull Request Creation - Github
  • Pull Request Creation - Gitlab
  • Pull Request Creation - Bitbucket

Inputs


Argument Name Description
pack The name of the pack.
use_command Which command to use. Possible commands: gitlab-branch-list GitHub-get-branch * bitbucket-branch-get

Outputs


Path Description Type
AvailableBranch Available branch name based on the pack name. string
import demistomock as demisto
import pytest

FIND_AVAILABLE_BRANCH_CASES = [
    (
        "Hi",
        "bitbucket-branch-get",  # case bitbucket
    )
]

expected_branch_name = "Hi"

mocker_bitbucket = {
    "Contents": "Failed to execute bitbucket-branch-get command\nError: Error in API call [404] - "
    'Not Found\n{"type": "error", "error": {"message": "Hi"}}',
    "Type": 4,
}


@pytest.mark.parametrize("pack_name, command_get_branch", FIND_AVAILABLE_BRANCH_CASES)
def test_find_available_branch(pack_name, command_get_branch, mocker):
    """
    Given:
        - A pack name and a command to execute.
    When:
        - There isn't a branch name in the incident fields.
    Then:
        - Returning an available name for the new branch
    """
    from SuggestBranchName import find_available_branch

    mocker.patch.object(demisto, "executeCommand", return_value=mocker_bitbucket)
    branch_name = find_available_branch(pack_name, command_get_branch)
    assert branch_name == expected_branch_name


RESPONSE = [{"Contents": {"value": [{"name": "Test/Test"}, {"name": "Test/Test_1"}]}}]


@pytest.mark.parametrize("pack_name, response, expected_available_branch_name", [("Test", RESPONSE, "refs/heads/Test_2")])
def test_find_available_branch_azure_devops(mocker, pack_name: str, response: list[dict], expected_available_branch_name: str):
    """
    Given:
        - A pack name
    When:
        - Two branches exist
    Then:
        - Returning an available name for the new branch
    """
    from SuggestBranchName import find_available_branch_azure_devops

    mocker.patch.object(demisto, "executeCommand", return_value=response)
    branch_name = find_available_branch_azure_devops(pack_name)
    assert branch_name == expected_available_branch_name