Skip to content

Commit e31155c

Browse files
Astraea Quinn SFullyTyped
authored andcommitted
fix branch parsing script
1 parent 58423ec commit e31155c

File tree

4 files changed

+194
-16
lines changed

4 files changed

+194
-16
lines changed

.github/workflows/integration-tests.yml

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,63 @@ jobs:
1717
python-version: ["3.13"]
1818

1919
steps:
20-
- name: Parse testing SDK branch from PR body
20+
- name: Parse testing SDK branch from PR body (Python)
2121
id: parse
22+
shell: bash
2223
run: |
23-
# Look for a line like: TESTING_SDK_BRANCH: feature/foo
24-
REF=$(printf '%s\n' '${{ github.event.pull_request.body }}' | sed -n 's/^TESTING_SDK_BRANCH:[[:space:]]*//p' | head -n1)
25-
if [ -z "$REF" ]; then REF="main"; fi
26-
echo "testing_ref=$REF" >> "$GITHUB_OUTPUT"
27-
echo "Using testing SDK branch: $REF"
28-
24+
set -euo pipefail
25+
26+
python - << 'PYTHON'
27+
import os
28+
import re
29+
30+
body = os.environ.get("PR_BODY", "") or ""
31+
default_ref = "main"
32+
33+
# Regex:
34+
# - (?i) case-insensitive
35+
# - TESTING_SDK_BRANCH
36+
# - optional spaces
37+
# - : or =
38+
# - optional spaces
39+
# - capture rest of line
40+
pattern = re.compile(r"(?i)TESTING_SDK_BRANCH\s*[:=]\s*(.+)$")
41+
42+
ref = None
43+
for line in body.splitlines():
44+
m = pattern.search(line)
45+
if m:
46+
# Strip trailing whitespace
47+
candidate = m.group(1).strip()
48+
if candidate:
49+
ref = candidate
50+
break
51+
52+
if not ref:
53+
ref = default_ref
54+
55+
github_output = os.environ.get("GITHUB_OUTPUT")
56+
if not github_output:
57+
raise SystemExit("GITHUB_OUTPUT not set")
58+
59+
with open(github_output, "a", encoding="utf-8") as fh:
60+
fh.write(f"testing_ref={ref}\n")
61+
62+
print(f"Using testing SDK branch: {ref}")
63+
PYTHON
64+
env:
65+
PR_BODY: ${{ github.event.pull_request.body }}
2966
- name: Checkout Language SDK (this PR)
3067
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
3168
with:
3269
path: language-sdk
3370

71+
- name: Parse testing SDK branch from PR body
72+
id: parse
73+
run: python ops/parse_sdk_branch.py
74+
env:
75+
PR_BODY: ${{ github.event.pull_request.body }}
76+
3477
- name: Checkout Testing SDK
3578
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
3679
with:
@@ -69,20 +112,17 @@ jobs:
69112
AWS_REGION: us-west-2
70113

71114
steps:
72-
- name: Parse testing SDK branch from PR body
73-
id: parse
74-
run: |
75-
# Look for a line like: TESTING_SDK_BRANCH: feature/foo
76-
REF=$(printf '%s\n' '${{ github.event.pull_request.body }}' | sed -n 's/^TESTING_SDK_BRANCH:[[:space:]]*//p' | head -n1)
77-
if [ -z "$REF" ]; then REF="main"; fi
78-
echo "testing_ref=$REF" >> "$GITHUB_OUTPUT"
79-
echo "Using testing SDK branch: $REF"
80-
81115
- name: Checkout Language SDK (this PR)
82116
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
83117
with:
84118
path: language-sdk
85119

120+
- name: Parse testing SDK branch from PR body
121+
id: parse
122+
run: python ops/parse_sdk_branch.py
123+
env:
124+
PR_BODY: ${{ github.event.pull_request.body }}
125+
86126
- name: Checkout Testing SDK
87127
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
88128
with:

.github/workflows/test-parser.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Test Parser
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- 'ops/parse_sdk_branch.py'
7+
- 'ops/__tests__/**'
8+
push:
9+
branches: [ main ]
10+
paths:
11+
- 'ops/parse_sdk_branch.py'
12+
- 'ops/__tests__/**'
13+
14+
jobs:
15+
test-parser:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
19+
20+
- name: Run parser tests
21+
run: python ops/__tests__/test_parse_sdk_branch.py
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
import os
5+
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
6+
7+
from parse_sdk_branch import parse_sdk_branch
8+
9+
10+
def test_parse_sdk_branch():
11+
test_cases = [
12+
# Basic cases
13+
("TESTING_SDK_BRANCH = feature/test", "feature/test"),
14+
("TESTING_SDK_BRANCH: feature/test", "feature/test"),
15+
("TESTING_SDK_BRANCH=feature/test", "feature/test"),
16+
("testing_sdk_branch: feature/test", "feature/test"),
17+
18+
# Complex PR body with backticks and contractions
19+
("""Updated the script to safely parse the testing SDK branch from the PR body, handling case insensitivity and whitespace.
20+
21+
The goal here is to fix the usage of backticks such as in `foo`, and contractions that we've been using such as `we've`
22+
23+
```
24+
plus of course the usage of multiple backticks to include code
25+
```
26+
27+
TESTING_SDK_BRANCH = main
28+
29+
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.""", "main"),
30+
31+
# Edge cases with markdown and special characters
32+
("""# PR Title
33+
34+
Some `code` and we've got contractions here.
35+
36+
```python
37+
def test():
38+
return "test"
39+
```
40+
41+
TESTING_SDK_BRANCH: feature/fix-backticks
42+
43+
More text with `inline code` and don't forget contractions.""", "feature/fix-backticks"),
44+
45+
# Multiple occurrences (should take first)
46+
("""TESTING_SDK_BRANCH = first-branch
47+
48+
Some text here.
49+
50+
TESTING_SDK_BRANCH = second-branch""", "first-branch"),
51+
52+
# Whitespace variations
53+
(" TESTING_SDK_BRANCH = feature/spaces ", "feature/spaces"),
54+
("TESTING_SDK_BRANCH:feature/no-space", "feature/no-space"),
55+
56+
# Default cases
57+
("No branch specified", "main"),
58+
("", "main"),
59+
("Just some random text", "main"),
60+
61+
# Case with backticks in branch name
62+
("TESTING_SDK_BRANCH = feature/fix-`backticks`", "feature/fix-`backticks`"),
63+
64+
# Case with contractions in surrounding text
65+
("We've updated this and TESTING_SDK_BRANCH = feature/test and we're done", "feature/test"),
66+
]
67+
68+
for i, (input_text, expected) in enumerate(test_cases):
69+
result = parse_sdk_branch(input_text)
70+
if result != expected:
71+
print(f"FAIL Test {i+1}: Expected '{expected}', got '{result}'")
72+
print(f"Input: {repr(input_text[:100])}...")
73+
return False
74+
else:
75+
print(f"PASS Test {i+1}: {expected}")
76+
77+
print(f"\nAll {len(test_cases)} tests passed!")
78+
return True
79+
80+
81+
if __name__ == "__main__":
82+
success = test_parse_sdk_branch()
83+
sys.exit(0 if success else 1)

ops/parse_sdk_branch.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import re
5+
6+
7+
8+
def parse_sdk_branch(pr_body: str, default_ref: str = "main") -> str:
9+
"""Parse PR body for TESTING_SDK_BRANCH and return the branch reference."""
10+
pattern = re.compile(r"(?i)TESTING_SDK_BRANCH\s*[:=]\s*(.+)$", re.MULTILINE)
11+
12+
match = pattern.search(pr_body)
13+
if match:
14+
ref = match.group(1).strip()
15+
if ref:
16+
return ref
17+
18+
return default_ref
19+
20+
21+
def main():
22+
pr_body = os.environ.get("PR_BODY", "")
23+
ref = parse_sdk_branch(pr_body)
24+
25+
github_output = os.environ.get("GITHUB_OUTPUT")
26+
if github_output:
27+
with open(github_output, "a", encoding="utf-8") as f:
28+
f.write(f"testing_ref={ref}\n")
29+
30+
print(f"Using testing SDK branch: {ref}")
31+
32+
33+
if __name__ == "__main__":
34+
main()

0 commit comments

Comments
 (0)