File size: 13,766 Bytes
af46a40 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | ---
author: Steven Hoang
pubDatetime: 2024-08-21T12:00:00Z
title: "[DevOps] Automating Branch Cleanup in Azure DevOps with Node.js"
postSlug: tools-devops-repositories-branches-cleanup
featured: false
draft: false
tags:
- azure-devops
- repo-cleanup
- tools
description: "A comprehensive guide on automating the cleanup of old branches in Azure DevOps Git repositories using a Node.js script.
The script identifies branches that haven't been updated in the last 90 days and deletes them if they meet certain criteria."
---
## Introduction
As software projects evolve, Git repositories can become cluttered with outdated or redundant branches. This clutter makes repository navigation cumbersome and can introduce confusion or errors in the development process. Automating the cleanup of these branches helps maintain an organized and efficient development environment.
In this guide, we'll walk through setting up a TypeScript script that automatically deletes old, unnecessary branches in Azure DevOps. We'll cover the essential steps, focusing on the implementation and automation of the cleanup process.
---
## Table of Contents
- [Introduction](#introduction)
- [Table of Contents](#table-of-contents)
- [Why Automate Branch Cleanup?](#why-automate-branch-cleanup)
- [Prerequisites](#prerequisites)
- [Project Setup](#project-setup)
- [Configuration File](#configuration-file)
- [Implementing the TypeScript Script](#implementing-the-typescript-script)
- [1. Loading Environment Variables](#1-loading-environment-variables)
- [2. Defining the Configuration Interface](#2-defining-the-configuration-interface)
- [3. Setting Constants](#3-setting-constants)
- [4. Getting the Git API Client](#4-getting-the-git-api-client)
- [5. Loading the Configuration](#5-loading-the-configuration)
- [6. Retrieving Repositories and Branches](#6-retrieving-repositories-and-branches)
- [7. Determining the Last Commit Date](#7-determining-the-last-commit-date)
- [8. Checking if a Branch is Merged](#8-checking-if-a-branch-is-merged)
- [9. Deleting a Branch](#9-deleting-a-branch)
- [10. Compiling the Exclusion List](#10-compiling-the-exclusion-list)
- [11. Cleaning Up Branches](#11-cleaning-up-branches)
- [Automating with Azure DevOps Pipeline](#automating-with-azure-devops-pipeline)
- [Conclusion](#conclusion)
- [Additional Resources](#additional-resources)
- [Thank You](#thank-you)
---
## Why Automate Branch Cleanup?
Automating branch cleanup is essential for several reasons:
- **Reduce Clutter**: Keeps the repository clean, making it easier for developers to navigate.
- **Improve Performance**: Enhances CI/CD pipeline performance by reducing overhead.
- **Prevent Confusion**: Minimizes the risk of developers working on or merging outdated branches.
- **Enhance Security**: Removes obsolete branches that may contain vulnerabilities.
---
## Prerequisites
Ensure you have the following before starting:
- **Azure DevOps Account**: Access to your organization's Azure DevOps instance.
- **Personal Access Token (PAT)**: A PAT with permissions to access and manage repositories.
- **Node.js and npm**: Installed on your machine (Node.js version 14 or later).
- **TypeScript**: Installed globally (`npm install -g typescript`).
- **Azure DevOps Node API Package**: Install via `npm install azure-devops-node-api`.
- **dotenv Package**: Install via `npm install dotenv`.
---
## Project Setup
1. **Create a New Directory**: Initialize a new Node.js project.
```bash
mkdir azure-devops-branch-cleanup
cd azure-devops-branch-cleanup
npm init -y
```
2. **Install Dependencies**:
```bash
npm install @azure/identity @microsoft/microsoft-graph-client azure-devops-node-api dayjs dotenv
npm install --save-dev typescript @types/node
```
---
## Configuration File
Create a `config.json` file in your project root to specify branches that should be excluded from deletion:
```json
{
"globalExcludes": ["master", "develop", "main", "release"],
"repositoryExcludes": {
"your-repo-name": ["feature/important-branch"]
}
}
```
- **globalExcludes**: Branches excluded from deletion across all repositories.
- **repositoryExcludes**: Specific branches to exclude in specific repositories.
---
## Implementing the TypeScript Script
Create a TypeScript file, e.g., `cleanup.ts`, and implement the following steps:
### 1. Loading Environment Variables
Use the `dotenv` package to load environment variables.
```typescript
import * as dotenv from "dotenv";
dotenv.config();
const isDryRun = process.env.DryRun === "true";
```
**Environment Variables Required**:
- `AZURE_DEVOPS_URL`: Your Azure DevOps organization URL.
- `AZURE_DEVOPS_PAT`: Your Personal Access Token.
- `AZURE_DEVOPS_PROJECT`: Your project name.
- `DryRun`: Set to `"true"` for dry-run mode (no actual deletions).
### 2. Defining the Configuration Interface
Define an interface to ensure type safety.
```typescript
interface Config {
globalExcludes: string[];
repositoryExcludes: {
[repoName: string]: string[];
};
}
```
### 3. Setting Constants
Define constants used in the script.
```typescript
const DAYS_90_MS = 90 * 24 * 60 * 60 * 1000; // 90 days in milliseconds
```
### 4. Getting the Git API Client
Authenticate and obtain the Git API client.
```typescript
import * as azdev from "azure-devops-node-api";
import * as GitApi from "azure-devops-node-api/GitApi";
async function getGitApi(): Promise<GitApi.IGitApi> {
const orgUrl = process.env.AZURE_DEVOPS_URL;
const token = process.env.AZURE_DEVOPS_PAT;
if (!orgUrl || !token) {
throw new Error(
"Azure DevOps URL or PAT is not set in environment variables."
);
}
const authHandler = azdev.getPersonalAccessTokenHandler(token);
const connection = new azdev.WebApi(orgUrl, authHandler);
return await connection.getGitApi();
}
```
### 5. Loading the Configuration
Load the `config.json` file.
```typescript
import * as fs from "fs";
import * as path from "path";
function loadConfig(): Config {
const configPath = path.join(__dirname, "config.json");
const configContent = fs.readFileSync(configPath, "utf-8");
return JSON.parse(configContent) as Config;
}
```
### 6. Retrieving Repositories and Branches
Get the list of repositories and branches.
```typescript
import * as GitInterfaces from "azure-devops-node-api/interfaces/GitInterfaces";
async function getRepositories(
gitApi: GitApi.IGitApi,
project: string
): Promise<GitInterfaces.GitRepository[]> {
return await gitApi.getRepositories(project);
}
async function getBranches(
gitApi: GitApi.IGitApi,
project: string,
repoId: string
): Promise<GitInterfaces.GitRef[]> {
const branches = await gitApi.getRefs(repoId, project);
return branches.filter(b => b.name.startsWith("refs/heads/"));
}
```
### 7. Determining the Last Commit Date
Get the date of the last commit on a branch.
```typescript
async function getLastCommitDate(
gitApi: GitApi.IGitApi,
project: string,
repoId: string,
branchName: string
): Promise<Date | null> {
const commits = await gitApi.getCommits(
repoId,
{ itemVersion: { version: branchName } },
project,
undefined,
1
);
if (commits.length > 0) {
const commitDate = commits[0].committer.date || commits[0].author.date;
return new Date(commitDate);
}
return null;
}
```
### 8. Checking if a Branch is Merged
Check if a branch is merged into any of the target branches.
```typescript
import { GitVersionType } from "azure-devops-node-api/interfaces/GitInterfaces";
async function isBranchMerged(
gitApi: GitApi.IGitApi,
project: string,
repoId: string,
branch: string,
targetBranches: string[]
): Promise<boolean> {
for (const targetBranch of targetBranches) {
const diff = await gitApi.getCommitDiffs(
repoId,
project,
true,
1,
undefined,
{ baseVersionType: GitVersionType.Branch, baseVersion: branch },
{ targetVersionType: GitVersionType.Branch, targetVersion: targetBranch }
);
if (diff && diff.aheadCount === 0) {
return true;
}
}
return false;
}
```
### 9. Deleting a Branch
Delete the branch if it meets the criteria.
```typescript
async function deleteBranch(
gitApi: GitApi.IGitApi,
project: string,
repoId: string,
branch: GitInterfaces.GitRef
): Promise<void> {
if (!isDryRun) {
if (branch.isLocked) {
await gitApi.updateRef(
{ name: branch.name, isLocked: false },
repoId,
"",
project
);
}
await gitApi.updateRefs(
[
{
name: branch.name,
newObjectId: "0000000000000000000000000000000000000000",
oldObjectId: branch.objectId,
},
],
repoId,
"",
project
);
}
console.log(`Deleted branch: ${branch.name} (Dry Run: ${isDryRun})`);
}
```
### 10. Compiling the Exclusion List
Combine global and repository-specific exclusions.
```typescript
function getExclusionList(config: Config, repoName: string): string[] {
const globalExcludes = config.globalExcludes || [];
const repoSpecificExcludes = config.repositoryExcludes[repoName] || [];
return [...new Set([...globalExcludes, ...repoSpecificExcludes])];
}
```
### 11. Cleaning Up Branches
Main function orchestrating the cleanup.
```typescript
async function cleanUpBranches(): Promise<void> {
const project = process.env.AZURE_DEVOPS_PROJECT;
if (!project) {
throw new Error(
"Azure DevOps project name is not set in environment variables."
);
}
const config = loadConfig();
const now = new Date();
const gitApi = await getGitApi();
const repositories = await getRepositories(gitApi, project);
for (const repo of repositories) {
console.log(`Processing repository: ${repo.name}`);
const excludeBranches = getExclusionList(config, repo.name);
const branches = await getBranches(gitApi, project, repo.id);
for (const branch of branches) {
const branchName = branch.name.replace("refs/heads/", "");
if (excludeBranches.includes(branchName)) {
console.log(`Skipping excluded branch: ${branchName}`);
continue;
}
const lastCommitDate = await getLastCommitDate(
gitApi,
project,
repo.id,
branchName
);
if (
!lastCommitDate ||
now.getTime() - lastCommitDate.getTime() < DAYS_90_MS
) {
console.log(`Branch is recent or active: ${branchName}`);
continue;
}
const isMerged = await isBranchMerged(
gitApi,
project,
repo.id,
branchName,
config.globalExcludes
);
if (isMerged) {
await deleteBranch(gitApi, project, repo.id, branch);
} else {
console.log(`Branch is not merged: ${branchName}`);
}
}
}
}
cleanUpBranches().catch(err => {
console.error("An error occurred:", err);
});
```
---
## Automating with Azure DevOps Pipeline
To automate the script execution, set up an Azure DevOps Pipeline.
1. **Create a Variable Group**:
- Navigate to **Pipelines** > **Library** in Azure DevOps.
- Click **"Variable groups"** > **"Add variable group"**.
- Name the group, e.g., `az-devops`.
- Add the variables:
- `AZURE_DEVOPS_URL`
- `AZURE_DEVOPS_PAT` (set as secret)
- `AZURE_DEVOPS_PROJECT`
- Save the variable group.
2. **Create the Pipeline YAML File**:
Create a `azure-pipelines.yml` file in your repository:
```yaml
trigger: none
schedules:
- cron: "0 0 * * 0" # Runs every Sunday at 00:00
displayName: "Weekly Branch Cleanup"
branches:
include:
- main
always: true
batch: false
pool:
vmImage: ubuntu-latest
variables:
- group: az-devops
steps:
- task: NodeTool@0
inputs:
versionSpec: "14.x"
displayName: "Install Node.js"
- script: |
npm ci
npx ts-node cleanup.ts
displayName: "Run Branch Cleanup Script"
env:
AZURE_DEVOPS_URL: $(AZURE_DEVOPS_URL)
AZURE_DEVOPS_PAT: $(AZURE_DEVOPS_PAT)
AZURE_DEVOPS_PROJECT: $(AZURE_DEVOPS_PROJECT)
```
- **Notes**:
- Replace `cleanup.ts` with the path to your script.
- Ensure the pipeline has access to the variable group.
---
## Conclusion
Automating branch cleanup ensures your repositories remain organized, improving developer productivity and reducing potential errors. By following this guide, you can set up a script to automatically identify and delete old, unused branches in Azure DevOps, and schedule it using Azure Pipelines for regular maintenance.
**Benefits**:
- **Efficiency**: Saves time and resources.
- **Consistency**: Maintains a consistent repository state.
- **Scalability**: Easily extends to multiple projects and repositories.
---
## Additional Resources
- **Full Working Source Code**: [drunkcoding public code](https://dev.azure.com/drunk24/drunkcoding-public/_git/az.tools?path=/az-devops-delete-branches&version=GBmain)
- **Azure DevOps Node API Documentation**: [Git API Reference](https://github.com/microsoft/azure-devops-node-api/blob/master/api/GitApi.ts)
- **Azure DevOps REST API Reference**: [Git Repositories](https://docs.microsoft.com/en-us/rest/api/azure/devops/git/repositories)
---
**Note**: Always test scripts in a controlled environment before deploying them in production. Ensure compliance with your organization's policies and procedures.
---
## Thank You
Thank you for taking the time to read this guide! I hope it has been helpful, feel free to explore further, and happy coding! 🌟✨
**Steven** | *[GitHub](https://github.com/baoduy)*
|