Skip to content

Commit

Permalink
Adapt code
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienvermeille committed Mar 29, 2024
1 parent 8b4d0a7 commit 1d7ee54
Show file tree
Hide file tree
Showing 25 changed files with 57 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test:
--cov-report html:build/coverage-html \
--cov-report xml:build/coverage.xml \
--cov-report term \
--cov=release/ \
--cov=main/ \
tests/

releasability-check:
Expand Down
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ parametrized = "*"
python_version = "3.10"

[scripts]
releasability_check = "python release/releasability_check.py"
releasability_check = "python main/main.py"
File renamed without changes.
8 changes: 4 additions & 4 deletions release/main.py → main/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import sys

from release.releasability.releasability import Releasability, CouldNotRetrieveReleasabilityCheckResultsException
from release.utils.github import GitHub
from release.utils.github_action_helper import set_output
from main.releasability.releasability_service import ReleasabilityService, CouldNotRetrieveReleasabilityCheckResultsException
from main.utils.github import GitHub
from main.utils.github_action_helper import set_output


def do_releasability_checks():
try:
github = GitHub()
release_request = github.get_release_request()
releasability = Releasability(release_request)
releasability = ReleasabilityService(release_request)
correlation_id = releasability.start_releasability_checks()
report = releasability.get_releasability_report(correlation_id)
print(report)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from release.releasability.releasability_check_result import ReleasabilityCheckResult
from main.releasability.model.releasability_check_result import ReleasabilityCheckResult


class ReleasabilityChecksReport:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import json
from boto3 import Session

from release.releasability.releasability_check_result import ReleasabilityCheckResult
from release.releasability.releasability_checks_report import ReleasabilityChecksReport
from release.releasability.ReleaseRequest import ReleaseRequest
from release.utils.timeout import has_exceeded_timeout
from release.utils.version_helper import VersionHelper
from release.releasability.vars import releasability_aws_region
from main.releasability.model.releasability_check_result import ReleasabilityCheckResult
from main.releasability.model.releasability_checks_report import ReleasabilityChecksReport
from main.releasability.model.ReleaseRequest import ReleaseRequest
from main.utils.timeout import has_exceeded_timeout
from main.utils.version_helper import VersionHelper
from main.releasability.vars import releasability_aws_region


class ReleasabilityException(Exception):
Expand All @@ -21,7 +21,7 @@ class CouldNotRetrieveReleasabilityCheckResultsException(ReleasabilityException)
pass


class Releasability:
class ReleasabilityService:
SQS_MAX_POLLED_MESSAGES_AT_A_TIME = 10
SQS_POLL_WAIT_TIME = 10
FETCH_CHECK_RESULT_TIMEOUT_SECONDS = 60 * 5
Expand All @@ -43,9 +43,9 @@ def _get_aws_account_id(self) -> str:
return boto3.client('sts').get_caller_identity().get('Account')

def _define_arn_constants(self, aws_region: str, aws_account_id: str):
self.TRIGGER_TOPIC_ARN = f"{Releasability.ARN_SNS}:{aws_region}:{aws_account_id}:ReleasabilityTriggerTopic"
self.RESULT_TOPIC_ARN = f"{Releasability.ARN_SNS}:{aws_region}:{aws_account_id}:ReleasabilityResultTopic"
self.RESULT_QUEUE_ARN = f"{Releasability.ARN_SQS}:{aws_region}:{aws_account_id}:ReleasabilityResultQueue"
self.TRIGGER_TOPIC_ARN = f"{ReleasabilityService.ARN_SNS}:{aws_region}:{aws_account_id}:ReleasabilityTriggerTopic"
self.RESULT_TOPIC_ARN = f"{ReleasabilityService.ARN_SNS}:{aws_region}:{aws_account_id}:ReleasabilityResultTopic"
self.RESULT_QUEUE_ARN = f"{ReleasabilityService.ARN_SQS}:{aws_region}:{aws_account_id}:ReleasabilityResultQueue"

def start_releasability_checks(self):
standardized_version = VersionHelper.as_standardized_version(self.release_request)
Expand Down Expand Up @@ -117,7 +117,7 @@ def _get_check_results(self, correlation_id: str):

now = time.time()
while (received_message_count < expected_message_count
and not has_exceeded_timeout(now, Releasability.FETCH_CHECK_RESULT_TIMEOUT_SECONDS)):
and not has_exceeded_timeout(now, ReleasabilityService.FETCH_CHECK_RESULT_TIMEOUT_SECONDS)):
filtered_messages = self._fetch_filtered_check_results(filters)

for message_payload in filtered_messages:
Expand All @@ -131,14 +131,14 @@ def _get_check_results(self, correlation_id: str):
)
)

time.sleep(Releasability.FETCH_SLEEP_TIME_SECONDS)
time.sleep(ReleasabilityService.FETCH_SLEEP_TIME_SECONDS)

if expected_message_count == received_message_count:
return received_check_results
else:
raise CouldNotRetrieveReleasabilityCheckResultsException(
f'Received {received_message_count}/{expected_message_count} check result messages within '
f'allowed time ({Releasability.FETCH_CHECK_RESULT_TIMEOUT_SECONDS} seconds)')
f'allowed time ({ReleasabilityService.FETCH_CHECK_RESULT_TIMEOUT_SECONDS} seconds)')

@staticmethod
def _build_filters(correlation_id: str) -> []:
Expand All @@ -161,8 +161,8 @@ def _fetch_check_results(self) -> list:

sqs_queue_messages = sqs_client.receive_message(
QueueUrl=sqs_queue_url,
MaxNumberOfMessages=Releasability.SQS_MAX_POLLED_MESSAGES_AT_A_TIME,
WaitTimeSeconds=Releasability.SQS_POLL_WAIT_TIME,
MaxNumberOfMessages=ReleasabilityService.SQS_MAX_POLLED_MESSAGES_AT_A_TIME,
WaitTimeSeconds=ReleasabilityService.SQS_POLL_WAIT_TIME,
)

result = []
Expand Down
File renamed without changes.
Empty file added main/utils/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion release/utils/github.py → main/utils/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging


from release.releasability.ReleaseRequest import ReleaseRequest
from main.releasability.model.ReleaseRequest import ReleaseRequest


ALLOWED_GITHUB_ACTIONS = set(['release', 'workflow_dispatch'])
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from release.releasability import ReleaseRequest
from main.releasability.model import ReleaseRequest


class VersionHelper:
Expand Down
6 changes: 6 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sonar.organization=sonarsource
sonar.projectKey=SonarSource_gh-action_releasability
sonar.python.version=3.10
sonar.python.coverage.reportPaths=build/coverage.xml
sonar.sources=src
sonar.tests=tests
14 changes: 7 additions & 7 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
import unittest
from unittest.mock import patch, mock_open

from release.releasability.releasability import Releasability
from release.main import do_releasability_checks
from main.main import do_releasability_checks
from main.releasability.releasability_service import ReleasabilityService


class MainTest(unittest.TestCase):

@patch.dict(os.environ, {"GITHUB_EVENT_NAME": "workflow_dispatch"}, clear=True)
@patch(
"release.utils.github.json.load",
"main.utils.github.json.load",
return_value={
"inputs": {"version": "1.0.0.0"},
"repository": {"default_branch": "master", "full_name": "org/project"},
},
)
@patch.object(Releasability, '_get_aws_account_id')
@patch.object(Releasability, 'start_releasability_checks')
@patch.object(Releasability, "get_releasability_report")
@patch.object(ReleasabilityService, '_get_aws_account_id')
@patch.object(ReleasabilityService, 'start_releasability_checks')
@patch.object(ReleasabilityService, "get_releasability_report")
@patch.object(sys, 'exit')
def test_do_releasability_checks(
self,
Expand All @@ -29,7 +29,7 @@ def test_do_releasability_checks(
releasability_start_releasability_checks,
releasability_get_releasability_status
):
with patch("release.utils.github.open", mock_open()) as open_mock:
with patch("main.utils.github.open", mock_open()) as open_mock:
do_releasability_checks()

open_mock.assert_called_once()
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

from release.releasability.releasability_check_result import ReleasabilityCheckResult
from release.releasability.releasability_checks_report import ReleasabilityChecksReport
from main.releasability.model.releasability_check_result import ReleasabilityCheckResult
from main.releasability.model.releasability_checks_report import ReleasabilityChecksReport


class ReleasabilityCheckReportTest(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from release.releasability.releasability_check_result import ReleasabilityCheckResult
from main.releasability.model.releasability_check_result import ReleasabilityCheckResult


class ReleasabilityCheckResultTest(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from unittest.mock import patch, MagicMock

from release.releasability.ReleaseRequest import ReleaseRequest
from release.releasability.releasability import Releasability, CouldNotRetrieveReleasabilityCheckResultsException
from main.releasability.model.ReleaseRequest import ReleaseRequest
from main.releasability.releasability_service import ReleasabilityService, CouldNotRetrieveReleasabilityCheckResultsException


class ReleasabilityTest(unittest.TestCase):
Expand All @@ -22,7 +22,7 @@ def test_build_sns_request_should_assign_correctly_properties(self):
branch_name = "feat/some"

release_request = ReleaseRequest(organization, project_name, version, build_number, branch_name, sha)
releasability = Releasability(release_request)
releasability = ReleasabilityService(release_request)

uuid = "42f23-3232-3232-32232"

Expand Down Expand Up @@ -58,7 +58,7 @@ def test_start_releasability_checks_should_invoke_publish(self):
branch_name = "feat/some"

release_request = ReleaseRequest(organization, project_name, version, build_number, branch_name, sha)
releasability = Releasability(release_request)
releasability = ReleasabilityService(release_request)

releasability.start_releasability_checks()

Expand All @@ -81,7 +81,7 @@ def test_start_releasability_checks_should_return_a_correlation_id_after_invokat
branch_name = "feat/some"

release_request = ReleaseRequest(organization, project_name, version, build_number, branch_name, sha)
releasability = Releasability(release_request)
releasability = ReleasabilityService(release_request)

correlation_id = releasability.start_releasability_checks()

Expand All @@ -93,7 +93,7 @@ def test_arn_to_sqs_url_should_return_expected_url_given_a_valid_sqs_arn(self):
queue_name = "great-project/great"
arn = f"arn:aws:sqs:{region}:{account_number}:{queue_name}"

sqs_url = Releasability._arn_to_sqs_url(arn)
sqs_url = ReleasabilityService._arn_to_sqs_url(arn)

self.assertEqual(sqs_url, f"https://sqs.{region}.amazonaws.com/{account_number}/{queue_name}")

Expand All @@ -103,7 +103,7 @@ def test_arn_to_sqs_url_should_return_expected_url_given_an_invalid_arn(self):
queue_name = "great-project/great"
arn = f"arn:aws:invalid:{region}:{account_number}:{queue_name}"

self.assertRaises(ValueError, lambda: Releasability._arn_to_sqs_url(arn))
self.assertRaises(ValueError, lambda: ReleasabilityService._arn_to_sqs_url(arn))

@mock.patch('boto3.Session.client')
def test_fetch_check_results_should_return_4_messages_given_the_provided_response_contains_4(self, mock_client):
Expand Down Expand Up @@ -159,7 +159,7 @@ def test_fetch_check_results_should_return_4_messages_given_the_provided_respons
branch_name = "feat/some"
release_request = ReleaseRequest(organization, project_name, version, build_number, branch_name, sha)

releasability = Releasability(release_request)
releasability = ReleasabilityService(release_request)

messages = releasability._fetch_check_results()

Expand Down Expand Up @@ -220,7 +220,7 @@ def test_fetch_filtered_check_results_should_return_2_messages_given_the_4_provi
branch_name = "feat/some"
release_request = ReleaseRequest(organization, project_name, version, build_number, branch_name, sha)

releasability = Releasability(release_request)
releasability = ReleasabilityService(release_request)

correlation_id = "b8e28245-3568-4257-970d-dcf47bd49ce5"

Expand All @@ -245,7 +245,7 @@ def test_get_check_results_should_return_a_list_of_the_same_size_as_the_one_rece
branch_name = "feat/some"
release_request = ReleaseRequest(organization, project_name, version, build_number, branch_name, sha)

releasability = Releasability(release_request)
releasability = ReleasabilityService(release_request)

correlation_id = "ffff-0000-ffff-0000"
filtered_check_results = [
Expand Down Expand Up @@ -285,9 +285,9 @@ def test_get_check_results_should_raise_an_exception_given_not_enough_check_resu
branch_name = "feat/some"
release_request = ReleaseRequest(organization, project_name, version, build_number, branch_name, sha)

Releasability.FETCH_CHECK_RESULT_TIMEOUT_SECONDS = 2
ReleasabilityService.FETCH_CHECK_RESULT_TIMEOUT_SECONDS = 2

releasability = Releasability(release_request)
releasability = ReleasabilityService(release_request)

correlation_id = "ffff-0000-ffff-0000"
filtered_check_results = [
Expand Down Expand Up @@ -357,7 +357,7 @@ def test_get_check_names(self, mock_client):
mock_sns_client = mock_client.return_value
mock_sns_client.list_subscriptions_by_topic.return_value = sns_response
release_request = MagicMock()
releasability = Releasability(release_request)
releasability = ReleasabilityService(release_request)

check_names = releasability._get_checks()
self.assertEqual(check_names, ["CheckWhiteSource", "CheckQualityGate", "CheckDependencies"])
2 changes: 1 addition & 1 deletion tests/utils/github_action_helper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import tempfile
import unittest

from release.utils.github_action_helper import set_output
from main.utils.github_action_helper import set_output


class GithubActionHelperTest(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/timeout_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
import unittest

from release.utils.timeout import has_exceeded_timeout
from main.utils.timeout import has_exceeded_timeout


class TimeoutTest(unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/version_helper_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import unittest

from parameterized import parameterized
from release.releasability.ReleaseRequest import ReleaseRequest
from release.utils.version_helper import VersionHelper
from main.releasability.model.ReleaseRequest import ReleaseRequest
from main.utils.version_helper import VersionHelper


class VersionHelperTest(unittest.TestCase):
Expand Down

0 comments on commit 1d7ee54

Please sign in to comment.