DEV Community

Cover image for OpenCommit: GitHub Action to improve commits with meaningful messages on every `git push` πŸ€―πŸ”«
Dima Sukharev
Dima Sukharev

Posted on

OpenCommit: GitHub Action to improve commits with meaningful messages on every `git push` πŸ€―πŸ”«

Hi Hackers,

About 10 days ago, I came across the #GitHubHack23 post and then I thought: "Hmm... ain't I got something to contribute?" β€” squinting my eyes like this πŸ˜‘

What I Built

Meet OpenCommit as a GitHub Action πŸͺ…

With OpenCommit action set in a repository, every commit pushed is automatically improved with a meaningful message about what was changed and the rationale behind those changes.

You can find the Action in the GitHub marketplace here and follow the instructions to set it up in your repository.

Thanks to @mishmanners for clarifying that building great things on top of existing great things is a great thing πŸ’š

Category Submission:

I'm submitting the Action for the 'Maintainer Must-Haves' category as it helps maintainers follow a rational behind the changes contributed by reading clean and meaningful commit messages.

And I'm also submitting the Action for the 'DIY Deployments' category as a custom CI script that improves open-source collaboration experience.

App Link

You can access the Action page here, and the repository here.

Screenshots

OpenCommit comparison png

Description

With the Action set in a repository, all commits are automatically improved with meaningful, clear, and easy-to-follow messages on every push to any branch.

You may exclude branches like main and dev from the Action via a custom setting.

Here's how to set up.

Link to Source Code

Find the source code for the Action here.

Permissive License

OpenCommit is distributed under the MIT License β€” you can find the license here.

Background

i'am something of a maintainer myself meme

I was inspired to create OpenCommit from my experiences as a maintainer. When there are tens of PRs waiting to be merged β€” it's a true joy to open a PR and follow a trail of meaningful commit messages that tell you what changed and why.

And now if some of the contributions lack clear and concise commit messages β€” you set OpenCommit GitHub Action on top of your repo to solve the problem and auto-improve commit messages on every push to any branch you want.

How I Built It

Wow, it's a long story :)

The journey of creating this GitHub Action started with a known problem in the current GitHub ecosystem: the quality and consistency of commit messages depends on a collaborator. I decided to develop a solution leveraging GitHub Actions and OpenCommit to make commit messages being auto-generated for all the collaborators.

I guess, with more and more powerful LLMs coming β€” we are the last generation of engineers who are manually typing the commit messages (and maybe typing at all lol)

The Stack

The Action is built using TypeScript and Node.js, popular choices for GitHub Actions due to their excellent support for asynchronous operations, a crucial requirement considering the multiple I/O operations involved. In the initial setup, I used the actions/toolkit package which provides useful utilities to streamline the creation of GitHub Actions. I picked @actions/core for basic functionalities such as inputs, outputs, and error handling, @actions/github to interact with GitHub's REST API, and @actions/exec to execute shell commands.

The Algorithm

The core function of the action is improving commit messages. I achieved this by combining GitHub's APIs with the openAI's GPT (3.5 model β€” cheap and powerful). I utilized Octokit, GitHub’s official client library for Node.js, to fetch commit messages from the PR context. These commit messages are then passed as prompts to the openAI API, which then generates an enhanced version of each commit message.

Workflow and Usage

The action is designed to run on push events, specifically when a new commit is pushed to a PR. This triggers the action, which then fetches the commit messages, improves them as per Conventional Commits concept, and finally replaces the original ones.

You may also turn on GitMoji convention if you prefer your messages baked with emojis 🀠

Testing and Refactoring

The initial prototype had some shortcomings; for instance, it did not handle errors and exceptions well, making it less robust. Therefore, a significant amount of time was spent on refactoring the codebase. The action was thoroughly tested across a variety of scenarios to ensure its reliability and robustness.

Challenges and Learnings

Throughout the journey, there were numerous challenges. However, they presented learning opportunities. One significant challenge was ensuring the correct handling of Git commands in different environments. I learned a great deal about GitHub Actions internal workings, and how to manage and manipulate commit histories. Another challenge was working with the GPT API and optimize the calls to make the tool run cheap.

Future Improvements

Building on an already powerful package that offers an array of features was a pivotal part of this GitHub Action. The base package supports more than ten languages, incorporates GitMoji, and offers robust algorithms to manage any-size commit diffs. This, combined with smart prompts for GPT to generate the best commit message results, provides a solid foundation for future enhancements.

Looking ahead, I plan to introduce more customization options, allowing users to specify the level of verbosity and the style of their commit messages with prefixes and postfixes.

Additionally, I aim to enhance language support further, catering to global non-English speaking users, thus broadening the action's reach.

I'm also adding GitHub Codespaces configs to the repo to make open-source collaboration experience easier β€” you would just click "Run in a Codespace" on the README and instantly create a PR from your browser πŸ§™

In conclusion, the development of this GitHub Action has been a highly rewarding process, full of valuable learnings about GitHub's ecosystem, CI/CD practices, Git operations, and the power of AI in automating mundane tasks.

Additional Resources/Info

Here are some helpful resources that guided me in this project:

A big thank you to @mishmanners, @github, and dev.to for such a fun 10 days πŸ˜…

Feel free to give OpenCommit a try in your projects, and any feedback is welcomed!

I need to get some sleep now

GitHubHack23 πŸͺ©

Top comments (31)

Collapse
 
cocakolya profile image
Nikolay Nelidov

cool idea! I've been trying to get my team to write better commits, but it didn't really work πŸ™‚

Collapse
 
kkm000 profile image
Cy "kkm" K'Nelson

Obligatory xkcd.com/1296/

Collapse
 
disukharev profile image
Dima Sukharev

lol true, you start with good and finish with ui

Collapse
 
disukharev profile image
Dima Sukharev

hihi, thanks, that happens, lol :) if you try and like (or don't) OpenCommit as an Action for the whole team repo β€” please leave me a feedback in the issues

Collapse
 
kentezrabetita profile image
Kent Ezra A. Betita

Nice work, Dima! I can see myself using this in the future. Would it be possible to also generate a PR description as well? Like when someone creates a pull request an action will be triggered that will generate a description based on the changes.

Collapse
 
breinich profile image
Vencel Bajnok

+1 That would be great for PRs also.

Collapse
 
disukharev profile image
Dima Sukharev

gonna do the PRs, also thought about it :) fully automated git pipelines

next opencommit is doing is writing code for you and committing it after AI-TDD tests pass

Collapse
 
rudolfolah profile image
Rudolf Olah

This is very cool, this part in particular where you're generating the commit message based on the diff and how the prompt for that is constructed with one example, it would be interesting to see the output if the prompt's example is based on the programming languages used in the diff.

Collapse
 
disukharev profile image
Dima Sukharev

thank you bro! i also like the code, try it out as a Github Action :)

Collapse
 
__febe88 profile image
Anna D • Edited

Hey, this is a pretty clever use of GPT. I can see how this could really help in maintaining a clean and coherent commit history. As someone who sometimes gets a bit lazy with commit messages, I might give it a shot. Thanks for your work on this!

Collapse
 
disukharev profile image
Dima Sukharev

that's great, I was so bored with lame messages like fixed and `done β€” so i created this tool i use everyday

Collapse
 
kkm000 profile image
Cy "kkm" K'Nelson • Edited

I guess, with more and more powerful LLMs coming β€” we are the last generation of engineers who are manually typing the commit messages (and maybe typing at all lol)

And thus deprive future LLMs of training data. Or, rather, force future LLMs data pipelines to learn throwing away previous LLM-generated data from the training set, letting through only the human-generated commit messages. N-n-nice! This is how the new technology's tail strikes you hard at the forehead when you think you're running up front. Cool! That's something to philosophise about!

Collapse
 
disukharev profile image
Dima Sukharev

hahah :)

Collapse
 
dmitry_i_57e465c3faaac74f profile image
Dmitry i

so, we do not need to create commit messages manually anymore at all.. πŸ₯‚ great idea

Collapse
 
disukharev profile image
Dima Sukharev

That was my idea of like getting rid of manually coming up with a clever commit message yourself, i guess future developers will remember our generation as the last one who created commit messages manually :)

Collapse
 
jquinten profile image
Joran Quinten

Any tedious, lenient process that can be automated, should be automated. Love this idea!

Collapse
 
goodevilgenius profile image
Dan Jones

So, you commit with whatever message you want, and the Action rewrites your commits? Am I understanding that right?

If so, you then have to reset your local repo every time so you have the new commit messages and don't get merge conflicts.

That sounds incredibly complicated. Why not build it as a prepare-commit-message hook so it's done locally?

Collapse
 
disukharev profile image
Dima Sukharev

it doesn't create any conflicts, you can check the demo :)

and well it's open source, so i would be extremely happy if you contribute your ideas, cause prepare-commit-message is a smart thing to implement as an option

Collapse
 
goodevilgenius profile image
Dan Jones

But you had to do a git pull --rebase which, it would seem, wiped out the original commit, probably because it's now empty. If you'd done just git pull, without the rebase, you'd have a conflict.

All this rewriting of history after it's been pushed to a public remote repo seems pretty messy.

Thread Thread
 
disukharev profile image
Dima Sukharev • Edited

no bro, if you do pull --no-rebase it's same, no conflicts are there because opencommit doesn't change any code, only message. SHA becomes different, but git doesnt create any conflicts you would need to solve, because code is same.. all good :)

Collapse
 
darthwalsh profile image
Carl Walsh

Do your docs have any before and after graphic of how the Action changes the branch commits?

My first reaction was the same, that this would create a problem with my local branch being 1 commit ahead and behind the remote, but I don't think I understand what the Action is actually doing.

Collapse
 
tomasci profile image
Evgeny

I hope I will try it someday) cool work!

Btw, Can you make this action as a GitHub hook for Husky for example? Or as a standalone app maybe. Is it possible to run it locally? Because we don't use GitHub and someone maybe doesn't use GitLab, etc. So it will be great to have some local solution

Collapse
 
disukharev profile image
Dima Sukharev

sure, you can set it up as a cli npm i -g opencommit and then just run oco in any repo :)

Collapse
 
scycer profile image
Daniel Hoek

Ive Been doing this for a few weeks now but with gitlab merges instead. Doings a diff of the source and destination branch and getting GPT to list changes in each file, why it's done and a summary overall. Also asked for emoji's to make it more fun. Using GPT4, it's spitting out really great summaries that definitely improve the review process. Nice idea with opencommit!

Collapse
 
disukharev profile image
Dima Sukharev

thank you man :)

Collapse
 
mrdgh2821 profile image
Mihir Rabade

I'm waiting for git staging version of OpenCommit nowπŸ˜‚

Collapse
 
mrdgh2821 profile image
Mihir Rabade

Wait my bad, I didn't look deeply into this, but there's CLI version πŸ€”

Time to use that!

Collapse
 
mishmanners profile image
Michelle Mannering

Always happy to clarify things πŸ˜‰

Collapse
 
edrtz profile image
ed

Do I need a paid chagpt account?

Collapse
 
disukharev profile image
Dima Sukharev

you use you own openAI api, they give you $15 free credits which is ~2 months opencommit usage. then you bind a card and pay for your own requests

Collapse
 
enigmagam profile image
Enigma Gaming

Kudos!