Skip to content

Commit

Permalink
[TECH-272] Add link to deployed service
Browse files Browse the repository at this point in the history
branch: feature/TECH-272-upgrade-monorepo
  • Loading branch information
SamTheisens committed May 12, 2023
1 parent 8a5a1ab commit 621c1a3
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 19 deletions.
37 changes: 28 additions & 9 deletions src/mpyl/reporting/formatting/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from junitparser import TestSuite

from ...project import Stage, Project
from ...steps import Output, ArtifactType
from ...steps.deploy.kubernetes import DEPLOYED_SERVICE_KEY
from ...steps.run import RunResult
from ...steps.steps import StepResult, collect_test_results
from ...utilities.junit import TestRunSummary, sum_suites
Expand All @@ -16,14 +18,24 @@ def summary_to_markdown(summary: TestRunSummary):
f"πŸ’” {summary.errors} πŸ™ˆ {summary.skipped}"


def __add_link(name: str, output: Output) -> str:
if output.produced_artifact and output.produced_artifact.artifact_type == ArtifactType.DEPLOYED_HELM_APP:
url = output.produced_artifact.spec.get(DEPLOYED_SERVICE_KEY)
if url:
return f'[{name}]({url}/swagger/index.html)'

return name


def __to_oneliner(result: list[StepResult], plan: set[Project]) -> str:
project_names: list[str] = []
if plan:
sorted_plan = sorted(plan, key=operator.attrgetter('name'))
for proj in sorted_plan:
found_result = next((r for r in result if r.project.name == proj.name), None)
if found_result:
project_names.append(f'*{proj.name}*' if found_result.output.success else f'~~{proj.name}~~')
project_names.append(f'*{__add_link(proj.name, found_result.output)}*' if found_result.output.success
else f'~~{proj.name}~~')
else:
project_names.append(f'_{proj.name}_')
else:
Expand All @@ -42,20 +54,27 @@ def stage_to_icon(stage: Stage):
return '➑️'


def markdown_for_stage(run_result: RunResult, stage: Stage):
step_results: list[StepResult] = run_result.results_for_stage(stage)
plan: set[Project] = run_result.plan_for_stage(stage)
result = ""
if step_results or plan:
result += f"{stage_to_icon(stage)} {__to_oneliner(step_results, plan)} \n"
test_results = collect_test_results(step_results)
if test_results:
result += to_markdown_test_report(
test_results) + f' [link]({run_result.run_properties.details.tests_url}) \n'

return result


def run_result_to_markdown(run_result: RunResult) -> str:
result: str = f'{run_result.status_line} \n'
if run_result.exception:
result += f"\n```\n{run_result.exception}\n```\n"

for stage in Stage:
step_results: list[StepResult] = run_result.results_for_stage(stage)
plan: set[Project] = run_result.plan_for_stage(stage)
if step_results or plan:
result += f"{stage_to_icon(stage)} {__to_oneliner(step_results, plan)} \n"
test_results = collect_test_results(step_results)
if test_results:
result += to_markdown_test_report(
test_results) + f' [link]({run_result.run_properties.details.tests_url}) \n'
result += markdown_for_stage(run_result, stage)

return result

Expand Down
4 changes: 3 additions & 1 deletion src/mpyl/reporting/targets/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
from atlassian import Jira

from . import Reporter
from ..formatting.markdown import markdown_for_stage
from ...project import Stage
from ...steps.run import RunResult


Expand Down Expand Up @@ -137,7 +139,7 @@ def to_markdown_summary(ticket: JiraTicket, run_result: RunResult) -> str:
details = run_result.run_properties.details

build_status = f"πŸ—οΈ Build [{details.build_id}]({details.run_url}) {run_result.status_line}, " \
f"started by _{details.user}_"
f"started by _{details.user}_ \n{markdown_for_stage(run_result, Stage.DEPLOY)}"
return f"## πŸ“• [{ticket.ticket_id}]({ticket.ticket_url}) {ticket.summary} " \
f"![{ticket.user_email}]({ticket.user_avatar}) \n" \
f"{description_markdown}\n" \
Expand Down
19 changes: 14 additions & 5 deletions src/mpyl/steps/deploy/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
from .k8s import deploy_helm_chart, CustomResourceDefinition
from .k8s.chart import ChartBuilder, to_service_chart
from .. import Step, Meta
from ..models import Input, Output, ArtifactType
from ..models import Input, Output, ArtifactType, input_to_artifact
from ...project import Stage

DEPLOYED_SERVICE_KEY = 'url'


class DeployKubernetes(Step):

Expand All @@ -33,8 +35,15 @@ def try_extract_endpoint(chart: dict[str, CustomResourceDefinition]) -> Optional
def execute(self, step_input: Input) -> Output:
builder = ChartBuilder(step_input)
chart = to_service_chart(builder)
endpoint = self.try_extract_endpoint(chart)
if endpoint:
self._logger.info(f"Service {step_input.project.name} reachable at: {endpoint}")

return deploy_helm_chart(self._logger, chart, step_input, builder.release_name)
deploy_result = deploy_helm_chart(self._logger, chart, step_input, builder.release_name)
if deploy_result.success:
endpoint = self.try_extract_endpoint(chart)
spec = {}
if endpoint:
self._logger.info(f"Service {step_input.project.name} reachable at: {endpoint}")
spec[DEPLOYED_SERVICE_KEY] = endpoint
artifact = input_to_artifact(ArtifactType.DEPLOYED_HELM_APP, step_input, spec=spec)
return Output(success=True, message=deploy_result.message, produced_artifact=artifact)

return deploy_result
4 changes: 3 additions & 1 deletion src/mpyl/steps/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ def to_yaml(cls, representer, node):
"""A docker image"""
JUNIT_TESTS = 2
"""A test suite in junit compatible `.xml` format"""
NONE = 3
DEPLOYED_HELM_APP = 3
"""A helm chart deployed to kubernetes"""
NONE = 4


@yaml_object(yaml)
Expand Down
12 changes: 11 additions & 1 deletion tests/reporting/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime

from mpyl.steps.deploy.kubernetes import DEPLOYED_SERVICE_KEY
from src.mpyl.project import Stages, Project, Stage
from src.mpyl.steps.models import Output, Artifact, ArtifactType
from src.mpyl.steps.run import RunResult
Expand All @@ -20,9 +21,11 @@ def create_test_result() -> RunResult:
def create_test_result_with_plan() -> RunResult:
build_projects = [test_data.get_project(), __get_other_project()]
test_projects = [__get_other_project()]
deploy_projects = [__get_other_project()]
return RunResult(run_properties=test_data.RUN_PROPERTIES, run_plan={
Stage.BUILD: set(build_projects),
Stage.TEST: set(test_projects)
Stage.TEST: set(test_projects),
Stage.DEPLOY: set(deploy_projects)
})


Expand All @@ -41,6 +44,13 @@ def append_results(result: RunResult) -> None:
producing_step='Docker Test',
spec={TEST_OUTPUT_PATH_KEY: test_resource_path})),
timestamp=datetime.fromisoformat('2019-01-04T16:41:45+02:00')))
result.append(StepResult(stage=Stage.DEPLOY, project=other_project,
output=Output(success=True, message='Deploy successful',
produced_artifact=
Artifact(artifact_type=ArtifactType.DEPLOYED_HELM_APP, revision='revision',
producing_step='Kubernetes Deploy',
spec={DEPLOYED_SERVICE_KEY: 'https://some.location.com'})),
timestamp=datetime.fromisoformat('2019-01-04T16:41:45+02:00')))


def __get_other_project():
Expand Down
1 change: 1 addition & 0 deletions tests/reporting/formatting/test_resources/markdown_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
πŸ—οΈ _dockertest_, _test_
πŸ§ͺ _test_
πŸ§ͺ 51 ❌ 1 πŸ’” 0 πŸ™ˆ 0 [link](http://localhost/tests)
πŸš€ _test_
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
πŸ—οΈ ~~dockertest~~, *test*
πŸ§ͺ *test*
πŸ§ͺ 51 ❌ 1 πŸ’” 0 πŸ™ˆ 0 [link](http://localhost/tests)
πŸš€ *[test](https://some.location.com/swagger/index.html)*
2 changes: 2 additions & 0 deletions tests/reporting/formatting/test_resources/simple_run.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ Case Invoice routes should refuse to serve invoices if user reference in path do
Case Invoice routes should refuse request without filter
Case Invoice routes should use proper headers when returning a PDF invoice
Case Invoice routes should GET '/api/v1/organizations/{customerReference}/invoices/{invoiceId}' should return a 200 with invoice details
Stage DEPLOY
2019-01-04 16:41:45+02:00 - test - Stage.DEPLOY - success: True
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ Similarly to mpl **modules**, we *should* update the PR details with card descri
[πŸ‘©β€πŸ’»](https://vandebron.atlassian.net/browse/TECH-290/jira/people/6151b89d72f6970069e87968) is tagged


πŸ—οΈ Build [id](http://localhost/run) ❌ Failed, started by _somebody_
πŸ—οΈ Build [id](http://localhost/run) ❌ Failed, started by _somebody_
πŸš€ _test_
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
πŸ—οΈ ~dockertest~, *test*
πŸ§ͺ *test*
πŸ§ͺ 51 ❌ 1 πŸ’” 0 πŸ™ˆ 0 <http://localhost/tests|link>
πŸš€ *<https://some.location.com/swagger/index.html|test>*
2 changes: 1 addition & 1 deletion tests/reporting/test_markdown_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ def test_should_measure_progress(self):
result.append(StepResult(stage=Stage.BUILD, project=test_data.get_project(),
output=Output(success=False, message='Build failed'),
timestamp=datetime.fromisoformat('2019-01-04T16:41:24+02:00')))
assert round(result.progress_fraction * 100) == 33, 'Should be at one third'
assert round(result.progress_fraction * 100) == 25, 'Should be at one quarter'
append_results(result)
assert result.progress_fraction == 1.0, 'Should be 100% at end of run'

0 comments on commit 621c1a3

Please sign in to comment.