Automate Redmine with Python

Hi all. In this post, I will describe my process and motivation to automate Redmine with Python. But, what is Redmine? If you are reading this I guest that you already know but for the rest:

Redmine is a flexible project management web application. Written using the Ruby on Rails framework, it is cross-platform and cross-database. Redmine is open source and released under the terms of the GNU General Public License v2 (GPL).

From https://www.redmine.org/

In my current company we are using Redmine for project and time tracking and because of that every day we need to log time there. Also, we also need to create and email a daily report based on Redmine. This usually isn’t a time-consuming task, but for me, this is one place that could be automated. So, I started researching this topic. So, how can I automate Redmine with python?

After a short google search, I discovered something amazing https://python-redmine.com/. This is a Python library with REST API to Redmine. Amazing.

Installation

To install this amazing library you just need to enter one command, if you have pip installed:

pip install python-redmine

Check is library installed correctly by checking with next command:

$ pip list | grep redmine
python-redmine                2.3.0

Let’s start

But how difficult is to start with this library. There is great official documentation at https://python-redmine.com/introduction.html. So, you can check this documentation for your work case, but I will explain my work process.

For this post I’m using python 3.8 and you can check your version with:

$ python --version
Python 3.8.6

Let’s start to automate Redmine with Python. Enter inside the Python shell and figure out what we need and then we can easily convert that to the python script, and then we can create a python program that can start automatically at the end of your workday. The first command that we need is to import Redmine library into our python shell and that is done with:

 from redminelib import Redmine

Next, we need to login to our Redmine with our username and password, and for that we need next line:

redmine = Redmine('https://redmine.your_redmine.com', username='your_username', password='your_password')

We now have the Redmine object, which is our REST API entry point. Let’s now list your projects. This is also simplified by this python library and can be done with next for loop:

>>> for project in redmine.project.all():
...     print (project)

Select your desired project from redmine.project.all() list and write it down to the project object. Double-check your selected project by cheeking its ID and name with the next two commands:

project.id
project.name

Filter issues

Next, we can check issues that are inside this Project. Because the focus for this post is on how to automate daily reports, I wanted to extract only issues that are assigned to me, and the status of that issue is “Opened” or “Accepted”. With next command let’s filter issues with desired status.

listoftasks = project.issues.filter(status__name__in=('Opened', 'Accepted'))

and then let’s filter all issue that are assigned to you with:

listoftasks = listoftasks.filter(assigned_to__name__in=('Username'))

List time entries for selected task

Ok, now we have a list listoftask of all issues that are assigned to us and are Opened or Accepted. Again let’s select the task that we are working on and print time entries already added to that task. Create for loop that will print time entries added for this task with the next command:

>>> for time in selected_task.time_entries:
...     print (time.comments)
...     print (time.spent_on)

Create and save new time entry

Our daily task is to create a new time entry with comments of our work and log spent time on that task. Command to create time entry is:

new_time_entry = redmine.time_entry.new()

and to populate this object we can use next commands:

new_time_entry.issue_id = selected_task.id
new_time_entry.spent_on = date.today()
new_time_entry.comments = "Your Comment for Task"
new_time_entry.hours = "Spent time on that task"

and then we can commit our new_time_entry with command:

new_time_entry.save()

After this we can see our time entry committed to redmine.

Final words

Python-Redmine library gives us a great opportunity to automatize daily reports and time and project tracking on Redmine. Can you imagine upgrading the existing script that also creates a daily report excel file from the template for a date from Redmine and automatically sends the email to the desired address? Furthermore, if you are on Linux, we could add this script to cron job, so our script will automatically half an hour before our deadline for sending the daily report. And we are golden.

I hope that you enjoy in that post, and if you are interested check out my other posts on https://bln364.com/ and check out my YouTube page https://www.youtube.com/channel/UCwxBekoI-WZUPYSh5GZ9RUg. See you in the next one.

Leave a Comment