Home Streamline your Git/Bitbucket/Jira Workflow with CommandGit
Post
Cancel

Streamline your Git/Bitbucket/Jira Workflow with CommandGit

Simplify Your Git Workflow with CommandGit’s UI Button

Are you tired of juggling multiple commands to commit your changes, update your Jira ticket, and comment on your Bitbucket commits? CommandGit has the perfect solution for you!

The purpose of this Bash script is to streamline the process of updating Jira tickets with a commit message and the list of changed files. Designed to be utilized with CommandGit’s UI input capture functionality, the script captures a Jira ticket number and commit message provided by the user through CommandGit’s interface. It then proceeds to validate whether the ticket number and commit message have been successfully captured. If either of these inputs is missing, the script cancels the Jira ticket update. Only when the necessary information is successfully captured, a commit is executed, and the Jira ticket is updated with the commit message, along with a list of modified files. Additionally, the script includes the ticket number in the commit message to facilitate seamless ticket grouping in Bitbucket. This convenient automation allows users to efficiently manage Jira ticket updates and commits in one smooth process, enhancing their workflow and productivity.

This is a Bash script ready to be used in your button config.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
TICKET_KEY="[user_input`Enter Jira TICKET``]"
if [ -z "$TICKET_KEY" ]
then
  echo "TICKET_KEY cannot be empty. Exiting."
  exit 1
fi
COMMIT_MSG="[user_input`Enter the commit message``]"
if [ -z "$COMMIT_MSG" ]
then
  echo "COMMIT_MSG cannot be empty. Exiting."
  exit 1
fi
git add . && git commit -m "${TICKET_KEY} ${COMMIT_MSG}" && {
  GIT_STATUS=$(git status --porcelain | awk '{if ($1 != "??") print $1 " " $2}');
  GIT_STATUS=$(echo "$GIT_STATUS" | sed ':a;N;$!ba;s/\n/\\n/g');
  _sleep_4000;
  COMMIT_MSG=$(echo "$COMMIT_MSG" | sed ':a;N;$!ba;s/\n/\\n/g');
  JIRA_DOMAIN="BITBUCKET DOMAIN GOES HERE";
  API_TOKEN="BITBUCKET API TOKEN GOES HERE";
  USER_EMAIL="YOUR BITBUCKET EMAIL GOES HERE";
  COMMENT_BODY="{ \"body\": \"${COMMIT_MSG}\n\nModified files:\n\n${GIT_STATUS}\" }";
  curl -D- \
    -u ${USER_EMAIL}:${API_TOKEN} \
    -X POST \
    -H "Content-Type: application/json" \
    --data "${COMMENT_BODY}" \
    https://${JIRA_DOMAIN}/rest/api/2/issue/${TICKET_KEY}/comment;
} ;

The unit of commands provided above demonstrates the power of CommandGit’s UI button in simplifying your Git workflow. Let’s break down the commands and understand why they were chosen for this Windows Git Bash script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
TICKET_KEY="[user_inputEnter Jira TICKET]"
# This command sets the TICKET_KEY variable to the value entered by the user. 
# It prompts the user to enter a Jira ticket and stores the input in the variable.

if [ -z "$TICKET_KEY" ]
then
  echo "TICKET_KEY cannot be empty. Exiting."
  exit 1
fi
# This is an if statement that checks if the TICKET_KEY variable is empty. 
# If it is, the script will display the message "TICKET_KEY cannot be empty. Exiting." and exit with an error code of 1.

COMMIT_MSG="[user_inputEnter the commit message]"
# This command sets the COMMIT_MSG variable to the value entered by the user. 
# It prompts the user to enter a commit message and stores the input in the variable.

if [ -z "$COMMIT_MSG" ]
then
  echo "COMMIT_MSG cannot be empty. Exiting."
  exit 1
fi
# This is another if statement that checks if the COMMIT_MSG variable is empty. 
# If it is, the script will display the message "COMMIT_MSG cannot be empty. Exiting." and exit with an error code of 1.

git add . && git commit -m "${TICKET_KEY} ${COMMIT_MSG}"
# This command adds all the files in the current directory to the Git staging area 
# and commits them with a commit message that includes the TICKET_KEY and COMMIT_MSG variables.

GIT_STATUS=$(git status --porcelain | awk '{if ($1 != "??") print $1 " " $2}')
# This command retrieves the status of the Git repository and filters out any untracked files. 
# It then stores the modified files and their status in the GIT_STATUS variable.

_sleep_4000
# This is likely a placeholder for a sleep command, which pauses the script execution for 4000 milliseconds (4 seconds) before proceeding.

COMMIT_MSG=$(echo "$COMMIT_MSG" | sed ':a;N;$!ba;s/\n/\\n/g')
# This command replaces any newline characters in the COMMIT_MSG variable with the string "\n". 
# This is done to format the commit message properly when posting it later.

JIRA_DOMAIN="BITBUCKET DOMAIN GOES HERE"
# This sets the JIRA_DOMAIN variable to the URL of the Bitbucket domain where the Jira issue is hosted.

API_TOKEN="BITBUCKET API TOKEN GOES HERE"
# This sets the API_TOKEN variable to the API token required for authentication with the Bitbucket API.

USER_EMAIL="YOUR BITBUCKET EMAIL GOES HERE"
# This sets the USER_EMAIL variable to your Bitbucket email address, which is used for authentication with the Bitbucket API.

COMMENT_BODY="{ \"body\": \"${COMMIT_MSG}\n\nModified files:\n\n${GIT_STATUS}\" }"
# This command constructs the JSON body for the comment to be posted on the Jira issue. 
# It includes the formatted commit message and the list of modified files.

curl -D- -u ${USER_EMAIL}:${API_TOKEN} -X POST -H "Content-Type: application/json" --data "${COMMENT_BODY}" https://${JIRA_DOMAIN}/rest/api/2/issue/${TICKET_KEY}/comment;
# This command uses the curl tool to make an HTTP POST request to the Bitbucket API, 
# adding a comment to the specified Jira issue with the constructed JSON body.

The Alternative: Using Bitbucket Hooks

While CommandGit’s UI button provides a user-friendly approach, some developers prefer using Bitbucket hooks for similar functionality. Bitbucket hooks allow you to automate actions after specific events, such as pushing commits or creating pull requests.

Pros of CommandGit’s UI Button:

User-Friendly: CommandGit’s UI button eliminates the need to remember complex hooks configurations, making it more approachable for all developers.

Easy Integration: CommandGit integrates effortlessly with any CLI, not just Git, providing a unified experience for all your favorite tools.

Cons of Bitbucket Hooks:

Learning Curve: Configuring Bitbucket hooks can be challenging for beginners, requiring knowledge of scripting and server-side configurations.

Limited to Bitbucket: Hooks are specific to Bitbucket, meaning they won’t work with other version control systems like GitHub or GitLab.

Getting Your Bitbucket API Key:

To use the Bitbucket API, you’ll need an API token. Here’s how to generate it:

Log in to Bitbucket. Click on your profile picture in the lower-left corner. Go to “Personal settings” > “App passwords.” Click “Create app password.” Provide a label for your token, and choose the permissions you want to grant. Click “Create.”

In Conclusion

CommandGit’s UI button empowers developers with a simple and efficient way to manage Git commits, Jira tickets, and Bitbucket comments with just one click. Embrace the power of CommandGit and streamline your development workflow today! Don’t waste time on manual tasks; let CommandGit do the heavy lifting for you. Happy coding!

This post is licensed under CC BY 4.0 by the author.