Skip to content

Commit

Permalink
Merge pull request #41 from wp-media/develop
Browse files Browse the repository at this point in the history
Test SonarQube integration
  • Loading branch information
MathieuLamiot authored Oct 19, 2023
2 parents 85f0fe9 + eea51de commit 2f00320
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
17 changes: 17 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
from sources.TechTeamBot import TechTeamBot
from flask import Flask
from logging.config import dictConfig

dictConfig({
'version': 1,
'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
}},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
})

# This `app` represents your existing Flask app
flask_app = Flask(__name__)
Expand Down
14 changes: 9 additions & 5 deletions sources/handlers/GithubTaskHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import json
from pathlib import Path
from flask import current_app
from sources.factories.GithubGQLCallFactory import GithubGQLCallFactory
from sources.factories.SlackMessageFactory import SlackMessageFactory
from sources.models.InitGithubTaskParam import InitGithubTaskParam
Expand Down Expand Up @@ -120,6 +121,9 @@ def process_update(self, app_context, node_id):
and project_item_details["typeField"]["name"]
and project_item_details["typeField"]["name"] == 'dev-team-escalation'):
self.dev_team_escalation_update(app_context, node_id)
else:
app_context.push()
current_app.logger.info("GitHubTaskHandler.process_update: No corresponding flow.")

def dev_team_escalation_update(self, app_context, node_id):
"""
Expand All @@ -137,11 +141,8 @@ def dev_team_escalation_update(self, app_context, node_id):
project_item_assignees = 'No one.'

# Search for Slack thread based on channel, author and itemId part of the GitHub link
query = 'itemId=' + str(project_item_details["databaseId"]) + ' in:dev-team-escalation from:tbtt'
try:
found_slack_messages = self.slack_message_factory.search_message(app_context, query)
except KeyError:
return
query = 'itemId=' + str(project_item_details["databaseId"]) + ' in:dev-team-escalation from:TB-TT'
found_slack_messages = self.slack_message_factory.search_message(app_context, query)
slack_thread = found_slack_messages["messages"]["matches"][0]
# Maybe update the thread parent
old_parent_message_split = slack_thread["text"].splitlines(False)
Expand All @@ -157,3 +158,6 @@ def dev_team_escalation_update(self, app_context, node_id):
thread_response += ' and currently assigned to: ' + project_item_assignees
self.slack_message_factory.post_reply(app_context,
slack_thread["channel"]["id"], slack_thread["ts"], thread_response)
else:
app_context.push()
current_app.logger.info("dev_team_escalation_update: Nex message identical to the current one.")
3 changes: 3 additions & 0 deletions sources/handlers/GithubWebhookHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ def project_v2_item_update_callback(self, payload_json):
"""
# Keep only update actions
if "action" not in payload_json or "edited" != payload_json["action"]:
current_app.logger.info("project_v2_item_update_callback: Not an update action.")
return
# Keep only changes on status or assignees
if (payload_json["changes"]["field_value"]["field_type"] != "assignees" and
payload_json["changes"]["field_value"]["field_node_id"] != self.github_config['statusFieldId']):
current_app.logger.info("project_v2_item_update_callback: Update not related to assignee nor status.")
return

node_id = payload_json["projects_v2_item"]["node_id"]
thread = Thread(target=self.github_project_item_handler.process_update, kwargs={
"app_context": current_app.app_context(), "node_id": node_id})
current_app.logger.info("project_v2_item_update_callback: Starting processing thread...")
thread.start()
3 changes: 2 additions & 1 deletion sources/utils/Security.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import hashlib
import hmac
from flask import current_app


def validate_github_webhook_signature(payload, secret):
Expand All @@ -15,7 +16,7 @@ def validate_github_webhook_signature(payload, secret):
signature_header = payload.headers['X-Hub-Signature-256']
sha_name, github_signature = signature_header.split('=')
if sha_name != 'sha256':
print('ERROR: X-Hub-Signature-256 in payload headers was not sha256=****')
current_app.logger.error('ERROR: X-Hub-Signature-256 in payload headers was not sha256=****')
return False

# Create our own signature
Expand Down
16 changes: 11 additions & 5 deletions tests/unit/GithubTaskHandlerTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def test_process_update_dev_team_escalation_full(mock_post_reply, mock_edit_mess
call_get_project_item = [call('app_context', 'the_node_id')]
mock_get_project_item_for_update.assert_has_calls(call_get_project_item)
mock_get_dev_team_escalation_item_update.assert_has_calls(call_get_project_item)
call_search_message = [call('app_context', 'itemId=123456 in:dev-team-escalation from:tbtt')]
call_search_message = [call('app_context', 'itemId=123456 in:dev-team-escalation from:TB-TT')]
mock_search_message.assert_has_calls(call_search_message)
call_edit_message = [call('app_context', 'C12345678', '1508795665.000236',
'The first line\nStatus: In Progress\nAssignees: MathieuLamiot, theOtherOne, ')]
Expand Down Expand Up @@ -406,18 +406,19 @@ def test_process_update_dev_team_escalation_no_update(mock_post_reply, mock_edit
mock_get_project_item_for_update):
"""
Test process_update with the dev-team-escalation flow
"""
github_task_handler = GithubTaskHandler()
github_task_handler.process_update('app_context', ('the_node_id'))
call_get_project_item = [call('app_context', 'the_node_id')]
mock_get_project_item_for_update.assert_has_calls(call_get_project_item)
mock_get_dev_team_escalation_item_update.assert_has_calls(call_get_project_item)
call_search_message = [call('app_context', 'itemId=123456 in:dev-team-escalation from:tbtt')]
call_search_message = [call('app_context', 'itemId=123456 in:dev-team-escalation from:TB-TT')]
mock_search_message.assert_has_calls(call_search_message)
mock_edit_message.assert_not_called()
mock_post_reply.assert_not_called()
"""


@patch.object(GithubGQLCallFactory, "get_project_item_for_update", return_value={"typeField": {"name": "dev-team-escalation"}})
Expand Down Expand Up @@ -493,7 +494,7 @@ def test_process_update_dev_team_escalation_no_assignees(mock_post_reply, mock_e
call_get_project_item = [call('app_context', 'the_node_id')]
mock_get_project_item_for_update.assert_has_calls(call_get_project_item)
mock_get_dev_team_escalation_item_update.assert_has_calls(call_get_project_item)
call_search_message = [call('app_context', 'itemId=123456 in:dev-team-escalation from:tbtt')]
call_search_message = [call('app_context', 'itemId=123456 in:dev-team-escalation from:TB-TT')]
mock_search_message.assert_has_calls(call_search_message)
call_edit_message = [call('app_context', 'C12345678', '1508795665.000236',
'The first line\nStatus: In Progress\nAssignees: No one.')]
Expand Down Expand Up @@ -528,7 +529,12 @@ def test_process_update_dev_team_escalation_no_messages(mock_post_reply, mock_ed
mock_request.post.side_effect = mock_request_search_message_no_match

github_task_handler = GithubTaskHandler()
github_task_handler.process_update('app_context', ('the_node_id'))
error_caught = False
try:
github_task_handler.process_update('app_context', ('the_node_id'))
except KeyError:
error_caught = True
assert error_caught

call_get_project_item = [call('app_context', 'the_node_id')]
mock_get_project_item_for_update.assert_has_calls(call_get_project_item)
Expand Down

0 comments on commit 2f00320

Please sign in to comment.