Skip to content

Commit

Permalink
Fixed Overflow Message Error (#157)
Browse files Browse the repository at this point in the history
* added logging statements

* fixed logging bug

* fixing pylint issues

* updated packages and pylint requirements

* reformatted

* updated depedencies

* adding verify flag to avoid timeouts, and fixing global wxt api var

* reverting verify and changing wxt api version

* fixed overflow bug

---------

Co-authored-by: Paul <>
  • Loading branch information
KJStick authored Mar 3, 2023
1 parent f0afbff commit f7a9152
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ format: $(VENV_BIN)/activate ## Format code
$(VENV_BIN)/yapf --style=yapf.ini -i -r *.py $(PYDIRS)

pylint: $(VENV_BIN)/activate ## Run pylint
$(VENV_BIN)/pylint --output-format=parseable --rcfile .pylintrc *.py $(PYDIRS)
$(VENV_BIN)/pylint --output-format=parseable --rcfile pylint/.pylintrc *.py $(PYDIRS)

check: check-format pylint ## Check code format & lint

Expand Down
27 changes: 14 additions & 13 deletions git_cidrbot/gitauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def __init__(self):
self.table = ''
self.Api = WebexTeamsAPI()
self.private_key = ''
self.timeout_value = 60

def get_git_key(self):
session = boto3.session.Session()
Expand Down Expand Up @@ -132,11 +133,11 @@ def webhook_request(self, event):
},
"body":
json.dumps({
f"User authenticated app without using webex auth flow":
"User authenticated app without using webex auth flow":
"Please uninstall app (Uninstall cidrbot) then follow this process: " +
f" 1) Invite the bot to a secure webex teams room " + f" 2) Type @CIDRbot add repo " +
f" 3) Complete the auth process by clicking the link the bot messages you " +
f" 4) You will receive a message in both the room and direct messages that the bot authed successfully. REDIRECTING IN 15 SECONDS"
" 1) Invite the bot to a secure webex teams room " + " 2) Type @CIDRbot add repo " +
" 3) Complete the auth process by clicking the link the bot messages you " +
" 4) You will receive a message in both the room and direct messages that the bot authed successfully. REDIRECTING IN 15 SECONDS"
})
}

Expand Down Expand Up @@ -166,7 +167,7 @@ def webhook_request(self, event):

expire_date = int(time.time()) + 3600

repo_info = self.git_repo_info(token, f'https://api.github.com/installation/repositories')
repo_info = self.git_repo_info(token, 'https://api.github.com/installation/repositories')
user_info = self.git_user_info(
encoded_key, f'https://api.github.com/app/installations/{install_id}'
)
Expand All @@ -193,7 +194,7 @@ def webhook_request(self, event):

text_direct = f"Authentication successful, the following repos are added to room: {room_name} \n" + repo_list
text = (
f"Authentication successful, type @CIDRbot help to begin. To add repos, please" +
"Authentication successful, type @CIDRbot help to begin. To add repos, please" +
''' visit <a href="https://github.com/settings/installations/">Github applications</a> and click "configure" for the cidrbot app '''
)

Expand Down Expand Up @@ -221,7 +222,7 @@ def webhook_request(self, event):
},
"body":
json.dumps({
f"Expired link was used":
"Expired link was used":
"Please uninstall cidrbot app and follow the process " +
"described by the page you will be redirected to. REDIRECTING IN 15 SECONDS"
})
Expand Down Expand Up @@ -251,10 +252,10 @@ def git_repo_info(self, token, URL):
return None

def git_user_info(self, encoded_key, URL):
headers = {"Authorization": "Bearer {}".format(encoded_key), 'Accept': 'application/vnd.github.v3+json'}
headers = {"Authorization": f"Bearer {encoded_key}", 'Accept': 'application/vnd.github.v3+json'}
post_data = {}

response = requests.get(URL, json=post_data, headers=headers)
response = requests.get(URL, json=post_data, headers=headers, timeout=self.timeout_value)
if response.status_code == 200:
self.logging.debug("User info granted")
resp = str(response.text)
Expand All @@ -265,10 +266,10 @@ def git_user_info(self, encoded_key, URL):
return None

def send_webex_message(self, post_data):
URL = f'https://webexapis.com/v1/messages'
URL = 'https://webexapis.com/v1/messages'
headers = {'Authorization': 'Bearer ' + self.wxt_access_token, 'Content-type': 'application/json;charset=utf-8'}

requests.post(URL, json=post_data, headers=headers)
requests.post(URL, json=post_data, headers=headers, timeout=self.timeout_value)

def add_installation(self, user_id, install_id, person_id, user_name, room_id, token, repo_path_list, expire_date):
self.dynamodb = boto3.resource('dynamodb')
Expand Down Expand Up @@ -335,10 +336,10 @@ def check_state(self, state):

def create_token(self, installation_id, encoded_key):
URL = f'https://api.github.com/app/installations/{installation_id}/access_tokens'
headers = {"Authorization": "Bearer {}".format(encoded_key), 'Accept': 'application/vnd.github.v3+json'}
headers = {"Authorization": f"Bearer {encoded_key}", 'Accept': 'application/vnd.github.v3+json'}
post_data = {}

response = requests.post(URL, json=post_data, headers=headers)
response = requests.post(URL, json=post_data, headers=headers, timeout=self.timeout_value)
if response.status_code == 201:
self.logging.debug("Access key granted")
resp = str(response.text)
Expand Down
114 changes: 114 additions & 0 deletions pylint/.pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# pylint: skip-file .pylintrc
# pylint: disable=E

# The format of this file isn't really documented; just use --generate-rcfile

[messages control]
# abstract-class-little-used: abstract classes are often useful as interface descriptions
# abstract-class-not-used : No, you just missed it.
# broad-except : seriously, sometimes you need to catch all exceptions
# cyclic-import : detection of this doesn't work well
# duplicate-code : detection of this doesn't work well, mostly catches imports anyway
# fixme : why are you throwing warnings about todos
# incomplete-protocol : this check is just broken.
# interface-not-implemented : No, you just missed it.
# invalid-name : too difficult to configure sanely
# locally-disabled : stupid
# locally-enabled : stupid
# maybe-no-member : if type inference fails, don't warn.
# missing-docstring : docstrings on everything?
# no-self-use : if it were supposed to have been a method, it would be
# star-args : seriously, does no one write wrapper functions
# useless-else-on-loop : detection is broken, doesn't notice "return" statements
#
# Temporarily disabled ones:
# line-too-long
#
# Django generated code:
# bad-continuation
# bad-whitespace
# trailing-whitespace
# old-style-class
# no-init

disable=
abstract-class-little-used,
abstract-class-not-used,
broad-except,
cyclic-import,
duplicate-code,
fixme,
incomplete-protocol,
interface-not-implemented,
invalid-name,
locally-disabled,
locally-enabled,
maybe-no-member,
missing-docstring,
missing-module-docstring,
no-self-use,
star-args,
useless-else-on-loop,
useless-object-inheritance,
line-too-long,
invalid-name,
bad-continuation,
bad-whitespace,
trailing-whitespace,
old-style-class,
no-init,
len-as-condition,
superfluous-parens,
no-else-raise,

[basic]
bad-functions=apply,input

module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$
const-rgx=(([A-Z_][A-Za-z0-9_]*)|(__.*__))$
class-rgx=[A-Za-z_][a-zA-Z0-9_]+$
function-rgx=[a-zA-Z_][a-zA-Z0-9_]{2,45}$
method-rgx=[a-z_][a-zA-Z0-9_]{1,80}$
attr-rgx=[A-Za-z_][A-Za-z0-9_]{1,30}$
argument-rgx=[a-z_][a-z0-9_]{0,30}$
variable-rgx=[a-z_][a-z0-9_]{0,30}$
class-attribute-rgx=([A-Za-z_][A-Za-z0-9_]{1,50}|(__.*__))$
inlinevar-rgx=[A-Za-z_][A-Za-z0-9_]*$

good-names=i,j,k,ex,Run,_,urlpatterns,application,Input,_Input,HOSTS
bad-names=foo,bar,baz,toto,tutu,tata

[classes]
defining-attr-methods=__init__,__new__,setUp,initialize

[design]
# too-many-* and too-few-* warnings are pretty useless, so make them all
# arbitrarily high
max-args=1000
max-locals=1000
max-returns=1000
max-branches=1000
max-statements=1000
max-parents=1000
max-attributes=1000
max-public-methods=1000
min-public-methods=0

[format]
max-line-length=120
max-module-lines=100000

[imports]
deprecated-modules=regsub,TERMIOS,Bastion,rexec

[reports]
output-format=text
reports=no

[typecheck]
ignored-classes=sha1, md5, Popen, Request, SplitResult, execute, API
generated-members=REQUEST,acl_users,aq_parent,^depends$

[variables]
additional-builtins=_
dummy-variables-rgx=_|dummy
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
webexteamssdk==1.6
webexteamssdk==1.6.1
pygithub==1.55
boto3==1.17.6
requests==2.26.0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_namespace_packages

with open("README.md", "r") as fh:
with open("README.md", "r", encoding='UTF-8') as fh:
long_description = fh.read()

includes = [
Expand Down
4 changes: 2 additions & 2 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
coverage==4.0.3
flake8==3.4.1
isort==4.2.15
isort==4.3.21
mock==1.0.1
pylint==2.4.4
pylint==2.4.0
pytest==2.8.7
pytest-cov==2.3.1
tox==2.6.0
Expand Down
8 changes: 5 additions & 3 deletions wxt_cidrbot/cidrbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def weekly_reminder_email(self):
message += text + '\n'
else:
message = (
f"**Weekly reminder to review your issues**, " +
"**Weekly reminder to review your issues**, " +
f" -To disable these messages, type: **disable reminders** \n \n Room: {room_name} \n"
)
message += text + '\n'
Expand Down Expand Up @@ -220,8 +220,9 @@ def check_message_overflow(self, message, room_id, message_id, pt_id, request_ty
message_first_part += issue_title

for repo in split_message:
self.logging.debug("REPO: %s", str(repo))
self.logging.debug("Repo: %s", str(repo))
self.logging.debug("Length of repo %s", len(repo))

if len(message_first_part) + len(repo) < self.WEBEX_MESSAGE_CHAR_LIMIT:
message_first_part += split_keyword
message_first_part += repo
Expand All @@ -238,12 +239,13 @@ def check_message_overflow(self, message, room_id, message_id, pt_id, request_ty
#check see if entire repo fit in one message so don't waste memory and time splitting and repiecing
if is_not_able_to_append and len(repo) < self.WEBEX_MESSAGE_CHAR_LIMIT:
msgs_to_be_sent.append(message_first_part)
message_first_part += split_keyword + repo
message_first_part = split_keyword + repo
continue
if is_not_able_to_append:
msgs_to_be_sent.append(message_first_part)
message_first_part = ""

# means is able to append
message_first_part += split_keyword + repo_name
for repo_part in split_repo:
if len(message_first_part) + len(repo_part) < self.WEBEX_MESSAGE_CHAR_LIMIT:
Expand Down
14 changes: 7 additions & 7 deletions wxt_cidrbot/cidrbot_room_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ def invited(self, json_string):
'person_id': person_id
})

text = f"Hello, thank you for adding cidrbot to your room, one moment while I set things up: \n - Setting up webhooks..."
text = "Hello, thank you for adding cidrbot to your room, one moment while I set things up: \n - Setting up webhooks..."
post_message = {'roomId': room_id, 'markdown': text}

message_id = self.post_message(post_message)

text += f"\n - Webhook setup complete"
text += "\n - Webhook setup complete"
self.webex.edit_message(message_id, text, room_id)

id_list = self.webex_webhook_setup(room_id)
self.dynamo.create_room(room_id, member_info, id_list)

text += f"\n - Room setup complete \n\n To begin type **@CIDRbot manage repos** \n For a list of commands type **@CIDRbot help**"
text += "\n - Room setup complete \n\n To begin type **@CIDRbot manage repos** \n For a list of commands type **@CIDRbot help**"
self.webex.edit_message(message_id, text, room_id)

def webex_webhook_setup(self, room_id):
Expand Down Expand Up @@ -139,10 +139,10 @@ def webex_webhook_setup(self, room_id):
return id_list

def post_webhook(self, post_data):
URL = f'https://webexapis.com/v1/webhooks'
URL = 'https://webexapis.com/v1/webhooks'
headers = {'Authorization': 'Bearer ' + self.wxt_access_token, 'Content-type': 'application/json;charset=utf-8'}

response = requests.post(URL, json=post_data, headers=headers)
response = requests.post(URL, json=post_data, headers=headers, timeout=60)
if response.status_code == 200:
self.logging.debug("Webhook created successfully")
self.logging.debug(str(response.text))
Expand All @@ -154,10 +154,10 @@ def post_webhook(self, post_data):
return "no id"

def post_message(self, post_data):
URL = f'https://webexapis.com/v1/messages'
URL = 'https://webexapis.com/v1/messages'
headers = {'Authorization': 'Bearer ' + self.wxt_access_token, 'Content-type': 'application/json;charset=utf-8'}

response = requests.post(URL, json=post_data, headers=headers)
response = requests.post(URL, json=post_data, headers=headers, timeout=60)
if response.status_code == 200:
self.logging.debug("Message created successfully")
self.logging.debug(str(response.text))
Expand Down
Loading

0 comments on commit f7a9152

Please sign in to comment.