-
Notifications
You must be signed in to change notification settings - Fork 2
/
update-lecture.py
64 lines (50 loc) · 2.35 KB
/
update-lecture.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import re
import subprocess
# Get environment variables
issue_body = os.getenv('ISSUE_BODY')
config_file = "_config.yml"
issue_number = os.getenv('GITHUB_ISSUE_NUMBER')
# Parse issue body
lecture_number = re.search(r'Lecture Number: (\d+)', issue_body).group(1).strip()
demo = re.search(r'Demo: (.+)', issue_body).group(1).strip()
slides = re.search(r'Slides: (.+)', issue_body).group(1).strip()
# Create demo link if demo is not 'None'
if demo.lower() != "none":
demo_link = f"https://data8.datahub.berkeley.edu/hub/user-redirect/git-pull?repo=https://github.com/data-8/materials-su24&urlpath=tree/materials-su24/lec/{demo}/{demo}.ipynb&branch=main"
else:
demo_link = "None"
# Replace text in config file
with open(config_file, 'r') as file:
config_content = file.read()
slide_line = f" slide{lecture_number}: \"Slides\""
demo_line = f" demo{lecture_number}: \"• Demo\""
if demo_link == "None":
demo_replacement = f" demo{lecture_number}: \"\""
else:
demo_replacement = f" demo{lecture_number}: \"• [Demo]({demo_link})\""
slides_replacement = f" slide{lecture_number}: \"[Slides]({slides})\""
config_content = config_content.replace(slide_line, slides_replacement)
config_content = config_content.replace(demo_line, demo_replacement)
with open(config_file, 'w') as file:
file.write(config_content)
# Git operations
subprocess.run(["git", "config", "--global", "user.name", "jonathanferrari"])
subprocess.run(["git", "config", "--global", "user.email", "jonathanferrari@berkeley.edu"])
subprocess.run(["git", "add", config_file])
subprocess.run(["git", "commit", "-m", f"Update _config.yml for lecture {lecture_number} materials"])
subprocess.run(["git", "push"])
# Writing to environment file
with open(os.getenv('GITHUB_ENV'), 'a') as env_file:
env_file.write(f"Lecture_Number={lecture_number}\n")
env_file.write(f"Demo={demo}\n")
env_file.write(f"Demo_Link={demo_link}\n")
env_file.write(f"Slides={slides}\n")
# Create GitHub issue comment and close issue
if demo_link == "None":
demo_text = "None"
else:
demo_text = f"• [Demo]({demo_link})"
comment_body = f"Updated `_config.yml` for lecture {lecture_number}:\n- Slides: [Slides]({slides})\n- Demo: {demo_text}"
subprocess.run(["gh", "issue", "comment", issue_number, "--body", comment_body])
subprocess.run(["gh", "issue", "close", issue_number])