← Back to the Build Log Products

One button to commit and update the Jira ticket

D
Daniel · Aug 2, 2023 · 3 min read
A flat illustration of a single glowing teal button with two paths branching out, one to a git commit node and one to a ticket card, on a deep navy background

Here is a small annoyance that used to eat a few minutes of every workday. I would finish a change, commit it, then go do the bookkeeping: switch to the browser, open the Jira ticket, paste in what I had just done, and add the list of files I touched so the next person had a clue. Every commit, the same little ritual, by hand.

So I wrapped the whole thing onto one button in TerminalNexus. I click it, it asks me for the ticket and the message, and it handles the rest. It commits with the ticket number in the message, then drops a comment on the Jira issue with that message and the list of changed files.

The two prompts use TerminalNexus's input capture, so the button stops and asks before anything runs. The ticket key going into the commit message is the part that matters most. That is what ties the commit back to the issue, so it shows up against the ticket later instead of floating off on its own.

Here is the button. It is a Bash script, set up for Git Bash on Windows, which is where I run it:

TICKET_KEY="[user_input`Enter Jira ticket``]"
if [ -z "$TICKET_KEY" ]; then
  echo "No ticket. Stopping."
  exit 1
fi

COMMIT_MSG="[user_input`Enter the commit message``]"
if [ -z "$COMMIT_MSG" ]; then
  echo "No message. Stopping."
  exit 1
fi

git add . && git commit -m "${TICKET_KEY} ${COMMIT_MSG}" && {
  CHANGED=$(git status --porcelain | awk '{if ($1 != "??") print $1 " " $2}')
  CHANGED=$(echo "$CHANGED" | 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="your-domain.atlassian.net"
  API_TOKEN="ATLASSIAN API TOKEN GOES HERE"
  USER_EMAIL="your-atlassian-email"

  BODY="{ \"body\": \"${COMMIT_MSG}\n\nChanged files:\n\n${CHANGED}\" }"
  curl -s -u "${USER_EMAIL}:${API_TOKEN}" \
    -X POST -H "Content-Type: application/json" \
    --data "${BODY}" \
    "https://${JIRA_DOMAIN}/rest/api/2/issue/${TICKET_KEY}/comment"
}

A few things worth pointing at.

The [user_input...] bits are TerminalNexus placeholders. When the button runs, each one pops a prompt and drops your answer into the script in its place. The _sleep_4000 is another one, an internal four second pause, just to give the previous step a beat before the API call goes out.

The git status line lists what changed and skips untracked files, so the comment shows the files that were actually part of the commit. The sed dance right after it is turning real newlines into the \n that JSON wants, because a raw newline in that body gets you a bad request and no hint as to why. I lost a little time to that one.

Use an Atlassian API token, not your account password. You generate one in your Atlassian account settings and treat it like a password, which means it stays out of any script you share or commit. That is why it is a placeholder above.

That is the whole thing. It is not clever, it just moves a dull two minute job onto one click, and over a few hundred commits that stops being nothing. The same shape works for anything with an API behind it: capture what you need, run the real command, then fire the follow-up call. Jira happens to be mine.

Thanks for reading. If you have a button like this that quietly saves you from yourself, send me a line. I collect them.

Comments