Git: Does this push trigger the workflow?

Evaluate the branch filters of a GitHub Actions workflow.

What to build

A program that decides which pushed branches trigger a workflow.

Requirements

  • Patterns use * (any characters except /) and ** (any characters, including /); all other characters match themselves. A branch triggers if it matches any pattern completely.
  • Print run BRANCH or skip BRANCH for each branch.

Input

Line 1: p. Then p patterns. Then n and n branch names.

Output

n lines.

Constraints

p, n ≤ 100.

Example

Input:

2
main
release/**
4
main
release/2.1
release/2.1/hotfix
feature/x

Output:

run main
run release/2.1
run release/2.1/hotfix
skip feature/x

Why it works

Branch filters are glob patterns, and translating each into an exact regular expression decides a match in one step.

Examples

Example 1 · Input
2
main
release/**
4
main
release/2.1
release/2.1/hotfix
feature/x
Expected output
run main
run release/2.1
run release/2.1/hotfix
skip feature/x
Example 2 · Input
1
feature/*
3
feature/login
feature/login/form
feature
Expected output
run feature/login
skip feature/login/form
skip feature

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Turn a pattern into a regular expression: `**` becomes `.*`, `*` becomes `[^/]*`, and everything else is escaped.

Hint 2

The whole branch name must match; a branch triggers if any pattern matches.

Solution

Show one correct solution (try it yourself first)
import re


def compile_pattern(pattern):
    out, i = "", 0
    while i < len(pattern):
        if pattern.startswith("**", i):
            out += ".*"
            i += 2
        elif pattern[i] == "*":
            out += "[^/]*"
            i += 1
        else:
            out += re.escape(pattern[i])
            i += 1
    return re.compile(out + r"\Z")


patterns = [compile_pattern(input().strip()) for _ in range(int(input()))]
for _ in range(int(input())):
    branch = input().strip()
    print(("run " if any(p.match(branch) for p in patterns) else "skip ") + branch)

Lesson: .gitignore and GitHub Actions →