skip to content
ainoya.dev

JIRA and GitHub Daily Workflow Script

/ 3 min read

When managing tickets in JIRA and managing code in GitHub, the development workflow often involves the following steps:

  1. Create a Git branch named after the JIRA ticket number.
  2. Retrieve the ticket title from JIRA using its ticket number.
  3. Make an empty commit in Git with the ticket number and title.
  4. Push the branch to GitHub.
  5. Create a draft Pull Request (PR) on GitHub and fill the PR template with the JIRA ticket link.

To simplify this process, I crafted a script that accomplishes these tasks in one go. The usage is straightforward:

./jw PRJ-1234

The script leverages go-jira/jira and the GitHub CLI. It’s important to have these CLI tools set up beforehand.

Here’s an overview of the script:

#!/usr/bin/env ruby
# Fetch information from JIRA, create a branch,
# push it and create a draft PR

require 'json'
require 'tempfile'

# Retrieve the first argument
key=ARGV[0]
# If key is missing, output error
if key == nil then
  puts "Please input a key"
  exit
end
puts "Fetching information for #{key}"

info=JSON.parse(`jira view #{key} -t debug`)

parent_key=info.dig("fields", "parent", "key")
issue_summary=info["fields"]["summary"]

comment="#{key} #{issue_summary}"

# If parent_key exists, fetch information of the parent
if parent_key != nil then
  parent_info=JSON.parse(`jira view #{parent_key} -t debug`)
  parent_summary=parent_info["fields"]["summary"]
  puts "#{parent_summary}"
  comment="#{key} #{parent_summary} / #{issue_summary}"
end

puts "Starting work on #{comment}"

puts `git checkout master && git pull origin master`
# Exit if there's an error
if $? != 0 then
  puts "Failed to git pull"
  exit
end

puts `git checkout -b #{key}`

puts `git commit --allow-empty -m "#{comment}"`

# Prompt for yes/no input
def prompt_for_yes_no
  loop do
    print "Do you want to push? (yes/y/no): "

    case $stdin.gets.chomp.downcase
    when "yes"
      return true
    when "y"
      return true
    else
      return false
    end
  end
end

push_command = "git push origin #{key}"

# Open .github/PULL_REQUEST_TEMPLATE.md and embed the key in the corresponding ticket line
#
# - {{JIRA_TICKET_LINK}}
#
template_path = ".github/PULL_REQUEST_TEMPLATE.md"
template = File.read(template_path)
template.gsub!(/{{JIRA_TICKET_LINK}}/, "#{key}")
# Create a temporary file using Tempfile and write to it
pr_body = Tempfile.open do |f|
  f.write(template)
  f
end.path

# Create a DRAFT PR using the gh command
gh_command = "gh pr create --title '#{comment}' --body-file #{pr_body}  --draft"

if prompt_for_yes_no then
  puts `#{push_command}`
  # Create a DRAFT PR with the comment title using the GH command
  puts `#{gh_command}`
else
  puts "Did not push. If you want to push and create a pr, please execute the following commands"
  puts push_command
  puts gh_command
end

Key Takeaways

This time, I developed the tool using ChatGPT and GitHub Copilot. In the past, writing small tools often meant programming while searching through Google. However, using ChatGPT and GitHub Copilot, the development process has evolved into something akin to sculpting from a block of Large Language Models (LLMs), which are like a compressed representation of the internet, as mentioned in The New Yorker. Through prompts, I carve out tools that meet my specific needs, a process that feels like sculpting. This approach, much like a sculptor’s craft, is an immensely enjoyable experience.