forked from mozilla-conduit/git-hg-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapping.py
114 lines (87 loc) · 2.93 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import re
from collections.abc import Sequence
from dataclasses import dataclass
from functools import cached_property
from typing import TypeAlias
import pydantic
from git_hg_sync.events import Push
@dataclass
class SyncBranchOperation:
# Source (git)
source_commit: str
# Destination (hg)
destination_branch: str
@dataclass
class SyncTagOperation:
# Source (git)
source_commit: str
# Destination (hg)
tag: str
tags_destination_branch: str
SyncOperation: TypeAlias = SyncBranchOperation | SyncTagOperation
@dataclass
class MappingMatch:
destination_url: str
operation: SyncBranchOperation | SyncTagOperation
class Mapping(pydantic.BaseModel):
source_url: str
def match(self, event: Push) -> Sequence[MappingMatch]:
raise NotImplementedError()
# Branch Mapping
class BranchMapping(Mapping):
branch_pattern: str
destination_url: str
destination_branch: str
@cached_property
def _branch_pattern(self) -> re.Pattern:
return re.compile(self.branch_pattern)
def match(self, event: Push) -> Sequence[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(
destination_url=destination_url,
operation=SyncBranchOperation(
source_commit=commit,
destination_branch=destination_branch,
),
)
)
return matches
# Tag Mapping
class TagMapping(Mapping):
tag_pattern: str
destination_url: str
tags_destination_branch: str
@cached_property
def _tag_pattern(self) -> re.Pattern:
return re.compile(self.tag_pattern)
def match(self, event: Push) -> Sequence[MappingMatch]:
if event.repo_url != self.source_url:
return []
matches: list[MappingMatch] = []
for tag_name, commit in event.tags.items():
if not self._tag_pattern.match(tag_name):
continue
destination_url = re.sub(self._tag_pattern, self.destination_url, tag_name)
matches.append(
MappingMatch(
destination_url=destination_url,
operation=SyncTagOperation(
tag=tag_name,
source_commit=commit,
tags_destination_branch=self.tags_destination_branch,
),
)
)
return matches