Skip to content

Commit c889ce5

Browse files
code-atomSiteleaf
authored and
Siteleaf
committed
Added Enforce Branch Naming Convention Using Husky Dot Net
1 parent a6410f5 commit c889ce5

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: Enforce Branch Naming Convention Using Husky.NET
3+
date: 2024-06-02 00:07:00 +05:30
4+
categories:
5+
- asp.net core
6+
tags:
7+
- automation
8+
layout: post
9+
---
10+
11+
## **Add Pre-Commit Hooks**
12+
13+
To set up Husky.NET with a dotnet project, please follow the instructions in this [link](https://codetalk.in/posts/2024/06/01/integrate-githooks-using-husky-dot-net). Next, you'll need to establish a branch naming convention using Husky.NET. To do this, create a pre-commit hook script in the `.husky` directory that was generated by Husky.NET.
14+
15+
```bash
16+
dotnet husky add pre-commit "branch naming convention"
17+
```
18+
19+
## **Prepare Branch Naming Convention Script**
20+
21+
Now, we will create a PowerShell script to enforce branch naming conventions. In this instance, we will utilize PowerShell to guarantee adherence to branch naming conventions. A PowerShell script for validation needs to be created and can be stored anywhere in your repository. It is recommended to save this script under the .husky folder.
22+
23+
```powershell
24+
$branchName = git rev-parse --abbrev-ref HEAD
25+
$pattern = '^(topic)\/((\d+)(\.\d+)?(\.\d+))\/(\d+)\-(\w.*)$|Release_\d.*'
26+
if ($branchName -notmatch $pattern) {
27+
Write-Output "Error: The branch name does not conform to the required naming convention. Please use the format 'topic/<version>/<azure-task-id>-<description>' or 'Release_<version>'." -ErrorAction Stop
28+
Exit -1;
29+
}
30+
```
31+
32+
## **Setting Up Branch Naming Validation in Husky.NET Task Runner**
33+
34+
Let's proceed by adding the PowerShell script to the Husky.NET taskrunner. To begin, we should navigate to the .husky folder within our repository. Inside this folder, locate the **task-runner.json** file. Within this file, we will configure our PowerShell script as a task that can be executed in pre-commit Git hooks.
35+
36+
```json
37+
{
38+
"tasks": [
39+
{
40+
"name": "Branch Naming Convention",
41+
"command": "pwsh",
42+
"args": [".husky/pre-commit-branch-naming-convention.ps1" ]
43+
},
44+
]
45+
}
46+
```
47+
48+
## **Configure Pre-Commit Hooks**
49+
50+
To configure the pre-commit hook, access the file in the .husky folder. Then, include a command to trigger the branch naming convention task execution.
51+
52+
```bash
53+
#!/bin/sh
54+
. "$(dirname "$0")/_/husky.sh"
55+
dotnet husky run --name "Branch Naming Convention"
56+
/// other tasks.
57+
```
58+
59+
Now, every time you try to commit, Husky.NET will check if your branch name adheres to the naming convention. If it doesn’t, the commit will be aborted, and you’ll see an error message.

0 commit comments

Comments
 (0)