forked from mozilla-conduit/git-hg-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapping.py
55 lines (43 loc) · 1.42 KB
/
mapping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import re
from dataclasses import dataclass
from functools import cached_property
import pydantic
from git_hg_sync.events import Push
class MappingSource(pydantic.BaseModel):
url: str
branch_pattern: str
class MappingDestination(pydantic.BaseModel):
url: str
branch: str
@dataclass
class MappingMatch:
source_commit: str
destination_url: str
destination_branch: str
class Mapping(pydantic.BaseModel):
source: MappingSource
destination: MappingDestination
@cached_property
def _branch_pattern(self) -> re.Pattern:
return re.compile(self.source.branch_pattern)
def match(self, event: Push) -> list[MappingMatch]:
if event.repo_url != self.source.url:
return []
matches: list[MappingMatch] = []
for branch_name, commit in event.branches.items():
if not self._branch_pattern.match(branch_name):
continue
destination_url = re.sub(
self._branch_pattern, self.destination.url, branch_name
)
destination_branch = re.sub(
self._branch_pattern, self.destination.branch, branch_name
)
matches.append(
MappingMatch(
source_commit=commit,
destination_url=destination_url,
destination_branch=destination_branch,
)
)
return matches