Python script that sends an email to a configured address that Jira is known to be polling, and then checks Jira to see if that email appeared as a comment.

heads

tip
browse log

clone

read-only
https://hg.sr.ht/~redradishtech/testemail2jira
read/write
ssh://hg@hg.sr.ht/~redradishtech/testemail2jira

#Test Email to Jira

Sends an email to a configured address that Jira is known to be polling, and then checks Jira to see if that email appeared as a comment.

#Installation

run:

python3 -m venv venv
. venv/bin/activate
pip3 install -r requirements.txt
./testemail2jira.py

If successful, the script will run but fail for want of a config file. More setup is required:

#OAuth setup

First you will need to establish OAuth trust with Jira before the script will work. To do so:

  1. Generate a RSA key pair:
openssl genrsa -out rsa.pem 1024
openssl rsa -in rsa.pem -pubout -out rsa.pub
  1. In Jira, log in as an administrator (note: it seems that whatever user created the OAuth is used as default reporter for tickets. Not sure how to fix this).

  2. Create a new Application Link with a bogus URL, e.g. http://testemail2jira

  3. On the next screen, fill in bogus but plausible values. The only thing that matters is that the Create incoming link checkbox is ticked.

    Key Value
    Application Name testemail2jira
    Application Type Generic Application
    Service Provider Name test2email2jira
    Consumer Key testemail2jira
    Shared secret testemail2jira
    Request Token URL http://testemail2jira
    Access Token URL http://testemail2jira
    Authorize URL http://testemail2jira
    Create incoming link checked

    Click Continue

  4. On the next screen fill in:

    Key Value
    Consumer Key testemail2jira
    Consumer Name a user-friendly name e.g. Email to Jira Test Script
    Public Key the contents of rsa.pub
  5. We now need to do the OAuth dance.

There are undoubtedly better ways of doing this, but I tend to use the hacky little Python 2 script for this at https://bitbucket.org/atlassian_tutorial/atlassian-oauth-examples

  1. Download the script
  2. customize oauth_setup.py variables
  3. Log out of python 3 venv
  4. Using python 2, run pipenv
pip install --user pipenv
~/.local/bin/pipenv install
~/.local/bin/pipenv run python2 oauth_setup.py

#Create a config file

Now run:

cp testemail2jira.json5.sample testemail2jira.json5
$EDITOR testemail2jira.json5    # Customize with 

Upon successful completion of the OAuth dance, oauth_setup.py will give you an oauth token and token secret. Paste those into testemail2jira.json5, and customize the other variables too.

Tip: the JSON file specifies 'localhost' as the SMTP server, on the assumption that you have a local MTA. I have Postfix configured with with 'relayhost', 'smtp_sasl_password_maps' and 'sender_dependent_relayhost_maps' to relay emails on to google.

At this point, with OAuth trust established and the config file filled in, testemail2jira.py should run successfully:

Started Check that email to JIRA works.
INFO        Connected to https://issues.example.com
INFO        Sending email to ac-680@example.com..
INFO        ..Email sent
INFO        Validating that email was received on AC-680
INFO        count=23.0 Found a comment, but it's not the one we want (containing '2019-01-29 14:32:23.730093')
INFO        count=22.0 Found a comment, but it's not the one we want (containing '2019-01-29 14:32:23.730093')
INFO        count=21.0 Found a comment, but it's not the one we want (containing '2019-01-29 14:32:23.730093')
INFO        count=20.0 Found a comment, but it's not the one we want (containing '2019-01-29 14:32:23.730093')
INFO        count=19.0 Found a comment, but it's not the one we want (containing '2019-01-29 14:32:23.730093')
INFO        count=18.0 Found a comment, but it's not the one we want (containing '2019-01-29 14:32:23.730093')
INFO        count=17.0 Found a comment, but it's not the one we want (containing '2019-01-29 14:32:23.730093')
INFO        count=16.0 Found a comment, but it's not the one we want (containing '2019-01-29 14:32:23.730093')
INFO        count=15.0 Found a comment, but it's not the one we want (containing '2019-01-29 14:32:23.730093')
INFO        count=14.0 Found a comment, but it's not the one we want (containing '2019-01-29 14:32:23.730093')
INFO        Hooray, found marker 2019-01-29 14:32:23.730093 in comment {noformat}
From: jeff@example.com
To: jira@example.com
Subject: [ac-680] Testing email from jeff@example.com to ac-680@example.com
Date: Tue, 29 Jan 2019 14:32:23 +1100
{noformat}

#Systemd service installation

The systemd service is parametrized with Jira instance name. To install the service for instance 'coastec_jira' at /opt/atlassian/coastec_jira:

cd /etc/systemd/system
ln -s /opt/atlassian/coastec_jira/current/emailprocessing/testemail2jira/testemail2jira@.service testemail2jira@coastec_jira.service
systemctl enable testemail2jira@coastec_jira.service
ln -s /opt/atlassian/coastec_jira/current/emailprocessing/testemail2jira/testemail2jira@.timer testemail2jira@coastec_jira.timer
systemctl enable testemail2jira@coastec_jira.timer

Now 'systemctl start testemail2jira@coastec_jira.timer' will kick off a test. The next run can be seen with 'systemctl list-timers'