Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added comment reviewer function code files #84

Merged
merged 4 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/comment_reviewer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: deploy-comment-reviewer
on:
push:
branches:
- master
paths:
- 'comment_reviewer/**'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Deploy code to Lambda
uses: qxf2/py-lambda-action@v1.0.2
with:
lambda_directory: 'comment_reviewer'
lambda_function_name: 'arn:aws:lambda:ap-south-1:285993504765:function:comment-reviewer'
lambda_layer_arn: 'arn:aws:lambda:ap-south-1:285993504765:layer:comment-reviewer:1'
requirements_txt: 'comment_reviewer/requirements.txt'
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ${{ secrets.SKYPE_SENDER_REGION }}
133 changes: 133 additions & 0 deletions comment_reviewer/comment_reviewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
"""
This Lambda will :
- Give a list of comment reviewers
"""
import json
import os
import boto3
QUEUE_URL = 'https://sqs.ap-south-1.amazonaws.com/285993504765/skype-sender'
at_Qxf2Bot = '<at id="8:live:.cid.92bd244e945d8335">qxf2bot</at>!'
at_Qxf2Bot_english = '@qxf2bot!'
COMMANDS = [f'comment reviewers, please {at_Qxf2Bot}',
f'i need comment reviewers {at_Qxf2Bot}',
f'comment reviewers please {at_Qxf2Bot}',
f'comment reviewers, please {at_Qxf2Bot_english}',
f'i need comment reviewers {at_Qxf2Bot_english}',
f'comment reviewers please {at_Qxf2Bot_english}']
FIRST_SSM = 'first_comment_reviewer_index'
SECOND_SSM = 'second_comment_reviewer_index'
RESET_COMMANDS = [f'reset primary code reviewer {at_Qxf2Bot}',
f'reset secondary code reviewer {at_Qxf2Bot}']


def read_parameter(client, parameter_name, decryption_flag = False):
"Read a SSM parameter"
parameter = client.get_parameter(Name=parameter_name, WithDecryption = decryption_flag)

return parameter['Parameter']['Value']

def write_parameter(client, parameter_name, value, decryption_flag = False):
"Write to a SSM parameter"
response = client.put_parameter(
Name = parameter_name,
Value = value,
Overwrite = True
)

return True if response['ResponseMetadata']['HTTPStatusCode'] == 200 else False

def get_reviewer_index(reviewer_type):
"Return the index of the reviewer"
client = boto3.client("ssm")
reviewer_index = read_parameter(client, reviewer_type)

return int(reviewer_index)

def update_reviewer_index(reviewer_type,increment=1):
"Increment the reviewer index by 1"
client = boto3.client("ssm")
reviewer_index = read_parameter(client, reviewer_type)
write_parameter(client,
reviewer_type,
str(int(reviewer_index) + increment))

def get_comment_reviewers(reviewer_type):
"Return a list of primary code reviewers"
return os.environ.get(reviewer_type,"").split(',')

def get_message_contents(event):
"Retrieve the message contents from the SQS event"
record = event.get('Records')[0]
message = record.get('body')
message = json.loads(message)['Message']
message = json.loads(message)

return message

def write_message(message, channel):
"Send a message to Skype Sender"
sqs = boto3.client('sqs')
print(channel)
message = str({'msg':f'{message}', 'channel':channel})
print(message)
sqs.send_message(QueueUrl=QUEUE_URL, MessageBody=(message))

def in_correct_channel(channel):
"Is the message in the right channel?"
return channel == os.environ.get('channel','')

def is_code_reviewer_command(message):
"Is this a code reviewer command?"
return message.lower() in COMMANDS

def is_reviewer_reset_command(message):
"Is this is a reset command?"
return message.lower() in RESET_COMMANDS

def get_reply():
"Get the primary and secondary code reviewers"
first_comment_reviewers = get_comment_reviewers('first_comment_reviewers')
second_comment_reviewers = get_comment_reviewers('second_comment_reviewers')
primary_index = get_reviewer_index(FIRST_SSM)
secondary_index = get_reviewer_index(SECOND_SSM)
first_comment_reviewer = first_comment_reviewers[primary_index%len(first_comment_reviewers)]
second_comment_reviewer = second_comment_reviewers[secondary_index%len(second_comment_reviewers)]
reply = f'First comment reviewer: {first_comment_reviewer}\n\nSecond comment reviewer: {second_comment_reviewer}\n'

return reply

def update_reviewer_indexes():
"Increment the reviewer indexes by 1"
update_reviewer_index(FIRST_SSM)
update_reviewer_index(SECOND_SSM)

def lambda_handler(event, context):
"Code reviewer lambda"
message_contents = get_message_contents(event)
message = message_contents['msg'].strip()
channel = message_contents['chat_id']
user = message_contents['user_id']

if in_correct_channel(channel):
if is_code_reviewer_command(message):
reply = get_reply()
write_message(reply, os.environ.get('channel',''))
update_reviewer_indexes()
if is_reviewer_reset_command(message):
prev = -1
curr = -1
if 'primary' in message.lower():
prev = get_reviewer_index(FIRST_SSM)
update_reviewer_index(FIRST_SSM, -1)
curr = get_reviewer_index(FIRST_SSM)
if 'secondary' in message.lower():
prev = get_reviewer_index(SECOND_SSM)
update_reviewer_index(SECOND_SSM, -1)
curr = get_reviewer_index(SECOND_SSM)
message = f'Reset index from {prev} to {curr}'
write_message(message, os.environ.get('channel',''))

return {
'statusCode': 200,
'body': json.dumps('Done!')
}
1 change: 1 addition & 0 deletions comment_reviewer/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
boto3==1.17.90