|
13 | 13 | from api.shared.mixins import RepoPropertyMixin |
14 | 14 | from core.models import Branch, Pull |
15 | 15 | from graphs.settings import settings |
| 16 | +from services.components import commit_components |
16 | 17 |
|
17 | 18 | from .helpers.badge import format_coverage_precision, get_badge |
18 | 19 | from .helpers.graphs import icicle, sunburst, tree |
@@ -127,6 +128,10 @@ def get_coverage(self): |
127 | 128 | if flag: |
128 | 129 | return self.flag_coverage(flag, commit), coverage_range |
129 | 130 |
|
| 131 | + component = self.request.query_params.get("component") |
| 132 | + if component: |
| 133 | + return self.component_coverage(component, commit), coverage_range |
| 134 | + |
130 | 135 | coverage = ( |
131 | 136 | commit.totals.get("c") |
132 | 137 | if commit is not None and commit.totals is not None |
@@ -156,6 +161,39 @@ def flag_coverage(self, flag_name, commit): |
156 | 161 | return flag.totals.coverage |
157 | 162 | return None |
158 | 163 |
|
| 164 | + def component_coverage(self, component_id, commit): |
| 165 | + """ |
| 166 | + Looks into a commit's report sessions and returns the coverage for a particular component. |
| 167 | +
|
| 168 | + Parameters |
| 169 | + component_id (string): id or name of component |
| 170 | + commit (obj): commit object containing report |
| 171 | + """ |
| 172 | + |
| 173 | + report = commit.full_report |
| 174 | + if report is None: |
| 175 | + log.warning( |
| 176 | + "Commit's report not found", |
| 177 | + extra=dict(commit=commit, component=component_id), |
| 178 | + ) |
| 179 | + return None |
| 180 | + components = commit_components(commit, None) |
| 181 | + try: |
| 182 | + component = next( |
| 183 | + c |
| 184 | + for c in components |
| 185 | + if c.component_id == component_id or c.name == component_id |
| 186 | + ) |
| 187 | + except StopIteration: |
| 188 | + # Component not found |
| 189 | + return None |
| 190 | + |
| 191 | + component_flags = component.get_matching_flags(report.get_flag_names()) |
| 192 | + |
| 193 | + report.filter(flags=component_flags, paths=component.paths) |
| 194 | + |
| 195 | + return report.totals.coverage |
| 196 | + |
159 | 197 |
|
160 | 198 | class GraphHandler(APIView, RepoPropertyMixin, GraphBadgeAPIMixin): |
161 | 199 | permission_classes = [AllowAny] |
|
0 commit comments