Created
July 22, 2024 22:09
-
-
Save ErmisCat/e3f7f80b62a796c5b2988422fc879d60 to your computer and use it in GitHub Desktop.
Azure Devops: Create Card w/ parent + Create git branch to match
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function add_ado_git_branch() { | |
| local project="<project name>" | |
| local title=$1 | |
| local description=$2 | |
| local org_url="https://dev.azure.com/<org name>" | |
| local api_version="6.0" | |
| local feature_id="<target feature id>" # (to link as parent to the created story) | |
| local assigned_to="<Your Name in Azure Devops>" | |
| local area="<org name>/<board name>" | |
| if [[ -z "$title" || -z "$description" ]]; then | |
| echo "Usage: add-branch <title> <description>" | |
| return 1 | |
| fi | |
| # Attempt to create the Azure DevOps work item (card) | |
| response=$(az boards work-item create \ | |
| --organization "$org_url" \ | |
| --project "$project" \ | |
| --area $area \ | |
| --type "User Story" \ | |
| --title "$title" \ | |
| --description "$description" \ | |
| --assigned-to $assigned_to) | |
| # Check if the response contains an error | |
| if echo "$response" | grep -q "ERROR"; then | |
| echo "Failed to create DevOps card. Error: $response" | |
| return 1 | |
| fi | |
| cleaned_response=$(echo "$response" | sed 's/\\D//g') | |
| # Extract the card ID and title | |
| card_id=$(echo "$cleaned_response" | jq -r '.id' 2>/dev/null) | |
| card_title=$(echo "$cleaned_response" | jq -r '.fields["System.Title"]' 2>/dev/null) | |
| az boards work-item relation add \ | |
| --organization "$org_url" \ | |
| --id $card_id \ | |
| --relation-type parent \ | |
| --target-id $feature_id | |
| if [[ -z "$card_id" || -z "$card_title" ]]; then | |
| echo "Failed to create DevOps card or parse response" | |
| return 1 | |
| fi | |
| # Format the branch name | |
| sanitized_title=$(echo "$card_title" | tr '[:upper:]' '[:lower:]' | tr -cd '[:alnum:]\n' | tr ' ' '-') | |
| branch_name="feature/${card_id}-${sanitized_title}" | |
| # Create the git branch | |
| git checkout -b "$branch_name" | |
| echo "Created branch: $branch_name" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the sanitization of $response into $cleaned_response is because of my --area name, for my org it is: "org/DevOps", and that is an escape character which breaks JQ. so just fyi you can adjust the script as needed for your own use case.