Skip to content

Commit 2708164

Browse files
authored
Merge pull request #527 from adrn/unstaged-changes
Enable get_modified_tutorials.py script to find staged and unstaged (i.e. uncommitted) changed files
2 parents 82fb08b + 94f0978 commit 2708164

File tree

3 files changed

+116
-7
lines changed

3 files changed

+116
-7
lines changed

.github/get_modified_tutorials.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1-
import sys
21
from git import Repo
32

43

54
def main(repo_path, main_branch, **kw):
6-
r = Repo(repo_path)
5+
repo = Repo(repo_path)
6+
7+
# Check committed changes on this branch against the main branch,
8+
# modified files in staging area,
9+
# unstaged changes
10+
diff_lists = [
11+
repo.commit(main_branch).diff(repo.head),
12+
repo.head.commit.diff(),
13+
repo.head.commit.diff(None)
14+
]
15+
16+
files_changed = set()
17+
for diffs in diff_lists:
18+
files_changed = files_changed.union([
19+
diff.b_path for diff in diffs
20+
if diff.change_type in ['M', 'A', 'R'] # modified, added, renamed
21+
and diff.b_path.endswith('.ipynb')
22+
])
723

8-
# NOTE: assumes the main branch is named "main"
9-
files_changed = r.git.diff(
10-
f'{str(r.head.object.hexsha)}..{main_branch}',
11-
'--name-only').split("\n")
12-
files_changed = [f for f in files_changed if f.endswith('.ipynb')]
1324
if files_changed:
1425
print(" ".join(files_changed))
1526

.github/workflows/prs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ on:
66
types:
77
# We also want this workflow triggered if the 'Run all tutorials' label
88
# is added or present when PR is updated
9+
- opened
10+
- reopened
911
- synchronize
1012
- labeled
1113

.github/workflows/test-scripts.yml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
name: Test utility scripts
2+
on:
3+
pull_request:
4+
branches:
5+
- main
6+
types:
7+
# We want this workflow triggered if the 'infrastructure' label is added
8+
# or present when PR is updated
9+
- opened
10+
- reopened
11+
- synchronize
12+
- labeled
13+
14+
jobs:
15+
notebooks:
16+
17+
# get_modified_tutorials.py
18+
name: "Test get_modified_tutorials.py script"
19+
runs-on: ubuntu-latest
20+
if: contains(github.event.pull_request.labels.*.name, 'infrastructure')
21+
steps:
22+
- uses: actions/checkout@v2
23+
with:
24+
fetch-depth: 0
25+
26+
- name: Set up Python
27+
uses: actions/setup-python@v2
28+
with:
29+
python-version: 3.9
30+
31+
- name: Install dependencies and setup
32+
run: |
33+
sudo apt-get install pandoc
34+
python -m pip install -U pip
35+
python -m pip install gitpython
36+
git config --global user.email "bot@robot.com"
37+
git config --global user.name "Botty McBotface"
38+
39+
# Make sure if a file is removed, it does not appear in the modified list:
40+
- name: Test 1a - removed (staged)
41+
run: |
42+
git rm tutorials/FITS-header/FITS-header.ipynb
43+
MODIFIED=$(python .github/get_modified_tutorials.py --main-branch origin/main)
44+
[ ! -z "$MODIFIED" ] && exit 1 || echo "Success!"
45+
git reset --hard HEAD
46+
47+
- name: Test 1b - removed and committed
48+
run: |
49+
git rm tutorials/FITS-header/FITS-header.ipynb
50+
git commit -m "TEST COMMIT"
51+
MODIFIED=$(python .github/get_modified_tutorials.py --main-branch origin/main)
52+
[ ! -z "$MODIFIED" ] && exit 1 || echo "Success!"
53+
git reset --hard HEAD~1
54+
55+
# Make sure if a file is modified, it appears in the modified list:
56+
- name: Test 2a - edited
57+
run: |
58+
echo "TEST" >> tutorials/FITS-header/FITS-header.ipynb
59+
MODIFIED=$(python .github/get_modified_tutorials.py --main-branch origin/main)
60+
[ "$MODIFIED" != "tutorials/FITS-header/FITS-header.ipynb" ] && exit 1 || echo "Success!"
61+
git reset --hard HEAD
62+
63+
- name: Test 2b - edited and staged
64+
run: |
65+
echo "TEST" >> tutorials/FITS-header/FITS-header.ipynb
66+
git add tutorials/FITS-header/FITS-header.ipynb
67+
MODIFIED=$(python .github/get_modified_tutorials.py --main-branch origin/main)
68+
[ "$MODIFIED" != "tutorials/FITS-header/FITS-header.ipynb" ] && exit 1 || echo "Success!"
69+
git reset --hard HEAD
70+
71+
- name: Test 2c - edited and committed
72+
run: |
73+
echo "TEST" >> tutorials/FITS-header/FITS-header.ipynb
74+
git add tutorials/FITS-header/FITS-header.ipynb
75+
git commit -m "TEST COMMIT"
76+
MODIFIED=$(python .github/get_modified_tutorials.py --main-branch origin/main)
77+
[ "$MODIFIED" != "tutorials/FITS-header/FITS-header.ipynb" ] && exit 1 || echo "Success!"
78+
git reset --hard HEAD~1
79+
80+
# Make sure if a file is created, it appears in the modified list:
81+
- name: Test 3a - created and staged
82+
run: |
83+
touch tutorials/FITS-header/FITS-header2.ipynb
84+
git add tutorials/FITS-header/FITS-header2.ipynb
85+
MODIFIED=$(python .github/get_modified_tutorials.py --main-branch origin/main)
86+
[ "$MODIFIED" != "tutorials/FITS-header/FITS-header2.ipynb" ] && exit 1 || echo "Success!"
87+
git reset --hard HEAD
88+
89+
- name: Test 3b - created and committed
90+
run: |
91+
touch tutorials/FITS-header/FITS-header2.ipynb
92+
git add tutorials/FITS-header/FITS-header2.ipynb
93+
git commit -m "TEST COMMIT"
94+
MODIFIED=$(python .github/get_modified_tutorials.py --main-branch origin/main)
95+
[ "$MODIFIED" != "tutorials/FITS-header/FITS-header2.ipynb" ] && exit 1 || echo "Success!"
96+
git reset --hard HEAD~1

0 commit comments

Comments
 (0)