Skip to content
Snippets Groups Projects
Commit cd9c4e35 authored by Johan Lorenzo's avatar Johan Lorenzo
Browse files

sync-ac: Generate message expressions automatically

parent 29ad8669
No related branches found
No related tags found
No related merge requests found
Source diff could not be displayed: it is too large. Options to address this: view the blob.
#!/usr/bin/env python3
import json
import logging
from pathlib import Path
log = logging.getLogger(__name__)
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.DEBUG,
)
DATA_DIR = (Path(__file__).parent / "data").absolute()
GITHUB_URL_TEMPLATE = "https://github.com/{repo_owner}/{repo_name}/{number_type}"
REPO_OWNER = "mozilla-mobile"
REPO_NAME_TO_IMPORT = "android-components"
def divide_chunks(sequence, n):
for i in range(0, len(sequence), n):
yield sequence[i : i + n]
def order_repo_names(repo_names):
ordered_list = list(repo_names)
ordered_list.remove(REPO_NAME_TO_IMPORT)
# The regex of the repo to import may take precedence over other regexes.
# Running them last makes sure the other URLs got replaced first.
ordered_list.append(REPO_NAME_TO_IMPORT)
return ordered_list
def main():
with open(DATA_DIR / "repo-numbers.json") as f:
repo_numbers = json.load(f)
regexes = [
"regex:^==>[components] ",
]
for repo_name in order_repo_names(repo_numbers.keys()):
numbers = repo_numbers[repo_name]
if repo_name.startswith("$"):
continue
for number_type in ("issues", "pulls"):
for chunk in divide_chunks(numbers[number_type], 100):
regex = "regex:(({repo_owner}/)?{repo_name}){repo_suffix}#({current_numbers})(\D|$)==>{url}/\\3\\4".format(
repo_owner=REPO_OWNER,
repo_name=f"[{repo_name[0].upper()}{repo_name[0].lower()}]{repo_name[1:]}",
repo_suffix="?" if repo_name == REPO_NAME_TO_IMPORT else "\\s*",
current_numbers="|".join(str(number) for number in chunk),
url=GITHUB_URL_TEMPLATE.format(
repo_owner=REPO_OWNER,
repo_name=repo_name,
number_type="pull" if number_type == "pulls" else number_type,
),
)
regexes.append(regex)
with open(DATA_DIR / "message-expressions.txt", "w") as f:
f.write("\n".join(regexes))
__name__ == "__main__" and main()
......@@ -10,15 +10,7 @@ REPO_BRANCH_NAME='firefox-android'
TAG_PREFIX='components-'
MONOREPO_URL='git@github.com:mozilla-mobile/firefox-android.git'
EXPRESSIONS_FILE_PATH='/tmp/git/expressions.txt'
REPLACE_MESSAGE_EXPRESSIONS=$(cat <<'EOF'
regex:^==>[components]
regex:(([fF]or|[iI]ssue|[fF]ix(es)?|[cC]loses?)\s+){1,2}#(\d+)==>\1mozilla-mobile/android-components#\4
regex:([fF]or|[iI]ssue|[fF]ixes?|[cC]loses?)\s+[fF]enix\s*#(\d+)==>\1mozilla-mobile/fenix#\2
regex:\(#(\d+)\)==>(mozilla-mobile/android-components#\1)
regex:(\s+)#(\d+)==>\1(mozilla-mobile/android-components#\2)
EOF
)
EXPRESSIONS_FILE_PATH="$SCRIPT_DIR/data/message-expressions.txt"
function _is_github_authenticated() {
......@@ -41,7 +33,6 @@ function _test_prerequisites() {
function _setup_temporary_repo() {
rm -rf "$REPO_PATH"
mkdir -p "$REPO_PATH"
echo "$REPLACE_MESSAGE_EXPRESSIONS" > "$EXPRESSIONS_FILE_PATH"
git clone "git@github.com:mozilla-mobile/$REPO_NAME_TO_SYNC.git" "$REPO_PATH"
cd "$REPO_PATH"
......@@ -54,6 +45,11 @@ function _update_repo_branch() {
git push origin "$REPO_BRANCH_NAME" --force
}
function _update_repo_numbers() {
"$SCRIPT_DIR/generate-repo-numbers.py"
"$SCRIPT_DIR/generate-replace-message-expressions.py"
}
function _rewrite_git_history() {
git filter-repo \
--to-subdirectory-filter 'android-components/' \
......@@ -76,13 +72,13 @@ function _merge_histories() {
function _clean_up_temporary_repo() {
rm -rf "$REPO_PATH"
rm -f "$EXPRESSIONS_FILE_PATH"
}
_test_prerequisites
_setup_temporary_repo
_update_repo_branch
_update_repo_numbers
_rewrite_git_history
_remove_old_tags
_merge_histories
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment