Thursday, April 13, 2017

Building a Python Code Review Scheduler: Review Follow-Up

Building a Python Code Review Scheduler: Review Follow-Up

In the third part of this series, you saw how to save the code review request information for follow-up. You created a method called read_email to fetch the emails from the inbox to check if a reviewer has responded to the code review request. You also implemented error handling in the code review scheduler code.

In this part of the series, you'll use the saved code review information and the information from the emails to check if the reviewer has responded to the review request. If a request has not been responded to, you'll send a follow-up email to the reviewer.

Getting Started

Start by cloning the source code from the third part of the tutorial series.

Modify the config.json file to include some relevant email addresses, keeping the royagasthyan@gmail.com email address. It's because git has commits related to this particular email address which are required for the code to execute as expected. Modify the SMTP credentials in the schedule.py file:

Navigate to the project directory CodeReviewer and try to execute the following command in the terminal.

It should send the code review request to random developers for review and create a reviewer.json file with review information.

Implementing a Follow-Up Request

Let's start by creating a follow-up request method called followup_request. Inside the followup_request method, read the reviewer.json file and keep the content in a list. Here is how the code looks:

Next, pull in the email information using the read_email method that you implemented in the last tutorial.

If the reviewer has responded to the review request, there should be an email with the same subject matter and a Re: tag prefixed to it. So iterate through the review information list and compare the review subject with the email subject to see if the reviewer has responded to the request.

As seen in the above code, you iterated through the review_info list and checked the review information subject against the email subject to see if the reviewer has responded.

Now, once the reviewer has responded to the code review request, you don't need to keep the particular review information in the reviewer.json file. So create a Python method called Delete_Info to remove the particular review information from the reviewer.json file. Here is how Delete_Info looks:

As seen in the above code, you have iterated through the review information list and deleted the entry which matches the Id. After removing the information from the file, return the list.

You need to call the Delete_Info method when a particular piece of review information is replied to. When calling the Delete_Info method, you need to pass a copy of the review_info so that the original info list isn't altered. You'll need the original review information list for comparison later. So import the copy Python module to create a copy of the original review information list.

Create a copy of the review_info list.

When deleting the review information that has been responded to from the original list, pass the copy list to the Delete_Info method.

Here is the followup_request method:

Now, once the review_info list has been iterated, you need to check if there are any changes in the reviewer.json file. If any existing review information has been removed, you need to update the reviewer.json file appropriately. So check if review_info_copy and review_info are the same, and update the reviewer.json file. 

Here is the complete followup_request method:

Make a call to the followup_request method to follow up on the review requests that have already been sent.

Save the above changes. To test the follow-up functionality, delete the reviewer.json file from the project directory. Now run the scheduler so that code review requests are sent to random developers. Check if that information has been saved in the reviewer.json file.

Ask the particular developer to respond to the code review request by replying to the email. Now run the scheduler again, and this time the scheduler program should be able to find the response and remove it from the reviewer.json file.

Sending Reminder Emails

Once the reviewer has responded to the code review request emails, that information needs to be removed from the reviewer.json file since you don't need to track it further. If the reviewer has not yet responded to the code review request, you need to send a follow-up mail to remind him or her about the review request.

The code review scheduler would run on a daily basis. When it's run, you first need to check if it's been a certain time since the developer has responded to the review request. In the project configuration, you can set a review period during which, if the reviewer has not responded, the scheduler would send a reminder email.

Let' start by adding a configuration in the project config. Add a new config called followup_frequency in the config file.

So, when the reviewer has not responded for followup_frequency number of days, you'll send a reminder email. Read the configuration into a global variable while reading the configurations:

Inside the followup_request method, send a reminder email when the reviewer has not replied to the follow-up requests for followup_frequency number of days. Calculate the number of days since the review was sent.

If the number of days is greater than the follow-up frequency date in the configurations, send the reminder email.

Here is the complete followup_request method:

Wrapping It Up

In this tutorial, you saw how to implement the logic to follow up on code review requests. You also added the functionality to send a reminder email if the reviewer hasn't responded to the email for a certain number of days. 

This Python code reviewer can be further enhanced to suit your needs. Do fork the repository and add new features, and let us know in the comments below.

Source code from this tutorial is available on GitHub


No comments:

Post a Comment