Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically add JIRA ticket number to commit messages #3285

Conversation

navDhammu
Copy link
Collaborator

@navDhammu navDhammu commented Jun 28, 2024

Description

This is a little feature I worked on for myself to automate the process of adding a JIRA ticket number to commit messages. I was getting a bit tired of having to remember to add it everytime I make a commit, so I experimented and came up with this solution. I just wanted to share it here in case others may benefit from it as well and potentially merge it in the repository.

What it does
Automatically adds the JIRA ticket number to the commit message on each commit.

How it works
Git natively provides several hooks that allow you to do something on certain actions. Like the pre-commit hook allows you to run some code before committing, there are several others documented here.

For this case I used the prepare-commit-msg hook that allows you to modify commit messages before a commit. Since LiteFarm is using husky, I set up the hook under .husky as mentioned in their docs. I used node.js because that's what I'm most familiar with but it can be done in other ways like using bash scripting.

What the script does is checks for the presence of a Jira ticket number in the branch name, and if it exists, reads the commit message and adds it to the front.

Tests
I added tests for automatically testing a few different scenarios and edge cases. Initially I set it up without mocking - by creating a test branch, making test commits on that branch, then checking for the presence of ticket number in the commit message. But I found that it was slow (500ms - 1s) and prone to inconsistencies, so I ended up refactoring to use mocks.

Note:

  • The tests use built-in node test runner so nothing is required to be installed, but node.js version 18 or higher is needed. For best results, use version 20, as it has the stable release for the test runner.
  • To run the tests, use command node --test /pathToTestFile

How to test manually:

  1. Checkout this branch locally and run npm install or npm run prepare
  2. Change the branch name so that a fake jira ticket number is appended to it. Like LF-XXXX branch-name.
  3. Make a test commit without adding a ticket number to it.
  4. Hopefully you should have the JIra ticket number automatically added to the message.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Passes test case
  • UI components visually reviewed on desktop view
  • UI components visually reviewed on mobile view
  • Other (please explain)

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • The precommit and linting ran successfully
  • I have added or updated language tags for text that's part of the UI
  • I have added "MISSING" for all new language tags to languages I don't speak
  • I have added the GNU General Public License to all new files

@navDhammu navDhammu force-pushed the automatically-append-jira-ticket-number-to-commit-message branch 2 times, most recently from 4aec15b to db6ed3f Compare July 6, 2024 03:36
@navDhammu navDhammu marked this pull request as ready for review July 6, 2024 15:35
@navDhammu navDhammu requested review from a team as code owners July 6, 2024 15:35
@navDhammu navDhammu requested review from Duncan-Brain and kathyavini and removed request for a team July 6, 2024 15:35
@navDhammu navDhammu marked this pull request as draft July 8, 2024 03:02
@navDhammu navDhammu marked this pull request as ready for review July 10, 2024 23:45
@navDhammu navDhammu added the enhancement New feature or request label Jul 10, 2024
@navDhammu navDhammu changed the title add prepare-commit-msg git hook Automatically add JIRA ticket number to commit messages Jul 10, 2024
@Duncan-Brain
Copy link
Collaborator

Duncan-Brain commented Jul 30, 2024

Hey @navDhammu ! That is pretty neat!

It works for me if I use the message flag on git commit -m "message". But not when I make a longer commit message with editor using plain old git commit.

Is that expected?

@navDhammu
Copy link
Collaborator Author

@Duncan-Brain Thank you for having a look at this

Do you mean when making a commit inside an editor like vim? I don't usually use that so didn't take that into consideration. But I think what may be happening is that it's matching the ticket number inside the comments that are autogenerated in the editor.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch LF-3443-test-brach
# Changes to be committed:

@Duncan-Brain
Copy link
Collaborator

@navDhammu

Yes plain old git commit that may default to vim. Our previous tech lead Ivan mentioned we should be preferring this method. I still use -m short message flag for most commits. But longer commit messages I will use the editor.

It is handy that the branch number is previewed in the vim commit comments. But I still need to manually enter it for it to be added to my commit message. I think what I was expecting is that a message using base commandgit commit also pre-populates the ticket number.

@navDhammu
Copy link
Collaborator Author

navDhammu commented Jul 31, 2024

@Duncan-Brain I made some changes to work with the editor. Please let me know if it's working for you now.

  • The prepare-commit-msg was running before the editor is opened so I changed it to use commit-msg hook which runs after.
  • Added some filtering to filter out lines starting with '#' so the regex doesn't match the ticket number inside the comments

Copy link
Collaborator

@kathyavini kathyavini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is great! I learned some stuff reading this (didn't know there was a Node test runner!) and the test cases cover all the variants I can think of.

Copy link
Collaborator

@Duncan-Brain Duncan-Brain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it must be matching something else I am getting the already exists error on the message.

git commit > i(insert) > type in message above comments>ctrl^c>:w(write)>:q (exit vim)

Screenshot 2024-08-08 at 4 41 30 PM

I also haven't tried setting my default editor to anything else like nano or somethig.

Flag still works:

@navDhammu
Copy link
Collaborator Author

@Duncan-Brain Thats weird, its working for me when I do the exact same steps you described... did you forget to pull latest changes?

Copy link
Collaborator

@Duncan-Brain Duncan-Brain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@navDhammu Your right I must not have had recent changes -- I remember both rebasing, because it was rebased and deleting because I suck at rebasing others branches.

But it works now!

@Duncan-Brain Duncan-Brain added this pull request to the merge queue Aug 9, 2024
Merged via the queue into integration with commit c6a59d3 Aug 9, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
4 participants