Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
Add types to build context lib
Browse files Browse the repository at this point in the history
Summary:
This is just improving type coverage in preparation for some improvements to how
we handle emails.

Test Plan: mypy

Reviewers: naphat

Reviewed By: naphat

Subscribers: changesbot

Differential Revision: https://tails.corp.dropbox.com/D221760
  • Loading branch information
kylec1 committed Aug 19, 2016
1 parent 6dd9771 commit 6f023c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 13 additions & 3 deletions changes/lib/build_context_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

from changes.api.build_details import get_parents_last_builds
from changes.constants import Result
from changes.models.build import Build # NOQA
from changes.models.job import Job
from changes.models.jobstep import JobStep
from changes.models.log import LogSource, LogChunk
from changes.models.test import TestCase
from changes.utils.http import build_web_uri
from sqlalchemy.orm import subqueryload_all

from typing import Any, cast, Dict, List, Optional, Tuple # NOQA


def _get_project_uri(build):
return '/projects/{}/'.format(build.project.slug)
Expand Down Expand Up @@ -42,16 +45,19 @@ def _get_log_uri(log_source):


def _aggregate_count(items, key):
return sum(map(lambda item: item[key], items))
# type: (List[Dict[str, Any]], str) -> int
return sum(map(lambda item: cast(int, item[key]), items))


def get_collection_context(builds):
# type: (List[Build]) -> Dict[str, Any]
"""
Given a non-empty list of finished builds, returns a context for
rendering the build results.
"""

def sort_builds(builds_context):
# type: (List[Dict[str, Any]]) -> List[Dict[str, Any]]
result_priority_order = (
Result.passed,
Result.skipped,
Expand All @@ -72,12 +78,11 @@ def sort_builds(builds_context):
)

builds_context = sort_builds(map(_get_build_context, builds))
result = Result.unknown # type: Result
if all(map(lambda build: build['is_passing'], builds_context)):
result = Result.passed
elif any(imap(lambda build: build['is_failing'], builds_context)):
result = Result.failed
else:
result = Result.unknown

build = builds[0]
target, target_uri = _get_build_target(build)
Expand All @@ -99,6 +104,7 @@ def sort_builds(builds_context):


def _get_title(target, label, result):
# type: (str, unicode, Result) -> unicode
# Use the first label line for multi line labels.
if label:
lines = label.splitlines()
Expand All @@ -118,6 +124,7 @@ def _get_title(target, label, result):


def _get_build_target(build):
# type: (Build) -> Tuple[Optional[str], str]
"""
Returns the build's target and target uri (normally a phabricator
revision and diff url).
Expand All @@ -138,6 +145,7 @@ def _get_build_target(build):


def _get_build_context(build, get_parent=True):
# type: (Build, bool) -> Dict[str, Any]
jobs = list(Job.query.filter(Job.build_id == build.id))
jobs_context = map(_get_job_context, jobs)

Expand All @@ -163,6 +171,7 @@ def _get_build_context(build, get_parent=True):


def _get_job_context(job):
# type: (Job) -> Dict[str, Any]
def get_job_failing_tests(job):
failing_tests = TestCase.query.options(
subqueryload_all('messages')
Expand Down Expand Up @@ -218,6 +227,7 @@ def get_job_failing_log_sources(job):


def _get_log_clipping(logsource, max_size=5000, max_lines=25):
# type: (LogSource, int, int) -> str
if logsource.in_artifact_store:
# We don't yet get clippings for ArtifactStore logs.
return ""
Expand Down
1 change: 1 addition & 0 deletions changes/listeners/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def send(self, msg, build):
mail.send(msg)

def get_msg(self, builds):
# type: (List[Build]) -> Message
context = build_context_lib.get_collection_context(builds)
if context['result'] == Result.passed:
return None
Expand Down

0 comments on commit 6f023c8

Please sign in to comment.