Published in: 14 Jul

Git

Git Changelog Automation – Powershell

By Damian Rodriguez, in Git

Before a new package is deployed, writing package features, hotfix, enhancements, etc, to create a changelog can become a real headache. For that reason, we try to automate as much as possible all the processes involved in a deploy process.
To make this possible we need conventions. An important convention which all developers should apply it is the commit messaging.

For this script, we are going to use the conventions below for every commit in our project


hotfix-{taskId}-{simple description} -> hotfixes
fb-{taskId}-{simple description} -> features


We still can find commits like: merge- {bSource} – {bDest} or “change package version ..”. These commits are valid, they are not hotfix or feature neither. We can leave them there; we are focusing on hotfixes and features.

Project tagging is another important task and requires the correct tagging for every project version.
Based on this, all commits inside a version can be read.
Keep project versioning is critical to search commits, deploy, and have the control of multiple environments.

Let’s start
Step by step

First, create a new file changelog.ps1 at the root level.

Paste code below

 Set-Location (Split-Path $MyInvocation.MyCommand.Path)
 $branch = $args[0]
 $version = ($args[1]+"..")
 $path = $args[2]
 function WriteResult([string[]]$hotfixes, [string[]]$features, [string[]]$other)
 {
     $tab = "    "
   Add-Content change-log.md "# Hotfixes"
   foreach($c in $hotfixes){
     Add-Content change-log.md ($tab + "- " + $c)
   }
  
   Add-Content change-log.md "# Features"
   foreach($c in $features){
     Add-Content change-log.md ($tab + "- " + $c)
   }
  
   Add-Content change-log.md "# Other changes"
   foreach($c in $other){
     Add-Content change-log.md ($tab + "- " + $c)
   }
 }

 try{ 
     $hotfixes = @()
     $features = @()
     $other = @()

   $commits = (git checkout $branch | git log --boundary --pretty=oneline --pretty=format:%s $version)
    
   foreach($c in $commits){
     $splitArray = $c -Split "-" 
     if($splitArray[0].ToLower() -eq "hotfix"){
       $hotfixes += $splitArray -join "-"
     }elseif($splitArray[0].ToLower() -eq "fb"){
       $features += $splitArray -join "-"
     } else{
       $other += $splitArray -join "-"
     }
   }
  
   New-Item $path"change-log.md" -ItemType File -Force
   WriteResult $hotfixes $features $other
 }
 catch
 {
     Write-Error $_.Exception.ToString()
     Read-Host -Prompt "The above error occurred. Press Enter to exit"
 }

Add a new script command, in this example, it will be named “gen-cl”, execute it immediately after build, take into account that the new deploy version number must be specified EQUAL as the tag version description added in previous steps (ex v1.0.18)

Now simply run build command

npm run build-dev

And that is it, a change-log.md file should be generated

Thanks for reading, I really hope this script can help you and save some time before each deploy.