Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions github_rest_api/scripts/github/create_pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import sys
from github_rest_api import Repository
from github_rest_api.utils import compile_patterns


def parse_args(args=None, namespace=None) -> Namespace:
Expand Down Expand Up @@ -34,6 +35,19 @@ def parse_args(args=None, namespace=None) -> Namespace:
required=True,
help="The base branch to merge changes into.",
)
parser.add_argument(
"--ignore-patterns",
dest="ignore_patterns",
nargs="*",
default=["^_"],
help="A list of regular expression patterns. Branches matching any of these patterns will be ignored.",
)
parser.add_argument(
"--update",
dest="update",
action="store_true",
help="Update the head branch using the base branch before creating the pull request.",
)
return parser.parse_args(args=args, namespace=namespace)


Expand All @@ -43,10 +57,23 @@ def main() -> int:
The branch is updated (using dev) before creating the PR.
"""
args = parse_args()
# skip branches with the pattern _*
if args.head_branch.startswith("_"):
return 0
# skip branches matching any of the ignore patterns
try:
compiled = compile_patterns(args.ignore_patterns)
except ValueError as e:
print(e, file=sys.stderr)
return 1
for pattern in compiled:
if pattern.search(args.head_branch):
print(
f"Branch '{args.head_branch}' matches ignore pattern '{
pattern.pattern
}', skipping."
)
Comment thread
dclong marked this conversation as resolved.
return 0
Comment thread
dclong marked this conversation as resolved.
Comment thread
dclong marked this conversation as resolved.
repo = Repository(args.token, os.environ["GITHUB_REPOSITORY"])
if args.update:
repo.update_branch(update=args.head_branch, upstream=args.base_branch)
repo.create_pull_request(
{
"base": args.base_branch,
Expand Down
23 changes: 23 additions & 0 deletions github_rest_api/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Some generally useful util functions."""

from collections.abc import Sequence
from itertools import tee, filterfalse
import re


def partition(pred, iterable):
Expand All @@ -12,6 +14,27 @@ def partition(pred, iterable):
return filter(pred, it1), filterfalse(pred, it2)


def compile_patterns(patterns: str | Sequence[str] | None) -> list[re.Pattern[str]]:
"""Compile a list of regular expression patterns.

:param patterns: A list of regular expression patterns to compile.
:return: A list of compiled regular expression patterns.
"""
if not patterns:
return []
if isinstance(patterns, str):
patterns = [patterns]
compiled = []
for pattern in patterns:
try:
compiled.append(re.compile(pattern))
except re.error as e:
raise ValueError(
f"Invalid regular expression pattern '{pattern}': {e}"
) from e
return compiled


def strip_patch_version(version: str) -> str:
"""Strip the patch version.

Expand Down