Automate It: Be Lazy (Part 1)
Automate It: Be Lazy (Part 1)
Learn how to take steps to automate parts of your JIRA workflow, saving you time when working on projects like Apache Maven.
Join the DZone community and get the full member experience.Join For Free
Based on my passion as an open source developer, in particular for the Apache Maven Project, I work JIRA-based, which helps us and myself to organize our work and, of course, follow the reported issues, feature requests, etc.
Usually, I start my work based on a particular issue in JIRA and go via the following process:
Create an issue for myself (if it already exists, move to step 2).
Create a branch within the appropriate project based on the issue.
Assign the issue to myself.
Migrate the issue into the state of IN PROGRESS.
Do some work on the branch and commit.
Push the branch to remote and let Jenkins check the branch. Repeat with Step 5 until I finish my work.
Rebase against the master, if needed.
Merge the created branch into the master and delete the remote and local branch.
Close the appropriate JIRA issue with a reference to the commit in a comment.
Let us take a look at a real-life example. The exemplary issue is MCLEAN-87, which has been worked on and committed with an appropriate commit message, which should look like the following:
[JIRA-ISSUE] - Summary Text Optional description
The real-life commit looks like this:
commit c6eed44352c9ab623836a9329b7645dbb61413bc (HEAD -> master, origin/master, m/master) Author: Karl Heinz Marbaise <email@example.com> Date: Sat Jul 21 18:00:20 2018 +0200 [MCLEAN-87] - Upgrade maven-plugins parent to version 32
If you take a look at the list of process steps, it is a huge number of steps. Unfortunately, many of the steps are manual steps, which is time-consuming and error-prone.
So, about four or five months ago, I started to automate/simplify some steps of the above process.
Automation Step 1
In step 6, while I was working on a branch, I had to push the current state of the branch over and over again to remote and let Jenkins check if everything was going fine. Furthermore, it could be that I needed to rebase this branch against the master, which has to be followed by a
git push --force BRANCH, but a
--force is very dangerous, which means I had to use
git push --force-with-lease BRANCH instead. I already had a bash completion for git running, which saves a lot of typing, but I would like to make it more comfortable.
I started to write a bash script comprised of the following steps:
Check if we are on a branch, just to be sure.
Get the branch name .
git push origin --force-with-lease BRANCH
This means, in the end, I simply call the script:
The first step of automation is accomplished, which means a little bit of improvement, but not enough.
Automation Step 2
After a time working with that, I thought about step 8, which is a lot of typing and is error-prone. Let me summarize the steps:
Check if we are on a branch.
Get the branch name.
Check out the master.
Merge the branch only if a fast forward is possible — fail otherwise.
Push changes to the remote master.
Delete the remote branch.
Delete the local branch.
The result was my gitmergeandclean.sh script which I use, like the following:
The output looks like this:
~/ws-git-maven/plugins/maven-clean-plugin (MCLEAN-87)$ gitmergeandclean.sh Switched to branch 'master' Your branch is up to date with 'origin/master'. Updating 19b981e..c6eed44 Fast-forward pom.xml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) Total 0 (delta 0), reused 0 (delta 0) remote: Sending notification emails to: ['"firstname.lastname@example.org" <email@example.com>'] remote: To git@github:apache/maven-clean-plugin.git remote: 19b981e..c6eed44 c6eed44352c9ab623836a9329b7645dbb61413bc -> master remote: Syncing refs/heads/master... To https://gitbox.apache.org/repos/asf/maven-clean-plugin.git 19b981e..c6eed44 master -> master remote: Sending notification emails to: ['"firstname.lastname@example.org" <email@example.com>'] remote: To git@github:apache/maven-clean-plugin.git remote: - [deleted] MCLEAN-87 remote: Syncing refs/heads/MCLEAN-87 (FORCED)... To https://gitbox.apache.org/repos/asf/maven-clean-plugin.git - [deleted] MCLEAN-87 Deleted branch MCLEAN-87 (was c6eed44).
The second step of automation is accomplished, which means more improvement, but not enough.
Automation Step 3
If I work on a branch, I often do several commits on it until I think I am done. The result is a branch which contains 5, 6, or more commits which should be squashed into a single commit with a good commit message and a reference to the issue I am working on. This can be done manually and interactive via the following steps:
Count the number of commits you would like to squash.
git rebase -i HEAD~NUMBEROFCOMMIT
Going through the editor and replace
Save and rewrite the commit message.
This was really cumbersome, so I decided to find a scripted way to handle that. After some experiments, I found a solution which results in my gitrebasebranch.sh script:
and in the editor, I only rework the commit message. That's it. After this, I can
The third step of automation is accomplished, which means more improvement, but not enough.
The story will be continued in my next post.
Published at DZone with permission of Karl Heinz Marbaise . See the original article here.
Opinions expressed by DZone contributors are their own.