Coverage for src/pullapprove/schemas.py: 100%

50 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-10-31 23:22 -0500

1""" 

2JSON shapes for matches, shared by `pullapprove match --json` and the 

3PullApprove API. 

4 

5These describe the match layer: which scopes, paths and code a set of 

6files or a diff matched, before any reviews are counted. The API's 

7per-PR results wrap these same shapes with a status, so the keys (scope 

8name, path, code location_id) line up between the two. 

9""" 

10 

11from __future__ import annotations 

12 

13from typing import TypedDict 

14 

15from .config import ScopeModel 

16from .matches import ChangeMatches, ScopeCodeMatch, ScopePathMatch 

17 

18 

19class ScopeSchema(TypedDict): 

20 """The scope config behind a match.""" 

21 

22 name: str 

23 description: str 

24 ownership: str 

25 require: int 

26 reviewers: list[str] 

27 alternates: list[str] 

28 paths: list[str] 

29 code: list[str] 

30 checklist: bool 

31 

32 

33class PathMatchSchema(TypedDict): 

34 path: str 

35 scopes: list[str] 

36 

37 

38class CodeMatchSchema(TypedDict): 

39 path: str 

40 scopes: list[str] 

41 start_line: int 

42 end_line: int 

43 start_column: int 

44 end_column: int 

45 location_id: str 

46 

47 

48class MatchesSchema(TypedDict): 

49 scopes: dict[str, ScopeSchema] 

50 paths: dict[str, PathMatchSchema] 

51 code: dict[str, CodeMatchSchema] 

52 

53 

54def scope_schema(scope: ScopeModel) -> ScopeSchema: 

55 return { 

56 "name": scope.name, 

57 "description": scope.description, 

58 "ownership": str(scope.ownership), 

59 "require": scope.require, 

60 "reviewers": scope.reviewers, 

61 "alternates": scope.alternates, 

62 "paths": scope.paths, 

63 "code": scope.code, 

64 "checklist": scope.checklist is not None, 

65 } 

66 

67 

68def path_match_schema(path_match: ScopePathMatch) -> PathMatchSchema: 

69 return { 

70 "path": path_match.path, 

71 "scopes": path_match.scopes, 

72 } 

73 

74 

75def code_match_schema(code_match: ScopeCodeMatch) -> CodeMatchSchema: 

76 return { 

77 "path": code_match.path, 

78 "scopes": code_match.scopes, 

79 "start_line": code_match.start_line, 

80 "end_line": code_match.end_line, 

81 "start_column": code_match.start_column, 

82 "end_column": code_match.end_column, 

83 "location_id": code_match.location_id, 

84 } 

85 

86 

87def matches_schema(*, matches: ChangeMatches, all_paths: list[str]) -> MatchesSchema: 

88 """Everything a set of files or a diff matched. 

89 

90 `all_paths` is every file that was looked at, so files no scope owns 

91 show up in `paths` with empty `scopes`. 

92 """ 

93 paths: dict[str, PathMatchSchema] = {} 

94 for path in sorted(all_paths): 

95 if path in matches.paths: 

96 path_match = matches.paths[path] 

97 else: 

98 # Matches only keep the paths some scope owns 

99 path_match = ScopePathMatch(path=path, scopes=[]) 

100 paths[path] = path_match_schema(path_match) 

101 

102 code_matches = sorted( 

103 matches.code.values(), 

104 key=lambda c: (c.path, c.start_line, c.start_column), 

105 ) 

106 code: dict[str, CodeMatchSchema] = {} 

107 for code_match in code_matches: 

108 code[code_match.location_id] = code_match_schema(code_match) 

109 

110 scopes: dict[str, ScopeSchema] = {} 

111 for scope in matches.scopes.values(): 

112 scopes[scope.name] = scope_schema(scope) 

113 

114 return { 

115 "scopes": scopes, 

116 "paths": paths, 

117 "code": code, 

118 }