Skip to content

Commit f3f292b

Browse files
committed
Import the metadata parser workflow
1 parent 5965e8f commit f3f292b

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

.github/workflows/update.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Update Validation Patterns
2+
3+
on:
4+
schedule:
5+
- cron: "0 0 * * 0"
6+
7+
jobs:
8+
update:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v2
14+
15+
- name: Setup Python
16+
uses: actions/setup-python@v3
17+
18+
- name: Fetch third-party metadata
19+
run: |
20+
curl -o resources/metadata.xml https://raw.githubusercontent.com/google/libphonenumber/master/resources/PhoneNumberMetadata.xml
21+
22+
- name: Update metadata
23+
run: |
24+
python scripts/prepare-metadata
25+
26+
- name: Compare metadata
27+
id: compare
28+
run: |
29+
git diff --exit-code resources/metadata.xml || echo "::set-output name=differs::true"
30+
31+
- name: Create Pull Request
32+
if: steps.compare.outputs.differs == 'true'
33+
uses: peter-evans/create-pull-request@v5
34+
with:
35+
token: ${{ secrets.GH_TOKEN }}
36+
branch-suffix: short-commit-hash
37+
branch: update-validation-patterns
38+
title: Update the validation patterns
39+
commit-message: Update the validation patterns
40+
body: This PR updates the validation patterns i.e. metadata.xml to keep the repository up-to-date with the upstream changes.

scripts/prepare-metadata/__main__.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import json
2+
import re
3+
from pathlib import Path
4+
from xml.etree import ElementTree
5+
6+
project_root = Path(__file__).parent.parent.parent
7+
metadata_path = project_root / "resources" / "metadata.xml"
8+
patterns_path = project_root / "src" / "metadata" / "validations.json"
9+
countries_path = project_root / "src" / "metadata" / "countries.json"
10+
11+
tree = ElementTree.parse(metadata_path)
12+
territories = tree.find("territories")
13+
14+
15+
def update_mask(mask, length):
16+
length_without_cc = len(re.findall(r"[\d.]", re.sub(r"\+\d+\s", "", mask)))
17+
if length_without_cc != length:
18+
cc_mask = re.match(r"(\+\d+)\s", mask).group(1)
19+
ac_mask = re.match(r"\+\d+\s(?:(\S+)\s)?", mask).group(1) or ""
20+
ac_mask_length = len(re.findall(r"[\d.]", ac_mask))
21+
pn_mask_expected_length = length - ac_mask_length
22+
pn_mask_prefix = re.sub(r"\+\d+\s(?:\S+\s)?", "", mask).split()[0]
23+
pn_mask_parts = [pn_mask_prefix]
24+
while len("".join(pn_mask_parts)) < pn_mask_expected_length:
25+
pn_mask_parts.append("." * len(pn_mask_prefix))
26+
pn_mask = " ".join(pn_mask_parts)
27+
while len(pn_mask.replace(" ", "")) > pn_mask_expected_length:
28+
pn_mask = pn_mask[:-1]
29+
mask = f"{cc_mask} {ac_mask} {pn_mask}" if ac_mask else f"{cc_mask} {pn_mask}"
30+
return re.sub(r"\s(\.{1,2})$", r"\1", mask)
31+
32+
33+
with open(patterns_path) as fp:
34+
patterns = json.load(fp)
35+
36+
with open(countries_path) as fp:
37+
countries = json.load(fp)
38+
39+
for territory in territories:
40+
# Regenerate masks based on possible maximum lengths
41+
possible_lengths = map(lambda e: territory.find(f"{e.tag}/possibleLengths"), territory.iter())
42+
possible_lengths = map(lambda e: e.get("national"), filter(lambda e: e is not None, possible_lengths))
43+
possible_lengths = list(map(int, re.findall(r"\d+", ",".join(possible_lengths))))
44+
min_length, max_length = min(possible_lengths), max(possible_lengths)
45+
for country in [c for c in countries if c[0] == territory.get("id").lower()]:
46+
country[3] = update_mask(country[3], max_length)
47+
48+
# Update phone validation patterns
49+
general_desc = territory.find("generalDesc")
50+
national_number_pattern = general_desc.find("nationalNumberPattern").text
51+
national_number_pattern = re.sub(r"[\s\n]", "", national_number_pattern)
52+
patterns[territory.get("id").lower()] = [
53+
f"^\\d{{{min_length},{max_length}}}$" if min_length != max_length else f"^\\d{{{max_length}}}$",
54+
f"^{national_number_pattern}$"
55+
]
56+
57+
with open(patterns_path, "w") as fp:
58+
json.dump(patterns, fp, indent=2)
59+
60+
with open(countries_path, "w") as fp:
61+
json.dump(countries, fp, indent=2)

0 commit comments

Comments
 (0)