Building a Python code review scheduler: review follow-up

WBOY
Release: 2023-08-30 20:09:18
Original
1108 people have browsed it

Building a Python code review scheduler: review follow-up

In part three of this series, you learned how to save code review request information for later processing. You create a method called read_email to get an email from the inbox to check whether the 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 will use your saved code review information and information from your email to check whether the reviewer has responded to the review request. If the request has not been responded to, you will send a follow-up email to the reviewer.

start using

First clone the source code of the third part of this tutorial series.

git clone https://github.com/royagasthyan/CodeReviewer-Part3 CodeReviewer
Copy after login

Modify the config.json file to include some relevant email addresses and keep the royagasthyan@gmail.com email address. This is because git has a commit associated with this specific email address, which is required for the code to execute as expected. Modify the SMTP credentials in the schedule.py file:

FROM_EMAIL      = "your_email_address@gmail.com"
FROM_PWD        = "your_password"
Copy after login

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

python scheduler.py -n 20 -p "project_x"
Copy after login

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

Implement subsequent requests

We first create a followup request method named followup_request. Within the followup_request method, read the reviewer.json file and save the contents in a list. code show as below:

with open('reviewer.json','r') as jfile:
    review_info = json.load(jfile)
Copy after login

Next, extract the email information using the read_email method you implemented in the previous tutorial.

email_info = read_email(no_days)
Copy after login

If the reviewer has responded to the review request, there should be an email with the same subject and the Re: tag in front of it. So, iterate through the list of review messages and compare the review subject to the email subject to see if the reviewer has responded to the request.

for review in review_info:
    review_replied = false
    expected_subject = 'RE: ' + review['subject']
    for email in email_info:
        if expected_subject == email['subject']:
            review_replied = True
            print 'Reviewer has responded'
            break;
Copy after login

As shown in the code above, you iterate over the review_info list and check the review information topic against the email subject to see if the reviewer has replied.

Now, once a reviewer responds to a code review request, you don't need to retain specific review information in the reviewer.json file. Therefore, create a Python method named Delete_Info to delete specific review information from the reviewer.json file. Here's what Delete_Info looks like:

def Delete_Info(info, id):
    for i in xrange(len(info)):
        if info[i]['id'] == id:
            info.pop(i)
            break
    return info
Copy after login

As shown in the code above, you have iterated through the list of review information and deleted entries matching the Id. After removing the information from the file, return to the list.

When replying to a comment message, you need to call the Delete_Info method. When calling the Delete_Info method, you need to pass a copy of review_info so as not to alter the original list of information. You will need the original review information list for later comparison. So import the copy Python module to create a copy of the original list of comment messages.

from copy import copy
Copy after login

Create a copy of the review_info list.

review_info_copy = copy(review_info)
Copy after login

When deleting the replied comment information from the original list, pass the copied list to the Delete_Info method.

review_info_copy = Delete_Info(review_info_copy,review['id'])
Copy after login

This is followup_request Method:

def followup_request():
    with open('reviewer.json','r') as jfile:
        review_info = json.load(jfile)
    review_info_copy = copy(review_info)

    email_info = read_email(no_days)

    for review in review_info:
        review_replied = False
        expected_subject = 'Re: ' + review['subject']
        for email in email_info:
            if expected_subject == email['Subject']:
                review_replied = True
                review_info_copy = Delete_Info(review_info_copy,review['id'])
                break;
Copy after login

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

if review_info_copy != review_info:
    with open('reviewer.json','w') as outfile:
        json.dump(review_info_copy,outfile)
Copy after login

This is the complete followup_request Method:

def followup_request():
    with open('reviewer.json','r') as jfile:
        review_info = json.load(jfile)
    review_info_copy = copy(review_info)

    email_info = read_email(no_days)

    for review in review_info:
        review_replied = False
        expected_subject = 'Re: ' + review['subject']
        for email in email_info:
            if expected_subject == email['Subject']:
                review_replied = True
                review_info_copy = Delete_Info(review_info_copy,review['id'])
                break;

    if review_info_copy != review_info:
        with open('reviewer.json','w') as outfile:
            json.dump(review_info_copy,outfile)
Copy after login

Call the followup_request method to follow up the audit request that has been sent.

try:
    commits = process_commits()

    # Added the follow Up Method
    followup_request()

    if len(commits) == 0:
        print 'No commits found '
    else:
        schedule_review_request(commits)

except Exception,e:
    print 'Error occurred. Check log for details.'
    logger.error(str(datetime.datetime.now()) + " - Error occurred : " + str(e) + "\n")
    logger.exception(str(e))
Copy after login

Save the above changes. In order to test subsequent functionality, please delete the reviewer.json file from the project directory. Now run the scheduler to send code review requests to random developers. Check that this information has been saved in the reviewer.json file.

Require specific developers to respond to code review requests by replying to an email. Now run the scheduler again and this time the scheduler should be able to find the response and remove it from the reviewer.json file.

发送提醒电子邮件

审核者回复代码审核请求电子邮件后,需要从 reviewer.json 文件中删除该信息,因为您不需要进一步跟踪它。如果审核者尚未回复代码审核请求,您需要发送后续邮件提醒他或她审核请求。

代码审查调度程序将每天运行。当它运行时,您首先需要检查开发人员响应审核请求是否已经过去了一定时间。在项目配置中,您可以设置一个审核周期,在此期间,如果审核者没有回复,调度程序将发送提醒电子邮件。

让我们首先在项目配置中添加配置。在配置文件中添加一个名为 followup_Frequency 的新配置。

{
    "name": "project_x",
    "git_url": "https://github.com/royagasthyan/project_x",
    "followup_frequency":2,
    "members": [
        "royagasthyan@gmail.com",
    	"samon@gmail.com",
    	"sualonni@gmail.com",
    	"restuni@gmail.com"
    ]
}
Copy after login

因此,当审阅者在 followup_Frequency 天数内没有回复时,您将发送一封提醒电子邮件。读取配置的同时将配置读入全局变量:

for p in main_config:
    if p['name'] == project:
        project_url = p['git_url']
        project_members = p['members']
        followup_frequency = p['followup_frequency']
    break
Copy after login

followup_request 方法内部,当审稿人在 followup_frequest 天数内没有回复后续请求时,发送提醒邮件。计算自评论发送以来的天数。

review_date = datetime.datetime.strptime(review['sendDate'],'%Y-%m-%d')
today = datetime.datetime.today()
days_since_review = (today - review_date).days
Copy after login

如果天数大于配置中的后续频率日期,请发送提醒电子邮件。

if not review_replied:
    if days_since_review > followup_frequency:
        send_email(review['reviewer'],'Reminder: ' + review['subject'],'\nYou have not responded to the review request\n')
Copy after login

这是完整的 followup_request 方法:

def followup_request():
    with open('reviewer.json','r') as jfile:
        review_info = json.load(jfile)
    review_info_copy = copy(review_info)

    email_info = read_email(no_days)

    for review in review_info:
        review_date = datetime.datetime.strptime(review['sendDate'],'%Y-%m-%d')
        today = datetime.datetime.today()
        days_since_review = (today - review_date).days
        review_replied = False
        expected_subject = 'Re: ' + review['subject']
        for email in email_info:
            if expected_subject == email['Subject']:
                review_replied = True
                review_info_copy = Delete_Info(review_info_copy,review['id'])
                break;

        if not review_replied:
            if days_since_review > followup_frequency:
                send_email(review['reviewer'],'Reminder: ' + review['subject'],'\nYou have not responded to the review request\n')

    if review_info_copy != review_info:
        with open('reviewer.json','w') as outfile:
            json.dump(review_info_copy,outfile)
Copy after login

总结

在本教程中,您了解了如何实现跟进代码审核请求的逻辑。您还添加了如果审阅者在一定天数内没有回复电子邮件的情况下发送提醒电子邮件的功能。

这个 Python 代码审查器可以进一步增强以满足您的需求。请分叉存储库并添加新功能,并在下面的评论中告诉我们。

本教程的源代码可在 GitHub 上获取。

The above is the detailed content of Building a Python code review scheduler: review follow-up. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template