Skip to content

Commit a66c000

Browse files
committed
Use overrides for using the same branchname over multiple repos
1 parent f1411f7 commit a66c000

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

.github/workflows/coverage.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ jobs:
2525
- name: Set up PDM
2626
uses: pdm-project/setup-pdm@v4
2727

28+
- name: Generate overrides to use current branch if PR
29+
if: github.event_name == 'pull_request'
30+
run: |
31+
pdm run tools/gen_overrides.py ${{github.head_ref}} > overrides.txt
32+
echo "Generated overrides:"
33+
cat overrides.txt
34+
35+
- name: Relock PDM
36+
if: github.event_name != 'pull_request'
37+
run: pdm lock -d
38+
39+
- name: Relock PDM (PR)
40+
if: github.event_name == 'pull_request'
41+
run: pdm lock -d --override overrides.txt
42+
2843
- name: Install dependencies
2944
run: |
3045
pdm install

.github/workflows/main.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ jobs:
1919
fetch-depth: 0
2020
- name: Set up PDM
2121
uses: pdm-project/setup-pdm@v4
22+
23+
- name: Generate overrides to use current branch if PR
24+
if: github.event_name == 'pull_request'
25+
run: |
26+
pdm run tools/gen_overrides.py ${{github.head_ref}} > overrides.txt
27+
echo "Generated overrides:"
28+
cat overrides.txt
29+
30+
- name: Relock PDM
31+
if: github.event_name != 'pull_request'
32+
run: pdm lock -d
33+
34+
- name: Relock PDM (PR)
35+
if: github.event_name == 'pull_request'
36+
run: pdm lock -d --override overrides.txt
37+
2238
- name: Install dependencies
2339
run: |
2440
pdm install

.github/workflows/preview-docs.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ jobs:
2020
python-version: 3.12
2121
cache: true
2222

23+
- name: Generate overrides to use current branch if PR
24+
if: github.event_name == 'pull_request'
25+
run: |
26+
pdm run tools/gen_overrides.py ${{github.head_ref}} > overrides.txt
27+
echo "Generated overrides:"
28+
cat overrides.txt
29+
30+
- name: Relock PDM
31+
if: github.event_name != 'pull_request'
32+
run: pdm lock -d
33+
34+
- name: Relock PDM (PR)
35+
if: github.event_name == 'pull_request'
36+
run: pdm lock -d --override overrides.txt
37+
2338
- name: Install dependencies
2439
run: pdm install
2540

tools/gen_overrides.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# gen-overrides.py
2+
# /// script
3+
# requires-python = ">=3.10"
4+
# dependencies = [
5+
# "pyproject-parser",
6+
# "requirements-parser",
7+
# ]
8+
# ///
9+
import os
10+
import subprocess
11+
import sys
12+
import urllib
13+
from pathlib import Path
14+
15+
from pyproject_parser import PyProject
16+
from requirements.requirement import Requirement
17+
18+
19+
rootdir = Path(os.environ["PDM_PROJECT_ROOT"])
20+
21+
def get_branch(repo_dir):
22+
"""Get the current git branch"""
23+
return subprocess.check_output(['git', 'branch', '--show-current'], text=True).strip()
24+
25+
def get_remote_branch(repo, branch):
26+
return subprocess.call(
27+
['git', 'ls-remote', '--exit-code', '--heads', repo, f'refs/heads/{branch}'],
28+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0
29+
30+
def gen_overrides():
31+
if len(sys.argv) > 1:
32+
branch = sys.argv[1]
33+
if branch.startswith("refs/heads/"):
34+
branch = branch[11:]
35+
else:
36+
branch = get_branch(rootdir)
37+
prj = PyProject.load(rootdir / "pyproject.toml")
38+
reqs = prj.project['dependencies']
39+
git_reqs = [r for r in reqs if r.url and r.url.startswith('git+')]
40+
for r in git_reqs:
41+
parts = urllib.parse.urlparse(r.url, allow_fragments=True)
42+
# remove any branches that might already be there
43+
base = parts.path.rsplit(sep="@",maxsplit=1)[0]
44+
clone_url = urllib.parse.urlunparse(parts._replace(path=base, scheme="https"))
45+
if get_remote_branch(clone_url, branch):
46+
path = f"{parts.path}@{branch}"
47+
else:
48+
path = parts.path
49+
r.url = urllib.parse.urlunparse(parts._replace(path=path))
50+
print(str(r))
51+
52+
if __name__ == "__main__":
53+
gen_overrides()

0 commit comments

Comments
 (0)