The Changesets tool is very handy for managing the changelog of your project. But did you know it could be even more powerful when paired with JIRA? Let’s explore a quick method to automatically insert JIRA issue keys into your changesets using a shell script. A touch of automation to make your life easier!
Check out the script:
#!/bin/bash
# Get staged .changeset/*.md files
FILES=$(git diff --cached --name-only --diff-filter=d | grep ".changeset/.*.md$")
if [ "$FILES" == "" ]
then
exit 0
else
for FILE in $FILES
do
# Only proceed if the changeset document starts with ---
if [ "$(head -n 1 "$FILE")" == "---" ]; then
TICKET=$(git rev-parse --abbrev-ref HEAD | grep -Eo '^(\w+/)?(\w+[-_])?[0-9]+' | grep -Eo '(\w+[-])?[0-9]+' | tr "[:lower:]" "[:upper:]")
# Skip if there's no ticket number in the branch name
if [[ $TICKET == "" ]]; then
continue
fi
LINE_NUMBER=$(awk '/---/{i++} i==2{print NR+2; exit}' $FILE)
# Skip if there are no written changes
if [[ $LINE_NUMBER == "" ]]; then
continue
fi
LINE_TEXT=$(awk -v LINE_NUMBER=$LINE_NUMBER 'NR==LINE_NUMBER{print $0; exit}' $FILE)
# Skip if the ticket number has already been added
if [[ $LINE_TEXT == *"]" ]]; then
continue
fi
TEMP=$(mktemp)
awk -v LINE_NUMBER=$LINE_NUMBER -v TICKET=$TICKET 'NR==LINE_NUMBER{$0=$0 " [" TICKET "]"}1' $FILE > $TEMP && mv $TEMP $FILE
echo "$FILE has been augmented with a JIRA ticket number."
git add $FILE
fi
done
fi
Let’s break this down:
First, the script gets all staged markdown files under .changeset
directory.
FILES=$(git diff --cached --name-only --diff-filter=d | grep ".changeset/.*.md$")
If no files are found, the script ends.
if [ "$FILES" == "" ]
then
exit 0
If files are found, it processes each file, checking if it starts with ---
.
else
for FILE in $FILES
do
# Only proceed if the changeset document starts with ---
if [ "$(head -n 1 "$FILE")" == "---" ]; then
It then extracts the JIRA ticket number from the branch name. If no ticket number exists in the branch name, it moves on to the next file.
TICKET=$(git rev-parse --abbrev-ref HEAD | grep -Eo '^(\w+/)?(\w+[-_])?[0-9]+' | grep -Eo '(\w+[-])?[0-9]+' | tr "[:lower:]" "[:upper:]")
# Skip if there's no ticket number in the branch name
if [[ $TICKET == "" ]]; then
continue
fi
The script identifies the line where the changes are mentioned. If there are no changes, it skips to the next file.
LINE_NUMBER=$(awk '/---/{i++} i==2{print NR+2; exit}' $FILE)
LINE_TEXT=$(awk -v LINE_NUMBER=$LINE_NUMBER 'NR==LINE_NUMBER{print $0; exit}' $FILE)
# Skip if there are no written changes
if [[ $LINE_TEXT == "" ]]; then
continue
fi
If changes are present, it checks if the ticket number is already there.
# Skip if the ticket number has already been added
if [[ $LINE_TEXT == *"]" ]]; then
continue
fi
If not, the script appends the JIRA ticket number to the end of the changeset line, creates a copy of the file, replaces the original file with the modified one, adds the file to the staging area, and displays a success message.
TEMP=$(mktemp)
awk -v LINE_NUMBER=$LINE_NUMBER -v TICKET=$TICKET 'NR==LINE_NUMBER{$0=$0 " [" TICKET "]"}1' $FILE > $TEMP && mv $TEMP $FILE
echo "$FILE has been updated with a JIRA ticket number."
git add $FILE
And that’s it! Your changesets are now more functional with their new JIRA companions. Happy coding!