DEV Community

Jennifer Konikowski
Jennifer Konikowski

Posted on

7 5

Use the JIRA API to Post Tickets to JIRA

Originally posted on jmkoni.com

A while ago, I built this super basic Sinatra app to post tickets to JIRA. Here’s the use case: you have non-technical people who are part of your company/team that need to be able to add bugs to JIRA. However, they aren’t putting the right information into the ticket. Here comes this super basic app. To get it running, you just need to update .env with your JIRA username, password, and project key. However, I would recommend changing it to use OAuth. Right now, the form is very simple and, if you decide to use this, I would highly recommend you update it to ask for whatever information you want. Just don’t forget to update the JSON in sinatra_jira.rb! This application is completely open source - feel free to copy any of it for any reason, whole or partial. Let’s dig in a bit and do a quick overview of how Sinatra works.

To start off, the Gemfile is minimal. The biggest thing is that I’m using dotenv, a super useful gem that helps manage environment variables using .env files. Other than that, rubocop, sinatra, and we are using thin for the server.

source 'https://rubygems.org'
gem 'dotenv'
gem 'rubocop'
gem 'sinatra'
gem 'thin'
view raw Gemfile hosted with ❤ by GitHub

The main file (sinatra-jira.rb) contains the routes and the actions. It’s basically a combination of a controller and routes file all in one. The initial get just displays the form and all the work happens in post. Even that is fairly simple though… we just take the field contents and put them in the form that the JIRA API wants.

require 'sinatra'
require 'net/http'
require 'uri'
require 'json'
get '/' do
erb :form
end
# TODO: select box for issue type
post '/form' do
uri = URI.parse('https://company_name.jira.com/rest/api/2/issue/')
replicate = params['can_replicate'] == '0' ? 'no' : 'yes'
json = {
'fields' => {
'project' => { 'key' => ENV['PROJECT_KEY'] },
'summary' => params['title'],
'description' => "Description: #{params['description']}\n\n"\
"Source: #{params['source']}\n\n"\
"Able to replicate? #{replicate}\n\n"\
"Steps: #{params['steps']}\n\n"\
"Submitter Name: #{params['name']}",
'issuetype' => { 'name' => 'Bug' }
}
}
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri,
'Content-Type' => 'application/json')
request.body = json.to_json
request.basic_auth(ENV['JIRA_USERNAME'], ENV['JIRA_PASSWORD'])
response = http.request(request)
parsed_request = JSON.parse(response.body)
erb :response, locals: { key: parsed_request['key'] }
end
view raw sinatra_jira.rb hosted with ❤ by GitHub

The form is pretty simple too and really ugly. I would definitely recommend adding some styling and don’t be like me… internal users deserve nice looking apps too! Since the problem I was facing was that I wasn’t getting the right information, I made sure to put examples in the form to increase the chance that I would get the information that I need.

<h2>Submit a bug to JIRA!</h2>
<form action="/form" method="post">
<p>Name: <br>
<small>Name of bug submitter</small>
<br>
<input type="text" name="name">
</p>
<p>Descriptive Title: <br>
<small>Example: Got error page when trying to view a page</small>
<br>
<input type="text" name="title">
</p>
<p>Description:<br>​
<small>Example: I was trying to access the my profile when I got a 404</small><br>
<textarea id="description" rows="10" cols="70" name="description"></textarea></p>
<p>Source:
<br>
<small>Examples: You (please put your name), report from customer, etc.</small>
<br>
<input type="text" name="source">
</p>
<p>Do you see what they see?<br>
<small>Applicable if you did not discover the bug. Are you able to replicate it?</small><br>
<input type="checkbox" name="can_replicate">
</p>
<p>What Steps Did You Take To Find/Replicate This Issue: <br>
<small>Example: 1) Log in. 2) Click on Profile button. 3) Get a 404.</small>
<br>
<textarea id="steps" rows="10" cols="70" name="steps"></textarea></p>
<input type="submit">
</form>
view raw form.erb hosted with ❤ by GitHub

This is a SUPER basic response. Don’t miss that we are passing key to the response. That is the issue key which, depending on how much your end users use JIRA, might be useful to include.

<h2>Thank you for helping us be better!</h2>
<!-- add more text that includes anything you want as follow up from the user -->
view raw response.erb hosted with ❤ by GitHub

Hope this was somewhat useful in some way. I’d love to see feedback too!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
tyronemapp profile image
Tyrone Mapp

Cool idea. Too right internal users deserve nice apps too :)

I'm just starting down the Jira app dev route, digging up my old dev skills. Never done API work before though. I assume you wrote this in RoR? - one of my nagging questions with the Jira API App work is what can i / do i develop the app using - does it work ok with PHP, or is something else better etc.. :)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay