Building a Python Code Review Scheduler: Keeping the Review Information

In the second part of this series, you saw how to collect the commit information from the git logs and send review requests to random developers selected from the project members list.

In this part, you'll see how to save the code review information to follow up each time the code scheduler is run. You'll also see how to read emails to check if the reviewer has responded to the review request.

Getting Started

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

Modify the config.json file to include some relevant email addresses, keeping the [email protected] email address. It's because the git has commits related to the particular email address which is 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.

Keeping the Review Request Information

To follow up on the review request information, you need to keep it somewhere for reference. You can select where you want to keep the code review request information. It can be any database or may be a file. For the sake of this tutorial, we'll keep the review request information inside a reviewer.json file. Each time the scheduler is run, it'll check the info file to follow up on the review requests that haven't been responded to.

Create a method called save_review_info which will save the review request information inside a file. Inside the save_review_info method, create an info object with the reviewer, subject, and a unique Id.

For a unique Id, import the uuid Python module.

You also need the datetime Python module to get the current date. Import the datetime Python module.

You need to initialize the reviewer.json file when the program starts if it doesn't already exist.

If the file doesn't exist, you need to create a file called reviewer.json and fill it with an empty JSON array as seen in the above code.

This method will be called each time a review request is sent. So, inside the save_review_info method, open the reviewer.json file in read mode and read the contents. Append the new content information into the existing content and write it back to the reviewer.json file. Here is how the code would look:

Inside the schedule_review_request method, before sending the code review request mail, call the save_review_info method to save the review information.

Save the above changes and execute the scheduler program. Once the scheduler has been run, you should be able to view the reviewer.json file inside the project directory with the code review request information. Here is how it would look:

Reading the Email Data

You have collected all the code review request information and saved it in the reviewer.json file. Now, each time the scheduler is run, you need to check your mail inbox to see if the reviewer has responded to the code review request. So first you need to define a method to read your Gmail inbox.

Create a method called read_email which takes the number of days to check the inbox as a parameter. You'll make use of the imaplib Python module to read the email inbox. Import the imaplib Python module:

To read the email using the imaplib module, you first need to create the server.

Log in to the server using the email address and password:

Once logged in, select the inbox to read the emails:

You'll be reading the emails for the past n number of days since the code review request was sent. Import the timedelta Python module. 

Create the email date as shown:

Using the formatted_date, search the email server for emails.

It will return the unique IDs for each email, and using the unique IDs you can get the email details.

Now you'll make use of the first_email_id and the last_email_id to iterate through the emails and fetch the subject and the "from" address of the emails.

data will contain the email content, so iterate the data part and check for a tuple. You'll be making use of the email Python module to extract the details. So import the email Python module. 

You can extract the email subject and the "from" address as shown:

Here is the complete read_email method:

Save the above changes and try running the above read_email method:

It should print the email subject and "from" address on the terminal. 

Email Reading From Gmail

Now let's collect the "from" address and subject into an email_info list and return the data. 

Instead of printing the subject and the "from" address, append the data to the email_info list and return the email_info list.

Here is the modified read_email method:

Adding Logging for Error Handling

Error handling is an important aspect of software development. It's really useful during the debugging phase to trace bugs. If you have no error handling, then it gets really difficult to track the error. Since you're growing with a couple of new methods, I think it's the right time to add error handling to the scheduler code.

To get started with error handling, you'll be needing the logging Python module and the RotatingFileHandler class. Import them as shown:

Once you have the required imports, initialize the logger as shown:

In the above code, you initialized the logger and set the log level to INFO. 

Create a rotating file log handler which will create a new file each time the log file has reached a maximum size.

Attach the logHandler to the logger object.

Let's add the error logger to log errors when an exception is caught. In the read_email method's exception part, add the following code:

The first line logs the error message with the current date and time to the log file. The second line logs the stack trace to the error. 

Similarly, you can add the error handling to the main part of the code. Here is how the code with error handling would look:

Wrapping It Up

In this part of the series, you shelved the review request information in the reviewer.json file. You also created a method to read the emails. You'll be using both of these functions to follow up on the code review requests in the final part of this series.

Additionally, don’t hesitate to see what we have available for sale and for study in the marketplace, and don't hesitate to ask any questions and provide your valuable feedback using the feed below.

Source code from this tutorial is available on GitHub.

Do let us know your thoughts and suggestions in the comments below.

Tags:

Comments

Related Articles