LabKey as a company was started by a bunch of developers and is still run by these same developers. If you asked then how they want to manage tasks, their first response will be in the bug list. For some of our business related tasks, instead of fighting this, we decided to embrace it. To embrace the bug list, we needed a way to pragmatically create new bugs(Issues) from an existing business system.

A problem we had to overcome was how to programmatically create new Issues. LabKey Server currently does not have a client API for creating or managing Issues, so I had to do it the old fashion way.

Creating new LabKey Server Issues with Python

The first thing we have to do is authenticate to the server (See this blog post for more information on authenticating to LabKey Server).

{% highlight python linenos %} import urllib2 import urllib import cookielib

labkey_servername = ‘test.labkey.com’ labkey_project = ‘sampleproject’ labkey_useranme = ‘emailAddress’ labkey_password = ‘password’

Create the cookie jar

cj = cookielib.LWPCookieJar() cookie_handler = urllib2.HTTPCookieProcessor(cj)

Create the opener

opener = urllib2.build_opener(cookie_handler) return opener

Create the authentication string

authHeader = base64.encodestring(“%s:%s” % (labkey_email, labkey__password))[:-1] authHeader = “Basic %s” % authHeader

Create the URL for submitting the new Issue

labkey_url = ‘https://’ + labkey_servername + ‘/issues/’ + labkey_project.rstrip(‘/’).lstrip(‘/’) + “/insert.view?” {% endhighlight %}

The second step is to create the post data. To create a new Issue we need to post the following information

  • Title
    • field name = title
    • What is it: Title of the issue
  • Assigned To:
    • field name = assignedto
    • What is it: The UID of the user to which the new Issue will be assigned. You can find the UID for a user by looking at the Site Users page.
  • Area:
    • field name = area
    • What is it: The issue area. See the issues list for list of available areas.
  • Type:
    • field name = type
    • What is it: The issue type. See the issues list for list of available types.
  • Priority
    • field name = priority
    • What is it: The issue priority. See the issues list for list of available priorities.
  • Comment
    • field name = comment
    • What is it: Issue description, repro steps, etc
  • Action
    • field name = action
    • value = org.labkey.issue.IssuesController$InsertAction
    • The value is always the same.
  • Issue ID
    • field name = issueId
    • value = 0
    • For new issues, the value is always = 0

An example of creating the post data is below

{% highlight python linenos %} issue_title = ‘Task: Task #1’ post_comment = “A new task has been created. Below you can find the task information: \n\n” + \ “ - Task Name = Task #1 \n” + \ “ - Submitter = Brian \n” + \ “ - Email = my@email.email\n” + \ “ - Company/Account = My Fictious Company \n” + \ “ - Description: “ + issue_description + “\n\n\n” + \ “Created by get_task_push_to_issues.py \n” post_data = [ (‘title’, issue_title), (‘assignedTo’, int(3397356)), (‘type’, ‘Task’), (‘area’, ‘Business’), (‘priority’, int(‘3’)), (‘comment’, post_comment), (‘action’, ‘org.labkey.issue.IssuesController$InsertAction’), (‘issueId’, int(‘0’)) ] {% endhighlight %}

All that is left to do is submit it to the server.

{% highlight python linenos %} sys.stdout.write(‘Create New Issue for Task #1….. ‘) try: resp = opener.open(urllib2.Request(labkey_url, None, {“Authorization”: authHeader }),urllib.urlencode(post_data)) html = resp.read() #print html #print resp.info() #print resp.getcode() # # Check response to see if there was an error during the submission # A HTTP 200 response code does not always indicate a successful creation of the issue. # So we will need to review response and verify success_string = issue_title if html.find(success_string) != -1: sys.stdout.write(“SUCCESS\n”) else: sys.stdout.write(“FAILED \n”) print “\t Issue creation for “ + issue_title + “ failed” print “\t Submit URL = “+ resp.geturl() + “\n” #print html sys.exit(1) except urllib2.HTTPError as e: sys.stdout.write(“FAILED \n”)
print “\tThere was problem while attempting to create the new task to “ + e.geturl() print “\t - The HTTP response code was “ + str(e.getcode()) print “\t - The full error message and HTTP response is below: \n\n” print e.read() print e.info() print “\n\n”
{% endhighlight %}

That is all you need to do to programmatically create new Issues on a LabKey Server.