iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
✌️

Skyrocket Development Motivation by Visualizing Daily Pull Request Counts

に公開
2024/03/04

What I created

I made it so that when I run git push --set-upstream origin branchname, it displays the number and a list of Pull Requests I created today.

% git push --set-upstream origin branchname
You have created 3 pull request(s) today.

Showing 3 of 3 pull requests in xxx/xxx that match your search

ID  TITLE BRANCH           CREATED AT      
#1  PR1   feature/branch1  about 3 days ago
#2  PR2   feature/branch2  about 3 days ago
#3  PR3   fix/branch3      about 3 days ago

Motivation

As a personal goal, or rather as a way to quantify my daily actions, I measure the number of Pull Requests created that day.
Simply put, the purpose of setting this metric is to stay mindful of keeping my hands moving.

Sometimes I stop working when I start overthinking things, or I hesitate to take action on a new challenge because I'm worried about what others might think.

As an engineer, I believe that how much code you write can be measured as an "action" (though it's just one metric).
This comes from the hypothesis that it's good to be conscious of outputting something in the form of a Pull Request—even if it faces criticism or ends up being closed without merging.

Originally, I was doing this manually on weekends for measurement, but even if the numbers were lackluster for a particular week, it didn't lead to any improvement.
After some trial and error, I thought that visualizing how many I've made today at the very moment I create a Pull Request might boost motivation more than measuring at the end of the week, so I looked for a way to do that.

How it works

In the end, the mechanism triggers when running git push --set-upstream origin branchname rather than exactly when the Pull Request is created, but I figured it's almost the same thing, so I went with it.

Broadly speaking, the mechanism is as follows:

  • Use a git hook to run a script when pushing
    • Registered pre-push globally because I wanted it to run for all repositories
  • Use the gh command to fetch the PR count

That's the general idea.

Implementation

Setting up git hooks globally

Normally, hooks are configured for specific repositories, but since I wanted all repositories to detect pushes, here is the procedure.

Create ~/.config/git/hooks/pre-push (the directory can be anywhere). It’s fine to leave it empty for now.

$ touch ~/.config/git/hooks/pre-push
$ git config --global core.hooksPath ~/.config/git/hooks
$ chmod +x ~/.config/git/hooks/pre-push

This doesn't support repository-specific hooks, so if you need that, it was mentioned here:
https://qiita.com/ik-fib/items/55edad2e5f5f06b3ddd1#gitリポジトリ内のgit-hooksも実行するようにする

Writing the script

The script for the push event. The flow of what it does is:

  • Get the branch name and remote name
  • Check if the specific branch exists on the remote
  • Get the total count of all pull requests I created today (including merged, closed, etc.)
  • Output the number of pull requests using echo

By the way, since it uses the gh command, please install it. On Mac, you can install it with brew install gh and log in with gh auth login.

https://github.com/cli/cli

~/.config/git/hooks/pre-push
#!/bin/sh

# Get current branch name
current_branch=$(git branch --show-current)

# Specify remote name (using 'origin' here)
remote_name="origin"

# Check if the specific branch exists on the remote
if ! git ls-remote --heads $remote_name $current_branch | grep -q "refs/heads/$current_branch"; then
  # Get today's date in YYYY-MM-DD format
  TODAY=$(date "+%Y-%m-%d")
  # Get GitHub username
  GH_USER=$(gh api user | jq -r '.login')
  
  # Get the count of PRs created by the specified user today
  PR_COUNT=$(gh pr list --state all --search "created:$TODAY author:$GH_USER" --json number --jq '. | length')
  
  # Display the result
  echo "You have created \033[33m$PR_COUNT\033[0m pull request(s) today."
  # Display all PRs including those other than 'open'
  gh pr list --search "created:$TODAY author:$GH_USER" --state all
fi


# Return 0 to indicate script success
exit 0

With this, when I run git push --set-upstream origin branchname, the count and list of pull requests are displayed.

% git push --set-upstream origin branchname
You have created 3 pull request(s) today.

Showing 3 of 3 pull requests in xxx/xxx that match your search

ID  TITLE BRANCH           CREATED AT      
#1  PR1   feature/branch1  about 3 days ago
#2  PR2   feature/branch2  about 3 days ago
#3  PR3   fix/branch3      about 3 days ago

Conclusion

While what I'm doing is simple, I think it's a good design because it maintains development motivation by using a daily action (git push) as a trigger. I plan to improve the display format as I use it.
(For example, outputting a "keep it up" message if there are 0 pull requests, or showing many cracker emojis 🎉 when reaching a certain goal.)

If anyone tries this out, please feel free to leave feedback in the comments!

Discussion